|
|
@ -117,7 +117,7 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
|
//init projective texture program
|
|
|
|
GPUProgram projectedTextureProgram; |
|
|
|
projectedTextureProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/projector.vs"); |
|
|
|
projectedTextureProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/projector.fs"); |
|
|
|
projectedTextureProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/projector_shadow.fs"); |
|
|
|
projectedTextureProgram.Link(); |
|
|
|
|
|
|
|
projectedTextureProgram.DetectAttribute("pos"); |
|
|
@ -131,6 +131,23 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
|
projectedTextureProgram.DetectUniform("U_ProjectorMatrix"); |
|
|
|
projectedTextureProgram.DetectUniform("U_ProjectorNoTransScaleMatrix"); |
|
|
|
|
|
|
|
//init projective texture program2
|
|
|
|
GPUProgram projectedTextureProgram2; |
|
|
|
projectedTextureProgram2.AttachShader(GL_VERTEX_SHADER, "res/shader/projector.vs"); |
|
|
|
projectedTextureProgram2.AttachShader(GL_FRAGMENT_SHADER, "res/shader/projector_shadow2.fs"); |
|
|
|
projectedTextureProgram2.Link(); |
|
|
|
|
|
|
|
projectedTextureProgram2.DetectAttribute("pos"); |
|
|
|
projectedTextureProgram2.DetectAttribute("texcoord"); |
|
|
|
projectedTextureProgram2.DetectUniform("M"); |
|
|
|
projectedTextureProgram2.DetectUniform("V"); |
|
|
|
projectedTextureProgram2.DetectUniform("P"); |
|
|
|
projectedTextureProgram2.DetectUniform("U_ShadowMap"); |
|
|
|
projectedTextureProgram2.DetectUniform("U_MainTexture"); |
|
|
|
projectedTextureProgram2.DetectUniform("U_ProjectiveTexture"); |
|
|
|
projectedTextureProgram2.DetectUniform("U_ProjectorMatrix"); |
|
|
|
projectedTextureProgram2.DetectUniform("U_ProjectorNoTransScaleMatrix"); |
|
|
|
|
|
|
|
|
|
|
|
//init 3d model
|
|
|
|
ObjModel obj, quad, sphere; |
|
|
@ -148,7 +165,7 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
|
|
|
|
|
glm::mat4 model = glm::translate<float>(6.0f, 0.0f, -6.0f) * glm::rotate(-30.0f, 1.0f, 1.0f, 1.0f); |
|
|
|
|
|
|
|
glm::mat4 quadModel = glm::translate<float>(6.0f, -1.5f, -6.0f) * glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f) * glm::scale(10.0f, 10.0f, 10.0f); |
|
|
|
glm::mat4 quadModel = glm::translate<float>(6.0f, -1.5f, -6.0f) * glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f) * glm::scale(20.0f, 20.0f, 20.0f); |
|
|
|
|
|
|
|
glm::mat4 sphereModel = glm::translate<float>(0.0f, 0.0f, 0.0f) * glm::scale(0.3f, 0.3f, 0.3f); |
|
|
|
|
|
|
@ -156,7 +173,7 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
|
glm::mat4 projectionMatrix = glm::perspective(50.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f); |
|
|
|
|
|
|
|
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 projectorProjectionMatrix = glm::perspective(20.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; |
|
|
|
|
|
|
@ -175,6 +192,11 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
|
projectiveTextureFbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight); |
|
|
|
projectiveTextureFbo.Finish(); |
|
|
|
|
|
|
|
FBO projectiveTextureFbo2; |
|
|
|
projectiveTextureFbo2.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight); |
|
|
|
projectiveTextureFbo2.AttachDepthBuffer("depth", viewportWidth, viewportHeight); |
|
|
|
projectiveTextureFbo2.Finish(); |
|
|
|
|
|
|
|
FBO depthFbo; |
|
|
|
depthFbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGB, viewportWidth, viewportHeight); |
|
|
|
depthFbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight); |
|
|
@ -199,13 +221,13 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
|
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(); |
|
|
|
glCullFace(GL_BACK); |
|
|
|
|
|
|
|
/*Ôͼfbo*/ |
|
|
|
originalFbo.Bind(); |
|
|
@ -227,6 +249,8 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
|
originalFbo.Unbind(); |
|
|
|
|
|
|
|
/*ͶӰfbo*/ |
|
|
|
glEnable(GL_BLEND); |
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
|
|
projectiveTextureFbo.Bind(); |
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
|
glUseProgram(projectedTextureProgram.mProgram); |
|
|
@ -257,6 +281,43 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
|
quad.Draw(); |
|
|
|
|
|
|
|
projectiveTextureFbo.Unbind(); |
|
|
|
glDisable(GL_BLEND); |
|
|
|
|
|
|
|
|
|
|
|
/*ͶӰfbo ´øÒõÓ°*/ |
|
|
|
glEnable(GL_BLEND); |
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
|
|
projectiveTextureFbo2.Bind(); |
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
|
glUseProgram(projectedTextureProgram2.mProgram); |
|
|
|
glUniformMatrix4fv(projectedTextureProgram2.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix)); |
|
|
|
glUniformMatrix4fv(projectedTextureProgram2.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix)); |
|
|
|
glUniformMatrix4fv(projectedTextureProgram2.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model)); |
|
|
|
|
|
|
|
glUniformMatrix4fv(projectedTextureProgram2.GetLocation("U_ProjectorMatrix"), 1, GL_FALSE, glm::value_ptr(projectorMatrix)); |
|
|
|
glUniformMatrix4fv(projectedTextureProgram2.GetLocation("U_ProjectorNoTransScaleMatrix"), 1, GL_FALSE, glm::value_ptr(projectorNoTransScaleMatrix)); |
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE0); |
|
|
|
glBindTexture(GL_TEXTURE_2D, mainTexture); |
|
|
|
glUniform1i(projectedTextureProgram2.GetLocation("U_MainTexture"), 0); |
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE1); |
|
|
|
glBindTexture(GL_TEXTURE_2D, projectiveTexture); |
|
|
|
glUniform1i(projectedTextureProgram2.GetLocation("U_ProjectiveTexture"), 1); |
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE2); |
|
|
|
glBindTexture(GL_TEXTURE_2D, depthFbo.GetBuffer("depth")); |
|
|
|
glUniform1i(projectedTextureProgram2.GetLocation("U_ShadowMap"), 2); |
|
|
|
|
|
|
|
obj.Bind(projectedTextureProgram2.GetLocation("pos"), projectedTextureProgram2.GetLocation("texcoord"), projectedTextureProgram2.GetLocation("normal")); |
|
|
|
obj.Draw(); |
|
|
|
|
|
|
|
glUniformMatrix4fv(projectedTextureProgram2.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(quadModel)); |
|
|
|
quad.Bind(projectedTextureProgram2.GetLocation("pos"), projectedTextureProgram2.GetLocation("texcoord"), projectedTextureProgram2.GetLocation("normal")); |
|
|
|
quad.Draw(); |
|
|
|
|
|
|
|
projectiveTextureFbo2.Unbind(); |
|
|
|
glDisable(GL_BLEND); |
|
|
|
|
|
|
|
MSG msg; |
|
|
|
while (true) |
|
|
@ -292,6 +353,12 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
|
glUniform1i(depthRenderProgram.GetLocation("U_MainTexture"), 0); |
|
|
|
fsq.DrawToRightBottom(depthRenderProgram.GetLocation("pos"), depthRenderProgram.GetLocation("texcoord")); |
|
|
|
|
|
|
|
glUseProgram(originalProgram.mProgram); |
|
|
|
glActiveTexture(GL_TEXTURE0); |
|
|
|
glBindTexture(GL_TEXTURE_2D, projectiveTextureFbo2.GetBuffer("color")); |
|
|
|
glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0); |
|
|
|
fsq.DrawToLeftBottom(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord")); |
|
|
|
|
|
|
|
glFlush(); |
|
|
|
SwapBuffers(dc); |
|
|
|
} |
|
|
|