From 8adf864c398ac2655fd5ab481a4ad064170401fa Mon Sep 17 00:00:00 2001 From: ayachi3 <1592757525@qq.com> Date: Thu, 15 Dec 2022 16:13:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E6=96=87=E4=BB=B6=E7=9A=84=E5=A4=8D?= =?UTF-8?q?=E5=88=B6=E4=B8=8E=E7=BB=99=E4=BA=88=E6=9D=83=E9=99=90=E6=8A=BD?= =?UTF-8?q?=E8=B1=A1=E6=88=90=E4=B8=80=E4=B8=AA=E5=87=BD=E6=95=B0=E3=80=82?= =?UTF-8?q?=20=E6=B7=BB=E5=8A=A0=E5=85=89=E7=85=A7=E6=B8=B2=E6=9F=93?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FinalProject/fragmentshader.fs | 24 ++++++++++++++++++++---- FinalProject/illuminant.fs | 0 FinalProject/illuminant.vs | 11 +++++++++++ FinalProject/lightCaster.h | 4 ++-- FinalProject/sceneviewer.cpp | 13 ++++++++++--- FinalProject/sceneviewer.h | 1 + FinalProject/vertexshader.vs | 7 ++++++- 7 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 FinalProject/illuminant.fs create mode 100644 FinalProject/illuminant.vs 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