2013-05-26 341 views
0

我想繪製一個只有漫反射顏色的球體,但沒有出現。所有的OpenGL代碼都應該是正確的,因爲如果我將「顏色」着色器與「紋理」着色器交換,所有內容都會很好地顯示出來。簡單的顏色GLSL着色器不輸出任何東西

這是着色器選擇代碼。 if分支用於紋理對象,else分支僅用於純色對象。

GL.Disable(EnableCap.Blend); 

if (phong.diffuseTexture != 0) 
{ 
    texturedShader.Enable(); 
    GL.UniformMatrix4(texturedShader.GetUniformLocation("view"), false, ref camView); 
    GL.UniformMatrix4(texturedShader.GetUniformLocation("projection"), false, ref Camera.main.projection); 

    GL.ActiveTexture(TextureUnit.Texture0); 
    GL.BindTexture(TextureTarget.Texture2D, phong.diffuseTexture); 
    int colorMapLoc = Shader.currentShader.GetUniformLocation("colorMap"); 
    if (colorMapLoc > -1) GL.Uniform1(colorMapLoc, 0); 
} 
else 
{ 
    coloredShader.Enable(); 
    GL.UniformMatrix4(texturedShader.GetUniformLocation("view"), false, ref camView); 
    GL.UniformMatrix4(texturedShader.GetUniformLocation("projection"), false, ref Camera.main.projection); 
} 

// Continues with the VBO rendering code... 

所以第一個分支工作,但第二個分支不輸出任何東西。這是唯一的顏色着色器(大部分是剛剛從紋理着色器複製粘貼,這就是爲什麼有texcoords等仍然存在):

// VERTEX SHADER 
#version 330 

uniform mat4 projection; 
uniform mat4 view; 
uniform mat4 world; 

uniform vec3 diffuseColor; 

layout(location = 0) in vec3 in_position; 
layout(location = 1) in vec3 in_normal; 
layout(location = 2) in vec2 in_texcoord; 
layout(location = 3) in vec3 in_tangent; 
layout(location = 4) in vec3 in_binormal; 

out mat3 tangentToWorld; 
out vec2 texcoord; 
out vec3 normal; 
out vec3 color; 

void main(void)   
{ 
    gl_Position = projection * view * world * vec4(in_position, 1.0); 

    color = diffuseColor; 

    normal = (world * vec4(normalize(in_normal), 0.0)).xyz; 

    tangentToWorld[0] = (world * vec4(in_tangent, 0.0)).xyz; 
    tangentToWorld[1] = (world * vec4(in_binormal, 0.0)).xyz; 
    tangentToWorld[2] = (world * vec4(in_normal, 0.0)).xyz; 

    texcoord = in_texcoord; 
} 


// FRAGMENT SHADER 
#version 330 

in vec2 texcoord; 
in mat3 tangentToWorld; 
in vec3 normal; 
in vec3 color; 

layout(location = 0) out vec4 out_diffuse; 
layout(location = 1) out vec4 out_normal; 

void main(void)         
{ 
    out_diffuse = vec3(1, 0, 1, 1); 

    vec3 normal = vec3(0.5, 0.5, 1.0); 

    normal = 2.0 * normal - 1.0; 

    normal = tangentToWorld * normal; 
    normal = (normal + 1.0)/2; 
    normal = normalize(normal); 

    out_normal = vec4(normal.x, normal.y, normal.z, 1.0); 
} 

......而這裏的紋理着色器:

// VERTEX SHADER 
#version 330 

uniform mat4 projection; 
uniform mat4 view; 
uniform mat4 world; 

layout(location = 0) in vec3 in_position; 
layout(location = 1) in vec3 in_normal; 
layout(location = 2) in vec2 in_texcoord; 
layout(location = 3) in vec3 in_tangent; 
layout(location = 4) in vec3 in_binormal; 

out mat3 tangentToWorld; 
out vec2 texcoord; 
out vec3 normal; 

void main(void)   
{ 
    gl_Position = projection * view * world * vec4(in_position, 1.0); 

    normal = (world * vec4(normalize(in_normal), 0.0)).xyz; 

    tangentToWorld[0] = (world * vec4(in_tangent, 0.0)).xyz; 
    tangentToWorld[1] = (world * vec4(in_binormal, 0.0)).xyz; 
    tangentToWorld[2] = (world * vec4(in_normal, 0.0)).xyz; 

    texcoord = in_texcoord; 
} 


// FRAGMENT SHADER 
#version 330 

in vec2 texcoord; 
in mat3 tangentToWorld; 
in vec3 normal; 

layout(location = 0) out vec3 out_diffuse; 
layout(location = 1) out vec4 out_normal; 

uniform sampler2D colorMap;    
uniform sampler2D normalMap; 

void main(void)         
{ 
    out_diffuse = texture(colorMap, texcoord).xyz; 

    vec3 normal = texture(normalMap, texcoord).xyz; 

    normal = 2.0 * normal - 1.0; 

    normal = tangentToWorld * normal; 
    normal = (normal + 1.0)/2; 
    normal = normalize(normal); 

    out_normal = vec4(normal.x, normal.y, normal.z, texture(normalMap, texcoord).a); 
} 

對於它的緣故,如果我與紋理着色器渲染的一切,我得到這個:

Textured shader

...這是我使用這兩種着色器時,得到:

Both shaders

任何想法可能是造成這個?着色器的編譯沒有任何錯誤,並且一切與紋理着色器一起工作,它只是不着色的着色器。

+0

某些着色器代碼片段似乎丟失。我在那裏只看到一個着色源,哪一個是你的「紋理」,哪一個是你的「顏色」着色器? – datenwolf

回答

1

代替我解決了這個問題。在材料選擇代碼中,我有一個複製粘貼錯誤。我將視圖和投影矩陣設置爲錯誤着色器「texturedShader」,而我應該將它們設置爲「coloredShader」。複製粘貼,甚至一次,孩子們!

1

在沒有看到其他着色器的代碼,我最好的猜測是,你必須啓用

  • 混合一個glBlendFunc(GL_SRC_ALPHA, …)

  • 因爲你的顏色着色器使用VEC3對於顏色輸出,您隱式地獲得了alpha = 0,這使得不可見。

但這只是一個猜測。爲了檢驗這一假設在你的顏色片段着色器

// FRAGMENT SHADER 
#version 330 

layout(location = 0) out vec4 out_diffuse; 

void main(void)         
{ 
    out_diffuse = vec4(1, 0, 1, 1); 
} 
+0

我禁用了混合,添加四組件版本沒有幫助。我添加了我用於着色和紋理着色器的完整着色器源代碼。 – manabreak

+0

我試圖在我的愛好項目中實現SSAO,但是對於法線和位置我有黑屏。這令我驚訝!我必須在渲染之前禁用GL_BLEND,並重新啓用它。 – mudlee