[英]glsl light doesn't seem to be working
我正在為我的應用程序中的一些基本照明工作,並且無法獲得簡單的工作(到目前為止......)。
這是頂點着色器:
#version 150 core
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
uniform mat4 pvmMatrix;
uniform mat3 normalMatrix;
in vec3 in_Position;
in vec2 in_Texture;
in vec3 in_Normal;
out vec2 textureCoord;
out vec4 pass_Color;
uniform LightSources {
vec4 ambient = vec4(0.5, 0.5, 0.5, 1.0);
vec4 diffuse = vec4(0.5, 0.5, 0.5, 1.0);
vec4 specular = vec4(0.5, 0.5, 0.5, 1.0);
vec4 position = vec4(1.5, 7, 0.5, 1.0);
vec4 direction = vec4(0.0, -1.0, 0.0, 1.0);
} lightSources;
struct Material {
vec4 ambient;
vec4 diffuse;
vec4 specular;
float shininess;
};
Material mymaterial = Material(
vec4(1.0, 0.8, 0.8, 1.0),
vec4(1.0, 0.8, 0.8, 1.0),
vec4(1.0, 0.8, 0.8, 1.0),
0.995
);
void main() {
gl_Position = pvmMatrix * vec4(in_Position, 1.0);
textureCoord = in_Texture;
vec3 normalDirection = normalize(normalMatrix * in_Normal);
vec3 lightDirection = normalize(vec3(lightSources.direction));
vec3 diffuseReflection = vec3(lightSources.diffuse) * vec3(mymaterial.diffuse) * max(0.0, dot(normalDirection, lightDirection));
/*
float bug = 0.0;
bvec3 result = equal( diffuseReflection, vec3(0.0, 0.0, 0.0) );
if(result[0] && result[1] && result[2]) bug = 1.0;
diffuseReflection.x += bug;
*/
pass_Color = vec4(diffuseReflection, 1.0);
}
這是片段着色器:
#version 150 core
uniform sampler2D texture;
in vec4 pass_Color;
in vec2 textureCoord;
void main() {
vec4 out_Color = texture2D(texture, textureCoord);
gl_FragColor = pass_Color;
//gl_FragColor = out_Color;
}
我正在將紋理狼渲染到屏幕上作為測試。 如果我將片段着色器更改為使用out_Color
,我會看到狼正確渲染。 如果我使用pass_Color
,我在屏幕pass_Color
不到任何內容。
當我在片段着色器中使用out_Color
時,這就是屏幕的樣子:
通過在頂點着色器中取消注釋此代碼,我知道diffuseReflection
向量已滿0:
...
/*
float bug = 0.0;
bvec3 result = equal( diffuseReflection, vec3(0.0, 0.0, 0.0) );
if(result[0] && result[1] && result[2]) bug = 1.0;
diffuseReflection.x += bug;
*/
...
這將使diffuseReflection
矢量1.0
的x
分量變為紅色。
有沒有人看到任何明顯我在這里做錯了?
正如評論中所建議的那樣,嘗試逐步調試。 我看到你的着色器可能有多種錯誤。 也許你的normalMatrix沒有正確傳遞? 也許你的in_Normal沒有映射到適當的輸入? 也許當你將lightSources.direction轉換為vec3時,編譯器會做一些時髦的事情嗎? 也許你的着色器根本沒有運行,但你認為它是? 也許你有幾何或曲面細分單元並且沒有正確傳遞?
沒有人真的有機會正確回答這個問題。 至於我,它看起來很好 - 但同樣,上述任何因素都可能發生 - 可能更多。
要調試它,您需要將其分解。 正如評論中所建議的那樣,嘗試渲染法線。 然后,您應該嘗試渲染光線方向。 然后渲染你的n點l項。 然后乘以材質參數,再乘以紋理。 在某個地方,你會找出問題所在。 作為額外的提示,將您的清晰顏色更改為黑色以外的其他顏色,以便任何黑色渲染的對象突出。
值得注意的是,上述建議 - 將其分解 - 適用於調試的所有內容,而不僅僅是着色器。 在我看來,你還沒有這樣做過。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.