OpenGL中的几个重要缓冲系统有VBO(Vertex Buffer Object), PBO(Pixel Buffer Object),FBO(Frame Buffer Object),其中VBO和PBO我们前面已经介绍了,主要采用的是glGenBuffers()来生成的服务器缓冲(GPU的内存),来实现高效绘制。FBO主要是用来做离屏渲染的,咱们的窗口系统有默认的帧缓冲对象(其对象值默认为0),如果想实现动态纹理的绘制,或者多屏的同时渲染,这时咱们可以使用帧缓冲对象,帧缓冲对象和像素缓冲对象是实现动态纹理的两个主要途径,使用帧缓冲对象的过程如下:
1.生成帧缓冲对象 glGenFrameBuffer()
2.绑定,使我们生成的帧缓冲对象作为当前的帧缓冲绘制对象。glBindFrameBuffer()
3.给帧缓冲区配置颜色、深度测试、模板测试缓冲区。其中颜色缓冲区可以有多个,颜色缓冲区主要是通过和纹理对象来结合使用,深度测试、模板测试缓冲区是通过帧缓冲区渲染对象来实现(glGenRenderBuffers() , glBindRenderBuffer() )
4.在绑定好的当前帧缓冲区上进行绘制。
5.将绑定到帧缓冲区对象的共享纹理取出,用于其他绘制操作。也可以直接通过glBlitFrameBuffer()来拷贝对应的渲染缓冲区,拷贝之前需要通过glBindFrameBuffer()操作将源帧缓冲区绑定为GL_READ_FRAMEBUFFER,将目的缓冲区绑定为GL_DRAW_FRAMEBUFFER(或则GL_FRAME_BUFFER)。
如下是我写的一个帧缓冲对象的示例。大家可以看看。
// framebuffer.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <string>
#include <iostream>
#include <GL/glew.h>
#include <GL/glut.h>
#pragma comment(lib, "glew32.lib")
using namespace std;
GLuint framebuffer = 0;
GLuint texture = 0;
enum
{
vertex_buffer = 0,
coord_buffer,
idx_buffer,
vbo_buffer_size,
};
GLuint vboBuffer[vbo_buffer_size] = {0};
void init()
{
glGenFramebuffers(1,&framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,100,100,0,GL_RGB, GL_UNSIGNED_BYTE, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, texture, 0);
std::cout<<glGetError()<<std::endl;
assert(GLEW_NO_ERROR == glGetError());
GLenum r = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (r != GL_FRAMEBUFFER_COMPLETE)
std::cout<<"r="<<r;
float vertex[][3] = {
{-4,-4,0},{-4,4,0},
{4,4,0},{4,-4,0}
};
float coord[][2] =
{
{0,0},{0,1},
{1,1},{1,0},
};
GLubyte index[] = {0,1,2,3};
glGenBuffers(vbo_buffer_size, vboBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vboBuffer[vertex_buffer]);
glBufferData(GL_ARRAY_BUFFER,sizeof(float)*4*3,vertex, GL_STATIC_DRAW);
glVertexPointer(3,GL_FLOAT,0,0);
glBindBuffer(GL_ARRAY_BUFFER, vboBuffer[coord_buffer]);
glBufferData(GL_ARRAY_BUFFER,sizeof(float)*4*2,coord, GL_STATIC_DRAW);
glTexCoordPointer(2,GL_FLOAT,0,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboBuffer[idx_buffer]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte)*4, index,GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnable(GL_TEXTURE_2D);
GLenum fff = glGetError();
assert(glGetError() == 0);
}
void display()
{
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glPushAttrib(GL_ALL_ATTRIB_BITS);
glPushMatrix();
glClearColor(1.0f,1.0f,1.0,1.0f);
glViewport(0,0,100,100);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5,5,-5,5,-5,5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,0,1);
glRectf(-5,-5,0,5);
glColor3f(1,1,0);
glRectf(0,-5, 5,5);
glPopMatrix();
glPopAttrib();
// glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glPushAttrib(GL_ALL_ATTRIB_BITS);
glPushMatrix();
glBindTexture(GL_TEXTURE_2D,texture);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glViewport(0,0,100,100);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5,5,-5,5,-5,5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
// glBlitFramebuffer(0,0,100,100,0,0,100,100,GL_COLOR_BUFFER_BIT, GL_NEAREST);
glPopMatrix();
glPopAttrib();
glBindTexture(GL_TEXTURE_2D, 0);
glutSwapBuffers();
}
void reshape(int w, int h)
{
}
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> bits;
std::vector<int> enums;
std::vector<std::string> types;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(100,100);
glutInitWindowPosition(100,100);
glutCreateWindow("Frame Buffer Object");
assert(glewInit() == 0);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
getchar();
return 0;
}
运行效果图: