Browse Source

逐像素 blin

master
ubuntu20 4 years ago
parent
commit
abc5dfd247
  1. 39
      Res/blin.fs
  2. 17
      Res/blin.vs
  3. 4
      renderFramework.vcxproj
  4. 4
      renderFramework.vcxproj.filters
  5. 2
      scene.cpp

39
Res/blin.fs

@ -0,0 +1,39 @@
#ifdef GL_ES
precision mediump float;
#endif
uniform vec4 U_AmbientMaterial;
uniform vec4 U_AmbientLight;
uniform vec4 U_LightPos;
uniform vec4 U_DiffuseMaterial;
uniform vec4 U_DiffuseLight;
uniform vec4 U_CameraPos;
uniform vec4 U_SpecularMaterial;
uniform vec4 U_SpecularLight;
varying vec4 V_Normal;
varying vec4 V_WorldPos;
void main()
{
//ambient
vec4 ambirntColor = U_AmbientMaterial*U_AmbientLight;
//diffuse
vec3 L = normalize(U_LightPos.xyz - vec3(0));
vec3 n = normalize(V_Normal.xyz);
float diffuseIntensity = max(0.0, dot(L,n));
vec4 diffuseColor = U_DiffuseMaterial * U_DiffuseLight * diffuseIntensity;
//specular blin
vec4 specularColor = vec4(0);
if(diffuseIntensity > 0.0) {
vec3 vd = normalize((U_CameraPos - V_WorldPos).xyz);
vec3 halfD = normalize(L+vd);
float specularIntensity = pow(max(0.0, dot(n, halfD)),32.0);
specularColor = U_SpecularMaterial * U_SpecularLight * specularIntensity;
}
gl_FragColor=ambirntColor + diffuseColor + specularColor;
}

17
Res/blin.vs

@ -0,0 +1,17 @@
#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;
varying vec4 V_Normal;
varying vec4 V_WorldPos;
void main(){
V_Normal = normal * IT_ModelMatrix;
V_WorldPos = ModelMatrix * position;
gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * position;
}

4
renderFramework.vcxproj

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

4
renderFramework.vcxproj.filters

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

2
scene.cpp

@ -13,7 +13,7 @@ FullScreenQuad *fsq;
void Init() void Init()
{ {
model.Init("Res/Sphere.obj"); model.Init("Res/Sphere.obj");
model.mShader->Init("Res/specular.vs", "Res/specular.fs");
model.mShader->Init("Res/blin.vs", "Res/blin.fs");
model.SetPosition(0.0f, 0.0f, 0.0f); model.SetPosition(0.0f, 0.0f, 0.0f);
//设置环境光参数 //设置环境光参数

Loading…
Cancel
Save