blobt
5 years ago
9 changed files with 162 additions and 47 deletions
-
BIN.vs/shader3/v14/.suo
-
8FBO.cpp
-
68main.cpp
-
70res/shader/hdr.fs
-
13res/shader/hdrrender.fs
-
3shader3.vcxproj
-
9shader3.vcxproj.filters
-
33utils.cpp
-
3utils.h
@ -0,0 +1,70 @@ |
|||
uniform vec4 U_AmbientLightColor; |
|||
uniform vec4 U_AmbientMaterial; |
|||
uniform vec4 U_LightPos; |
|||
uniform vec4 U_LightDirection; |
|||
uniform float U_Cutoff; |
|||
uniform float U_DiffuseIntensity; |
|||
uniform vec4 U_DiffuseLightColor; |
|||
uniform vec4 U_DiffuseMaterial; |
|||
uniform vec3 U_EyePos; |
|||
uniform vec4 U_SpecularLightColor; |
|||
uniform vec4 U_SpecularMaterial; |
|||
|
|||
|
|||
varying vec3 V_Normal; |
|||
varying vec3 V_WorldPos; |
|||
|
|||
void main() |
|||
{ |
|||
|
|||
//common |
|||
vec3 N = normalize(V_Normal); |
|||
|
|||
//ambient |
|||
vec4 ambientColor = U_AmbientLightColor * U_AmbientMaterial; |
|||
|
|||
//diffuse |
|||
vec3 L = vec3(0.0); |
|||
float attenuation = 1.0; |
|||
float diffuseIntensity = 0.0; |
|||
if(U_LightPos.w != 0){// 点光 |
|||
|
|||
L = normalize(U_LightPos.xyz - V_WorldPos); |
|||
float distance = length(U_LightPos.xyz - V_WorldPos); |
|||
float constantFactor=0.5; |
|||
float linearFactor=0.3; |
|||
float expFactor=0.1; |
|||
attenuation = 1.0 / (constantFactor + linearFactor * distance + expFactor * distance * distance); |
|||
|
|||
if( U_Cutoff > 0){//聚光 |
|||
//角度转弧度 |
|||
float radianCutoff = U_Cutoff * 3.14 / 180; |
|||
float cosThta = cos(radianCutoff); |
|||
vec3 spotLightDirection = normalize(U_LightDirection.xyz); |
|||
float currentThta = max(0.0, dot(-L, spotLightDirection)); |
|||
|
|||
if(currentThta > cosThta){ |
|||
if(dot(L,N)>0.0) |
|||
{ |
|||
diffuseIntensity=pow(currentThta,U_LightDirection.w); |
|||
} |
|||
} |
|||
} else { |
|||
diffuseIntensity = max(0.0, dot(L,N)); |
|||
} |
|||
|
|||
} else { |
|||
// 平行光 |
|||
L = normalize(U_LightPos.xyz - vec3(0.0)); |
|||
diffuseIntensity = max(0.0, dot(L,N)); |
|||
} |
|||
|
|||
vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * attenuation * diffuseIntensity * U_DiffuseIntensity; |
|||
|
|||
//specualr |
|||
vec3 RD = normalize(reflect(-L,N)); |
|||
vec3 VD = normalize(U_EyePos - V_WorldPos); |
|||
vec4 specularColor = U_SpecularLightColor * U_SpecularMaterial * pow(max(0.0, dot(RD,VD)) ,128); |
|||
|
|||
gl_FragColor = (ambientColor + diffuseColor) * 10.0; |
|||
} |
@ -0,0 +1,13 @@ |
|||
varying vec2 V_Texcoord; |
|||
|
|||
uniform sampler2D U_MainTexture; |
|||
|
|||
void main() |
|||
{ |
|||
vec4 color = texture2D(U_MainTexture, V_Texcoord); |
|||
|
|||
if(color.r > 2.0){ |
|||
discard; |
|||
} |
|||
gl_FragColor = color; |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue