Browse Source

光源整理

master
blobt 4 years ago
parent
commit
ac9786876e
  1. BIN
      .vs/shader3/v14/.suo
  2. 11
      main.cpp
  3. 58
      res/shader/test.fs

BIN
.vs/shader3/v14/.suo

11
main.cpp

@ -98,15 +98,16 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
float ambientLightColor[] = {0.4f,0.4f,0.4f,1.0f};
float ambientMaterial[] = { 0.2f,0.2f,0.2f,1.0f };
float diffuseLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
float diffuseMaterial[] = { 0.6f,0.6f,0.6f,1.0f };
float lightPos[] = {0.0f,1.5f,-3.0f,0.0f};
float spotLightDirection[] = {0.0f, -1.0f, 0.0f, 64.0f};//把最后一位改成......
float spotLightCutoff = 30.0f;
float diffuseMaterial[] = { 0.6f, 0.6f, 0.6f, 1.0f };
float lightPos[] = {0.0f,1.5f,-3.0f,1.0f};
float spotLightDirection[] = {0.0f, -1.0f, 0.0f, 110.0f};//把最后一位改成......
float spotLightCutoff = 15.0f;
float specularLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
float specularMaterial[] = { 1.0f,1.0f,1.0f,1.0f };
float eyePos[] = { 0.0f,0.0f,0.0f };
glm::mat4 modelMatrix = glm::translate<float>(0.0f, -0.5f, -3.0f) * glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f)*glm::scale(2.0f,2.0f,2.0f);
glm::mat4 modelMatrix = glm::translate<float>(0.0f, -0.5f, -3.0f) * glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f) * glm::scale(2.0f, 2.0f, 2.0f);
glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
glm::mat4 normalMatrix = glm::inverseTranspose(modelMatrix);

58
res/shader/test.fs

@ -15,50 +15,46 @@ varying vec3 V_WorldPos;
void main()
{
//
float radianCutoff = U_Cutoff * 3.14 / 180;
float cosThta = cos(radianCutoff);
vec3 spotLightDirection = normalize(U_LightDirection.xyz);
//common
vec3 N = normalize(V_Normal);
//ambient
vec4 ambientColor = U_AmbientLightColor * U_AmbientMaterial;
//diffuse
vec3 L = vec3(0.0);
float distance = 0.0;
float attenuation = 1.0;
//light attribute
float constantFactor=0.5;
float linearFactor=0.3;
float expFactor=0.1;
if(U_LightPos.w==0.0){
//
L = normalize(U_LightPos.xyz);
float diffuseIntensity = 0.0;
if(U_LightPos.w == 0 && U_Cutoff == 0){
//
L = normalize(U_LightPos.xyz - vec3(0.0));
} else {
//
//model point -> light pos
//
L = normalize(U_LightPos.xyz - V_WorldPos);
distance = length(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);
}
//
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));
L = normalize(-L);
vec3 N = normalize(V_Normal);
if(currentThta > cosThta){
diffuseIntensity = pow(currentThta, U_LightDirection.w);
}
}
float currentCosThta = max(0.0, dot(L,spotLightDirection));
vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * max(0.0, dot(L,N)) * attenuation * diffuseIntensity;
float diffuseIntensity = 0.0;
if(currentCosThta > cosThta){
diffuseIntensity = pow(currentCosThta, U_LightDirection.w);
}
vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * diffuseIntensity * attenuation *4.0;
//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;
gl_FragColor = ambientColor + diffuseColor + specularColor;
}
Loading…
Cancel
Save