Search code examples
c++openglmathopengl-3geometry-shader

opengl texture coordinates for full-screen effect


how can i create the full-screen effect with a texture image ? till now i do this :

static void Draw(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity();
    glTranslatef(0.0f,0.0f,-5.0f);
    texture[0] = SOIL_load_OGL_texture // load an image file directly as a new OpenGL texture
    (
        "my_img.jpg",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
         SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
    );

    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBegin(GL_POLYGON);

    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);

    glEnd();
    glutSwapBuffers();
}

this code places my image in the top-left position of the screen, but i would like the full-screen texture effect. So, How can i accomplish that ?


Solution

  • As @BrettHale already suggested in the comment, just set all the matrices to the identity transformation:

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    

    Then get rid of any other transformations you have in place, like the glTranslatef() call you currently have in the code. Use 0.0f instead of 1.0f for the last coordinate of your vertices, and you should be good to go.