简单的彩色GLSL着色器不输出任何内容 [英] Simple color GLSL shader doesn't output anything

查看:86
本文介绍了简单的彩色GLSL着色器不输出任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制仅具有漫反射颜色的球体,但没有任何显示.所有的OpenGL代码都应该正确,因为如果我将颜色"着色器替换为纹理化"着色器,一切都会很好地显示.

I'm trying to draw a sphere with just a diffuse color, but nothing shows up. All the OpenGL code should be correct, because if I swap the "color" shader with "textured" shader, everything shows up nicely.

这是着色器选择代码. if分支用于带纹理的对象,else分支用于仅彩色的对象.

This is the shader selection code. The if-branch is for the textured objects, and the else-branch is for color-only objects.

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等的原因):

So the first branch works, but the second branch outputs nothing. Here's the color-only shaders (most of it is just copy-paste from the textured shader, that's why there's texcoords etc. still there):

// 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);
}

...这是纹理着色器:

... And here's the textured shader:

// 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);
}

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

For the sake of it, if I render everything with the textured shader, I get this:

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

... And this is what I get when using both shaders:

有什么想法会导致这种情况吗?着色器可以正确编译,并且所有内容都可以与纹理着色器一起使用,只是彩色着色器不输出任何内容.

Any ideas what could be causing this? Shaders do compile without errors, and everything works with the textured shader, it's just the colored shader that doesn't output anything.

推荐答案

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

I solved the problem. In the material selection code, I had a copy-paste error. I set the view and projection matrices to the wrong shader, "texturedShader", whereas I was supposed to set them to the "coloredShader". Copy-paste, not even once, kids!

这篇关于简单的彩色GLSL着色器不输出任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