0
我試圖渲染一個簡單的場景到framebuffer,然後將其作爲紋理傳遞給我的着色器。但是,我的渲染對象顯示爲黑色。我無法弄清楚我做錯了什麼。framebuffer對象,麻煩渲染並將紋理傳遞給着色器
在下面的代碼中,我創建了一個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之間的區別
我不確定你爲什麼在這裏使用malloc。這是分配內存的絕對*可怕的方式,並且在將它傳遞給glUniform後並不會刪除分配的內存,因此您將在此處發生泄漏。爲什麼不只浮動m [16]? – Robinson 2012-03-12 22:10:41
你是對的,這是非常臨時的。 – nyvaken 2012-03-13 18:49:07