这个想法是在 FrameBuffer 对象中以不同的颜色附件渲染场景的特定部分,然后使用深度缓冲区将这些部分组合为最终图像。在第一步中,我只想渲染到单个附件,然后渲染到屏幕上。EXT_framebuffer_object
由于我的旧显卡,我为此目的和规格使用了过剩。在主要功能中,我有:
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(600, 512);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
int mainHandle = glutCreateWindow("Demo");
glutSetWindow(mainHandle);
glutDisplayFunc(RenderCallback);
glutReshapeFunc(ReshapeCallback);
glutIdleFunc(IdleCallback);
glutKeyboardFunc(KeyboardCallback);
glutSpecialFunc(ArrowKeyCallback);
glutMouseFunc(MouseCallback);
glutMotionFunc(MotionCallback);
MotionCallback(0,0);
atexit(ExitNx);
// Setup default render states
glClearColor(0.3f, 0.4f, 0.5f, 1.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
// Setup lighting
glEnable(GL_LIGHTING);
float ambientColor[] = { 0.0f, 0.1f, 0.2f, 0.0f };
float diffuseColor[] = { 1.0f, 1.0f, 1.0f, 0.0f };
float specularColor[] = { 0.0f, 0.0f, 0.0f, 0.0f };
float position[] = { 100.0f, 100.0f, 400.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientColor);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseColor);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularColor);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHT0);
GLenum status,st1,st2;
GLint colorBufferCount ;
glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, &colorBufferCount);
printf("Max number of attachments:%d \n " ,colorBufferCount);
//initialising FBO
glGenFramebuffersEXT(1,&fb);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fb);
//attaching an image to render to
glGenRenderbuffersEXT(1,&colorbf);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT,colorbf);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT,GL_RGBA8,600,512);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorbf);
//creating depht buffer
glGenRenderbuffersEXT(1, &depth_rb);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depth_rb);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, 600, 512);
//attaching the depth buffer to the FBO
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depth_rb);
st1=glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
//checking for errors
st2=glGetError();
if(st1==GL_FRAMEBUFFER_COMPLETE_EXT)
printf("so far so good\n ");
else
if(st1==GL_FRAMEBUFFER_UNSUPPORTED_EXT)
printf("not good");
const GLubyte *sir= new GLubyte [256];
sir=gluErrorString(st1);
printf("error received %s \n", sir);
glutMainLoop();
}
在显示回调函数中我有
void RenderCallback()
{
// Setup projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, (float)glutGet(GLUT_WINDOW_WIDTH)/(float)glutGet(GLUT_WINDOW_HEIGHT), 1.0f, 10000.0f);
gluLookAt(gEye.x, gEye.y, gEye.z, gEye.x + gDir.x, gEye.y + gDir.y, gEye.z + gDir.z, 0.0f, 1.0f, 0.0f);
// Setup modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb); //setting our FBO to render to
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//render code
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0); //unbiding
//now I wish to see what I have written in the colorbf attachment , most likely my mistake is in the following to come
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//should I have done something else before?
glutSwapBuffers();
}
输出是一个空场景。