c++ - opengl着色器定向灯镜面反射随距离增加

标签 c++ opengl glsl shader specular

标题说明了一切。使用 opengls 内置照明系统,高光不会随着与物体的距离而增加或减少,但通过着色器实现。

顶点着色器:

#version 330

layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoord;
layout (location = 2) in vec3 normal;

out vec2 texCoord0;
out vec3 normal0;
out vec3 worldPos0;

uniform mat4 transform;
uniform mat4 normalRotation;
uniform mat4 transformProjected;

void main()
{
    gl_Position = transformProjected * vec4(position, 1.0);
    texCoord0 = texCoord;

    normal0 = normalize((normalRotation * vec4(normal, 0.0))).xyz;
    worldPos0 = (transform * vec4(position, 1.0)).xyz; 
}

片段着色器:

#version 330

in vec2 texCoord0;
in vec3 normal0;
in vec3 worldPos0;

out vec4 fragColor;

struct BaseLight
{
    vec3 colorDiffuse;
    vec3 colorSpecular;
    float intensityDiffuse;
};
struct DirectionalLight
{
    BaseLight base;
    vec3 direction;
};

uniform vec3 tint;
uniform sampler2D sampler;

uniform vec3 eyePos; // camera pos

uniform vec3 ambientLight;
uniform vec3 emissiveLight;

//material 
uniform float specularIntensity;
uniform float specularPower;

uniform DirectionalLight directionalLight;

vec4 calcLight(BaseLight base,vec3 direction, vec3 normal)
{
    float diffuseFactor = dot(normal, -direction);

    vec4 diffuseColorFinal = vec4(0,0,0,0);
    vec4 specularColorFinal = vec4(0,0,0,0);

    if(diffuseFactor > 0)
    {
      diffuseColorFinal =  vec4(base.colorDiffuse,1) * diffuseFactor * base.intensityDiffuse;

      vec3 directionToEye = normalize(eyePos - worldPos0);
      vec3 reflectDirection = normalize(reflect(direction, normal));

      float specularFactor = dot(directionToEye, reflectDirection);
      specularFactor = pow(specularFactor, specularPower);

      if(specularFactor > 0)
          specularColorFinal = vec4(base.colorSpecular,1) * specularFactor   * specularIntensity;
    }
    //
   return diffuseColorFinal + specularColorFinal;
}



void main()
{
    vec4 colorD = texture(sampler, texCoord0.xy) * vec4(tint,1);
    vec3 normal = normal0;
    vec4 totalLight = vec4(ambientLight,1) + vec4(emissiveLight,1); 

     totalLight += calcLight(directionalLight.base,-directionalLight.direction,normal);


    fragColor = colorD * totalLight;
}

enter image description here enter image description here

正如您从两张图片中看到的那样,相机离平面越远,镜面反射光占据的表面积就越大。在我使用 opengls 内置照明进行的测试中,这并没有发生。有没有办法来解决这个问题?我是照明新手,也许这对于定向光源来说是正常的?感谢您的帮助!

我还将我的 eyePos 制服设置为我的 cameraPos。我不知道这是否有帮助。

最佳答案

基本上你需要在片段和光 dist 之间保持距离。这对于定向光来说可能是个问题,因为你只有方向和距离被假定为无限大。也许切换到点光源?

当你有'dist'时,你可以使用公式

att = 1.0 / (Kc + Kl*dist + Kq*dist^2)
Kc - constant attenuation
Kl - linear attenuation
Kq - quadratic attenuation 

更简单的版本(仅使用 Kq,其余设置为 1.0):

float attenuation = 1.0 / (1.0 + light.attenuation * pow(distanceToLight, 2));

然后在照明方程式中,您基本上将计算出的颜色乘以这个 att 因子:

vec4 finalColor = ambient + (diffuseColorFinal + specularColorFinal)*att

http://www.ozone3d.net/tutorials/glsl_lighting_phong_p4.php#part_4

http://tomdalling.com/blog/modern-opengl/07-more-lighting-ambient-specular-attenuation-gamma/

关于c++ - opengl着色器定向灯镜面反射随距离增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19213891/

相关文章:

java - 纹理贴图和光照顶点着色器 ErrorJava OpenGL

c++ - 缩放 3D 形状(OpenGL 和 C++)

python - 无法让 glVertexAttribPointer 从着色器渲染颜色

c++ - GLSL需要什么数据?

C# pinvoke 具有 union 和数组的结构

c++ - 为什么我得到的 C 程序值是垃圾值?

c++ - 如何有效地绘制数千个顶点?

cocoa - NSOpenPanel 破坏了我的 SDL/OpenGL 应用程序

function - GLSL 有内置的双线性插值函数吗?

android - OpenCV 未定义对 cv::SURF::SURF() eclipse 的引用