0

我正在尝试仅使用漫反射颜色绘制球体,但没有显示任何内容。所有的 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);
}

为此,如果我使用纹理着色器渲染所有内容,我会得到:

纹理着色器

...这就是我在使用两个着色器时得到的:

两个着色器

有什么想法可能导致这种情况吗?着色器确实编译没有错误,并且一切都适用于纹理着色器,它只是不输出任何内容的彩色着色器。

4

2 回答 2

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);
}
于 2013-05-26T10:34:16.500 回答
1

我解决了这个问题。在材料选择代码中,我遇到了复制粘贴错误。我将视图和投影矩阵设置为错误的着色器“texturedShader”,而我应该将它们设置为“coloredShader”。复制粘贴,甚至一次都没有,孩子们!

于 2013-05-26T11:17:14.960 回答