#include <GL/glew.h>
#include <GL/freeglut.h>
#include <iostream>
#pragma comment(lib, "glew32.lib") GLuint g_VBO;
GLuint g_VBO_TexCoord;
GLuint g_VAO; GLuint g_FBO;
GLuint g_Texture0;
GLuint g_RBO; GLuint g_VBO2;
GLuint g_VBO2_TexCoord;
GLuint g_VBO2_Color;
GLuint g_VAO2; const unsigned int WIDTH = ;
const unsigned int HEIGHT = ; float xRot; float camRotX;
float camRotY;
float camRotZ; void InitVAO()
{
float vertices[] = {-1.0, -1.0, -0.0,
1.0, -1.0, -0.0,
1.0, 1.0, -0.0,
-1.0, 1.0, -0.0}; float texcoord[] = {0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0}; glGenVertexArrays(, &g_VAO);
glBindVertexArray(g_VAO); glGenBuffers(, &g_VBO);
glBindBuffer(GL_ARRAY_BUFFER, g_VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
//glEnableVertexAttribArray(0);//启用Shader时指定layout标识号
//glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexPointer(, GL_FLOAT, , ); glGenBuffers(, &g_VBO_TexCoord);
glBindBuffer(GL_ARRAY_BUFFER, g_VBO_TexCoord);
glBufferData(GL_ARRAY_BUFFER, sizeof(texcoord), texcoord, GL_STATIC_DRAW);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_Texture0);
glTexCoordPointer(, GL_FLOAT, , ); glBindVertexArray();
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY); } void InitVAO2()
{
float vertices[] = {-1.0, -1.0, -0.0,
1.0, -1.0, -0.0,
1.0, 1.0, -0.0}; float color[] = {0.0, 0.0, 1.0, 0.4,
0.0, 0.0, 1.0, 0.4,
0.0, 0.0, 1.0, 0.4,
};
glGenVertexArrays(, &g_VAO2);
glBindVertexArray(g_VAO2); glGenBuffers(, &g_VBO2);
glBindBuffer(GL_ARRAY_BUFFER, g_VBO2);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(, GL_FLOAT, , ); glGenBuffers(, &g_VBO2_Color);
glBindBuffer(GL_ARRAY_BUFFER, g_VBO2_Color);
glBufferData(GL_ARRAY_BUFFER, sizeof(color), color, GL_STATIC_DRAW);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(, GL_FLOAT, , );
glBindVertexArray();
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
void InitFBO()
{
glGenFramebuffers(, &g_FBO);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, g_FBO); glGenTextures(, &g_Texture0);
glBindTexture(GL_TEXTURE_2D, g_Texture0); glTexImage2D(GL_TEXTURE_2D, , GL_RGBA8, WIDTH, HEIGHT, , GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, g_Texture0, ); glGenRenderbuffers(, &g_RBO);
glBindRenderbuffer(GL_RENDERBUFFER, g_RBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, WIDTH, HEIGHT);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, g_RBO); GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
switch (status)
{
case GL_FRAMEBUFFER_COMPLETE:
{
std::cout << "Framebuffer SUCCESS!" << std::endl;
break;
}
case GL_FRAMEBUFFER_UNSUPPORTED:
{
std::cout << "FrameBufffer UNSCCESS" << std::endl;
break;
}
default:
break;
}
glBindFramebuffer(GL_FRAMEBUFFER, );
} void Init()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
xRot = 0.0f;
camRotX = 0.0f;
camRotY = 0.0f;
camRotZ = 0.0f;
InitVAO();
InitVAO2();
InitFBO();
} void Display()
{ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, g_FBO);
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(, , WIDTH, HEIGHT);
glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, WIDTH/HEIGHT, 1.0, 100.0);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -3.0f);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_Texture0);
glRotatef(xRot,0.0,1.0,0.0); glutSolidTeapot(0.1f);
glBindVertexArray(g_VAO2);
glDrawArrays(GL_TRIANGLES, , );
glBindVertexArray();
glPopAttrib();
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, ); glClearColor(1.0f, 0.2f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -3.0);
glRotatef(camRotX, 1.0, 0.0f, 0.0f);
glRotatef(camRotY, 0.0, 1.0f, 0.0f);
glRotatef(camRotZ, 0.0, 0.0f, 1.0f); glBindVertexArray(g_VAO);
glDrawArrays(GL_QUADS, , );
glBindVertexArray(); glutSwapBuffers();
} void Resize(int w, int h)
{
if ( == h)
{
h = ;
}
glViewport(, , w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, w/h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); } void OnTimer(int id)
{
xRot += 0.1;
//camRotX += 0.01;
camRotY += 0.05;
//camRotZ += 0.01;
Display();
glutTimerFunc(, OnTimer, id);
}
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(, );
glutCreateWindow("FBO-VBO-VAO");
GLenum res = glewInit();
if (GLEW_OK != res)
{
std::cout << "加载GLEW失败" << std::endl;
}
Init();
glutDisplayFunc(Display);
glutReshapeFunc(Resize);
glutTimerFunc(, OnTimer, );
glutMainLoop();
return EXIT_SUCCESS;
}