Jump to content
  • Advertisement
Sign in to follow this  
soivex

OpenGL Newbe question about perspective.

This topic is 3409 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, So, i'm using OpenGL ES 1.1 (iPhone). My game is 2d so there was no problem untill i need to add some 3d stuff. I need to draw a wall going far away from viewer, it's simple rectangle rotated in 3d. But... Since, my game was 2D before day X, i've initializing my view like this: glViewport(0,0,width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,width, height,0, -1.0f, 1.0f); glRotate(90.0f, 0,0,1.0f); // since i'm using landscape view in iPhone glTranslate(0.0f, -width, 0.0f); glMatrixMode(GL_MODELVIEW); ... So, when i'm drawing textures (sprites), i've just call my draw function with screen coordinates, like this: draw(10,10,100,100); This all works perfect, but day X is here. I need to draw couple of rectangles in 3D, and i'm stuck. I know, that my question is too newbe, but how do i setup my view do enable 3d drawing mode? Just adding z coordinate to my rectangle failed. The rectangle is still in 2D. In my situation: float verts[12] = { 0,0,0, 100,0,5, 0,100,0 100,100,5 }; glVertexPointer(3,GL_FLOAT,0,&verts); so, adding this 5 to z coordinate doesn'd give me any perspective view. If it not so hard for you, please help. N.b. the DepthBuffer is set, and glEnable(GL_DEPTH_TEST); is set too. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
The perspective does not happen on a single axis, since you are manually calculating your perspective (you shouldn't be after this post) You would need to scale all the axes to a vanishing point. Where the object is infinitely small, in all direction, based on the depth.

This is all kind of complex. Luckily some genius figured out the projection matrix. Which allows you to project geometry into a perspective view space. You should try it out.

What you need is a gluPerspective() alternate. They are VERY simple. You should be able to find one easily. Use this as your GL_PROJECTION_MATRIX rather than the one generated by calling glOrtho(). It will handle the vanishing.

Two notes:

1.) as mentioned later in this thread, it is entirely possible to do the perspective calculation yourself, you're just doing it wrong. You need to scale x and y, based on z, not z itself. You'd adjust only the z if you were doing a perspective projection.

2.) it is perfectly possible and VERY common to use both an orthogonal and perspective projection in the same code / graphics frame. It's one of the very first organizational topics of rendering, how to store and utilize all the different view and projection matrices.

For my renderers i bind a view and projection matrix together in a "camera" struct. Then there are viewports, a viewport has a reference to some camera and also a list of stuff to draw.. oh wait am i not supposed to give away all the answer.. scenes ah.. hierarchy.. chew..

[Edited by - bzroom on May 20, 2009 6:22:28 AM]

Share this post


Link to post
Share on other sites
The reason it seems to be 2d is glOrtho. This is a parallel projection orthogonal to the projection plane. In general parallel projections do not scale objects with respect to their distance from the view which you might refer to as "3d effect". If you want to take depth into account for your projection then do not use glOrtho but use a so called perspective projection.

Google for gluPerspective, for example, or glFrustum to learn how to setup a perspective projection instead.

Share this post


Link to post
Share on other sites
IMHO you have two options:
1. Set the projection matrix to perspective for the 3d stuff.
2. Do the z-divide by yourself, i.e. you might calculate the depth w in range ]0,1] (0 is excluded) and divide your x and y coordinates by w. I'd prefer no. 1 though.

Edit: ninja'd [grin]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!