Sign in to follow this  
JulianAssange

OpenGL Correct calculations of floats in OpenGL

Recommended Posts

Hello! I'm making a game in 3D. Everything is correct in my code, although I'm confused about one thing.

When I setting up my perspective (gluPerspective) I set it to zNear = 0.1f and zFar = 100.0f. So far so good. Now, I also wanna move things just in the x or y direction via glTranslate.... But, the origo starts in the absolute centrum of my screen. Like I have zFar and zNear, why isn't that properly to the x and y coordinates? Now it is like if I move my sprite -2.0f to left on x-axis and make glTranslate... to handle that, it almost out of screen. And the z-axis is not behave like that. That's make it a lot more difficult to handle calculations in all directions. It's quite hard to add an unique float value to an object and for now I just add these randomly to just make them stay inside screen.

So, I have problem calculate corrects value to each object. Have I missed something? Should I change or thinking of something? The reason that this is important is because I need to know the absolute left and right of my screen to make these calculations.

This is my onSurfaceChanged:

public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl, 45.0f, (float)width / (float)height,
0.1f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}

I've heard of gl.glOrtho.. etc, but then I don't know how I should assign my vertices value in my objects and also, I want to see things in 3D, but still be able to calculate values correctly in all directions to do my 3D game. So how should I setup everything correct for this?

Thanks in advance!

Share this post


Link to post
Share on other sites
Well you can't define a range of x and y that fit inside a perspective frustum. By definition it is a cone that expands outward from the eye, so the farther away you get the larger x/y range you can travel before you're out of the screen.

If you want to define an absolute region to be your screen, then you can consider orthographic projections which are much more friendly in this way. Being orthographic doesn't mean that it isn't 3d, it just doesn't use perspective foreshortening. Note the difference between these two cubes.



Otherwise if you want to keep perspective projection, you'll have to take the view frustum into account when you calculate your screen size.

Share this post


Link to post
Share on other sites
Thanks for reply. The problem is how should I setup this? I'm not that comfortable with mixing ortographic with 3D cubes and also having a perspective to have the 3D feeling.

Share this post


Link to post
Share on other sites
If an object is on the near plane, in your case .1, then the left edge x = -1, and the right edge x = 1.0. Top is y = 1.0, bottom = -1.0. Thats the best explanation I can give for now. When you have perspective you are seeing with a field of view. You cant see something 10 meters directly to your left, but if they start walking on the z-axis (still 10 meters to the left) they will eventually come into your view/peripherals.

If you are working with 2d sprites just use glOrtho(0,windowwidth, 0, windowheight), then you will have something more easy to use for 2d. You can still have sprites in the z-direction, they just wont be smaller the further away on the z-axis that they are.

Share this post


Link to post
Share on other sites
Again, if you want things farther away to be smaller, this is called perspective (gluPerspective), if you dont want them to be smaller its called orthographic (gluOrtho).

Thats all there is to it, you can move your objects in any (x,y,z) translation. Play around with moving objects to understand how it works. Trial and error.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this