|
|
@ -72,15 +72,6 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
|
|
|
|
|
glewInit(); |
|
|
|
|
|
|
|
//init fsqgpu program
|
|
|
|
GPUProgram fsqProgram; |
|
|
|
fsqProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs"); |
|
|
|
fsqProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/fullscreenquad.fs"); |
|
|
|
fsqProgram.Link(); |
|
|
|
fsqProgram.DetectAttribute("pos"); |
|
|
|
fsqProgram.DetectAttribute("texcoord"); |
|
|
|
fsqProgram.DetectUniform("U_MainTexture"); |
|
|
|
|
|
|
|
//init gpu program
|
|
|
|
GPUProgram gpuProgram; |
|
|
|
gpuProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/test.vs"); |
|
|
@ -94,21 +85,11 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
|
gpuProgram.DetectUniform("V"); |
|
|
|
gpuProgram.DetectUniform("P"); |
|
|
|
gpuProgram.DetectUniform("NM"); |
|
|
|
gpuProgram.DetectUniform("U_AmbientLightColor"); |
|
|
|
gpuProgram.DetectUniform("U_AmbientMaterial"); |
|
|
|
gpuProgram.DetectUniform("U_DiffuseLightColor"); |
|
|
|
gpuProgram.DetectUniform("U_DiffuseMaterial"); |
|
|
|
gpuProgram.DetectUniform("U_DiffuseIntensity"); |
|
|
|
gpuProgram.DetectUniform("U_LightPos"); |
|
|
|
gpuProgram.DetectUniform("U_LightDirection"); |
|
|
|
gpuProgram.DetectUniform("U_Cutoff"); |
|
|
|
gpuProgram.DetectUniform("U_SpecularLightColor"); |
|
|
|
gpuProgram.DetectUniform("U_SpecularMaterial"); |
|
|
|
gpuProgram.DetectUniform("U_EyePos"); |
|
|
|
|
|
|
|
//init 3d model
|
|
|
|
ObjModel cube; |
|
|
|
cube.Init("res/model/Cube.obj"); |
|
|
|
ObjModel obj; |
|
|
|
obj.Init("res/model/Sphere.obj"); |
|
|
|
|
|
|
|
float identity[] = { |
|
|
|
1.0f,0,0,0, |
|
|
@ -116,57 +97,15 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
|
0,0,1.0f,0, |
|
|
|
0,0,0,1.0f |
|
|
|
}; |
|
|
|
float ambientLightColor[] = {0.4f,0.4f,0.4f,1.0f}; |
|
|
|
float ambientMaterial[] = { 0.2f,0.2f,0.2f,1.0f }; |
|
|
|
float diffuseLightColor[] = { 1.0f,1.0f,1.0f,1.0f }; |
|
|
|
float diffuseMaterial[] = { 0.8f, 0.8f, 0.8f, 1.0f }; |
|
|
|
float lightPos[] = {0.0f,1.0f,0.0f,0.0f}; |
|
|
|
float diffuseIntensity = 1.0f; |
|
|
|
|
|
|
|
float spotLightDirection[] = {0.0f, -1.0f, 0.0f, 128.0f};//把最后一位改成......
|
|
|
|
float spotLightCutoff = 0.0f; |
|
|
|
|
|
|
|
|
|
|
|
float specularLightColor[] = { 1.0f,1.0f,1.0f,1.0f }; |
|
|
|
float specularMaterial[] = { 1.0f,1.0f,1.0f,1.0f }; |
|
|
|
float eyePos[] = { 0.0f,0.0f,0.0f }; |
|
|
|
|
|
|
|
|
|
|
|
glm::mat4 model1 = glm::translate<float>(-2.0f, 0.0f, -6.0f) * glm::rotate(-30.0f , 1.0f, 1.0f, 1.0f); |
|
|
|
glm::mat4 model2 = glm::translate<float>(2.0f, 0.0f, -6.0f) * glm::rotate(-30.0f, 1.0f, 1.0f, 1.0f); |
|
|
|
glm::mat4 model3 = glm::translate<float>(6.0f, 0.0f, -6.0f) * glm::rotate(-30.0f, 1.0f, 1.0f, 1.0f); |
|
|
|
|
|
|
|
glm::mat4 viewMatrix1 = glm::lookAt(glm::vec3(-0.0f, 1.5f, -3.0f), glm::vec3(-2.0f, 0.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f)); |
|
|
|
glm::mat4 viewMatrix2 = glm::lookAt(glm::vec3(3.0f, 1.5f, -3.0f), glm::vec3(2.0f, 0.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f)); |
|
|
|
glm::mat4 viewMatrix3 = glm::lookAt(glm::vec3(7.0f, 1.5f, -3.0f), glm::vec3(6.0f, 0.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f)); |
|
|
|
|
|
|
|
glm::mat4 model = glm::translate<float>(0.0f, 0.0f, -4.0f); |
|
|
|
glm::mat4 viewMatrix = glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, -2.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 normalMatrix = glm::inverseTranspose(model); |
|
|
|
|
|
|
|
glm::mat4 normalMatrix1 = glm::inverseTranspose(model1); |
|
|
|
glm::mat4 normalMatrix2 = glm::inverseTranspose(model2); |
|
|
|
glm::mat4 normalMatrix3 = glm::inverseTranspose(model3); |
|
|
|
|
|
|
|
//初始化fsq
|
|
|
|
FullScreenQuad fsq; |
|
|
|
fsq.Init(); |
|
|
|
|
|
|
|
//初始化FBO
|
|
|
|
FBO fboDirectionLight; |
|
|
|
fboDirectionLight.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight); |
|
|
|
fboDirectionLight.AttachDepthBuffer("depth", viewportWidth, viewportHeight); |
|
|
|
fboDirectionLight.Finish(); |
|
|
|
|
|
|
|
FBO fboPointerLight; |
|
|
|
fboPointerLight.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight); |
|
|
|
fboPointerLight.AttachDepthBuffer("depth", viewportWidth, viewportHeight); |
|
|
|
fboPointerLight.Finish(); |
|
|
|
|
|
|
|
FBO fboSpotLight; |
|
|
|
fboSpotLight.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight); |
|
|
|
fboSpotLight.AttachDepthBuffer("depth", viewportWidth, viewportHeight); |
|
|
|
fboSpotLight.Finish(); |
|
|
|
|
|
|
|
glClearColor(41.0f/255.0f, 71.0f/255.0f, 121.0f / 255.0f, 1.0f); |
|
|
|
ShowWindow(hwnd, SW_SHOW); |
|
|
|
UpdateWindow(hwnd); |
|
|
|
|
|
|
@ -184,103 +123,23 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ |
|
|
|
DispatchMessage(&msg); |
|
|
|
} |
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
|
|
|
|
|
glEnable(GL_DEPTH_TEST); |
|
|
|
glUseProgram(gpuProgram.mProgram); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix)); |
|
|
|
|
|
|
|
glUniform4fv(gpuProgram.GetLocation("U_AmbientLightColor"), 1,ambientLightColor); |
|
|
|
glUniform4fv(gpuProgram.GetLocation("U_AmbientMaterial"), 1, ambientMaterial); |
|
|
|
glUniform4fv(gpuProgram.GetLocation("U_DiffuseLightColor"), 1, diffuseLightColor); |
|
|
|
glUniform4fv(gpuProgram.GetLocation("U_DiffuseMaterial"), 1, diffuseMaterial); |
|
|
|
glUniform4fv(gpuProgram.GetLocation("U_LightDirection"), 1, spotLightDirection); |
|
|
|
glUniform4fv(gpuProgram.GetLocation("U_SpecularLightColor"), 1, specularLightColor); |
|
|
|
glUniform4fv(gpuProgram.GetLocation("U_SpecularMaterial"), 1, specularMaterial); |
|
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix)); |
|
|
|
glUniform3fv(gpuProgram.GetLocation("U_EyePos"), 1, eyePos); |
|
|
|
|
|
|
|
//left
|
|
|
|
fboDirectionLight.Bind(); |
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
|
lightPos[0] = 0.0f; |
|
|
|
lightPos[1] = 1.5f; |
|
|
|
lightPos[2] = 0.0f; |
|
|
|
lightPos[3] = 0.0f; |
|
|
|
diffuseIntensity = 1.0f; |
|
|
|
spotLightCutoff = 0.0f; |
|
|
|
glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos); |
|
|
|
glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity); |
|
|
|
glUniform1f(gpuProgram.GetLocation("U_Cutoff"), spotLightCutoff); |
|
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model1)); |
|
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix1)); |
|
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix1)); |
|
|
|
cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal")); |
|
|
|
cube.Draw(); |
|
|
|
fboDirectionLight.Unbind(); |
|
|
|
|
|
|
|
|
|
|
|
//middle
|
|
|
|
fboPointerLight.Bind(); |
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
|
lightPos[0] = 0.0f; |
|
|
|
lightPos[1] = 3.0f; |
|
|
|
lightPos[2] = -6.0f; |
|
|
|
lightPos[3] = 1.0f; |
|
|
|
diffuseIntensity = 3.0f; |
|
|
|
spotLightCutoff = 0.0f; |
|
|
|
glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos); |
|
|
|
glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity); |
|
|
|
glUniform1f(gpuProgram.GetLocation("U_Cutoff"), spotLightCutoff); |
|
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model2)); |
|
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix2)); |
|
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix2)); |
|
|
|
cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal")); |
|
|
|
cube.Draw(); |
|
|
|
fboDirectionLight.Unbind(); |
|
|
|
|
|
|
|
//right
|
|
|
|
fboSpotLight.Bind(); |
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
|
lightPos[0] = 6.0f; |
|
|
|
lightPos[1] = 3.0f; |
|
|
|
lightPos[2] = -6.0f; |
|
|
|
lightPos[3] = 1.0f; |
|
|
|
diffuseIntensity = 4.0f; |
|
|
|
spotLightCutoff = 15.0f; |
|
|
|
glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos); |
|
|
|
glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity); |
|
|
|
glUniform1f(gpuProgram.GetLocation("U_Cutoff"), spotLightCutoff); |
|
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model3)); |
|
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix3)); |
|
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix3)); |
|
|
|
cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal")); |
|
|
|
cube.Draw(); |
|
|
|
fboSpotLight.Unbind(); |
|
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model)); |
|
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix)); |
|
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix)); |
|
|
|
obj.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal")); |
|
|
|
obj.Draw(); |
|
|
|
|
|
|
|
|
|
|
|
glUseProgram(0); |
|
|
|
glFlush(); |
|
|
|
|
|
|
|
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
|
|
|
|
|
glUseProgram(fsqProgram.mProgram); |
|
|
|
glActiveTexture(GL_TEXTURE0); |
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, fboDirectionLight.GetBuffer("color")); |
|
|
|
glUniform1i(fsqProgram.GetLocation("U_MainTexture"), 0); |
|
|
|
fsq.DrawToLeftTop(fsqProgram.GetLocation("pos"), fsqProgram.GetLocation("texcoord")); |
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, fboSpotLight.GetBuffer("color")); |
|
|
|
glUniform1i(fsqProgram.GetLocation("U_MainTexture"), 0); |
|
|
|
fsq.DrawToLeftBottom(fsqProgram.GetLocation("pos"), fsqProgram.GetLocation("texcoord")); |
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, fboPointerLight.GetBuffer("color")); |
|
|
|
glUniform1i(fsqProgram.GetLocation("U_MainTexture"), 0); |
|
|
|
fsq.DrawToRightTop(fsqProgram.GetLocation("pos"), fsqProgram.GetLocation("texcoord")); |
|
|
|
glFlush(); |
|
|
|
|
|
|
|
SwapBuffers(dc); |
|
|
|
} |
|
|
|
return 0; |