diff --git a/.vs/shader2/v14/.suo b/.vs/shader2/v14/.suo index 5e4191a..a6d1dfb 100644 Binary files a/.vs/shader2/v14/.suo and b/.vs/shader2/v14/.suo differ diff --git a/main.cpp b/main.cpp index 85ba12f..6e1c620 100644 --- a/main.cpp +++ b/main.cpp @@ -137,11 +137,11 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine GLuint colorBuffer, depthBuffer; GLuint fbo = CreateFramebufferObject(colorBuffer, depthBuffer, windowWidth, windowHeight); + glViewport(0, 0, windowWidth, windowHeight); + GL_CALL(glClearColor(0.1f, 0.4f, 0.7f, 1.0f)); glEnable(GL_DEPTH_TEST); - glViewport(0,0, windowWidth, windowHeight); - //创建一个单位矩阵和一个投影矩阵,后面用来传递到shader float identify[] = { 1,0,0,0, @@ -171,7 +171,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine glUniformMatrix4fv(PLocation, 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(NMLocation, 1, GL_FALSE, glm::value_ptr(normalMatrix)); - glBindTexture(GL_TEXTURE_2D, mainTexture); + glBindTexture(GL_TEXTURE_2D, colorBuffer); glUniform1i(textureLocation, 0); glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model)); @@ -182,6 +182,12 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine glUseProgram(0); }; + /*使用固定管线绘制一个四边形,并把fbo内容贴上*/ + glMatrixMode(GL_PROJECTION); + glLoadMatrixf(glm::value_ptr(projection)); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + while (true) { if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { if (msg.message == WM_QUIT) { @@ -191,8 +197,26 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine DispatchMessage(&msg); } + //绘制内容到fbo + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + GL_CALL(glClearColor(0.1f, 0.4f, 0.7f, 1.0f)); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); what(); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + //把fbo绘制到一个四边形上 + GL_CALL(glClearColor(0.5f, 0.4f, 0.7f, 1.0f)); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, colorBuffer); + + glBegin(GL_QUADS); + glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -4.0f); + glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5f, -0.5f, -4.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f(0.5f, 0.5f, -4.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f, -4.0f); + glEnd(); + SwapBuffers(dc); } diff --git a/misc.cpp b/misc.cpp index 2826f64..6ae2905 100644 --- a/misc.cpp +++ b/misc.cpp @@ -37,8 +37,8 @@ GLuint CreateFramebufferObject(GLuint & colorBuffer, GLuint & depthBuffer, int w glBindTexture(GL_TEXTURE_2D, colorBuffer); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorBuffer, 0);//绑定刚才生成的texture到GL_COLOR_ATTACHMENT0 @@ -50,7 +50,7 @@ GLuint CreateFramebufferObject(GLuint & colorBuffer, GLuint & depthBuffer, int w glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_RGBA, GL_DEPTH_COMPONENT, nullptr); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthBuffer, 0); @@ -61,7 +61,7 @@ GLuint CreateFramebufferObject(GLuint & colorBuffer, GLuint & depthBuffer, int w } glBindFramebuffer(GL_FRAMEBUFFER, 0); - return GLuint(); + return fbo; } char* LoadFileContent(const char *path) {