#include#include #include #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 = 1024;const unsigned int HEIGHT = 1024;float xRot;float camRotX;float camRotY;float camRotZ;void InitVAO(){ float vertices[12] = {-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[8] = { 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0}; glGenVertexArrays(1, &g_VAO); glBindVertexArray(g_VAO); glGenBuffers(1, &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(3, GL_FLOAT, 0, 0); glGenBuffers(1, &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(2, GL_FLOAT, 0, 0); glBindVertexArray(0); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY);}void InitVAO2(){ float vertices[9] = {-1.0, -1.0, -0.0, 1.0, -1.0, -0.0, 1.0, 1.0, -0.0}; float color[12] = { 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(1, &g_VAO2); glBindVertexArray(g_VAO2); glGenBuffers(1, &g_VBO2); glBindBuffer(GL_ARRAY_BUFFER, g_VBO2); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, 0); glGenBuffers(1, &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(4, GL_FLOAT, 0, 0); glBindVertexArray(0); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY);}void InitFBO(){ glGenFramebuffers(1, &g_FBO); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, g_FBO); glGenTextures(1, &g_Texture0); glBindTexture(GL_TEXTURE_2D, g_Texture0); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, WIDTH, HEIGHT, 0, 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, 0); glGenRenderbuffers(1, &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, 0);}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(0, 0, 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, 0, 3); glBindVertexArray(0); glPopAttrib(); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); 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, 0, 4); glBindVertexArray(0); glutSwapBuffers();}void Resize(int w, int h){ if (0 == h) { h = 1; } glViewport(0, 0, 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(1, OnTimer, id);}int main(int argc, char ** argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize(1024, 800); glutCreateWindow("FBO-VBO-VAO"); GLenum res = glewInit(); if (GLEW_OK != res) { std::cout << "加载GLEW失败" << std::endl; } Init(); glutDisplayFunc(Display); glutReshapeFunc(Resize); glutTimerFunc(1, OnTimer, 1); glutMainLoop(); return EXIT_SUCCESS;}