Question

45I am trying to understand OpenGL projections on a single point. I am using QGLWidget for rendering context and QMatrix4x4 for projection matrix. Here is the draw function

    attribute vec4 vPosition;      
        uniform mat4 projection;  
        uniform mat4 modelView; 
        void main()
        {
          gl_Position = projection* vPosition;
        }       

        void OpenGLView::Draw()
        {
           glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glUseProgram(programObject);
    glViewport(0, 0, width(), height());

    qreal aspect = (qreal)800 / ((qreal)600);
    const qreal zNear = 3.0f, zFar = 7.0f, fov = 45.0f;

    QMatrix4x4 projection;
    projection.setToIdentity();
    projection.ortho(-1.0f,1.0f,-1.0f,1.0f,-20.0f,20.0f);
   // projection.frustum(-1.0f,1.0f,-1.0f,1.0f,-20.0f,20.0f);
   // projection.perspective(fov,aspect,zNear, zFar);

   position.setToIdentity();
   position.translate(0.0f, 0.0f, -5.0f);
   position.rotate(0,0,0, 0);

    QMatrix4x4 mvpMatrix =   projection * position;

    for (int r=0; r<4; r++)
        for (int c=0; c<4; c++)
            tempMat[r][c] = mvpMatrix.constData()[ r*4 + c ];

    glUniformMatrix4fv(projection, 1, GL_FALSE, (float*)&tempMat[0][0]);

    //Draw point at 0,0
    GLfloat f_RefPoint[2];
    glUniform4f(color,1, 0,1,1);
    glPointSize(15);
    f_RefPoint[0] = 0;
    f_RefPoint[1] = 0;
    glEnableVertexAttribArray(vertexLoc);
    glVertexAttribPointer(vertexLoc, 2, GL_FLOAT, 0, 0, f_RefPoint);
    glDrawArrays (GL_POINTS, 0, 1);            
        }

Observations:

1) projection.ortho: the point rendered on the window and translating the point with different z-axis value has no effect

2) projection.frustum: the point is drawn on the windown only the point is translated as translate(0.0f, 0.0f, -20.0f)

3) projection.perspective: the point is never rendered on the screen.

Could someone help me understand this behaviour?

Was it helpful?

Solution

  1. The ortho projection works this way. I suggest you search for some images or some videos about the differences between different projections.
  2. I don't know how you see a point translation in Z coordinate but if you would have a square it would become smaller by translating it further away (with ortho it would stay the same). There is an issue here as you use -20.0f for zNear while this value should be positive. The values inserted into this method should in most cases be generated with field of view, aspect ratio... Anyway you will not be able to see anything closer then zNear and anything further then zFar.
  3. This is the same as frustum but already takes parameters as field of view, aspect ratio. The reason you do not see anything is your zNear is at 3.0f and the point is .0f length away. By translating the point you will be able to see it but try translating it by anything from 3.0f to 7.0f (3.0f is your zNear and 7.0f is your zFar). Alternatives are increasing zFar or translating the projection matrix backwards. Or mostly in your case I suggest adding some "look at" system on the projection matrix as it will give you some easy-to-use tools to manipulate your "camera", in most cases you can set a point you are looking from, a point you are looking at and up vector.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top