View Full Version : Coordinate Confusion

RPGillespie

04-13-2013, 11:16 PM

Hello,

So far from all the tutorials I've done, all coordinates for stuff on the screen have been in terns of normalized device coordinates. (i.e. everything is between -1 and 1). I want to try to make a game, however, it seems like a nightmare to make all of my coordinates floats between -1 and 1.

What's the most common way to place objects in a world? Say I want a grid of 9 cubes... do I really have to calculate normalized floating point values for every vertex or can I have some sort of world coordinate system that I then transform to ndc?

Thanks.

Alfonse Reinheart

04-13-2013, 11:38 PM

You must not have done very many tutorials. The OpenGLTutorial.org (http://www.opengl-tutorial.org/) tutorials stop being in NDC space in the third tutorial. Mine (see my sig) stop in the fourth. Even the very incomplete OpenGLBook.org (http://openglbook.com/) stops using NDC space in the fourth tutorial. And those are just some of the tutorials out there (http://www.opengl.org/wiki/Getting_Started#Tutorials_and_How_To_Guides).

I want to try to make a game

Do you want to make a game, or do you want to learn OpenGL? If you want to learn OpenGL and graphics programming, then you shouldn't be thinking in terms of "how will this help me make a game?" You shouldn't be sifting through tutorials for bits of information that you can slap into a game. Focus on learning what those materials have to teach you about graphics. Absorb that information, then digest it.

Once you understand it, then you can figure out how it fits into making a game.

RPGillespie

04-13-2013, 11:59 PM

Alright, allow me to rephrase:

I need to have a game done by Monday for a class.

I've written the core of the openGL pipeline myself so I understand quite a bit of how opengl works. "If you've written most of the openGL pipeline," you say, "why don't you understand how coordinates work?" Well, if you've done the same, you know that all of the transformations in openGL happen with matrix multiplication. I didn't necessarily understand why taking the parameters of gluPerspective and plugging them into a 4x4 matrix and multiplying it by the current matrix made it work, only that it worked.

Here's what I don't understand. How do I take a world that is not in normalized coordinates and display it to the screen. Do I just change the parameters of gluPerspective? Let's say I have a cube with a center at (0,0,0) a cube with a center at (100,100,100) and a cube with a center at (200,200,200). How can I tell openGL that I want all three objects displayed on the screen?

Or, say, I have a bunch of tetris piece models that I made in blender 3d. Those will all have local coordinates. What is the general algorithm for placing and moving objects with local coordinates into a world?

Timppa

04-14-2013, 05:10 AM

I drew a picture in ms paint. (http://i.imgur.com/5eHPJax.png)

(EDIT: note that first arrow should read model matrix)

So, if you have those tetris blocks defined in local coordinates, you multiply each one of them by a model matrix in order to scale, rotate and position them in the world. The coordinates can be in metres, feet, whatever fits best for your purpose. You then use a view matrix to move all the objects in order to position the 'camera'. So if you have a cube at 200, 200, 200, you can use gluLookAt to make the camera face it. After that you need to set up the projection matrix which will change the eye space coordinates into normalised ones. You can do this using gluPerspective, just set the close and far plane so that everything that needs to be drawn lies between the two planes (also note that gluPerspective requires the field of view in the y direction).

I hope this helps you with the problem. :)

RPGillespie

04-14-2013, 10:42 AM

Thank you, that was very helpful!

So, just to clarify. If I have a locally defined cube like in your picture, and I want to place it at 100,100,100 I would just set the matrix mode to modelview and then call glTranslatef(100,100,100)? Now let's say this cube is in a game, and it has it's own class, and in this class it has an x,y,z coordinate. Would calling glTranslatef(x,y,z) every frame keep it up to date with it's current location?

Now, since I only want to move objects with local coordinates and not the entire scene, I would have to push the mv matrix, do the translate, and then pop it, correct? If I wanted to rotate the entire screen (simulating a camera) I would just do a glRotate without pushing the mv matrix, correct?

You mentioned setting up the projection matrix. Are there any functions I might use to do this besides gluPerspective, gluLookAt, glFrustrum, etc.? I figure once I've set up the projection matrix I just just leave it alone unless the windows gets resized or something, is that a fair assumption?

Thanks again for your help!

Timppa

04-14-2013, 11:58 AM

Everything you mentioned above seems correct.

You mentioned setting up the projection matrix. Are there any functions I might use to do this besides gluPerspective, gluLookAt, glFrustrum, etc.?

glOrtho can be used for an orthographic projection, may be useful for some 2d stuff. I guess you could also define your own matrix manually and load it with glLoadMatrix, although that probably isn't necessary.

I figure once I've set up the projection matrix I just just leave it alone unless the windows gets resized or something, is that a fair assumption?

Pretty much yes. I guess you could use the projection matrix for effects like zooming, but most things should take place in the modelview matrix.

You may want to note that all OpenGL matrix functions are deprecated in OpenGL 3.0 and removed from OpenGL 3.1 and up. If you ever plan on using those, you will have to provide your own matrix class. (http://glm.g-truc.net/)

Also, glad I was able to help.

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.