2014-12-02 103 views
0

我的鏡面照明有一些問題,我有環境和漫反射,但我現在正在尋找鏡面反射來嘗試製作漂亮的模型。OpenGL中的高光照明問題

我在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]); 
} 

任何人都可以看到我可能做錯了什麼,我可能有一些calculatiuons錯了,我只是沒有看到它。環境/漫射照明工作正常。這張照片展示了當前正在發生的事情,左側的環境,中間的漫反射以及右側強度設置爲30的鏡面反射。

enter image description here

回答

我忘了這個值傳遞到主:

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

一切正常,現在使用的答案選擇

+1

還有一點評論 - 通常,鏡面反射光不會乘以物體的顏色,而是會隨着光線顏色與鏡面反射強度的乘積而增加到最終顏色上。因此,對於白光,鏡面高光應該是白色的,而不是綠色的。 – vesan 2014-12-02 22:20:01

回答

3

您的gKsStrength值看起來方式太小:

glUniform1f(gKsStrengthLoc, 0.5f); 

該值控制對象的閃亮程度,更高的值使其更加閃亮。這是有道理的,如果你看一下在着色器計算:

specularFactor = pow(specularFactor, gKsStrength); 

較大的指數,更快的價值脫落,這意味着鏡面高光變得更窄。

典型值可能是10.0f,比如中等閃亮,30.0f比較閃亮,而對於非常閃亮的材料,其值可能更高。

隨着您的值爲0.5f,您會得到非常寬的高光「高光」。您的鏡面反射強度值也相當高(0.5),因此高光將覆蓋大部分物體,並使大部分的顏色飽和。

+0

感謝您的參與,我玩過這個價值,它似乎讓對象更有活力而不是閃亮,這是正確的效果嗎?我已經添加了一張照片來演示我的照明 – user2757842 2014-12-02 17:33:01

+0

我的錯,我忘了參考main中的gksStrengthLoc,您是正確的,先生,謝謝,我已經更新了我的錯誤 – user2757842 2014-12-02 17:52:45