2d to 3d

Started by
3 comments, last by dawidjoubert 18 years, 4 months ago
Hey guys.. I'm new to openGL.. I don't know if this is the right place to post this or for beginners.. but here goes anyhow.. I set my projection 'glFrustum(-1.0, 1.0, -1.0, 1.0, 10.0, 100.0); then I draw some quads (anyx, anyy, -90.0f) (I'm using SDL) then I'd like to get mouse coords from sdl and find out what quad I'm in.. (This is basically for a 2dish done in 3d rpg) Pointers, references, ideas, comments, etc welcome -- Bradd
Advertisement
You could try NeHe's article on using gluUnproject().
okay, that looks interesting and may be useful in the future.. but let me explain what I'm
trying to do here..

I make an openGL window with a projection.. my camera is top-down..

I will be using a grid (at a constant Z).. I will then get the mouse coords via SDL..

my grid center will be (0, 0, constZ).. going out in each direction by 1.0f.. so
each grid BOX will be (x * 1.0f, (x+1) * 1.0f), same for Y, and Z is constant


now my X and Y for the grid will be a multiple of 1.0f BUT I get screen coords from SDL which I'd like to find out what X, Y grid box (on a constZ) the mouse pointer is in..

basically I want to map SDL's x, y to a box on constZ and find out what box I'm in..

I don't care about any renderings above or below this constZ.. I want to know the grid box on the constZ plane that SDL's x, y map to..

hopefully this is understandable..


Thanks, Bradd

I still say you need gluUnproject. The reason for this is the calculations are dependant on the view matrix (the camera's position).

If your camera never moves along Z, then you could feasably calculate some kind of mapping from screen space to your map space, and just multiply the coordinates by that scale. For example, if one grid tile takes _exactly_ five screen space pixels, you can just divide the mouse coordinates by five, offset by the map origin and you have map coordinates.

It probably won't be perfect, you'll have to do a bunch of calculations by hand, and it won't be any better than not using gluUnproject in the first place.
If your camera never moves along Z,

Trust me on this one use glUnProject, i tried doing the calculations self, and i still did scaling *how ever i had an ortho scene*, but stick the gluUnproject the reason being is that once you have done it right you can look from anywhere in your scene and still get the correct coordinates *an absolute must for a 3d strategy game*
----------------------------

http://djoubert.co.uk

This topic is closed to new replies.

Advertisement