使用glteximage2d呈现纹理,结果空白。 (Dreamcast GLDC)

发布于 2025-02-04 14:51:58 字数 3673 浏览 2 评论 0 原文

我正在尝试使用GLDC(SEGA Dreamcast的OpenGL实现)创建一个快速渲染示例。我已经验证了我的纹理和框架对象都是完整的,但是Framebuffer产生的纹理中只有1个白点。

首先,我生成一个空的纹理并准备将其写入。

func genTextures(){
    glGenTextures(1, &renderedTexture[0]);
    glBindTexture(GL_TEXTURE_2D, renderedTexture[0]);  
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smaller than texture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
}

接下来,我生成一个FBO并绑定我们刚刚创建的新纹理。

func genFBO() {
  glGenFramebuffersEXT(1, &fbo);
  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); 
  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, 
                            GL_TEXTURE_2D, renderedTexture[0], 0);
}

在这一点上,FBO和纹理都应被视为完整。主循环是类似的结构化的:

int main(int argc, char **argv)
{
    glKosInit();
    InitGL(640, 480);
    ReSizeGLScene(640, 480);
    genTextures();
    genFBO();

    while(1) {
        if(check_start())
            break;

        
        // I checked here for FBO and Texture completeness, both return True. 
 
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); // bind to the FBO
        DrawGLScene(); // Draw our cube to the FBO

        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // back to default
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 

        ReSizeGLScene(640,480);
        
       
        DrawGLUI(); //Draw the quad with the framebuffers texture
    }

    return 0;
}


这是绘制几何形状的两个函数:

void DrawGLScene()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear The Screen And The Depth Buffer
    glLoadIdentity();               // Reset The View

    glTranslatef(0.0f,0.0f,-5.0f);              // move 5 units into the screen.

    glRotatef(xrot,1.0f,0.0f,0.0f);     // Rotate On The X Axis
    glRotatef(yrot,0.0f,1.0f,0.0f);     // Rotate On The Y Axis
    glRotatef(zrot,0.0f,0.0f,1.0f);     // Rotate On The Z Axis

    glBindTexture(GL_TEXTURE_2D, texture[0]);   // choose the texture to use.

    glBegin(GL_QUADS);                      // begin drawing a cube

    // Draw my textured cube, works fine.

    glEnd();                                   // done with the polygon.

    xrot+=1.5f;                     // X Axis Rotation
    yrot+=1.5f;                     // Y Axis Rotation
    zrot+=1.5f;                     // Z Axis Rotation

    glKosSwapBuffers();
}

void DrawGLUI(){

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);       // This Will Clear The Background Color To Black
    glClearDepth(1.0);              // Enables Clearing Of The Depth Buffer

    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glLoadIdentity();
    glBindTexture(GL_TEXTURE_2D, renderedTexture[0]);
    glBegin(GL_QUADS);
        //glColor3f(1.0f, 0.0f, 0.0);
         glTexCoord2f(0.0, 0.0); glVertex2f(0.0, 0.0);
         glTexCoord2f(1.0, 0.0); glVertex2f(1.0, 0.0);
         glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0);
         glTexCoord2f(0.0, 1.0); glVertex2f(0.0, 1.0);
    glEnd();

    glEnable(GL_DEPTH_TEST);
    ReSizeGLScene(640,480);
    glFlush();
    }

结果是 “在此处输入图像描述”

我想将多个位置呈现到纹理上,然后将纹理应用于Quad右上角...

I am trying to create a quick render to texture example using GLdc, a OpenGL implementation for the Sega Dreamcast. I have verified that both my Texture and Framebuffer Object are complete, yet the texture resulting from the framebuffer only has 1 white dot in it.

First, I generate an empty texture and prepare it to be written to.

func genTextures(){
    glGenTextures(1, &renderedTexture[0]);
    glBindTexture(GL_TEXTURE_2D, renderedTexture[0]);  
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smaller than texture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
}

Next, I generate an FBO and bind the new texture we just created to it.

func genFBO() {
  glGenFramebuffersEXT(1, &fbo);
  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); 
  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, 
                            GL_TEXTURE_2D, renderedTexture[0], 0);
}

At this point the FBO and the Texture should both be considered complete. The main loop is structured something like this:

int main(int argc, char **argv)
{
    glKosInit();
    InitGL(640, 480);
    ReSizeGLScene(640, 480);
    genTextures();
    genFBO();

    while(1) {
        if(check_start())
            break;

        
        // I checked here for FBO and Texture completeness, both return True. 
 
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); // bind to the FBO
        DrawGLScene(); // Draw our cube to the FBO

        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // back to default
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 

        ReSizeGLScene(640,480);
        
       
        DrawGLUI(); //Draw the quad with the framebuffers texture
    }

    return 0;
}


Here are the two functions that draw geometry:

void DrawGLScene()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear The Screen And The Depth Buffer
    glLoadIdentity();               // Reset The View

    glTranslatef(0.0f,0.0f,-5.0f);              // move 5 units into the screen.

    glRotatef(xrot,1.0f,0.0f,0.0f);     // Rotate On The X Axis
    glRotatef(yrot,0.0f,1.0f,0.0f);     // Rotate On The Y Axis
    glRotatef(zrot,0.0f,0.0f,1.0f);     // Rotate On The Z Axis

    glBindTexture(GL_TEXTURE_2D, texture[0]);   // choose the texture to use.

    glBegin(GL_QUADS);                      // begin drawing a cube

    // Draw my textured cube, works fine.

    glEnd();                                   // done with the polygon.

    xrot+=1.5f;                     // X Axis Rotation
    yrot+=1.5f;                     // Y Axis Rotation
    zrot+=1.5f;                     // Z Axis Rotation

    glKosSwapBuffers();
}

void DrawGLUI(){

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);       // This Will Clear The Background Color To Black
    glClearDepth(1.0);              // Enables Clearing Of The Depth Buffer

    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glLoadIdentity();
    glBindTexture(GL_TEXTURE_2D, renderedTexture[0]);
    glBegin(GL_QUADS);
        //glColor3f(1.0f, 0.0f, 0.0);
         glTexCoord2f(0.0, 0.0); glVertex2f(0.0, 0.0);
         glTexCoord2f(1.0, 0.0); glVertex2f(1.0, 0.0);
         glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0);
         glTexCoord2f(0.0, 1.0); glVertex2f(0.0, 1.0);
    glEnd();

    glEnable(GL_DEPTH_TEST);
    ReSizeGLScene(640,480);
    glFlush();
    }

The result is enter image description here

Where I would like to have the cube rendered to a texture then that texture applied to the quad in the upper right corner...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城月光淡如水﹏ 2025-02-11 14:51:58

必须用 glviewport 切换FrameBuffer时:

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glViewport(0, 0, 128, 128);

// [...]

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glViewport(0, 0, 640, 480);

// [...]

The size of the viewport must be adjusted to the size of the framebuffer with glViewport when the framebuffer is switched:

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glViewport(0, 0, 128, 128);

// [...]

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glViewport(0, 0, 640, 480);

// [...]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文