OpenGL渲染到多个纹理,结果是白色的
Posted
技术标签:
【中文标题】OpenGL渲染到多个纹理,结果是白色的【英文标题】:OpenGL Rendering to Multiple Textures, results are white 【发布时间】:2012-05-16 19:51:30 【问题描述】:我已经开始切换我的渲染代码以支持着色器,并且在渲染到后台缓冲区时一切正常。所以现在我正在努力渲染到 FBO,但我得到的只是颜色和法线的白色纹理。
这是我的 FBO 创建代码:
void RenderTarget_GL::CreateFBO (void)
// if the machine supports the GL FBO extension
if (s_supportfbo)
// Create FBO
glGenFramebuffersEXT(1, &m_fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);
// Create default texture buffer
char *buffer = new char [static_cast<int>(g_window->GetWidth() * m_screenWidth) * static_cast<int>(g_window->GetHeight() * m_screenHeight) * 4];
std::memset(buffer, 0, static_cast<int>(g_window->GetWidth() * m_screenWidth) * static_cast<int>(g_window->GetHeight() * m_screenHeight) * 4);
// Create Render Texture
glGenTextures(1, &m_rendertexture);
glBindTexture(GL_TEXTURE_2D, m_rendertexture);
glTexImage2D(GL_TEXTURE_2D, 0, 4, static_cast<int>(g_window->GetWidth() * m_screenWidth), static_cast<int>(g_window->GetHeight() * m_screenHeight), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// Bind Render Texture to FBO
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_rendertexture, 0);
// Create Normal Texture if this FBO will be rendering normals
if (m_hasnormal)
glGenTextures(1, &m_normaltexture);
glTexImage2D(GL_TEXTURE_2D, 0, 4, static_cast<int>(g_window->GetWidth() * m_screenWidth), static_cast<int>(g_window->GetHeight() * m_screenHeight), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// Bind Normal Texture to FBO
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, m_normaltexture, 0);
// UnBind FBO and cleanup default buffer
delete [] buffer;
Clear();
以及我用来设置当前渲染目标的代码:
void RenderTarget_GL::Set (void)
if (s_supportfbo && g_glgraphics->GetShaderEnabled())
static const GLenum buffer1[] = GL_COLOR_ATTACHMENT0_EXT;
static const GLenum buffer2[] = GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT;
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);
if (m_hasnormal)
glDrawBuffers(2, buffer2);
else
glDrawBuffers(1, buffer1);
最后,我的实际绘图代码:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Setup the camera transformation
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
if (m_camera)
m_camera->GLMatrix();
else
m_defaultCam.GLMatrix();
// Setup Render Target
if (m_shaderenabled)
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0,0,g_window->GetWidth(),g_window->GetHeight());
m_initialpass->Set();
// Draw All Objects with their per-object shaders
// Clear render target and shader bindings
if (m_shaderenabled)
glPopAttrib();
RenderTarget_GL::Clear();
Shader_GL::ClearShaderBinding();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
// Draw Scene
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_initialpass->GetColorTexture());
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
Texture_GL::ClearTextureBinding();
glPopMatrix();
// Swap Buffers
【问题讨论】:
【参考方案1】:GL_TEXTURE_MIN_FILTER
默认为GL_NEAREST_MIPMAP_LINEAR
。提供 mipmap 或切换到 GL_LINEAR
或 GL_NEAREST
。
OpenGL Wiki has more.
【讨论】:
谢谢,忘记了 mipmapping 并且认为这不会有问题。也错过了对 FBO 的 glClear 的调用,我的纹理颠倒了,但这些都是微不足道的修复 是的,这个问题似乎吸引了很多人。因此 Wiki 上的常见问题解答 :)以上是关于OpenGL渲染到多个纹理,结果是白色的的主要内容,如果未能解决你的问题,请参考以下文章