From d52ee915d62958fa53ab7efe62ec765eed589f66 Mon Sep 17 00:00:00 2001 From: ubuntu20 Date: Sun, 11 Oct 2020 09:49:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=82=B9=E5=85=89=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Res/direction_light.fs | 32 ++++++++++++++++++++++++ Res/direction_light.vs | 14 +++++++++++ Res/point_light.fs | 43 +++++++++++++++++++++++++++++++++ Res/point_light.vs | 14 +++++++++++ renderFramework.vcxproj | 4 +-- renderFramework.vcxproj.filters | 4 +-- scene.cpp | 4 +-- 7 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 Res/direction_light.fs create mode 100644 Res/direction_light.vs create mode 100644 Res/point_light.fs create mode 100644 Res/point_light.vs diff --git a/Res/direction_light.fs b/Res/direction_light.fs new file mode 100644 index 0000000..da04587 --- /dev/null +++ b/Res/direction_light.fs @@ -0,0 +1,32 @@ +uniform vec4 U_AmbientMaterial; +uniform vec4 U_AmbientLight; +uniform vec4 U_DiffuseMaterial; +uniform vec4 U_DiffuseLight; +uniform vec4 U_SpecularMaterial; +uniform vec4 U_SpecularLight; +uniform vec4 U_LightPos; +uniform vec4 U_CameraPos; +varying vec4 V_Normal; +varying vec4 V_WorldPos; + +void main(){ + //ambient + vec4 ambientColor = U_AmbientMaterial * U_AmbientLight; + + //diffuse + vec3 n = normalize(V_Normal.xyz); + vec3 l = normalize(U_LightPos - vec3(0)); + float duffuseIntensity = max(0.0, dot(l,n)); + vec4 duffuseColor = U_DiffuseMaterial * U_DiffuseLight * duffuseIntensity; + + //specular + vec4 specularColor = vec4(0); + if(duffuseIntensity > 0){ + vec3 rd = normalize(reflect(-l,n)); + vec3 vd = normalize((U_CameraPos - V_WorldPos).xyz); + float specularIntensity = pow(max(0.0,dot(vd,rd)), 128); + specularColor = U_SpecularMaterial * U_SpecularLight * specularIntensity; + } + + gl_FragColor = ambientColor + duffuseColor + specularColor; +} diff --git a/Res/direction_light.vs b/Res/direction_light.vs new file mode 100644 index 0000000..c7d70b8 --- /dev/null +++ b/Res/direction_light.vs @@ -0,0 +1,14 @@ +attribute vec4 position; +attribute vec4 texcoord; +attribute vec4 normal; +uniform mat4 ModelMatrix; +uniform mat4 ProjectionMatrix; +uniform mat4 ViewMatrix; +uniform mat4 IT_ModelMatrix; +varying vec4 V_Normal; +varying vec4 V_WorldPos; +void main(){ + V_WorldPos = ModelMatrix * position; + V_Normal = IT_ModelMatrix * normal; + gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * position; +} \ No newline at end of file diff --git a/Res/point_light.fs b/Res/point_light.fs new file mode 100644 index 0000000..f79c9ae --- /dev/null +++ b/Res/point_light.fs @@ -0,0 +1,43 @@ +uniform vec4 U_AmbientMaterial; +uniform vec4 U_AmbientLight; +uniform vec4 U_DiffuseMaterial; +uniform vec4 U_DiffuseLight; +uniform vec4 U_SpecularMaterial; +uniform vec4 U_SpecularLight; +uniform vec4 U_LightPos; +uniform vec4 U_CameraPos; +varying vec4 V_Normal; +varying vec4 V_WorldPos; + +void main(){ + //ambient + vec4 ambientColor = U_AmbientMaterial * U_AmbientLight; + + //diffuse + vec3 n = normalize(V_Normal.xyz); + vec3 l = normalize(U_LightPos.xyz - V_WorldPos.xyz); + float duffuseIntensity = max(0.0, dot(l,n)); + + //·½Ïò¹âË¥¼õ¼ÆËã + float attenuation = 1.0; + float distance = 0; + float constantFactor = 0.5; + float linearFactor = 0.3; + float expFactor = 0.1; + + distance =length(U_LightPos.xyz - V_WorldPos.xyz); + attenuation = 1.0 / (constantFactor + linearFactor*distance + expFactor*distance*distance); + + vec4 duffuseColor = U_DiffuseMaterial * U_DiffuseLight * duffuseIntensity * attenuation; + + //specular + vec4 specularColor = vec4(0); + if(duffuseIntensity > 0){ + vec3 rd = normalize(reflect(-l,n)); + vec3 vd = normalize((U_CameraPos - V_WorldPos).xyz); + float specularIntensity = pow(max(0.0,dot(vd,rd)), 128); + specularColor = U_SpecularMaterial * U_SpecularLight * specularIntensity; + } + + gl_FragColor = ambientColor + duffuseColor + specularColor; +} diff --git a/Res/point_light.vs b/Res/point_light.vs new file mode 100644 index 0000000..c7d70b8 --- /dev/null +++ b/Res/point_light.vs @@ -0,0 +1,14 @@ +attribute vec4 position; +attribute vec4 texcoord; +attribute vec4 normal; +uniform mat4 ModelMatrix; +uniform mat4 ProjectionMatrix; +uniform mat4 ViewMatrix; +uniform mat4 IT_ModelMatrix; +varying vec4 V_Normal; +varying vec4 V_WorldPos; +void main(){ + V_WorldPos = ModelMatrix * position; + V_Normal = IT_ModelMatrix * normal; + gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * position; +} \ No newline at end of file diff --git a/renderFramework.vcxproj b/renderFramework.vcxproj index 670d033..9a9c4b2 100644 --- a/renderFramework.vcxproj +++ b/renderFramework.vcxproj @@ -149,8 +149,8 @@ - - + + diff --git a/renderFramework.vcxproj.filters b/renderFramework.vcxproj.filters index 324d8dc..2c61020 100644 --- a/renderFramework.vcxproj.filters +++ b/renderFramework.vcxproj.filters @@ -62,10 +62,10 @@ - + æºæ–‡ä»¶ - + æºæ–‡ä»¶ diff --git a/scene.cpp b/scene.cpp index 805fc22..c34652a 100644 --- a/scene.cpp +++ b/scene.cpp @@ -13,7 +13,7 @@ FullScreenQuad *fsq; void Init() { model.Init("Res/Sphere.obj"); - model.mShader->Init("Res/blin.vs", "Res/blin.fs"); + model.mShader->Init("Res/point_light.vs", "Res/point_light.fs"); model.SetPosition(0.0f, 0.0f, 0.0f); //ÉèÖû·¾³¹â²ÎÊý @@ -21,7 +21,7 @@ void Init() model.mShader->SetVec4("U_AmbientLight", 0.1f, 0.1f, 0.1f, 1.0f); //ÉèÖû·¾³¹â²ÎÊý - model.mShader->SetVec4("U_LightPos", 0.0f, 1.0f, 0.0f, 0.0f); + model.mShader->SetVec4("U_LightPos", 0.0f, 1.4f, 0.0f, 0.0f); model.SetDiffuseMaterial(0.4f, 0.4f, 0.4f, 1.0f); model.mShader->SetVec4("U_DiffuseLight", 0.8f, 0.8f, 0.8f, 1.0f);