1
votes

So, I'm a beginner learning graphics programmer. I'm working on a program for camera movement. I think there's something wrong with the vertex shader. The program runs with no errors but the screen is completely blank. Here is the vertex shader I'm using:

#version 330

in vec4 vPosition;
out vec4 vColor;

uniform mat4 model_view;
uniform mat4 projection;


void main()
{
    vec4 pos = projection * model_view * vPosition / vPosition.w;
    gl_Position = pos;
    vColor = vPosition;
}

If I switch the shader back to basic version:

#version 330

in vec4 vPosition;
out vec4 vColor;

void
main()
{
    gl_Position = vPosition;
    vColor = vPosition;
}

The program runs and renders a triangle successfully. So, I'm pretty sure the error is with the shader.

The shader is called in the initialize function:

  void initialize(void)
{
    glClearColor(1.0, 1.0, 1.0, 1.0);   // white background
    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    // Create and initialize a buffer object
    GLuint buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);



    // Load shaders and use the resulting shader program
    GLuint program = InitShader("res/shaders/vshader21.glsl", "res/shaders/fshader21.glsl");

    model_view = glGetUniformLocation(program, "model_view");
    projection = glGetUniformLocation(program, "projection");

    glUseProgram(program);

    // Initialize the vertex position attribute from the vertex shader
    GLuint loc = glGetAttribLocation(program, "vPosition");
    glEnableVertexAttribArray(loc);
    glVertexAttribPointer(loc, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));

}

the 'points' in glBufferData is as follows:

const int WIDTH = 500, HEIGHT = 500;
/* Positions */
vec4 points[] = {
vec4(0.5,0.5, 1, 1),
vec4(-0.5,0.5, 1, 1),
vec4(0.5,-0.5, 1, 1) ,
vec4(-0.5,-0.5, 1, 1)
};

model_view and projection are of GLuint type in main application and global.

I set the uniform variables (position, model_view) in the display functions.

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);     // clear the window
    glPointSize(20.0);

    // Projection transformation parameters
    GLfloat left = -1.0, right = 1.0;
    GLfloat bottom = -1.0, top = 1.0;
    GLfloat zNear = 0, zFar = 3.0;
    mat4 p = Ortho(left, right, bottom, top, zNear, zFar);
    glUniformMatrix4fv(projection, 1, GL_TRUE, p);

    vec4 eye(0.0, 0.0, -1.0, 1.0);
    vec4 at(0.0, 0.0, 0.0, 1.0);
    vec4 up(0.0, 1.0, 0.0, 0.0);
    mat4 mv = LookAt(eye, at, up);
    glUniformMatrix4fv(model_view, 1, GL_TRUE, mv);

    glDrawArrays(GL_TRIANGLES, 0, 3); // draw the points


    glFlush();
}

What could possibly be going wrong?

1
How do you set the matrix uniforms (model_view, projection)? Can you please show that part of the code?Rabbid76
I declared them right above the initialize function: GLuint model_view; GLuint projection; void initialize(void) { ....Udit Singh
No. Where do you set the values to the uniform variables?Rabbid76
Apologies for my lack of understanding, I just understood your question. The uniform variables are being set in display func (I've edited my post to show that function). I've made the changes you've mentioned. Still end up with a blank screen somehow. Apologies again.Udit Singh
Do not transpose the matrices GL_TRUE -> GL_FALSE. See the answer.Rabbid76

1 Answers

1
votes

The explicit division by the .w component is superfluous.

vec4 pos = projection * model_view * vPosition / vPosition.w;

vec4 pos = projection * model_view * vPosition;

Note, the Perspective divide is automatically performed after clipping.

Since the vector is multiplied to the uniforms form the right, you do not have to transpose the matrices:

glUniformMatrix4fv(projection, 1, GL_TRUE, p);

glUniformMatrix4fv(projection, 1, GL_FALSE, p);

glUniformMatrix4fv(model_view, 1, GL_TRUE, mv);

glUniformMatrix4fv(model_view, 1, GL_FALSE, mv);

See GLSL Programming/Vector and Matrix Operations