0

我的镜面光照有一些问题,我有环境光和漫反射,但我现在正在研究镜面光以尝试制作一个漂亮的模型。

我的 vertexShader 中有以下内容:

#version 330

layout (location = 0) in vec3 Position;
layout (location = 1) in vec3 Normal;

out vec4 Colour0;

// Transforms
uniform mat4 gModelToWorldTransform;
uniform mat4 gWorldToViewToProjectionTransform;

// Ambient light parameters
uniform vec3 gAmbientLightIntensity;

// Directional light parameters
uniform vec3 gDirectionalLightIntensity;
uniform vec3 gDirectionalLightDirection;

// Material constants
uniform float gKa;
uniform float gKd;
uniform float gKs;
uniform float gKsStrength;

                                                                   
void main()
{  
    // Transform the vertex from local space to homogeneous clip space 
    vec4 vertexPositionInModelSpace = vec4(Position, 1);
    vec4 vertexInWorldSpace = gModelToWorldTransform * vertexPositionInModelSpace;
    vec4 vertexInHomogeneousClipSpace = gWorldToViewToProjectionTransform * vertexInWorldSpace;
    gl_Position = vertexInHomogeneousClipSpace;

    // Calculate the directional light intensity at the vertex
    // Find the normal in world space and normalise it
    vec3 normalInWorldSpace = (gModelToWorldTransform * vec4(Normal, 0.0)).xyz;
    normalInWorldSpace = normalize(normalInWorldSpace);

    // Calculate the ambient light intensity at the vertex
    // Ia = Ka * ambientLightIntensity
    vec4 ambientLightIntensity = gKa * vec4(gAmbientLightIntensity, 1.0);

    // Setup the light direction and normalise it
    vec3 lightDirection = normalize(-gDirectionalLightDirection);
    
    //lightDirection = normalize(gDirectionalLightDirection);
    // Id = kd * lightItensity * N.L
    // Calculate N.L
    float diffuseFactor = dot(normalInWorldSpace, lightDirection);
    diffuseFactor = clamp(diffuseFactor, 0.0, 1.0);

    // N.L * light source colour * intensity
    vec4 diffuseLightIntensity = gKd * vec4(gDirectionalLightIntensity, 1.0f) * diffuseFactor;

    vec3 lightReflect = normalize(reflect(gDirectionalLightDirection, Normal));

    //Calculate the specular light intensity at the vertex
    float specularFactor = dot(normalInWorldSpace, lightReflect);
    specularFactor = pow(specularFactor, gKsStrength);
    vec4 specularLightIntensity = gKs * vec4(gDirectionalLightIntensity, 1.0f) * specularFactor;
   
    // Final vertex colour is the product of the vertex colour
    // and the total light intensity at the vertex 

    vec4 colour = vec4(0.0, 1.0, 0.0, 1.0);
    Colour0 = colour * (ambientLightIntensity + diffuseLightIntensity + specularLightIntensity);
}

然后在我的 main.cpp 中,我有一些代码可以尝试让它一起工作,镜面光肯定在做一些事情,只是,它并没有让模型看起来有光泽,它似乎将光增强到我可以的程度'看不到任何细节。

我创建以下变量:

// Lighting uniforms location

GLuint gAmbientLightIntensityLoc;
GLuint gDirectionalLightIntensityLoc;
GLuint gDirectionalLightDirectionLoc;
GLuint gSpecularLightIntensityLoc;

// Materials uniform location
GLuint gKaLoc;
GLuint gKdLoc;
GLuint gKsLoc;
GLuint gKsStrengthLoc;

renderSceneCallBack()然后我在主要调用的函数中设置我的变量:

// Set the material properties
glUniform1f(gKaLoc, 0.2f); 
glUniform1f(gKdLoc, 0.9f);
glUniform1f(gKsLoc, 0.5f);
glUniform1f(gKsStrengthLoc, 0.5f);

然后我创建一个initLights()我想处理所有照明的函数,这也被主要调用:

static void initLights()
{
    // Setup the ambient light
    vec3 ambientLightIntensity = vec3(0.2f, 0.2f, 0.2f);
    glUniform3fv(gAmbientLightIntensityLoc, 1, &ambientLightIntensity[0]);
    
    // Setup the direactional light
    vec3 directionalLightDirection = vec3(0.0f, 0.0f, -1.0f);
    normalize(directionalLightDirection);
    glUniform3fv(gDirectionalLightDirectionLoc, 1, &directionalLightDirection[0]);

    vec3 directionalLightIntensity = vec3(0.8f, 0.8f, 0.8f);
    glUniform3fv(gDirectionalLightIntensityLoc, 1, &directionalLightIntensity[0]);

    //Setup the specular Light
    vec3 specularLightIntensity = vec3(0.5f, 0.5f, 0.5f);
    glUniform3fv(gSpecularLightIntensityLoc, 1, &specularLightIntensity[0]);
}

谁能看到我可能做错了什么,我可能有一些计算错误,我只是没有看到。环境/漫射照明都正常工作。这张照片说明了当前发生的情况,左侧为环境,中间为漫反射,右侧为强度设置为 30 的镜面反射。

在此处输入图像描述

回答

我忘了把这个值传递给主:

gKsStrengthLoc = glGetUniformLocation(shaderProgram, "gKsStrength");
    //assert(gDirectionalLightDirectionLoc != 0xFFFFFFFF);

使用所选答案现在一切正常

4

1 回答 1

3

你的价值gKsStrength看起来太小了:

glUniform1f(gKsStrengthLoc, 0.5f);

该值控制对象的光泽度,较高的值使其更闪亮。如果您查看着色器中的计算,这是有道理的:

specularFactor = pow(specularFactor, gKsStrength);

指数越大,值下降的越快,这意味着镜面高光变得更窄。

典型值可能类似于 10.0f 表示中等光泽,30.0f 表示非常闪亮,甚至更高的非常闪亮的材料。

使用 0.5f 的值,您将获得非常宽的镜面反射“高光”。您的镜面反射强度值也相当高(0.5),因此高光将覆盖大部分对象,并使大部分颜色饱和。

于 2014-12-02T17:25:03.070 回答