From cfd9f93fef6d8554a44f697f68d47821ae1deab4 Mon Sep 17 00:00:00 2001 From: blobt Date: Fri, 26 Jun 2020 22:47:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E5=8F=96=E6=B7=B1=E5=BA=A6=E7=BC=93?= =?UTF-8?q?=E5=86=B2=E5=8C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 88 +++++++++++++++++++++++++-------- res/shader/RenderDepthBuffer.fs | 9 ++++ shader2.vcxproj | 1 + shader2.vcxproj.filters | 3 ++ 4 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 res/shader/RenderDepthBuffer.fs diff --git a/main.cpp b/main.cpp index 8563c3b..0d4117b 100644 --- a/main.cpp +++ b/main.cpp @@ -91,7 +91,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine glewInit(); /*创建program*/ - GLuint program = CreateGPUProgram("res/shader/UI FullScreemQuad.vs", "res/shader/SimpleTexture.fs"); + GLuint program = CreateGPUProgram("res/shader/SimpleTexture.vs", "res/shader/SimpleTexture.fs"); GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, textureLocation; posLocation = glGetAttribLocation(program, "pos"); @@ -108,7 +108,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine int vertexCount = 0, indexCount = 0; Timer t; t.Start(); - VertexData* vertexes = LoadObjModel("res/model/Quad.obj", &indexes, vertexCount, indexCount); + VertexData* vertexes = LoadObjModel("res/model/niutou.obj", &indexes, vertexCount, indexCount); printf("load model cost %fs %d\n", t.GetPassedTime(), t.GetPassedTickers()); if (vertexes == nullptr) { @@ -116,18 +116,60 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine } /*创建vbo*/ - GLuint vbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * vertexCount, GL_STATIC_DRAW, vertexes); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glEnableVertexAttribArray(posLocation); - glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)0); - glEnableVertexAttribArray(texcoordLocation); - glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3)); - /*glEnableVertexAttribArray(normalLocation); - glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 5));*/ - glBindBuffer(GL_ARRAY_BUFFER, 0); + GLuint vao = CreateVAOWithVBOSettings([&]()->void + { + GLuint vbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * vertexCount, GL_STATIC_DRAW, vertexes); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glEnableVertexAttribArray(posLocation); + glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)0); + glEnableVertexAttribArray(texcoordLocation); + glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3)); + }); /*创建IBO*/ GLuint ibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indexCount, GL_STATIC_DRAW, indexes); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + /*创建fsqprogram*/ + GLuint fsqprogram = CreateGPUProgram("res/shader/UI FullScreemQuad.vs", "res/shader/RenderDepthBuffer.fs"); + + GLuint fsqposLocation, fsqtexcoordLocation, fsqnormalLocation, fsqMLocation, fsqVLocation, fsqPLocation, fsqtextureLocation; + fsqposLocation = glGetAttribLocation(fsqprogram, "pos"); + fsqtexcoordLocation = glGetAttribLocation(fsqprogram, "texcoord"); + fsqnormalLocation = glGetAttribLocation(fsqprogram, "normal"); + + fsqMLocation = glGetUniformLocation(fsqprogram, "M"); + fsqVLocation = glGetUniformLocation(fsqprogram, "V"); + fsqPLocation = glGetUniformLocation(fsqprogram, "P"); + textureLocation = glGetUniformLocation(program, "U_MainTexture"); + + /*load model*/ + unsigned int *fsqindexes = nullptr; + int fsqvertexCount = 0, fsqindexCount = 0; + Timer t2; + t2.Start(); + VertexData* fsqvertexes = LoadObjModel("res/model/Quad.obj", &fsqindexes, fsqvertexCount, fsqindexCount); + printf("load model cost %fs %d\n", t2.GetPassedTime(), t2.GetPassedTickers()); + + if (fsqvertexes == nullptr) { + printf("load obj model fail\n"); + } + + /*创建fsqvbo*/ + GLuint fsqvao = CreateVAOWithVBOSettings([&]()->void + { + GLuint fsqvbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * fsqvertexCount, GL_STATIC_DRAW, fsqvertexes); + glBindBuffer(GL_ARRAY_BUFFER, fsqvbo); + glEnableVertexAttribArray(fsqposLocation); + glVertexAttribPointer(fsqposLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)0); + glEnableVertexAttribArray(fsqtexcoordLocation); + glVertexAttribPointer(fsqtexcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3)); + }); + + /*创建fsqIBO*/ + GLuint fsqibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * fsqindexCount, GL_STATIC_DRAW, fsqindexes); + glBindBuffer(GL_ARRAY_BUFFER, 0); + /*创建纹理*/ GLuint mainTexture = CreateTextureFromFile("res/image/niutou.bmp"); @@ -161,7 +203,6 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine MSG msg; auto what = [&]()->void { - glm::mat4 normalMatrix = glm::inverseTranspose(model); glUseProgram(program); glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model)); @@ -171,10 +212,23 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine glBindTexture(GL_TEXTURE_2D, mainTexture); glUniform1i(textureLocation, 0); + glBindVertexArray(vao); glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0); + glBindVertexArray(0); + glUseProgram(0); + }; + + auto RenderFullScreenQuad = [&]()->void { + glUseProgram(fsqprogram); + glBindVertexArray(fsqvao); + glBindTexture(GL_TEXTURE_2D, depthBuffer); + glUniform1i(fsqtextureLocation, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, fsqibo); + glDrawElements(GL_TRIANGLES, fsqindexCount, GL_UNSIGNED_INT, 0); + glBindVertexArray(0); glUseProgram(0); }; @@ -199,15 +253,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine glBindFramebuffer(GL_FRAMEBUFFER, 0); 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, -2.0f); - glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5f, -0.5f, -2.0f); - glTexCoord2f(1.0f, 1.0f); glVertex3f(0.5f, 0.5f, -2.0f); - glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f, -2.0f); - glEnd(); + RenderFullScreenQuad(); glFlush(); SwapBuffers(dc); diff --git a/res/shader/RenderDepthBuffer.fs b/res/shader/RenderDepthBuffer.fs new file mode 100644 index 0000000..1a87c77 --- /dev/null +++ b/res/shader/RenderDepthBuffer.fs @@ -0,0 +1,9 @@ + +uniform sampler2D U_MainTexture; +varying vec2 V_Texcoord; + +void main() +{ + float depthValue=pow(texture2D(U_MainTexture,V_Texcoord).r,8.0); + gl_FragColor=vec4(vec3(depthValue),1.0); +} \ No newline at end of file diff --git a/shader2.vcxproj b/shader2.vcxproj index ce60435..f73a50b 100644 --- a/shader2.vcxproj +++ b/shader2.vcxproj @@ -159,6 +159,7 @@ + diff --git a/shader2.vcxproj.filters b/shader2.vcxproj.filters index 1578e22..af108ca 100644 --- a/shader2.vcxproj.filters +++ b/shader2.vcxproj.filters @@ -55,5 +55,8 @@ 婧愭枃浠 + + 婧愭枃浠 + \ No newline at end of file