我想繪製一個只有漫反射顏色的球體,但沒有出現。所有的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);
}
對於它的緣故,如果我與紋理着色器渲染的一切,我得到這個:
...這是我使用這兩種着色器時,得到:
任何想法可能是造成這個?着色器的編譯沒有任何錯誤,並且一切與紋理着色器一起工作,它只是不着色的着色器。
某些着色器代碼片段似乎丟失。我在那裏只看到一個着色源,哪一個是你的「紋理」,哪一個是你的「顏色」着色器? – datenwolf