nebuch
nebuch

Reputation: 7075

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:

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:

#version 150

layout(location = 0) in vec2 position;

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

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); or outColor = vec4(0.0, 0.0, 0.0, 1.0); in the fragment shader, I always get this output:

OpenGL output

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. 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.

If it helps, I'm running Debian with an NVIDIA graphics card and NVIDIA drivers. glGetString(GL_VERSION) returns "4.2.0 NVIDIA 304.117". I'm using freeglut as a render context.

Upvotes: 1

Views: 198

Answers (2)

karlphillip
karlphillip

Reputation: 93478

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.

Upvotes: 1

nebuch
nebuch

Reputation: 7075

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.

Upvotes: 1

Related Questions