Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: help with view and projection setup.

  1. #1
    Intern Newbie
    Join Date
    Aug 2012
    Posts
    48

    help with view and projection setup.

    I wrote a pretty long question here: http://stackoverflow.com/questions/3...ransformations

    but I'll also add some of the questions details down below.

    I am making some basic steps towards rendering with opengl. I wrote some shaders as well as a some math functions to do the linear algebra.

    I can get a quad that's 2d tris to render on screen but when it comes to applying any sort of transformations I see nothing.

    Here is the relevant code as to the matrix data that I am sending to the gpu, etc.

    **setup matricies**

    Code :
        mat4identity(&vm);
        vec3f eye    = {0, 0, 0};
        vec3f center = {0, 0, -1};
        vec3f up     = {0, 1, 0};
     
        mat4LookAt(&vm, &eye, &center, &up);
        mat4print("lookat",&vm);
     
        mat4OrthoProjection(&opm, 200, 0, 200, 0, 0.001, 1000.0);
        mat4print("2d projection",&opm);

    **render code**

    Code :
        glUseProgram(sp);
        glBindVertexArray(vao1);
     
        pMat = getUniformLocation(sp, "proj");
        vMat = getUniformLocation(sp, "view");
        transMat = getUniformLocation(sp, "transform");
     
        glUniformMatrix4dv(pMat, 1, GL_FALSE, opm.m);
        glUniformMatrix4dv(vMat, 1, GL_FALSE, vm.m);
        glUniformMatrix4dv(transMat, 1, GL_FALSE, tm.m);
     
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
     
        glBindVertexArray(0);

    the mat4 is a column major struct in c.

    **mat4 lookat function**

    Code :
        mat4* lookat(vec3f* eye, vec3f* center, vec3f* up) {
        vec3f x = {1, 0, 0};
        vec3f y = {0, 1, 0};
        vec3f z = {0, 0, 1};
     
        z = vec3fsub(eye, center);
        vec3fnorm(z);
        x = vec3fcross(y, z);
        y = vec3fcross(z, x);
        vec3fnorm(x);
        vec3fnorm(y);
     
        double x1, y1, z1;
        x1 = vec3fdotr(&x, eye);
        y1 = vec3fdotr(&y, eye);
        z1 = vec3fdotr(&z, eye);
     
        double data[] = { (&x)->p[0], (&y)->p[0], (&z)->p[0], 0,
                          (&x)->p[1], (&y)->p[1], (&z)->p[1], 0,
                          (&x)->p[2], (&y)->p[2], (&z)->p[2], 0,
                          x1,        y1,       z1,        1      };
        return makeMat4(data);
     
        }

    **orthographic projection code**

    Code :
        mat4* mat4Projection(double r, double l, double t, double b, double n, double f)
        {
            double data[] = { (2*n)/(r-l),  0,           (r+l)/(r-l),        0,
                               0,          (2*n)/(t-b),  (t+b)/(t-b),        0,
                               0,           0,           (-(f+n))/(f-n),     (-2*(f*n))/(f-n),
                               0,           0,           -1,                 1                };
            return makeMat4(data);
        }

    I also try to send down just a simple transform matrix that moves the objects 20 units in the +z direction. Running the above code and printing out the matrices along the way I get these results.

    Code :
        	lookat matrix
        [	1.000000	0.000000	0.000000	0.000000	]
        [	0.000000	1.000000	0.000000	0.000000	]
        [	0.000000	0.000000	1.000000	0.000000	]
        [	0.000000	0.000000	0.000000	1.000000	]
     
        	2d projection matrix
        [	0.010000	0.000000	0.000000	-1.000000	]
        [	0.000000	0.010000	0.000000	-1.000000	]
        [	0.000000	0.000000	-0.002000	-1.000002	]
        [	0.000000	0.000000	0.000000	1.000000	]
     
        	translation matrix	
        [	1.000000	0.000000	0.000000	0.000000	]
        [	0.000000	1.000000	0.000000	0.000000	]
        [	0.000000	0.000000	1.000000	20.000000	]
        [	0.000000	0.000000	0.000000	1.000000	]

    The error could be at this line when I am sending the data to the gpu

    Code :
    glUniformMatrix4dv(pMat, 1, GL_FALSE, opm.m);

    but again I am not sure. This is the vertex shader:

    Code :
        #version 330 core
     
        uniform mat4 view;
        uniform mat4 proj;
     
        in vec3 position;
     
        void main()
        {
            gl_Position = proj * view * vec4(position.x, position.y, position.z, 1.0);
        }

    removing the proj and view transformations I get the rectangle in the middle of the screen. Using either projection or view transformations and I can't see anything on screen, just the clear background.

  2. #2
    Junior Member Newbie
    Join Date
    Aug 2015
    Posts
    7
    Quote Originally Posted by blubee View Post
    **orthographic projection code**

    Code :
        mat4* mat4Projection(double r, double l, double t, double b, double n, double f)
        {
            double data[] = { (2*n)/(r-l),  0,           (r+l)/(r-l),        0,
                               0,          (2*n)/(t-b),  (t+b)/(t-b),        0,
                               0,           0,           (-(f+n))/(f-n),     (-2*(f*n))/(f-n),
                               0,           0,           -1,                 1                };
            return makeMat4(data);
        }
    The matrix content in data[] looks like a perspective projection matrix to me. And if that's the case, all of the entries except the last one at the bottom right are correct. The bottom right entry should be 0, instead of 1. Could that be the source of error?
    Last edited by deltayoung; 08-05-2015 at 07:04 PM. Reason: adding details

  3. #3
    Intern Newbie
    Join Date
    Aug 2012
    Posts
    48
    I checked but that doesn't seem to be the problem. I am trying to debug the code but I don't have any reference to check against so I am going piece by piece.

    Code :
        mat4identity(&vm);
        vec3f eye     =  {0, 0, 0};
        vec3f center  = {0, 0, -1};
        vec3f up       =  {0, 1, 0};
     
        mat4LookAt(&vm, &eye, &center, &up);

    I pass these values into the lookat function:

    Code :
    eye	[	x: 0.000000	y: 0.000000	z: 0.000000	]
    center	[	x: 0.000000	y: 0.000000	z: -1.000000	]
    up	[	x: 0.000000	y: 1.000000	z: 0.000000	]

    do the calculations to get the forward, up and right vectors.
    Code :
        vec3f f, u, r;
        f = vec3fsub(eye, center);
        f = vec3fnorm(f);
     
        u = vec3fnorm(up);
     
        r = vec3fcross(u, f);
        r = vec3fnorm(r);

    which gives me:
    Code :
    forward	[	x: 0.000000	y: 0.000000	z: 1.000000	]
    up     	[	x: 0.000000	y: 1.000000	z: 0.000000	]
    righ nr	[	x: 1.000000	y: 0.000000	z: 0.000000	]

    Code :
        float data[] = {  
                          r->p[0],  u->p[0], f->p[0], -(vec3fdotr(r, eye)),
                          r->p[1],  u->p[1], f->p[1], -(vec3fdotr(u,  eye)),
                          r->p[2],  u->p[2], f->p[2], -(vec3fdotr(f, eye)),
                          0,          0,          0,          1                      };
        copyMatrix(out, data);

    rendering without the projection matrix everything is fine, if I try to render with an projection matrix I see nothing.

    If I try to render with orthographic matrix

    Code :
    void mat4OrthoProjection(mat4* out, float r, float l, float t, float b, float n, float f)
    {
        float data[] = { (2)/(r-l),   0,          0,          -(r+l)/(r-l),
                          0,          (2)/(t-b),  0,          -(t+b)/(t-b),
                          0,          0,          (2/(f-n)),  -(f+n)/(f-n),
                          0,          0,          0,           0              };
        copyMatrix(out, data);
    }

    I see something like this
    Click image for larger version. 

