Browse Source

逐顶点 漫反射

master
blobt 4 years ago
parent
commit
f29c51c97d
  1. 30
      Res/diffuse_vs .vs
  2. 8
      Res/diffuse_vs.fs
  3. 30
      Res/diffuse_vs.vs
  4. 5
      renderFramework.vcxproj
  5. 7
      renderFramework.vcxproj.filters
  6. 7
      scene.cpp

30
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;
}

8
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;
}

30
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;
}

5
renderFramework.vcxproj

@ -149,11 +149,10 @@
<ClInclude Include="vertexbuffer.h" /> <ClInclude Include="vertexbuffer.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Res\ambient_vs.fs" />
<None Include="Res\ambient_vs.vs" />
<None Include="Res\diffuse_vs.fs" />
<None Include="Res\diffuse_vs.vs" />
<None Include="Res\fullscreenquad.fs" /> <None Include="Res\fullscreenquad.fs" />
<None Include="Res\fullscreenquad.vs" /> <None Include="Res\fullscreenquad.vs" />
<None Include="Res\gray.fs" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

7
renderFramework.vcxproj.filters

@ -68,13 +68,10 @@
<None Include="Res\fullscreenquad.vs"> <None Include="Res\fullscreenquad.vs">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</None> </None>
<None Include="Res\gray.fs">
<None Include="Res\diffuse_vs.fs">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</None> </None>
<None Include="Res\ambient_vs.fs">
<Filter>源文件</Filter>
</None>
<None Include="Res\ambient_vs.vs">
<None Include="Res\diffuse_vs.vs">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</None> </None>
</ItemGroup> </ItemGroup>

7
scene.cpp

@ -13,10 +13,13 @@ FullScreenQuad *fsq;
void Init() void Init()
{ {
model.Init("Res/Sphere.obj"); 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.SetPosition(0.0f, 0.0f, 0.0f);
model.SetAmbientMaterial(0.1f, 0.1f, 0.1f, 1.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.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)); viewMatrix = glm::lookAt(cameraPos, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
fsq = new FullScreenQuad(); fsq = new FullScreenQuad();
fsq->Init(); fsq->Init();
@ -32,6 +35,8 @@ void SetViewPortSize(float width, float height)
fbo->Finish(); fbo->Finish();
fbo->Bind(); 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); model.Draw(viewMatrix, projectionMatrix, cameraPos.x, cameraPos.y, cameraPos.z);
fbo->Unbind(); fbo->Unbind();
} }

Loading…
Cancel
Save