2014-11-23 29 views
0

我正在做一個關於OpenGL的聚光燈項目。我想我正確地編寫了代碼,但我無法在輸出中看到一個圓點。您的幫助將不勝感激。在這裏我正在寫我的片段着色器文件和燈光定義。SpotLight未見 - OpenGL

fragmentShader.fs

#version 330 

in vec3 N; // interpolated normal for the pixel 
in vec3 v; // interpolated position for the pixel 

// Uniform block for the light source properties 
layout (std140) uniform LightSourceProp { 
    // Light source position in eye space (i.e. eye is at (0, 0, 0)) 
    uniform vec4 lightSourcePosition; 

    uniform vec4 diffuseLightIntensity; 
    uniform vec4 specularLightIntensity; 
    uniform vec4 ambientLightIntensity; 

    // for calculating the light attenuation 
    uniform float constantAttenuation; 
    uniform float linearAttenuation; 
    uniform float quadraticAttenuation; 

    // Spotlight direction 
    uniform vec3 spotDirection; 
    uniform float cutOffExponent; 
    // Spotlight cutoff angle 
    uniform float spotCutoff; 
}; 

// Uniform block for surface material properties 
layout (std140) uniform materialProp { 
    uniform vec4 Kambient; 
    uniform vec4 Kdiffuse; 
    uniform vec4 Kspecular; 
    uniform float shininess; 
}; 

out vec4 color; 

// This fragment shader is an example of per-pixel lighting. 
void main() { 

    // Now calculate the parameters for the lighting equation: 
    // color = Ka * Lag + (Ka * La) + attenuation * ((Kd * (N dot L) * Ld) + (Ks * ((N dot HV)^shininess) * Ls)) 
    // Ka, Kd, Ks: surface material properties 
    // Lag: global ambient light (not used in this example) 
    // La, Ld, Ls: ambient, diffuse, and specular components of the light source 
    // N: normal 
    // L: light vector 
    // HV: half vector 
    // shininess 
    // attenuation: light intensity attenuation over distance and spotlight angle 

    vec3 lightVector; 
    float attenuation = 1.0; 
    float se; 


    // point light source 
    lightVector = normalize(lightSourcePosition.xyz - v); 

    //Calculate Spoteffect 
    // calculate attenuation  


float angle = dot(normalize(spotDirection), 
       normalize(lightVector)); 
    angle = max(angle,0); 

    // Test whether vertex is located in the cone 
    if(acos (angle) > radians(5)) 
{ 

    float distance = length(lightSourcePosition.xyz - v); 
    angle = pow(angle,2.0); 

attenuation = angle/(constantAttenuation + (linearAttenuation * distance) 
     +(quadraticAttenuation * distance * distance)); 

      //calculate Diffuse Color 
    float NdotL = max(dot(N,lightVector), 0.0); 

    vec4 diffuseColor = Kdiffuse * diffuseLightIntensity * NdotL; 

    // calculate Specular color. Here we use the original Phong illumination model. 
    vec3 E = normalize(-v); // Eye vector. We are in Eye Coordinates, so EyePos is (0,0,0) 

    vec3 R = normalize(-reflect(lightVector,N)); // light reflection vector 

    float RdotE = max(dot(R,E),0.0); 

    vec4 specularColor = Kspecular * specularLightIntensity * pow(RdotE,shininess); 

    // ambient color 
    vec4 ambientColor = Kambient * ambientLightIntensity; 

    color = ambientColor + attenuation * (diffuseColor + specularColor); 

    } 
    else 
     color = vec4(1,1,0,1); // lit (yellow) 


} 

在main.cpp中的光定義

struct SurfaceMaterialProp { 
    float Kambient[4]; //ambient component 
    float Kdiffuse[4]; //diffuse component 
    float Kspecular[4]; // Surface material property: specular component 
    float shininess; 
}; 

SurfaceMaterialProp surfaceMaterial1 = { 
    {1.0f, 1.0f, 1.0f, 1.0f}, // Kambient: ambient coefficient 
    {1.0f, 0.8f, 0.72f, 1.0f}, // Kdiffuse: diffuse coefficient 
    {1.0f, 1.0f, 1.0f, 1.0f}, // Kspecular: specular coefficient 
    5.0f // Shininess 
}; 

struct LightSourceProp { 
    float lightSourcePosition[4]; 
    float diffuseLightIntensity[4]; 
    float specularLightIntensity[4]; 
    float ambientLightIntensity[4]; 
    float constantAttenuation; 
    float linearAttenuation; 
    float quadraticAttenuation; 
    float spotlightDirection[4]; 
    float spotlightCutoffAngle; 
    float cutOffExponent; 
}; 

LightSourceProp lightSource1 = { 
    { 0.0,400.0,0.0, 1.0 }, // light source position 
    {1.0f, 0.0f, 0.0f, 1.0f}, // diffuse light intensity 
    {1.0f, 0.0f, 0.0f, 1.0f}, // specular light intensity 
    {1.0f, 0.2f, 0.0f, 1.0f}, // ambient light intensity 
    1.0f, 0.5, 0.1f, // constant, linear, and quadratic attenuation factors 
    {0.0,50.0,0.0}, // spotlight direction 
    {5.0f}, // spotlight cutoff angle (in radian) 
    {2.0f} // spotexponent 
    }; 
+0

你的代碼有什麼問題?你收到一個錯誤? – 2014-11-23 06:29:06

+0

沒有。我無法正確看到現貨 – user3791306 2014-11-23 09:33:59

回答

2

一對情侶在C++代碼LightSourceProp結構成員的順序是從一個在不同統一塊。

最後兩位均勻塊的成員:

uniform float cutOffExponent; 
    uniform float spotCutoff; 
}; 

C++結構的最後兩個成員:

float spotlightCutoffAngle; 
    float cutOffExponent; 
}; 

這兩個值被交換。

此外,截止角度看上去非常大:

{5.0f}, // spotlight cutoff angle (in radian) 

這就是286度,這是沒有太大的聚光燈的角度。對於實際的聚光燈,您可能需要更小的東西,如0.1f0.2f

,可能是給你帶來意想不到的結果的另一個方面是,你有很多環境強度:

{1.0f, 1.0f, 1.0f, 1.0f}, // Kambient: ambient coefficient 
... 
{1.0f, 0.2f, 0.0f, 1.0f}, // ambient light intensity 

取決於你如何在着色器代碼中使用這些值,很可能是你的顏色將飽和單單從環境強度來看,你不會從光源和材料的其他方面得到任何明顯的貢獻。由於環境強度不變,這將導致整個幾何體的顏色完全平坦。

+0

感謝您的迴應,我做了您所說的。我的輸出是相同的。 Plse見我的輸出https://imageshack.us/i/hlHRDikrp – user3791306 2014-11-23 14:11:04

+0

看起來你可能有太多的環境強度。我在答案中添加了一些文字。 – 2014-11-23 15:19:54

+0

謝謝,但也不以這種方式工作 – user3791306 2014-11-23 18:56:38