Perspectives and glFrustum in Opengles

I want to get a simple object(say, a triangle) to enlarge/shrink as it moves towards/away from the viewer respectively.

I dont want the viewer to move closer to/farther from the object. Rather, the object needs to move towards/away from the viewer. And the object size must change accordingly.

Can someone please give me just a summary of the sequence of opengles api calls I need to make?

I know glFrustum is somewhere in there, but somehow I feel I am not using the function correctly in my code. Either that, or I am missing out on some API calls.

Your help is really REALLY appreciated!

All movement is relative. It doesn’t really matter whether the viewer moves toward the object or the object moves toward the viewer.

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(left, right, bottom, top, near, far);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// here go your glTranslate/Rotate/Scale calls to 
// transform the object from model space to view space.

By default the viewer is at the origin in view space, looking along the negative Z axis. Also note that the order in which the gl transformations are applied appears to be reversed (but that is intentional).

[quote="XmasAll movement is relative. It doesn’t really matter whether the viewer moves toward the object or the object moves toward the viewer.
[/quote]

It does matter if there are multiple objects, and just one of those objects needs to move towards the viewer, right?

ie, if the user moves towards, say, two objects, both increase in size, but if one of the objects move towards the viewer, only that one should increase in size while the other one remains the same. That is the affect I am trying to arrive at.

I hope I’m making sense. Is this even possible in opengles?

One more thing about the parameters to glFrustum… in what units are they measured… how do I map them to a screen having X,Y and Z ranging from -1 to 1? Thanks for you help Xmas!

OpenGL is not a scene graph API, and there can only be one combined transformation set at any time.
Thus when you have two objects, you set the transformation for one, render it, then set the transformation for the other and render it.

One more thing about the parameters to glFrustum… in what units are they measured… how do I map them to a screen having X,Y and Z ranging from -1 to 1? Thanks for you help Xmas!

It’s in whatever unit you want it to be, the same units you like to attach to your vertex data.
The left, right, bottom and top parameters refer to the position of the edges of the screen at the near clip plane.

So for glFrustum(-3, 3, -3, 3, 5, 100), the vertex position (-3, -3, 5) will be transformed to normalized device coordinates (-1, -1, -1). That is then further transformed to window coordinates (0, 0, 0) – lower left corner of the viewport, at the near plane.

The below code execept the blue block displays two textured planes, which appear overlapping, even though they are separated along the z-axis.

In order to give the planes a perspective look, i have added the blue block, but it does not work and i see a blank screen. Please help me to know my mistake.

GLfloat planes[] = {
0.2f, 0.0f, 0.0f, //plane 1
0.6f, 0.0f, 0.0f,
0.6f, 0.4f, 0.0f,
0.2f, 0.4f, 0.0f,

0.0f, 0.0f, 0.2f,  //plane 2
0.4f, 0.0f, 0.2f, 
0.4f, 0.4f, 0.2f, 
0.0f, 0.4f, 0.2f, 

};

glViewport (0, 0, 800, 600);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glDepthRangef(0.0f, 1.0f);
glFrustumf(-1.0, 1.0, -1.0, 1.0, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* VBO creation and loading of vertex data */

glMatrixMode(GL_TEXTURE);
glLoadIdentity();
/* 2D Texture object creation and loading of desired texture */

glClearColor(0.0f,0.0f,0.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glDrawElements(GL_TRIANGLES, sizeof(indices),GL_UNSIGNED_BYTE, indices);

glFrustum generates a projection matrix “looking down the negative Z axis”, so in your case only objects in the range (-0.1, -100.0) in Z would be visible.

If you want to make the camera face in positive Z direction you can either rotate yoiur projection matrix 180° around the X or Y axes, or you can invert the Z axis by scaling it by -1.

You may need to change the winding order for culling (glCullFace).

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.