我正在尝试仅使用漫反射颜色绘制球体,但没有显示任何内容。所有的 OpenGL 代码都应该是正确的,因为如果我将“颜色”着色器与“纹理”着色器交换,一切都会很好地显示出来。
这是着色器选择代码。if-branch 用于纹理对象,else-branch 用于纯颜色对象。
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);
}
为此,如果我使用纹理着色器渲染所有内容,我会得到:
...这就是我在使用两个着色器时得到的:
有什么想法可能导致这种情况吗?着色器确实编译没有错误,并且一切都适用于纹理着色器,它只是不输出任何内容的彩色着色器。