1

I've encountered following problem:

I got this vertex shader:

#version 410 core

in vec3 vertex_position;

out vec3 color;

void main()
{
    color = vec3(0.0, 0.0, 0.0);
    gl_Position = vec4(vertex_position, 1.0);
}

And following fragment shader:

#version 410 core

in vec3 color;

out vec4 frag_color;

void main()
{
    frag_color = vec4(color, 1.0);
}

I want to pass the color vector from the vertex shader to the fragent shader, however, nothing is showing up on the screen. When I set the frag_color within the fragment shader like: frag_color = vec4(0.0, 0.0, 0.0, 1.0); it works. Thus, something with passing the value between the 2 stages must be wrong.

This is my shader code:

void Shader::CreateShaders(std::string filePathVert, std::string filePathFrag)
{
    // setup shaders
    std::string vertex_shader_source   = LoadSourceFile(filePathVert);
    std::string fragment_shader_source = LoadSourceFile(filePathFrag);
    const GLchar* vs_source = vertex_shader_source.c_str();
    const GLchar* fs_source = fragment_shader_source.c_str();

    GLuint vs = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vs, 1, &vs_source, NULL);
    glCompileShader(vs);
    CheckShaderError(vs, GL_COMPILE_STATUS, false, "Error: Shader compilation failed: ");

    GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fs, 1, &fs_source, NULL);
    glCompileShader(fs);
    CheckShaderError(fs, GL_COMPILE_STATUS, false, "Error: Shader compilation failed: ");

    this->shader_program = glCreateProgram();
    glAttachShader(this->shader_program, fs);
    glAttachShader(this->shader_program, vs);
    glLinkProgram(this->shader_program);
    CheckValidationError(this->shader_program);

    glDeleteShader(vs);
    glDeleteShader(fs);
}

void Shader::Bind()
{
    glUseProgram(this->shader_program);
}

void Shader::Destroy()
{
    glDeleteProgram(this->shader_program);
}

static std::string LoadSourceFile(std::string filePath)
{
    std::ifstream ifs(filePath);
    std::string content( (std::istreambuf_iterator<char>(ifs) ),
                        (std::istreambuf_iterator<char>()    ) );

    if(content.empty())
    {
        std::cerr << "source file was empty or not correctly loaded\n";
    }

    return content;
}

And my drawing code:

GLuint vao = 0;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, vbo_positions);
glVertexAttribPointer(glGetAttribLocation(shader.GetProgramID(), "vertex_position"), 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, vbo_colors);
glVertexAttribPointer(glGetAttribLocation(shader.GetProgramID(), "vertex_color"), 3, GL_FLOAT, GL_FALSE, 0, NULL);


while(!glfwWindowShouldClose(display.GetWindowID())) {
    display.UpdateFPSCounter();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    shader.Bind();
    glBindVertexArray(vao);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    update();

    glfwSwapBuffers(display.GetWindowID());
    display.CheckWindowShouldClose();
}

Does anyone see what I'm doing wrong?

Thanks in advance!

1
  • One problem is that you're using 0 as the argument for glEnableVertexAttribArray(). This will only work if the location is 0. To be safe, you should use the actual location, like you do for glVertexAttribPointer(). Commented Aug 26, 2015 at 14:30

2 Answers 2

1

vertex_color doesn't match the variable in your vertex shader.

Sign up to request clarification or add additional context in comments.

5 Comments

Which means he probably left out the code that is causing the error.
Not at all. Look in glVertexAtttribPointer
... yeah? he still hasn't used it in his example.
At first, I wanted to pass the color from code to the vertex shader and from there on to the fragment shader, that's why the vertex_color attrib pointer is still in the shader code. As it didn't work, I tried to figure out what was going wrong and removed the variable in the vertex shader to pass it to the fragment shader directly from there. Also when I comment out the two lines with the color, the triangle doesn't show up
My friend, you either change the variable in your vertex shader (and fragment shader) from color to vertex_color or you change the pointed to variable in your main program from vertex_color to color. It isn't difficult, and I'm convinced the variable mismatch is what's causing your problem.
0

The problem is here:

glEnableVertexAttribArray(0);
glVertexAttribPointer(glGetAttribLocation(shader.GetProgramID(), "vertex_position"),
                      3, GL_FLOAT, GL_FALSE, 0, NULL);

Both of these calls take the attribute location as the first argument. While the location for the position may happen to be 0, this is not guaranteed, and you should pass the same argument to both these calls:

GLuint posLoc = glGetAttribLocation(shader.GetProgramID(), "vertex_position");
glEnableVertexAttribArray(posLoc);
glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.