0

我正在尝试将一个简单的场景渲染到帧缓冲区,然后将其作为纹理传递给我的着色器。但是,我渲染的对象显示为黑色。我无法弄清楚我做错了什么。

在下面的代码中,我创建了一个 FBO 并为其附加了深度缓冲区和纹理。

GLuint fbo, fboDepth,fboTexture;

// Create the texture
glGenTexturesEXT(1, &fboTexture);
glBindTextureEXT(GL_TEXTURE_2D, fboTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 512, 0, GL_RGBA, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR );
// Create and bind an FBO
glGenFramebuffersEXT(1, &fbo);
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, fbo);

// Create depth renderbuffer
glGenRenderbuffersEXT(1, &fboDepth);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fboDepth);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, 512, 512);

glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, fboTexture, 0);
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, fboDepth);

GLenum fboStatus = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER_EXT);                                   

if (fboStatus != GL_FRAMEBUFFER_COMPLETE) {
    std::cout << "Couldn't create frame buffer" << std::endl;                               
    exit(0);                                                                                
} else {
    cout << fbo << fboTexture << fboDepth << endl;
}

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); // Reset framebuffer binding

下面的代码是我的渲染方法。我已经尝试过使用 tga 纹理,并且效果很好。

 void Renderer::render(Geometry* geo) {
glUseProgram(geo->shader->program);

// Bind our frame buffer for rendering 
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); 
glPushAttrib(GL_VIEWPORT_BIT | GL_ENABLE_BIT);
glViewport(0, 0, 512, 512);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    // render a triangle to the fbo
glBegin(GL_TRIANGLES);
glColor3f(1,0,0);
glVertex2f(-1,0);

glColor3f(0,1,0);
glVertex2f(1,-1);

glColor3f(0,0,1);
glVertex2f(.5,.5);
glEnd();

glPopAttrib(); 
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

GLuint texUniLoc = glGetUniformLocation(geo->shader->program, "Tex");

glActiveTexture(GL_TEXTURE0 + 1);
glBindTextureEXT(GL_TEXTURE_2D, fboTexture);
glUniform1i(texUniLoc, 1);

GLuint vpm = glGetUniformLocation(geo->shader->program, "ViewProjectionMatrix");
glUniformMatrix4fv(vpm, 1, GL_TRUE, ViewProjectionMatrix.vec);

float * m = (float*)malloc(sizeof(float)*16);
m[0] = 1;   m[1] = 0;   m[2] = 0;   m[3] = 0;
m[4] = 0;   m[5] = 1;   m[6] = 0;   m[7] = 0.0;
m[8] = 0;   m[9] = 0;   m[10] = 1;  m[11] = 0.0;
m[12] = 0;  m[13] = 0;  m[14] = 0;  m[15] = 1.0;

    GLuint model = glGetUniformLocation(geo->shader->program, "ModelMatrix");
glUniformMatrix4fv(model, 1, GL_TRUE, m);

// activate and specify pointer to vertex array
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_INDEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, geo->vertices);
glTexCoordPointer(2, GL_FLOAT, 0, geo->texcoords);
glDrawElements(GL_TRIANGLES,  2*3*32*32, GL_UNSIGNED_SHORT, geo->indices);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_INDEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY); 
glUseProgram(0);
}

我已经尝试过 .tga 纹理并且着色器运行良好。我正在使用 glfw 和 glew。我也不确定 glSomeFunction 和 glSomeFunctionEXT 之间的区别

4

1 回答 1

0

我修好了它。在绘制三角形之前,我不应该调用 glUseProgram。

于 2012-03-13T18:51:54.420 回答