Switching from 3D to 2D in OpenGL

1.2k views Asked by At

I'm making a weather simulation in Opengl 4.0 and am trying to create the sky by creating a fullscreen quad in the background. I'm trying to do that by having the vertex shader generate four vertexes and then drawing a triangle strip. Everything compiles just fine and I can see all the other objects I've made before, but the sky is nowhere to be seen. What am I doing wrong?

main.cpp

GLint stage = glGetUniformLocation(myShader.Program, "stage");
//...
glBindVertexArray(FS); //has four coordinates (-1,-1,1) to (1,1,1) in buffer object
glUniform1i(stage, 1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);

vertex shader

uniform int stage;
void main()
{
    if (stage==1)
    {
        gl_Position = vec4(position, 1.0f);
    }
    else
    {
        //...
    }    
}

fragment shader

uniform int stage;
void main()
{
    if (stage==1)
    { //placeholder gray colour so I can see the sky
        color = vec4(0.5f, 0.5f, 0.5f, 1.0f);
    }
    else
    {
        //...
    }
}

I should also mention that I'm a beginner in OpenGL and that it really has to be in OpenGL 4.0 or later.

EDIT: I've figured out where's the problem, but still don't know how to fix it. The square exists, but only displays if I multiply it with the view and projection matrix (but then it doesn't stay glued to the screen and just rotates along with the rest of the scene, which I do not want). Essentially, I somehow need to switch back to 2D or to screen space or however it's called, draw the square, and switch back to 3D so that all the other objects work fine. How?

1

There are 1 answers

1
Gweddry On BEST ANSWER

The issue was with putting a 1 as the z coord – putting 0.999f instead solved the issue.