OpenGL 3.3 different colours with fragment shader
Asked Answered
P

1

6

I'm trying to colour 3 circles but only 3 white circles are appearing. n is 3 in this example. Each vertice has 5 points, 2 for position and 3 for color

Here is where I think a problem may lie:

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glVertexAttribPointer(
        0,                  
        2,                 
        GL_FLOAT,           
        GL_FALSE,          
        5*sizeof(float), 
        (void*)0            
    );

    glEnableVertexAttribArray(1);
    glVertexAttribPointer(
        1, 
        3,
        GL_FLOAT,
        GL_FALSE, 
        5*sizeof(float), 
        (void*)(2*sizeof(float))
    );

    glDrawElements(GL_TRIANGLES, 20 * 3 * n, GL_UNSIGNED_INT, 0);

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);

My shaders:

#version 330 core

in vec3 Color;

out vec4 outColor;

void main()
{
    outColor = vec4(Color, 1.0);
}


#version 330 core

layout(location = 0) in vec2 position;

layout(location = 1) in vec3 color
out vec3 Color

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

Thanks for taking a look Andy

EDIT:

layout(location = 1) in vec3 color
    out vec3 Color

layout(location = 1) in vec3 color;
    out vec3 Color;
Pompon answered 2/11, 2013 at 16:38 Comment(8)
Actually, the parts you have posted so far do look OK to me. Can you post the code where you create your vertex arrays and create/fill the buffers?Paco
I've forgot to semicolon the color and Color vecs in my fragment shader... Been looking at this for hours now. Thanks for taking a look thoughPompon
Oh. I missed that, too...Paco
The shader info log would have warned you about this parse error, if you were not already aware of this feature, see: glGetShaderInfoLog (...)Husain
agree, as derhass pointed out, your code looks okay, the problem might be in the way you are creating the buffer, may be it is not filled with data correctly. please post that code.Retardation
It was a parse error with the Fragment Shader, as mentioned above. Anyway to mark this as solved? Thanks for looking at my code thoughPompon
Perhaps someone like @Andon M. Coleman could write that up in an answer, so this can be marked as closed?Cupola
@AndrewSeymour: The standard way is just for you to write the proper answer and accept itDikmen
H
2

(Posting the solutions from the comments to mark this question answered.)

You are missing semicolons at the end of these two lines:

layout(location = 1) in vec3 color
out vec3 Color

In the future, use glGetShader with GL_COMPILE_STATUS after compiling your shader to check if compilation succeeded, and glGetShaderInfoLog to retrieve the exact errors and warnings. See Shader Compilation for further detail and code samples.

Hummock answered 24/5, 2015 at 17:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.