diff --git a/FinalProject/fragmentshader.fs b/FinalProject/fragmentshader.fs index 229dce5..620dc74 100644 --- a/FinalProject/fragmentshader.fs +++ b/FinalProject/fragmentshader.fs @@ -2,14 +2,30 @@ out vec4 FragColor; in vec2 TexCoords; +in vec3 Normal; +in vec3 FragPos; uniform sampler2D texture_diffuse1; uniform sampler2D texture_specular1; +// above 2 uniform can caculate objectColor + +uniform vec3 lightPos; uniform vec3 lightColor; void main() -{ - FragColor = texture(texture_specular1, TexCoords); - FragColor = texture(texture_diffuse1, TexCoords); - FragColor = vec4(FragColor * vec4(lightColor,1.0f)); +{ + vec3 objectColor = texture(texture_specular1, TexCoords).rgb; + objectColor = texture(texture_diffuse1, TexCoords).rgb; + // ambient + float ambientStrength = 0; + vec3 ambient = ambientStrength * lightColor; + + // diffuse + vec3 norm = normalize(Normal); + vec3 lightDir = normalize(lightPos - FragPos); + float diff = max(dot(norm, lightDir), 0.0); + vec3 diffuse = diff * lightColor; + + vec3 result = (ambient + diffuse) * objectColor; + FragColor = vec4(result, 1.0); } \ No newline at end of file diff --git a/FinalProject/illuminant.fs b/FinalProject/illuminant.fs new file mode 100644 index 0000000..e69de29 diff --git a/FinalProject/illuminant.vs b/FinalProject/illuminant.vs new file mode 100644 index 0000000..c875a90 --- /dev/null +++ b/FinalProject/illuminant.vs @@ -0,0 +1,11 @@ +#version 430 core +layout (location = 0) in vec3 aPos; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + gl_Position = projection * view * model * vec4(aPos, 1.0); +} \ No newline at end of file diff --git a/FinalProject/lightCaster.h b/FinalProject/lightCaster.h index e01b132..d23d3ad 100644 --- a/FinalProject/lightCaster.h +++ b/FinalProject/lightCaster.h @@ -3,8 +3,8 @@ // 发光物不需要纹理,所以说需要另外的shader // 在sceneviewer中被包含 -class LightCaster { +class Illuminant { public: - LightCaster() {}; + Illuminant() {}; }; \ No newline at end of file diff --git a/FinalProject/sceneviewer.cpp b/FinalProject/sceneviewer.cpp index 0f12183..453a00c 100644 --- a/FinalProject/sceneviewer.cpp +++ b/FinalProject/sceneviewer.cpp @@ -5,7 +5,7 @@ #include #include #include - +#include #include "vbo.h" #include "vao.h" #include "shader.h" @@ -89,8 +89,8 @@ void SceneViewer::paintGL() { _shaderProgram.setUniform("projection", projection); - ///////////////////////////////////////最终不应该放在这里 - _shaderProgram.setUniform("lightColor", 0.0f, 1.0f, 0.0f); + ///////////////////////////////////////进行光照处理 + update_light(); ////////////////////////////////////// @@ -174,3 +174,10 @@ void SceneViewer::wheelEvent(QWheelEvent* event) { // Update the view update(); } + +void SceneViewer::update_light() { + auto r = time(NULL); + Logger::debug("1\n"); + _shaderProgram.setUniform("lightPos", (float)sin(r), (float)sin(r/4), (float)sin(r/2.1)); + _shaderProgram.setUniform("lightColor", 1.0f, 1.0f, 1.0f); +} diff --git a/FinalProject/sceneviewer.h b/FinalProject/sceneviewer.h index b45fd4e..2d9bd31 100644 --- a/FinalProject/sceneviewer.h +++ b/FinalProject/sceneviewer.h @@ -41,6 +41,7 @@ private: public: SceneViewer(QWidget* parent = 0); ~SceneViewer(); + void update_light(); protected: // OpenGL functions diff --git a/FinalProject/vertexshader.vs b/FinalProject/vertexshader.vs index 73091f6..fc9d2cf 100644 --- a/FinalProject/vertexshader.vs +++ b/FinalProject/vertexshader.vs @@ -4,6 +4,8 @@ layout (location = 1) in vec3 aNormal; layout (location = 2) in vec2 aTexCoords; out vec2 TexCoords; +out vec3 FragPos; +out vec3 Normal; uniform mat4 model; uniform mat4 view; @@ -11,6 +13,9 @@ uniform mat4 projection; void main() { + FragPos = vec3(model * vec4(aPos, 1.0)); + Normal = aNormal; + TexCoords = aTexCoords; - gl_Position = projection * view * model * vec4(aPos, 1.0); + gl_Position = projection * view * vec4(FragPos, 1.0); } \ No newline at end of file