相关代码如下
建立普通FBO,挂一个纹理和深度缓冲
BOOL CGLFRAME::CreateFBO(HWND hWnd)
{
HDC hDC=::GetDC(hWnd);
if(!wglMakeCurrent(hDC,this->m_hRC))
{//选为当前RC失败
return FALSE;
}
glGenTextures(1, &m_FBOTexIndex);
glBindTexture(GL_TEXTURE_2D, m_FBOTexIndex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16, m_TextrueSize,m_TextrueSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);
// 创建深度缓冲区
glGenRenderbuffersEXT(1, &m_DBIndex);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, m_DBIndex);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, m_TextrueSize,m_TextrueSize);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
// 创建FBO对象
glGenFramebuffersEXT(1, &m_FBIndex);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_FBIndex);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_FBOTexIndex, 0);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_DBIndex);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(status != GL_FRAMEBUFFER_COMPLETE_EXT)
{
return false;
}
wglMakeCurrent(hDC,NULL);
return true;
}
建立多重采样FBO,挂一个渲染和深度
BOOL CGLFRAME::CreateMS_FBO(HWND hWnd)
{
HDC hDC=::GetDC(hWnd);
if(!wglMakeCurrent(hDC,this->m_hRC))
{//选为当前RC失败
return FALSE;
}
//建立FB
glGenFramebuffersEXT(1,&m_MSFBIndex);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,m_MSFBIndex);
//建立RB
glGenRenderbuffersEXT(1,&m_MSRBIndex);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT,m_MSRBIndex);
glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT,8,
GL_RGBA,m_TextrueSize,m_TextrueSize);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,GL_COLOR_ATTACHMENT0,
GL_RENDERBUFFER_EXT,m_MSRBIndex);
//建立DB
glGenRenderbuffersEXT(1,&m_MSDBIndex);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT,m_MSDBIndex);
glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT,8,
GL_DEPTH_COMPONENT24,m_TextrueSize,m_TextrueSize);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT,m_MSDBIndex);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(status != GL_FRAMEBUFFER_COMPLETE_EXT)
{
return false;
}
wglMakeCurrent(hDC,NULL);
return true;
}
在多重采样FBO上画图,然后blit到普通FBO上
void CGLFRAME::DrawOnFBO(HDC hDC,HWND hWnd)
{
wglMakeCurrent(hDC, m_hRC);
//先在MSFB上作图
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_MSFBIndex);
SetMSFBOView();
glClearColor(0.0,1.0,0.0,1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glViewport(0,0,m_TextrueSize,m_TextrueSize); // 重置当前的视口
glMatrixMode(GL_PROJECTION); // 选择投影矩阵
glLoadIdentity();
glOrtho(-(GLdouble)m_TextrueSize/2.0,(GLdouble)m_TextrueSize/2.0,
-(GLdouble)m_TextrueSize/2.0,(GLdouble)m_TextrueSize/2.0,
-1000,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glEnable(GL_LIGHTING);
MyRender();
glPopMatrix();
glFlush();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
//将MSFB上的图blit到FB的纹理上
glBindFramebufferEXT(GL_READ_FRAMEBUFFER, m_MSFBIndex);
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER, m_FBIndex);
glBlitFramebufferEXT(0, 0, m_TextrueSize, m_TextrueSize, 0, 0, m_TextrueSize, m_TextrueSize, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindFramebufferEXT(GL_READ_FRAMEBUFFER, NULL);
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER, NULL);
wglMakeCurrent(hDC, NULL);
}
最后把FBO的纹理贴到屏幕上
void CGLFRAME::MapTextrue(HDC hDC,HWND hWnd)
{
//将FBO上的纹理贴到屏幕上
wglMakeCurrent(hDC, m_hRC);
SetScreenGLView(hWnd);
glClearColor(0.0,0.0,1.0,0.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,m_FBOTexIndex);
glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0); glVertex3d(-(GLdouble)m_TextrueSize/2.0,-(GLdouble)m_TextrueSize/2.0, 0.0);
glTexCoord2d(1.0, 0.0); glVertex3d( (GLdouble)m_TextrueSize/2.0,-(GLdouble)m_TextrueSize/2.0, 0.0);
glTexCoord2d(1.0, 1.0); glVertex3d( (GLdouble)m_TextrueSize/2.0, (GLdouble)m_TextrueSize/2.0, 0.0);
glTexCoord2d(0.0, 1.0); glVertex3d(-(GLdouble)m_TextrueSize/2.0, (GLdouble)m_TextrueSize/2.0, 0.0);
glEnd();
glEnable(GL_LIGHTING);
glPopMatrix();
glFlush();
SwapBuffers(hDC);
wglMakeCurrent(hDC, NULL);//多实例
}
结果如下
我画了两个圆柱,同样的绘图代码在不用FBO时就可以显示两个正常的光照圆柱,可为什么在FBO下圆柱整体颜色都是一样的?
那里设置的不对呢?菜鸟求解答
5 个解决方案
#1
上面的问题找到原因了,原来我设置光源的时候没有wglMakeCurrent,导致光源设置无效。
但现在有个新问题,初次运行的时候能正确显示如下
但再次绘制,如最小化之后,背景色会与物体融合,如下
实在是无从查起啊
但现在有个新问题,初次运行的时候能正确显示如下
但再次绘制,如最小化之后,背景色会与物体融合,如下
实在是无从查起啊
#3
赵老师,里面内容很多啊,有点着急呀,您知不知道我这问题对应的哪一个?
#4
直接问博主本人。
#5
楼主贴出解决方法提携一下后辈吧。
#1
上面的问题找到原因了,原来我设置光源的时候没有wglMakeCurrent,导致光源设置无效。
但现在有个新问题,初次运行的时候能正确显示如下
但再次绘制,如最小化之后,背景色会与物体融合,如下
实在是无从查起啊
但现在有个新问题,初次运行的时候能正确显示如下
但再次绘制,如最小化之后,背景色会与物体融合,如下
实在是无从查起啊
#2
#3
http://blog.csdn.net/pizi0475
赵老师,里面内容很多啊,有点着急呀,您知不知道我这问题对应的哪一个?
#4
http://blog.csdn.net/pizi0475
赵老师,里面内容很多啊,有点着急呀,您知不知道我这问题对应的哪一个?
直接问博主本人。
#5
楼主贴出解决方法提携一下后辈吧。