David Hartman
David Hartman

Reputation: 31

My OpenGL program's shaders don't affect the geometry or color the polygons

I have a vertex and fragment shader that worked fine not that long ago, but I tried to implement skeletal animation and somewhere along the line broke something. I have even gone away from using my custom functions and just set up a simple "Draw a triangle and color it" code block, but it's not working.

Drawing code:

GLfloat vertices[] = {
    0.5f,  0.5f, 0.0f,  // Top Right
    0.5f, -0.5f, 0.0f,  // Bottom Right
    -0.5f, -0.5f, 0.0f,  // Bottom Left
    -0.5f,  0.5f, 0.0f   // Top Left 
};
GLuint indices[] = {  // Note that we start from 0!
    0, 1, 3,  // First Triangle
    1, 2, 3   // Second Triangle
};
GLuint VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind

glBindVertexArray(0); 

while (!win.windowShouldClose) {

    win.clearScreen(glm::vec4(1.0f,1.0f,1.0f,1.0f));
    newShader.Use();
    GLint mvpLoc = glGetUniformLocation(newShader.Program, "MVP");
    glUniformMatrix4fv(mvpLoc, 1, GL_FALSE, glm::value_ptr(camera.proj * camera.view));
    glBindVertexArray(VAO);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);

    win.display();

}

Vertex shader:

#version 430
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoords;

out vec2 TexCoords;
out vec3 ourColor;

uniform mat4 MVP;

void main()
{
    gl_Position =  MVP * vec4(position,1.0f);
    TexCoords = texCoords;
    ourColor = vec3(1.0f, 0.0f, 1.0f);
}

Fragment shader:

#version 430

in vec2 TexCoords;
in vec3 ourColor;
out vec4 color;
uniform sampler2D texture_diffuse1;
void main() {
  color = vec4(ourColor,1.0f);
}

I have spent days working on this and I'm no closer to figuring out the solution. I believed that it was a problem with the data I was sending to the shaders, but even making the output of the fragment a constant as shown, the triangles are still black and not affected by the vertex shader at all, as if they were going straight from NDC to screen space.

Upvotes: 2

Views: 240

Answers (1)

licp
licp

Reputation: 66

Because of you did not show the camera attribute, I doubt there is any problem with your projection and view matrix (i.e., the MVP matrix may transform the object out of the screen). And I just have tested your program with glfw library at my computer, and the result of my test shows your code is OK. That is to say, there is no problem with "I believed that it was a problem with the data I was sending to the shaders"

enter image description here

Try to remove vertex shader's MVP matrix:

#version 430
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoords;

out vec2 TexCoords;
out vec3 ourColor;

uniform mat4 MVP;

void main()
{
    // Remove MVP matrix
    gl_Position = vec4(position,1.0f);
    TexCoords   = texCoords;
    ourColor    = vec3(1.0f, 0.0f, 1.0f);
}

Upvotes: 2

Related Questions