diff --git a/.vs/shader3/v14/.suo b/.vs/shader3/v14/.suo index ff4482d..ed3d7e4 100644 Binary files a/.vs/shader3/v14/.suo and b/.vs/shader3/v14/.suo differ diff --git a/FBO.cpp b/FBO.cpp index 84df702..dfb07a3 100644 --- a/FBO.cpp +++ b/FBO.cpp @@ -13,18 +13,19 @@ void FBO::AttachColorBuffer(const char*bufferName, GLenum attachment, GLenum dat glBindTexture(GL_TEXTURE_2D, colorBuffer); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - - if (dataType == GL_RGBA16F) { + if (dataType == GL_RGBA16F) + { glTexImage2D(GL_TEXTURE_2D, 0, dataType, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT, NULL); } - else { + else + { glTexImage2D(GL_TEXTURE_2D, 0, dataType, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); } glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, colorBuffer, 0); - + mDrawBuffers.push_back(attachment); - mBuffers.insert(std::pair(bufferName,colorBuffer)); + mBuffers.insert(std::pair(bufferName, colorBuffer)); glBindFramebuffer(GL_FRAMEBUFFER, 0); } @@ -52,7 +53,7 @@ void FBO::Finish() { GLenum *buffers = new GLenum[nCount]; int i = 0; - while (i < nCount) + while (isecond; } diff --git a/FBO.h b/FBO.h index bbc73d8..b04c2ac 100644 --- a/FBO.h +++ b/FBO.h @@ -12,7 +12,7 @@ public: public: FBO(); //hdr/normal color - void AttachColorBuffer(const char*bufferName,GLenum attachment,GLenum dataType,int width,int height); + void AttachColorBuffer(const char*bufferName, GLenum attachment, GLenum dataType, int width, int height); void AttachDepthBuffer(const char*bufferName, int width, int height); //complete fbo settings void Finish(); diff --git a/main.cpp b/main.cpp index 0db9acf..8e1e9d8 100644 --- a/main.cpp +++ b/main.cpp @@ -81,6 +81,15 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ originalProgram.DetectAttribute("texcoord"); originalProgram.DetectUniform("U_MainTexture"); + //init depth render program + GPUProgram depthRenderProgram; + depthRenderProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs"); + depthRenderProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/depthrender.fs"); + depthRenderProgram.Link(); + depthRenderProgram.DetectAttribute("pos"); + depthRenderProgram.DetectAttribute("texcoord"); + depthRenderProgram.DetectUniform("U_MainTexture"); + //init hdr program GPUProgram hdrProgram; hdrProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs"); @@ -120,6 +129,16 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ lightSourceProgram.DetectUniform("V"); lightSourceProgram.DetectUniform("P"); + //init depth program + GPUProgram depthProgram; + depthProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/sample_depth_buffer.vs"); + depthProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/sample_depth_buffer.fs"); + depthProgram.Link(); + depthProgram.DetectAttribute("pos"); + depthProgram.DetectUniform("M"); + depthProgram.DetectUniform("V"); + depthProgram.DetectUniform("P"); + //init gpu program GPUProgram gpuProgram; @@ -188,6 +207,8 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ glm::mat4 viewMatrix = glm::lookAt(glm::vec3(1.0f, 0.5f, -10.0f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 1.0f, 0.0f)); glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f); + glm::mat4 lightMatrix = glm::lookAt(glm::vec3(0.0f, 10.0f, 0.0f), glm::vec3(0.0f, 0.0f, -2.0f), glm::vec3(0.0f, 0.0f, -1.0f)); + glm::mat4 lightprojectionMatrix = glm::ortho(-5.0f, 5.0f, -5.0f, 5.0f, 0.1f, 15.0f); //³õʼ»¯fsq FullScreenQuad fsq; @@ -208,10 +229,34 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ HDRfbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight); HDRfbo.Finish(); + FBO depthfbo; + depthfbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGB, viewportWidth, viewportHeight); + depthfbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight); + depthfbo.Finish(); + ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd); - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); + glEnable(GL_DEPTH_TEST); + + depthfbo.Bind(); + glUseProgram(depthProgram.mProgram); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glUniformMatrix4fv(depthProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model)); + glUniformMatrix4fv(depthProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(lightMatrix)); + glUniformMatrix4fv(depthProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(lightprojectionMatrix)); + obj.Bind(depthProgram.GetLocation("pos"), depthProgram.GetLocation("texcoord"), depthProgram.GetLocation("normal")); + obj.Draw(); + + glUniformMatrix4fv(depthProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(quadModel)); + quad.Bind(depthProgram.GetLocation("pos"), depthProgram.GetLocation("texcoord"), depthProgram.GetLocation("normal")); + quad.Draw(); + + glUseProgram(0); + depthfbo.Unbind(); + + MSG msg; while (true) { @@ -224,7 +269,6 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ TranslateMessage(&msg); DispatchMessage(&msg); } - glEnable(GL_DEPTH_TEST); HDRfbo.Bind(); @@ -311,10 +355,10 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0); fsq.DrawToLeftTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord")); - glUseProgram(hdrProgram.mProgram); - glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("hdrcolor")); - glUniform1i(hdrProgram.GetLocation("U_MainTexture"), 0); - fsq.DrawToRightTop(hdrProgram.GetLocation("pos"), hdrProgram.GetLocation("texcoord")); + glUseProgram(depthRenderProgram.mProgram); + glBindTexture(GL_TEXTURE_2D, depthfbo.GetBuffer("depth")); + glUniform1i(depthRenderProgram.GetLocation("U_MainTexture"), 0); + fsq.DrawToRightTop(depthRenderProgram.GetLocation("pos"), depthRenderProgram.GetLocation("texcoord")); glUseProgram(gaussianProgram.mProgram); glBindTexture(GL_TEXTURE_2D, fbo[1].GetBuffer("color")); diff --git a/res/shader/depthrender.fs b/res/shader/depthrender.fs new file mode 100644 index 0000000..1a35236 --- /dev/null +++ b/res/shader/depthrender.fs @@ -0,0 +1,11 @@ + +varying vec2 V_Texcoord; + +uniform sampler2D U_MainTexture; + +void main() +{ + float depthValue=texture2D(U_MainTexture,V_Texcoord).r; + depthValue = pow(depthValue, 2); + gl_FragColor=vec4(vec3(depthValue),1.0); +} \ No newline at end of file diff --git a/res/shader/sample_depth_buffer.fs b/res/shader/sample_depth_buffer.fs new file mode 100644 index 0000000..6f62b58 --- /dev/null +++ b/res/shader/sample_depth_buffer.fs @@ -0,0 +1,4 @@ + +void main() +{ +} \ No newline at end of file diff --git a/res/shader/sample_depth_buffer.vs b/res/shader/sample_depth_buffer.vs new file mode 100644 index 0000000..8c7c4f1 --- /dev/null +++ b/res/shader/sample_depth_buffer.vs @@ -0,0 +1,12 @@ +attribute vec3 pos; +attribute vec2 texcoord; +attribute vec3 normal; + +uniform mat4 M; +uniform mat4 P; +uniform mat4 V; + +void main() +{ + gl_Position=P*V*M*vec4(pos,1.0); +} \ No newline at end of file diff --git a/shader3.vcxproj b/shader3.vcxproj index 8c68a39..6170bb0 100644 --- a/shader3.vcxproj +++ b/shader3.vcxproj @@ -82,7 +82,11 @@ + + + + diff --git a/shader3.vcxproj.filters b/shader3.vcxproj.filters index 51516cc..7c41b47 100644 --- a/shader3.vcxproj.filters +++ b/shader3.vcxproj.filters @@ -49,5 +49,17 @@ src + + src + + + src + + + src + + + src + \ No newline at end of file