Browse Source

点光源

master
ubuntu20 4 years ago
parent
commit
d52ee915d6
  1. 32
      Res/direction_light.fs
  2. 14
      Res/direction_light.vs
  3. 43
      Res/point_light.fs
  4. 14
      Res/point_light.vs
  5. 4
      renderFramework.vcxproj
  6. 4
      renderFramework.vcxproj.filters
  7. 4
      scene.cpp

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

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

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

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

4
renderFramework.vcxproj

@ -149,8 +149,8 @@
<ClInclude Include="vertexbuffer.h" />
</ItemGroup>
<ItemGroup>
<None Include="Res\blin.fs" />
<None Include="Res\blin.vs" />
<None Include="Res\point_light.fs" />
<None Include="Res\point_light.vs" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

4
renderFramework.vcxproj.filters

@ -62,10 +62,10 @@
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="Res\blin.fs">
<None Include="Res\point_light.fs">
<Filter>源文件</Filter>
</None>
<None Include="Res\blin.vs">
<None Include="Res\point_light.vs">
<Filter>源文件</Filter>
</None>
</ItemGroup>

4
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);

Loading…
Cancel
Save