From f29c51c97d3b93b13d530a155f69579c8ea23df1 Mon Sep 17 00:00:00 2001 From: blobt Date: Sun, 27 Sep 2020 10:11:49 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=90=E9=A1=B6=E7=82=B9=20=E6=BC=AB?= =?UTF-8?q?=E5=8F=8D=E5=B0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Res/diffuse_vs .vs | 30 ++++++++++++++++++++++++++++++ Res/diffuse_vs.fs | 8 ++++++++ Res/diffuse_vs.vs | 30 ++++++++++++++++++++++++++++++ renderFramework.vcxproj | 5 ++--- renderFramework.vcxproj.filters | 7 ++----- scene.cpp | 7 ++++++- 6 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 Res/diffuse_vs .vs create mode 100644 Res/diffuse_vs.fs create mode 100644 Res/diffuse_vs.vs diff --git a/Res/diffuse_vs .vs b/Res/diffuse_vs .vs new file mode 100644 index 0000000..a93c5f8 --- /dev/null +++ b/Res/diffuse_vs .vs @@ -0,0 +1,30 @@ +#ifdef GL_ES +precision mediump float; +#endif; +attribute vec4 position; +attribute vec4 texcoord; +attribute vec2 normal; +uniform mat4 ModelMatrix; +uniform mat4 ViewMatrix; +uniform mat4 ProjectionMatrix; +uniform mat4 IT_ModelMatrix; +uniform vec4 U_AmbientMaterial; +uniform vec4 U_AmbientLight; +uniform vec4 U_LightPos; +uniform vec4 U_DiffuseMaterial; +uniform vec4 U_DiffuseLight; +varying vec4 V_Color; +void main(){ + //ambient + vec4 ambientColor = U_AmbientMaterial*U_AmbientLight; + + //diffuse + vec3 L = normalize(U_LightPos.xyz - vec3(0)); + vec3 n = normalize(IT_ModelMatrix * normal.xyz); + float diffuseIntensity = max(0.0, dot(L,n)); + vec4 diffuseColor = U_DiffuseMaterial * U_DiffuseLight * diffuseIntensity; + + V_Color = ambientColor + diffuseColor; + + gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * position; +} \ No newline at end of file diff --git a/Res/diffuse_vs.fs b/Res/diffuse_vs.fs new file mode 100644 index 0000000..2f90d25 --- /dev/null +++ b/Res/diffuse_vs.fs @@ -0,0 +1,8 @@ +#ifdef GL_ES +precision mediump float; +#endif +varying vec4 V_Color; +void main() +{ + gl_FragColor=V_Color; +} \ No newline at end of file diff --git a/Res/diffuse_vs.vs b/Res/diffuse_vs.vs new file mode 100644 index 0000000..7e4564b --- /dev/null +++ b/Res/diffuse_vs.vs @@ -0,0 +1,30 @@ +#ifdef GL_ES +precision mediump float; +#endif; +attribute vec4 position; +attribute vec4 texcoord; +attribute vec4 normal; +uniform mat4 ModelMatrix; +uniform mat4 ViewMatrix; +uniform mat4 ProjectionMatrix; +uniform mat4 IT_ModelMatrix; +uniform vec4 U_AmbientMaterial; +uniform vec4 U_AmbientLight; +uniform vec4 U_LightPos; +uniform vec4 U_DiffuseMaterial; +uniform vec4 U_DiffuseLight; +varying vec4 V_Color; +void main(){ + //ambient + vec4 ambientColor = U_AmbientMaterial*U_AmbientLight; + + //diffuse + vec3 L = normalize(U_LightPos.xyz - vec3(0)); + vec3 n = normalize((IT_ModelMatrix * normal).xyz); + float diffuseIntensity = max(0.0, dot(L,n)); + vec4 diffuseColor = U_DiffuseMaterial * U_DiffuseLight * diffuseIntensity; + + V_Color = ambientColor + diffuseColor; + + gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * position; +} \ No newline at end of file diff --git a/renderFramework.vcxproj b/renderFramework.vcxproj index 556173e..6e7117f 100644 --- a/renderFramework.vcxproj +++ b/renderFramework.vcxproj @@ -149,11 +149,10 @@ - - + + - diff --git a/renderFramework.vcxproj.filters b/renderFramework.vcxproj.filters index 2cbce03..1219cc8 100644 --- a/renderFramework.vcxproj.filters +++ b/renderFramework.vcxproj.filters @@ -68,13 +68,10 @@ 源文件 - + 源文件 - - 源文件 - - + 源文件 diff --git a/scene.cpp b/scene.cpp index 41cf287..208f27b 100644 --- a/scene.cpp +++ b/scene.cpp @@ -13,10 +13,13 @@ FullScreenQuad *fsq; void Init() { model.Init("Res/Sphere.obj"); - model.mShader->Init("Res/ambient_vs.vs", "Res/ambient_vs.fs"); + model.mShader->Init("Res/diffuse_vs.vs", "Res/diffuse_vs.fs"); model.SetPosition(0.0f, 0.0f, 0.0f); model.SetAmbientMaterial(0.1f, 0.1f, 0.1f, 1.0f); model.mShader->SetVec4("U_AmbientLight", 0.1f, 0.1f, 0.1f, 1.0f); + model.SetDiffuseMaterial(0.4f, 0.4f, 0.4f, 1.0f); + model.mShader->SetVec4("U_DiffuseLight", 0.8f, 0.8f, 0.8f, 1.0f); + model.mShader->SetVec4("U_LightPos", 0.0f, 1.0f, 0.0f, 0.0f); viewMatrix = glm::lookAt(cameraPos, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); fsq = new FullScreenQuad(); fsq->Init(); @@ -32,6 +35,8 @@ void SetViewPortSize(float width, float height) fbo->Finish(); fbo->Bind(); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); model.Draw(viewMatrix, projectionMatrix, cameraPos.x, cameraPos.y, cameraPos.z); fbo->Unbind(); }