FBO的使用例程,使用了自己编写的GL封装类

  m_gl.BeginScene();
	glewInit();
	
	int OpenGLVersion[2];
	glGetIntegerv(GL_MAJOR_VERSION, &OpenGLVersion[0]);
	glGetIntegerv(GL_MINOR_VERSION, &OpenGLVersion[1]);
	GLFBO m_FBO(Width,Height,0);
	m_FBO.GenRenderBuffers();

	m_FBO.BindDrawFBO();
	m_FBO.BindReadFBO();
	glViewport(0,0,Width,Height);//每次绑定都要重新设置视口

	//glGenFramebuffersEXT(1,&FrameBufferObjectDrawID);
	//glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, FrameBufferObjectDrawID);
	//glGenFramebuffersEXT(1,&FrameBufferObjectReadID);
	//glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT,FrameBufferObjectReadID);
	////glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	////glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, ShadowTex, 0);

	//glGenRenderbuffersEXT(1, &RenderBufferColorID[0]);
	//glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, RenderBufferColorID[0]);
	//glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, 16, Width, Height);
	//glGenRenderbuffersEXT(1, &RenderBufferDepthID);
	//glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, RenderBufferDepthID);
	//glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, 16, Width, Height);

	//glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, RenderBufferColorID[0]);
	//glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, RenderBufferDepthID);

	//bool status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
	//if (!status)
	//	return;

	glClearColor(1.0f,1.0f,1.0f,1.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//一定要清理深度缓冲区,否则画不出来
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-Width/2, Width/2, -Height/2,  Height/2, -20000.0f, 20000.0f); 
	

	//DrawPencilline(Width,Height,pMesh);
	
	glBegin(GL_TRIANGLES);
	glColor4f(0.0, 0.0, 1.0,1.0f);
	glVertex3f(0.0 ,0.0 ,-100.0);
	glVertex3f(100.0 ,100.0 ,-100.0);
	glVertex3f(100.0 ,200.0 ,-100.0);
	glEnd();
	
	glFlush(); // remember to flush GL output!

	

	glReadBuffer(GL_COLOR_ATTACHMENT0);//这句可有可无,因为默认就是GL_COLOR_ATTACHMENT0
	glPixelStorei(GL_UNPACK_ALIGNMENT,1);
	GLubyte * readpixels = new GLubyte[Width*Height*4];
	glReadPixels(0,0,Width,Height,GL_RGBA,GL_UNSIGNED_BYTE,readpixels);

	CImagePixel img;
	img.SetSize(Width,Height);
	cvector<CiColor>::iterator p;
	p = img.GetBuffer();
	for(int j =0;j<Height;j++)
	{
		for (int i = 0;i<Width;i++)
		{
			p[j*Width+i].r = readpixels[((Height-j-1)*Width+i)*4+0];
			p[j*Width+i].g = readpixels[((Height-j-1)*Width+i)*4+1];
			p[j*Width+i].b = readpixels[((Height-j-1)*Width+i)*4+2];
			p[j*Width+i].a = readpixels[((Height-j-1)*Width+i)*4+3];

		}
	}
	char* name1 = new char[20];
	sprintf(name1,"D://test//FBOabce%d.bmp",id);

	BMPImage saver(img);
	CStreamFile is(name1, CStreamFile::WRITE);
	saver.Write(is);
	m_gl.EndScene();


你可能感兴趣的:(FBO的使用例程,使用了自己编写的GL封装类)