简体   繁体   English

GLSL着色器不影响渲染结果

[英]GLSL Shaders do not effect render result

I'm following this tutorial, just starting drawing polygons. 我正在学习教程,只是开始绘制多边形。 I have this problem though: the content of my shader sources doesn't matter. 但是我有一个问题:着色器源的内容无关紧要。 Here's relevant code: 以下是相关代码:

main.c : main.c

float vertices[] = {
     0.0,    0.5,
     0.5, -0.5,
    -0.5, -0.5
};

GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
char *vertsource = loadsource("vert.glsl");
glShaderSource(vertexShader, 1, &vertsource, NULL);
free(vertsource);
glCompileShader(vertexShader);

GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
char *fragsource = loadsource("frag.glsl");
glShaderSource(fragmentShader, 1, &fragsource, NULL);
free(fragsource);
glCompileShader(fragmentShader);

GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
//Only necesary if multiple outputs:
//glBindFragDataLocation(shaderProgram, 0, "outColor");
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);

//0 is the position input for vert shader
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);

vert.glsl : vert.glsl

#version 150

layout(location = 0) in vec2 position;

void main()
{
    gl_Position = vec4(position, 0.0, 1.0);
}

frag.glsl : frag.glsl

#version 150

out vec4 outColor;

void main()
{
        outColor = vec4(1.0, 1.0, 1.0, 1.0);
        //Alternative line:
        //outColor = vec4(0.0, 0.0, 0.0, 1.0);
}

As you can see, the code is very similar to the tutorial. 如您所见,该代码与本教程非常相似。 However, it doesn't matter whether I use the line outColor = vec4(1.0, 1.0, 1.0, 1.0); 但是,是否使用行outColor = vec4(1.0, 1.0, 1.0, 1.0);并不重要outColor = vec4(1.0, 1.0, 1.0, 1.0); or outColor = vec4(0.0, 0.0, 0.0, 1.0); outColor = vec4(0.0, 0.0, 0.0, 1.0); in the fragment shader, I always get this output: 在片段着色器中,我总是得到以下输出:

OpenGL输出

As long as the shader sources compile, I can execute and get this result. 只要着色器源能够编译,我就可以执行并获得此结果。 I don't even have to return anything, it'll still render the same. 我什至不必返回任何东西,它仍然会呈现相同的效果。 If I add or subtract the x or y value of position in the vert shader, I get the same result. 如果我在vert着色器中添加或减去position的x或y值,我将得到相同的结果。 If I make a syntax error, the program crashes as soon as opengl tries to compile, but it doesn't seem to matter what it compiles so long as it does. 如果我出现语法错误,则opengl尝试编译时该程序就会崩溃,但是只要它能够编译,似乎无关紧要。

If it helps, I'm running Debian with an NVIDIA graphics card and NVIDIA drivers. 如果有帮助,我将使用NVIDIA图形卡和NVIDIA驱动程序运行Debian。 glGetString(GL_VERSION) returns "4.2.0 NVIDIA 304.117". glGetString(GL_VERSION)返回“ 4.2.0 NVIDIA 304.117”。 I'm using freeglut as a render context. 我正在使用freeglut作为渲染上下文。

Ok, turns out there was an error in shader compilation. 好的,事实证明着色器编译中存在错误。 I was using (location = #) without including the line #extension GL_ARB_explicit_attrib_location : enable in my shader. 我正在使用(location = #)但未在着色器中包含#extension GL_ARB_explicit_attrib_location : enable#extension GL_ARB_explicit_attrib_location : enable

A comment I made earlier pointed out the problem: 我之前发表的评论指出了问题所在:

I suspect you have an error while compiling or linking the shaders. 我怀疑您在编译或链接着色器时出错。 You can call glGetShaderiv() to assist you on that. 您可以调用glGetShaderiv()来帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM