0
votes

I have just learnt about Perspective projection, and am finding applying it in openGl a little bit confusing.

Consider a simple Square .
Before using perspective projection, I could just define the coordinates of its vertices in [-1,1] space and for an input of {0.0f,0.0f,0.0f,1.0f,1.0f,1.0f,1.0f,0.0f} , the square would occupy the 1st quadrant of the window.
Consider the following parts from my code:

Setting up the matrix:

    glm::mat4 mvp(1.0);
    mvp*=glm::perspective(45.0f,1.0f,0.01f,100.0f);

Uniforms:

    loc = glGetUniformLocation(program.getHandle(),"mvp");
    glUniformMatrix4fv(loc,1,GL_FALSE,glm::value_ptr(mvp));

Vertex Shader:

    #version 330
    in vec2 pos;
    uniform mat4 mvp;
    void main()
    {
        gl_Position = mvp*vec4(pos,0.0f,1.0f);
    }

But, the result is a blank screen.
What extra transformations do I need to apply here ?
Also, in which coordinates should the vertex data be ?

Here is the complete code:

#include "program.h"
#include <GL/glew.h>
#include <SFML/Window.hpp>
#include <stdexcept>
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/core/type.hpp>

void GlewInit()
{
        GLenum err = glewInit();
        if (GLEW_OK != err)
        {
                std::cerr<<"Error: "<<glewGetErrorString(err)<<std::endl;
                throw(std::runtime_error("GLEW_INIT Failure."));
        }
}
int main()
{
    sf::Window win(sf::VideoMode(400,400),"Manasij");
    GlewInit();
    float vertexdata[]={0.0f,0.0f,0.0f,1.0f,1.0f,1.0f,1.0f,0.0f};
    GLubyte indexdata[]={0,1,2,3};
    float colordata[]={0.3f,0.0f,0.5f,1.0f};
    mm::Program program
    (
        {
            mm::Shader(GL_VERTEX_SHADER,"vshader.vert"),
            mm::Shader(GL_FRAGMENT_SHADER,"fshader.frag")
        }
    );

    glm::mat4 mvp(1.0);

    mvp*=glm::perspective(45.0f,1.0f,0.01f,100.0f);


    GLuint vao,vbo,ibo;
    glGenVertexArrays(1,&vao);
    glBindVertexArray(vao);

    glGenBuffers(1,&vbo);
    glBindBuffer(GL_ARRAY_BUFFER,vbo);
    glBufferData(GL_ARRAY_BUFFER,8*sizeof(GLfloat),vertexdata,GL_STATIC_DRAW);

    glGenBuffers(1,&ibo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,4*sizeof(GLubyte),indexdata,GL_STATIC_DRAW);

    glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,0);
    glEnableVertexAttribArray(0);
    glBindAttribLocation(program.getHandle(),0,"pos");


    glUseProgram(program.getHandle());
    auto loc = glGetUniformLocation(program.getHandle(),"col");
    glUniform4fv(loc,1,colordata);
    loc = glGetUniformLocation(program.getHandle(),"mvp");
    glUniformMatrix4fv(loc,1,GL_FALSE,glm::value_ptr(mvp));
    glUseProgram(0);


    glClearColor(1.0f,1.0f,1.0f,1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    while(win.isOpen())
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glUseProgram(program.getHandle());
//      glDrawArrays(GL_QUADS,0,4);
        glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,nullptr);
        glUseProgram(0);

        win.display();
        sf::Event eve;
        while(win.pollEvent(eve))
            if(eve.type==sf::Event::Closed)
                win.close();
    }
    return 0;
}
2
You can change glm::mat4 mvp(1.0); mvp*=glm::perspective(45.0f,1.0f,0.01f,100.0f); into glm::mat4 mvp = glm::perspective(...); I think.Oskar

2 Answers

4
votes

But, the result is a blank screen. What extra transformations do I need to apply here?

You see those last two parameters of glm::perspective call, those define the near and far clip distance. Your square is at Z=0, and without further transformation hence falls out of the viewing volume. Solution: Move the seqare a few units in -Z direction.

Note the following constraints on near and far values:

  • abs(far) > abs(near) > 0

  • sgn(near) = sgn(far)

If sgn(near) = sgn(far) = - you also either must swap the signs of the depth range, or change the depth testing function to GL_LESS.

Since the depth buffer resolution isn't distributed linearily, but in a 1/z-like fashion

  • abs(near) should be as large as possible
  • abs(far) should be as small as possible.
0
votes

Perhaps this website can help you, it explains the setup of the projection matrix in OpenGL:

http://www.songho.ca/opengl/gl_projectionmatrix.html