Name:	Screen Shot 2015-08-06 at 7.13.50 PM.jpg 
Views:	152 
Size:	4.8 KB 
ID:	2040

    removing the projection matrix from my shader and I get the image below.
    Click image for larger version. 

Name:	Screen Shot 2015-08-06 at 7.14.11 PM.jpg 
Views:	170 
Size:	5.0 KB 
ID:	2041

    the two other parts of my code that I think could affect this is my functions
    r = vec3fcross(u, f);
    r = vec3fnorm(r)

    which looks like this:
    cross
    Code :
    vec3f vec3fcross(const vec3f* a, const vec3f* b)
    {
        vec3f out;
        out.p[0] = a->p[1] * b->p[2] - b->p[1] * a->p[2];
        out.p[1] = a->p[2] * b->p[0] - b->p[2] * a->p[0];
        out.p[2] = a->p[0] * b->p[1] - b->p[0] * a->p[1];
    }

    vec3fnorm
    Code :
    vec3f void vec3fnorm(vec3f* in)
    {
        t = sqrtf(in->p[0] * in->p[0] + in->p[1] * in->p[1] + in->p[2] * in->p[2]);
        if(t != 0)
        {
            out->p[0] = in->p[0]/t;
            out->p[1] = in->p[1]/t;
            out->p[2] = in->p[2]/t;
        }
    }

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,002
    Quote Originally Posted by blubee View Post
    If I try to render with orthographic matrix

    Code :
    void mat4OrthoProjection(mat4* out, float r, float l, float t, float b, float n, float f)
    {
        float data[] = { (2)/(r-l),   0,          0,          -(r+l)/(r-l),
                          0,          (2)/(t-b),  0,          -(t+b)/(t-b),
                          0,          0,          (2/(f-n)),  -(f+n)/(f-n),
                          0,          0,          0,           0              };
        copyMatrix(out, data);
    }
    That is even worse than the perspective matrix, as it sets w to zero.

  5. #5
    Intern Newbie
    Join Date
    Aug 2012
    Posts
    48
    @CGIelements can you help me understand or at least point me to where I can get the answer to this. It's obvious that I am having problems understanding what am I doing wrong?

  6. #6
    Junior Member Newbie
    Join Date
    Aug 2015
    Posts
    7
    Your projection matrix in the first post was not an orthographic projection, but close to a perspective projection matrix. Here is the correct form I meant:
    Quote Originally Posted by blubee View Post
    mat4* mat4Projection(double r, double l, double t, double b, double n, double f)
    {
    double data[] = { (2*n)/(r-l), 0, (r+l)/(r-l), 0,
    0, (2*n)/(t-b), (t+b)/(t-b), 0,
    0, 0, (-(f+n))/(f-n), (-2*(f*n))/(f-n),
    0, 0, -1, 1 0 };
    return makeMat4(data);
    }
    And your projection matrix in the later post was similar to an orthographic projection, but the last row with all 0's can't be right, as pointed out by GClements. I'd try it with the following correction:

    Quote Originally Posted by blubee View Post

    If I try to render with orthographic matrix

    void mat4OrthoProjection(mat4* out, float r, float l, float t, float b, float n, float f)
    {
    float data[] = { (2)/(r-l), 0, 0, -(r+l)/(r-l),
    0, (2)/(t-b), 0, -(t+b)/(t-b),
    0, 0, (2/(f-n)), -(f+n)/(f-n),
    0, 0, 0, 0 1 };
    copyMatrix(out, data);
    }
    If trying out these 2 options doesn't work for you, then the error will be somewhere else beyond my knowledge

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,002
    Quote Originally Posted by blubee View Post
    @CGIelements can you help me understand or at least point me to where I can get the answer to this. It's obvious that I am having problems understanding what am I doing wrong?
    You can find some standard projection projection matrices in the OpenGL 2 reference pages, specifically glOrtho and gluOrtho2D for orthographic projections and glFrustum and gluPerspective for perspective projections.

    For an orthographic projection, the W coordinate of the transformed vertex should be constant, so the bottom row of the matrix should be [0 0 0 1]. For a perspective projection, the W coordinate of the transformed vertex should be proportional to the Z coordinate of the original vertex, so the bottom row should be [0 0 k 0] for some k (k is typically -1; a negative value results in the eye-space +Z axis pointing out of the screen, i.e. a right-handed coordinate system).

  8. #8
    Intern Newbie
    Join Date
    Aug 2012
    Posts
    48
    I spent some more time and got the orthographic projection to work. I also think the perspective projection is also correct but atm I am a bit confused as how to bring my models back to the center of the view.

    Here's the way I setup the matrices.

    void mat4Projection(mat4* out, float r, float l, float t, float b, float n, float f)
    Code :
    {
        float data[] = { (2*n)/(r-l),   0,           (r+l)/(r-l),        0,
                          0,            (2*n)/(t-b), (t+b)/(t-b),        0,
                          0,            0,           (-(f+n))/(f-n),     (-2*(f*n))/(f-n),
                          0,            0,           -1,                  0                };
        copyMatrix(out, data);
    }
     
    void mat4OrthoProjection(mat4* out, float r, float l, float t, float b, float n, float f)
    {
        float data[] = { (2)/(r-l),   0,          0,          -((r+l)/(r-l)),
                          0,          (2)/(t-b),  0,          -((t+b)/(t-b)),
                          0,          0,          (-2/(f-n)), -((f+n)/(f-n)),
                          0,          0,          0,           1              };
        copyMatrix(out, data);
    }

    calling ortho like this:
    Code :
    mat4OrthoProjection(&opm, -100, 100, -100, 100, 0.001, 1000.0);

    If I scale up my model by an appropriate amount I can see it in the middle of the screen again.

    If I change the left, right, top and bottom values from -100 to 100 to say something like
    0 to 200

    then I lose track my model in the scene. I think this is the same problem that I am having with the perspective projection atm. Not being able to locate the model in the scene.

  9. #9
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,002
    Quote Originally Posted by blubee View Post
    Code :
    void mat4Projection(mat4* out, float r, float l, float t, float b, float n, float f)
     
    void mat4OrthoProjection(mat4* out, float r, float l, float t, float b, float n, float f)

    calling ortho like this:
    Code :
    mat4OrthoProjection(&opm, -100, 100, -100, 100, 0.001, 1000.0);
    Your parameters have l and r swapped and t and b swapped. This will typically result in the view being rotated by 180 degrees.

    Quote Originally Posted by blubee View Post
    If I change the left, right, top and bottom values from -100 to 100 to say something like
    0 to 200 then I lose track my model in the scene.
    Values of -100,100,-100,100 (for l,r,b,t) will place the eye-space origin in the centre of the viewport ((l+r)/2,(b+t)/2 is at the centre of the viewport). Values of 0,200,0,200 will place the eye-space origin in the lower-left corner (or the upper-right corner with the parameters swapped).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •