首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenGL中光谱照明的故障

OpenGL中光谱照明的故障
EN

Stack Overflow用户
提问于 2014-12-02 17:03:31
回答 1查看 1.1K关注 0票数 0

我有一些问题,我的镜面照明,我有环境和漫射,但我现在看到镜面,试图做一个漂亮的模型。

我的vertexShader中有以下内容:

代码语言:javascript
复制
#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中,我有一些代码试图让它一起工作,镜面灯肯定是在做一些事情,只是,它没有使模型看起来闪闪发亮,它似乎加强了光线,以至于我看不到任何细节。

我创建了以下变量:

代码语言:javascript
复制
// Lighting uniforms location

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

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

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

代码语言:javascript
复制
// Set the material properties
glUniform1f(gKaLoc, 0.2f); 
glUniform1f(gKdLoc, 0.9f);
glUniform1f(gKsLoc, 0.5f);
glUniform1f(gKsStrengthLoc, 0.5f);

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

代码语言:javascript
复制
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。

回答

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

代码语言:javascript
复制
gKsStrengthLoc = glGetUniformLocation(shaderProgram, "gKsStrength");
    //assert(gDirectionalLightDirectionLoc != 0xFFFFFFFF);

现在,使用所选的答案,一切都正常。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-02 17:25:03

您的gKsStrength值看起来太小了:

代码语言:javascript
复制
glUniform1f(gKsStrengthLoc, 0.5f);

该值控制对象的闪亮程度,更高的值使其更加闪亮。如果您查看着色器中的计算,这是有意义的:

代码语言:javascript
复制
specularFactor = pow(specularFactor, gKsStrength);

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

典型的数值可能是中度发亮的10.0f,相当闪亮的30.0f,非常闪亮的材料更高。

随着你的价值0.5f,你会得到一个非常广泛的镜面“突出”。你的镜面强度值也相当高(0.5),所以高光将覆盖大部分物体,并使大部分物体的颜色饱和。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27254953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档