0
$\begingroup$

I wrote a program to render a simple triangle with colors, the triangle is there but there's no color.

GLfloat vertices[] = {
        0.6f,0.4f , 0.3f,0.5f, 0.1f,0.3f, 0.8f,0.2f, 0.1f,-0.4f, 0.9f,0.3f
    };


    unsigned int program;
    if (!loadShader(program))
        return -1;

    unsigned int VAO;
    glGenVertexArrays(1, &VAO);
    
    unsigned int buffer;
    /* GENERATE THE BUFFER (1 BUUFER) */
    glGenBuffers(1, &buffer);
    glBindVertexArray(VAO);
    /* SELECT THAT BUFFER TO WORK WITH */
    glBindBuffer(GL_ARRAY_BUFFER, buffer);

    //glBufferData(GL_ARRAY_BUFFER, RealPositions.size()*sizeof(float), RealPositions.data(), GL_STATIC_DRAW);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

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

   

    glBindVertexArray(0);
    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        
        glUseProgram(program);

    
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
}

and this the vertex shader:

#version 440


layout (location = 0) in vec3 vertices;
layout (location = 1) in vec3 vertex_color;
out vec3 vs_color;
void main()
{
    gl_Position=vec4(vertices, 1.f);
    vs_color=vertex_color;
}

and this is the fragment shader:

#version 440 core  


in vec4 vs_pos;
in vec3 vs_color;
out vec4 fs_color;


void main()
{
    fs_color=vec4(vs_color, 1.f);
}

what am i doing wrong?

$\endgroup$
2
  • $\begingroup$ The out variable "vs_colour" in your vertex shader does not match any 'in' variable in your fragment shader, i see "vertex_color" as the fragment shaders input, you should match the VS's "out vec3 vs_color;" with a "in vec3 vs_color" in the FS. You will need a different output variable in your fragment shader to output the fragment colour. $\endgroup$
    – PaulHK
    Commented Mar 30, 2021 at 4:08
  • $\begingroup$ sorry, I edited the post, I posted the vertex shader twice $\endgroup$
    – Gamer
    Commented Mar 30, 2021 at 9:10

1 Answer 1

1
$\begingroup$

Your inputs and outputs still don't line up.

#version 440 core  

// Comment out the line below
// in vec4 vs_pos;    <----- No corresponding output in the vertex shader
in vec3 vs_color; 

out vec4 fs_color;


void main()
{
    fs_color=vec4(vs_color, 1.f);
}

The gl_Position output is consumed and does not need a specific input in the fragment shader. Only variable declared as "out" in the vertex shader need a corresponding "in".

The next issue is the vertices need to have colors in them. The way it is setup now all the components are consumed before even a single triangle is drawn.

The vertex buffer needs to be setup: vertex,color, vertex,color, vertex,color

where each vertex and color has 3 floating point values.

GLfloat vertices[] = {

//    vertex           color
-0.5f, -0.5f, 0.0f,  1.0f,0.0f,0.0f
 0.5f, -0.5f, 0.0f,  0.0f,1.0f,0.0f
 0.0f,  0.5f, 0.0f,  0.0f,0.0f,1.0f 
};

Something like the above. Is what you need for the vertices.

$\endgroup$
2
  • $\begingroup$ I comment it but still not working. maybe this information will help: I use a Intel HD Graphics 5500 and I updated it with the latest driver from HP $\endgroup$
    – Gamer
    Commented Mar 30, 2021 at 14:28
  • $\begingroup$ @gamer updated the answer $\endgroup$
    – pmw1234
    Commented Mar 30, 2021 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.