|
@ -81,6 +81,25 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
originalProgram.DetectAttribute("texcoord"); |
|
|
originalProgram.DetectAttribute("texcoord"); |
|
|
originalProgram.DetectUniform("U_MainTexture"); |
|
|
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 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 projector program
|
|
|
//init projector program
|
|
|
GPUProgram sampleProgram; |
|
|
GPUProgram sampleProgram; |
|
|
sampleProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/sample.vs"); |
|
|
sampleProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/sample.vs"); |
|
@ -110,6 +129,7 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
projectedTextureProgram.DetectUniform("U_MainTexture"); |
|
|
projectedTextureProgram.DetectUniform("U_MainTexture"); |
|
|
projectedTextureProgram.DetectUniform("U_ProjectiveTexture"); |
|
|
projectedTextureProgram.DetectUniform("U_ProjectiveTexture"); |
|
|
projectedTextureProgram.DetectUniform("U_ProjectorMatrix"); |
|
|
projectedTextureProgram.DetectUniform("U_ProjectorMatrix"); |
|
|
|
|
|
projectedTextureProgram.DetectUniform("U_ProjectorNoTransScaleMatrix"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//init 3d model
|
|
|
//init 3d model
|
|
@ -132,12 +152,14 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
|
|
|
|
|
|
glm::mat4 sphereModel = glm::translate<float>(0.0f, 0.0f, 0.0f) * glm::scale(0.3f, 0.3f, 0.3f); |
|
|
glm::mat4 sphereModel = glm::translate<float>(0.0f, 0.0f, 0.0f) * glm::scale(0.3f, 0.3f, 0.3f); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glm::mat4 viewMatrix = glm::lookAt(glm::vec3(10.0f, 3.0f, 0.0f), glm::vec3(6.0f, -1.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f)); |
|
|
glm::mat4 viewMatrix = glm::lookAt(glm::vec3(10.0f, 3.0f, 0.0f), glm::vec3(6.0f, -1.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f)); |
|
|
glm::mat4 projectionMatrix = glm::perspective(50.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f); |
|
|
glm::mat4 projectionMatrix = glm::perspective(50.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f); |
|
|
|
|
|
|
|
|
glm::mat4 projectorMatrix = glm::translate<float>(0.5f, 0.5f, 0.5f)*glm::scale(0.5f, 0.5f, 0.5f) * projectionMatrix * viewMatrix; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glm::mat4 projectorViewMatrix = glm::lookAt(glm::vec3(0.0f, 4.0f, 0.0f), glm::vec3(6.0f, -1.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f)); |
|
|
|
|
|
glm::mat4 projectorProjectionMatrix = glm::perspective(50.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f); |
|
|
|
|
|
glm::mat4 projectorMatrix = glm::translate<float>(0.5f, 0.5f, 0.5f)*glm::scale(0.5f, 0.5f, 0.5f) * projectorProjectionMatrix * projectorViewMatrix; |
|
|
|
|
|
glm::mat4 projectorNoTransScaleMatrix = projectorProjectionMatrix * projectorViewMatrix; |
|
|
|
|
|
|
|
|
//³õʼ»¯fsq
|
|
|
//³õʼ»¯fsq
|
|
|
FullScreenQuad fsq; |
|
|
FullScreenQuad fsq; |
|
|
fsq.Init(); |
|
|
fsq.Init(); |
|
@ -152,16 +174,40 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
projectiveTextureFbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight); |
|
|
projectiveTextureFbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight); |
|
|
projectiveTextureFbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight); |
|
|
projectiveTextureFbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight); |
|
|
projectiveTextureFbo.Finish(); |
|
|
projectiveTextureFbo.Finish(); |
|
|
|
|
|
|
|
|
|
|
|
FBO depthFbo; |
|
|
|
|
|
depthFbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGB, viewportWidth, viewportHeight); |
|
|
|
|
|
depthFbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight); |
|
|
|
|
|
depthFbo.Finish(); |
|
|
|
|
|
|
|
|
GLuint mainTexture = SOIL_load_OGL_texture("res/image/stone.bmp", 0, 0, SOIL_FLAG_POWER_OF_TWO); |
|
|
GLuint mainTexture = SOIL_load_OGL_texture("res/image/stone.bmp", 0, 0, SOIL_FLAG_POWER_OF_TWO); |
|
|
GLuint projectiveTexture = SOIL_load_OGL_texture("res/image/grass.png", 0, 0, SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_INVERT_Y); |
|
|
|
|
|
|
|
|
GLuint projectiveTexture = SOIL_load_OGL_texture("res/image/head.png", 0, 0, SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_INVERT_Y); |
|
|
|
|
|
|
|
|
ShowWindow(hwnd, SW_SHOW); |
|
|
ShowWindow(hwnd, SW_SHOW); |
|
|
UpdateWindow(hwnd); |
|
|
UpdateWindow(hwnd); |
|
|
|
|
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
|
|
glEnable(GL_DEPTH_TEST); |
|
|
glEnable(GL_DEPTH_TEST); |
|
|
|
|
|
glEnable(GL_CULL_FACE); |
|
|
|
|
|
glCullFace(GL_FRONT); |
|
|
|
|
|
depthFbo.Bind(); |
|
|
|
|
|
glViewport(0, 0, viewportWidth, viewportHeight); |
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
|
|
|
glUseProgram(depthProgram.mProgram); |
|
|
|
|
|
glUniformMatrix4fv(depthProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectorProjectionMatrix)); |
|
|
|
|
|
glUniformMatrix4fv(depthProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model)); |
|
|
|
|
|
glUniformMatrix4fv(depthProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(projectorViewMatrix)); |
|
|
|
|
|
obj.Bind(depthProgram.GetLocation("pos"), depthProgram.GetLocation("texcoord"), depthProgram.GetLocation("normal")); |
|
|
|
|
|
obj.Draw(); |
|
|
|
|
|
glCullFace(GL_BACK); |
|
|
|
|
|
|
|
|
|
|
|
glUniformMatrix4fv(depthProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(quadModel)); |
|
|
|
|
|
quad.Bind(depthProgram.GetLocation("pos"), depthProgram.GetLocation("texcoord"), depthProgram.GetLocation("normal")); |
|
|
|
|
|
quad.Draw(); |
|
|
|
|
|
|
|
|
|
|
|
depthFbo.Unbind(); |
|
|
|
|
|
|
|
|
|
|
|
/*Ôͼfbo*/ |
|
|
originalFbo.Bind(); |
|
|
originalFbo.Bind(); |
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
glUseProgram(sampleProgram.mProgram); |
|
|
glUseProgram(sampleProgram.mProgram); |
|
@ -180,6 +226,7 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
quad.Draw(); |
|
|
quad.Draw(); |
|
|
originalFbo.Unbind(); |
|
|
originalFbo.Unbind(); |
|
|
|
|
|
|
|
|
|
|
|
/*ͶӰfbo*/ |
|
|
projectiveTextureFbo.Bind(); |
|
|
projectiveTextureFbo.Bind(); |
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
glUseProgram(projectedTextureProgram.mProgram); |
|
|
glUseProgram(projectedTextureProgram.mProgram); |
|
@ -188,6 +235,7 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
glUniformMatrix4fv(projectedTextureProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model)); |
|
|
glUniformMatrix4fv(projectedTextureProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model)); |
|
|
|
|
|
|
|
|
glUniformMatrix4fv(projectedTextureProgram.GetLocation("U_ProjectorMatrix"), 1, GL_FALSE, glm::value_ptr(projectorMatrix)); |
|
|
glUniformMatrix4fv(projectedTextureProgram.GetLocation("U_ProjectorMatrix"), 1, GL_FALSE, glm::value_ptr(projectorMatrix)); |
|
|
|
|
|
glUniformMatrix4fv(projectedTextureProgram.GetLocation("U_ProjectorNoTransScaleMatrix"), 1, GL_FALSE, glm::value_ptr(projectorNoTransScaleMatrix)); |
|
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE0); |
|
|
glActiveTexture(GL_TEXTURE0); |
|
|
glBindTexture(GL_TEXTURE_2D, mainTexture); |
|
|
glBindTexture(GL_TEXTURE_2D, mainTexture); |
|
@ -197,6 +245,10 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
glBindTexture(GL_TEXTURE_2D, projectiveTexture); |
|
|
glBindTexture(GL_TEXTURE_2D, projectiveTexture); |
|
|
glUniform1i(projectedTextureProgram.GetLocation("U_ProjectiveTexture"), 1); |
|
|
glUniform1i(projectedTextureProgram.GetLocation("U_ProjectiveTexture"), 1); |
|
|
|
|
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE2); |
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, depthFbo.GetBuffer("depth")); |
|
|
|
|
|
glUniform1i(projectedTextureProgram.GetLocation("U_ShadowMap"), 2); |
|
|
|
|
|
|
|
|
obj.Bind(projectedTextureProgram.GetLocation("pos"), projectedTextureProgram.GetLocation("texcoord"), projectedTextureProgram.GetLocation("normal")); |
|
|
obj.Bind(projectedTextureProgram.GetLocation("pos"), projectedTextureProgram.GetLocation("texcoord"), projectedTextureProgram.GetLocation("normal")); |
|
|
obj.Draw(); |
|
|
obj.Draw(); |
|
|
|
|
|
|
|
@ -234,6 +286,12 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0); |
|
|
glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0); |
|
|
fsq.DrawToRightTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord")); |
|
|
fsq.DrawToRightTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord")); |
|
|
|
|
|
|
|
|
|
|
|
glUseProgram(depthRenderProgram.mProgram); |
|
|
|
|
|
glActiveTexture(GL_TEXTURE0); |
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, depthFbo.GetBuffer("depth")); |
|
|
|
|
|
glUniform1i(depthRenderProgram.GetLocation("U_MainTexture"), 0); |
|
|
|
|
|
fsq.DrawToRightBottom(depthRenderProgram.GetLocation("pos"), depthRenderProgram.GetLocation("texcoord")); |
|
|
|
|
|
|
|
|
glFlush(); |
|
|
glFlush(); |
|
|
SwapBuffers(dc); |
|
|
SwapBuffers(dc); |
|
|
} |
|
|
} |
|
|