blobt
5 years ago
6 changed files with 122 additions and 30 deletions
-
BIN.vs/shader2/v14/.suo
-
52main.cpp
-
71res/shader/MixLight.fs
-
19res/shader/MixLight.vs
-
4shader2.vcxproj
-
4shader2.vcxproj.filters
@ -0,0 +1,71 @@ |
|||
#version 400 core |
|||
subroutine vec4 SurfaceColor(); |
|||
|
|||
uniform sampler2D U_MainTexture; |
|||
subroutine uniform SurfaceColor U_SurfaceColor; |
|||
varying vec3 V_Normal; |
|||
varying vec4 V_WorldPos;//当前的位置 |
|||
varying vec2 V_Texcoord; |
|||
|
|||
subroutine (SurfaceColor) vec4 Ambient(){ |
|||
|
|||
//ambient |
|||
vec4 AmbientLightColor = vec4(0.4, 0.4, 0.4, 1.0); |
|||
vec4 AmbientMaterial = vec4(0.2, 0.2, 0.2, 1.0); |
|||
vec4 AmbientColor = AmbientLightColor*AmbientMaterial; |
|||
|
|||
vec4 ret = AmbientColor; |
|||
return ret; |
|||
} |
|||
|
|||
subroutine (SurfaceColor) vec4 Diffuse(){ |
|||
|
|||
vec3 lightPos = vec3(10.0, 10.0, 0.0); |
|||
vec3 L=lightPos; |
|||
L = normalize(L); |
|||
vec3 n = normalize(V_Normal); |
|||
|
|||
//ambient |
|||
vec4 AmbientLightColor = vec4(0.4, 0.4, 0.4, 1.0); |
|||
vec4 AmbientMaterial = vec4(0.2, 0.2, 0.2, 1.0); |
|||
vec4 AmbientColor = AmbientLightColor*AmbientMaterial; |
|||
|
|||
//diffuse |
|||
vec4 DiffuseLightColor = vec4(1.0,1.0,1.0,1.0); |
|||
vec4 DiffuseMaterial = vec4(0.8, 0.8, 0.8, 1.0); |
|||
vec4 diffuseColor = DiffuseLightColor*DiffuseMaterial*max(0.0, dot(L,n)); |
|||
|
|||
vec4 ret = AmbientColor + texture2D(U_MainTexture, V_Texcoord) * diffuseColor; |
|||
return ret; |
|||
} |
|||
|
|||
subroutine (SurfaceColor) vec4 Specular(){ |
|||
vec3 lightPos = vec3(10.0, 10.0, 0.0); |
|||
vec3 L=lightPos; |
|||
L = normalize(L); |
|||
vec3 n = normalize(V_Normal); |
|||
|
|||
//ambient |
|||
vec4 AmbientLightColor = vec4(0.4, 0.4, 0.4, 1.0); |
|||
vec4 AmbientMaterial = vec4(0.2, 0.2, 0.2, 1.0); |
|||
vec4 AmbientColor = AmbientLightColor*AmbientMaterial; |
|||
|
|||
//diffuse |
|||
vec4 DiffuseLightColor = vec4(1.0,1.0,1.0,1.0); |
|||
vec4 DiffuseMaterial = vec4(0.8, 0.8, 0.8, 1.0); |
|||
vec4 diffuseColor = DiffuseLightColor*DiffuseMaterial*max(0.0, dot(L,n)); |
|||
|
|||
//specular |
|||
vec4 SpecularLightColor = vec4(1.0,1.0,1.0,1.0); |
|||
vec4 SpecularMaterial = vec4(0.9,0.9,0.9,1.0); |
|||
vec3 reflectDir = normalize(reflect(-L,n));//-L就是光线的入射方向向量,以法线为基础计算方式向量 |
|||
vec3 viewDir = normalize(vec3(0.0) - V_WorldPos.xyz);//vec3(0.0)表示已中心为观察点,这里求的是眼睛到物体的视线向量 |
|||
vec4 specularColor = SpecularLightColor*SpecularMaterial*pow(max(0.0,dot(viewDir,reflectDir)),128.0); |
|||
|
|||
vec4 ret = AmbientColor + texture2D(U_MainTexture, V_Texcoord) * diffuseColor + specularColor; |
|||
return ret; |
|||
} |
|||
|
|||
void main() { |
|||
gl_FragColor = U_SurfaceColor(); |
|||
} |
@ -0,0 +1,19 @@ |
|||
attribute vec3 pos; |
|||
attribute vec2 texcoord; |
|||
attribute vec3 normal; |
|||
|
|||
uniform mat4 M; |
|||
uniform mat4 V; |
|||
uniform mat4 P; |
|||
uniform mat4 NM; |
|||
|
|||
varying vec3 V_Normal; |
|||
varying vec4 V_WorldPos;//µ±Ç°µÄλÖà |
|||
varying vec2 V_Texcoord; |
|||
|
|||
void main() { |
|||
V_Normal = mat3(NM)*normal; |
|||
V_WorldPos = M * vec4(pos, 1.0); |
|||
V_Texcoord = texcoord; |
|||
gl_Position = P*V*M*vec4(pos,1.0); |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue