0
votes

I have this obj files (also tried with other, more 3d models):

            Triangle                  
            v 0 0 0                 
            v 0 0.5 0               
            v 1 0 0     
            f 1 2 3        

I use the following vertexshader:

    uniform mat4 projection;
    attribute vec4 pos;

    void main(void)
    {
        gl_Position = projection* pos;
    }

Super simple. If I run it with just uniform_Projection = identity matrix I see a triangle, 0.5 up and 1 to the right. Seems right.

Now, what I want to do is to use a camera ala gluLookAt aswell as some perspective projection. And this is where I get so confused.

In world coordinates, the triangle is located at the vertex positions above. So lets pick an eye and a lookat point. eye = (0,0,3), lookAt = (0,0,0). Basically we want the eye to be at the origin. So we have to move the world origin to the eye position (lets do it even if it is the identity matrix):

   | 1 0 0 0 |   | 1 0 0 0 |   | 1 0 0 0 |
   | 0 1 0 0 | * | 0 1 0 0 | = | 0 1 0 0 |
   | 0 0 1 -3|   | 0 0 1 0 |   | 0 0 1 -3| 
   | 0 0 0 1 |   | 0 0 0 1 |   | 0 0 0 1 | 

And boom, the triangle is gone.. And all I did was moving the world along the direction the original camera was looking. eye = (0,0,3) still gone.. Super confused :S I have turned off any form of culling etc. Can someone explain to me how this works?

Because later my idea was the following:

       v = normalize(lookat-eye)
       r = normalize(crossProduct(v, up));
       u = crossProduct(r, v);

       viewMatrix = |r1 r2 r3 -eye1|
                    |u1 u2 u3 -eye1|
                    |v1 v2 v3 -eye1|
                    |0  0  0   1   |

And as a projection matrix I was thinking either (from my lectures):

       projMatrix = |1 0 0    0|
                    |0 1 0    0|
                    |0 0 1    0|
                    |0 0 -1/d 1|

Or this link: gluPerspective was removed in OpenGL 3.1, any replacements? Can't say I quite understand any of them right now.

Well to me it seems I don't understand some key parts of how the coordinate systems works.

1
Please do not prefix variable names with their storage class like "uniform_" and "attribute_".Nicol Bolas
Hehe y, sry about that. The curse of tutorials..Vejto

1 Answers

1
votes

Your problem with this matrix:

| 1 0 0 0 |   | 1 0 0 0 |   | 1 0 0 0 |
| 0 1 0 0 | * | 0 1 0 0 | = | 0 1 0 0 |
| 0 0 1 -3|   | 0 0 1 0 |   | 0 0 1 -3| 
| 0 0 0 1 |   | 0 0 0 1 |   | 0 0 0 1 | 

Is that you've translated it, but you haven't adjusted the clip planes at all. The default identity projection matrix clips from -1 to 1 on the Z axis. You've translated your eye to Z=3, so your new clip planes are from 4 to 2, and your triangle is outside this range, so it's clipped.

So if you want to move your eye, you need a new projection matrix. Since you're talking about perspective, I'd recommend either implementing the equations yourself from gluPerspective, or downloading a library that will generate a projection matrix for you, like OpenGL Mathematics