Jackson Zheng
Jackson Zheng

Reputation: 177

what is the coordinate of the camera in opengl

I want to determine what's the coordinate of camera in opengl. So I simply draw a sphere in a window, the code is like this:

glutSolidSphere (1.0, 20, 16); //draw a sphere, its radius is 1

//I use glOrtho to set the x,y coordinate

//1
glOrtho(-1,1,-1,1,-0.99,-1.0);

//2
glOrtho(-1,1,-1,1,-1.0,-0.99);
//3
glOrtho(-1,1,-1,1,1.0,0.99);

//5
glOrtho(-1,1,-1,1,1.0,1.0);
//6
glOrtho(-1,1,-1,1,10,10);
//7
glOrtho(-1,1,-1,1,0.0,0.0);

//8
glOrtho(-1,1,-1,1,-0.5,0.5);

//9
//glOrtho(-1,1,-1,1,0.0,0.1);

in case 1,2,3,4, the picture is like this: a small circle

in case 5,6,7, the sphere just the same size of the window.

in case 8, the picture is like this: like a torus,strange

According to glOrtho description:

void glOrtho(   GLdouble    left,
    GLdouble    right,
    GLdouble    bottom,
    GLdouble    top,
    GLdouble    nearVal,
    GLdouble    farVal);

Let's assume that the coordinate of camera is fixed in opengl. from case 1, it seems that the camera is at (0,0,0);

1) but if then, how can case 2,3,4 is the same as case1?

2) how case 5,6,7 come out?

3) how case 8 come out?

Upvotes: 2

Views: 567

Answers (1)

derhass
derhass

Reputation: 45332

You seem to be confusing several things.

Conceptually, the default glOrtho and glFrustum()/gluPerspecitve() functions assume that the camera is at eye space origin and looking at negative z direction. If you have left the ModelView matrix at idendity (the default), it means your object space will be identical to the eye space, so you are drawing directly in eye space.

OpenGL defines a three-dimensional viewing volume. This means that there is not only a 2D rectangle limited by your viewport/window size, but there are aloe near and far clipping planes. That viewing volume is described as a axis-aligned cube -1 <= x,y,z <= 1 in _normalized device coordinates`.

The purpose of the projection matrix is to transfrom some viewing volume to that normalized cube. With an orthogonal projection, there will be no perspective effect. Objects which are far away will not appear smaller. So you can interpret the ortho matrix as defining an axis-aligned cuboid in eye space, which defines the part ot the space that will be visibile on the screen. Note that you can set up that projection such that you can see things which are actually behind your "camera" (by using neagtive values for near or far).

Your cases 1-4 all appear identically because you cut out only a tiny section z in [0.99, 1] or z in [-1, -0.99]. where the intersection with a sphere will just appear as a disc. It doesn't matter if you flip the ranges, since that will only flip what is in front or behind. Whithout lighting, you basically see only the silhuette, so you can't see the differences.

Your cases 5, 6 and 7 are just invalid, the parameters near and far must not be identical. That code will just generate a GL error and create no ortho matrix at all, which means that the projection matrix is left at identity - and then, you get excatly the [-1,1]^3 viewing volume. Since you draw a sphere with radius 1 centered at the origin, it will exactly fit.

Case 8 is just a cut of the spehre, the intersecion within -0.5 <= z <= 0.5.

Upvotes: 4

Related Questions