Archived

This topic is now archived and is closed to further replies.

webmastermattd

3D Coordinate System help.

Recommended Posts

age old problem though can''t seem to find a clear answer for it. What I need to do is take the coordinates of the mouse relative to the window (given in pixels from the top left of the window) and translate them to openGL 3D scene. Is there an easy way to convert these to the coordinates used in an openGL scene? At the moment what I have is a standard perspective scene with the camera pointed at the origin that is centered in the window. Will I need to move the camera so that the origin is at the top left before implementing a coordinate system? I have read about people using a 1 unit = 1m relation though for this project I would like to relate to everything in units that are more closely resembling pixels in the window than meters in real life as mouse click/drag operations need to be performed. Hope this is enough information in order to get some tips on finding the solution to the problem. Also, if someone could give some tips on implementing 3ds file coordinates into this aswell (not too shore if they differ from openGL coordinates in any fashion, but I have already got the objects being drawn just need to know how to place them correctly) it would be of great help. Later, WebmasterMattD. Why doth thou try to criticise me? Doth thou not trust thine judgement? Thou will only find clarity through faith, of which faith is the extension of knowledge. For if thou only knew what I knew, thou would not have a single fear in the world.

Share this post


Link to post
Share on other sites
Well, i think that for the mouse coordinates, you can do it with PICKING. Check this tut from nehe''s site.
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=32
Also, what do you mean with 3ds file coordinates? The 3ds file doesn''t have, just like ogl, a specific sized structure man, and you can make it any size. What you should do though, is make a STANDARD, which will help you not to go scaling everything. For instance, make all .3ds files the same height(or scale), and import em. Now, when you import them, you can check if they are either too big or too small, and depending on that, you can adjust the ogl coordinate system to 3ds''s one. It''s just testing and correcting.

Share this post


Link to post
Share on other sites
Had a fealling it would need to be trial and error, though was unsure about whether or not they had anything built into the file format.

Thanks for your help.

Later,
WebmasterMattD
http://www.webmastermattd.net

Why doth thou try to criticise me?
Doth thou not trust thine judgement?
Thou will only find clarity through faith,
of which faith is the extension of knowledge.
For if thou only knew what I knew,
thou would not have a single fear in the world.

Share this post


Link to post
Share on other sites
Okay, thread revival time.

Have been looking into gluProject and gluUnProject as these appear to be what I want.

At the moment I am using gluUnProject in order to translate pixel coordinates inside of a 640x480 window but am getting completely weired values back from the function.

the modelview Matrix is being initialised by
glGetDoublev( GL_MODELVIEW_MATRIX, modelviewMatrix ); // modelview[ 16 ]

the projection Matrix is being initialised by
glGetDoublev( GL_PROJECTION_MATRIX, projMatrix ); // projMatrix[ 16 ]

the viewport is being initialised by
glGetIntegerv(GL_VIEWPORT, viewport); // viewport[ 4 ]

Any ideas on what the problem would be as from as far as I can tell, I have settup the matricies correctly and the propper values are being passed into the function but what I am getting out is not what I would have expected.

Later,
WebmasterMattD
http://www.webmastermattd.net

Share this post


Link to post
Share on other sites
OpenGL''s coordinate matrix is upsidedown to the norm - when reading a mouse coordinate, subtract the mouse''s y-coordinate from the height of the window before passing it to openGL as
OpenGL''s (0,0) is the BOTTOM left corner of the screen and not the top-left corner.

Share this post


Link to post
Share on other sites
Thanks for your reply, though I probably didn't state my problem correctly. (have implemented your suggestion though)

The values that I am getting back from gluProject are in the 10's of thousands instead of 0 and 640 (640 is the pixel width of the window). This is the same with gluUnProject in which the values are in the region of 0.001 to 0.010.

Any help on this would be appreciated.

Later,
WebmasterMattD

Why doth thou try to criticise me?
Doth thou not trust thine judgement?
Thou will only find clarity through faith,
of which faith is the extension of knowledge.
For if thou only knew what I knew,
thou would not have a single fear in the world.

[edited by - webmastermattd on October 17, 2003 5:24:39 AM]

Share this post


Link to post
Share on other sites
Okay, stupid mistake on my behalf gave me the weired values from gluUnProject and gluProject.

Anyway, the problem I am now facing is that with using gluPerspective( 45.0f, (GLfloat)sceneWidth / (GLfloat)sceneHeight, 1.0f, zdepth ); as what defines the actual view of the openGL scene I am getting objects positioned where they should not be.

Does anyone know of an algorithm to deal with this offset? Is there another stupid mistake that I am making which causes these things to occur?

Later,
WebmasterMattD
http://www.webmastermattd.net

Why doth thou try to criticise me?
Doth thou not trust thine judgement?
Thou will only find clarity through faith,
of which faith is the extension of knowledge.
For if thou only knew what I knew,
thou would not have a single fear in the world.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sorry to hijack this thread, but I have problems with 3D coordinate systems aswell.

Why doesn''t this code work? I bet it''s the position of my camera, but I''ve been checking it over and changing it many times, but it still doesn''t work (I see only a blank screen instead of the large white quad I am trying to display).

Here is the code:


void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

float cameraX = (MAP_X*MAP_SCALE)/2.0f;
float cameraY = 150.0f;
float cameraZ = -(MAP_Z*MAP_SCALE)/2.0f;

float lookX = cameraX;
float lookY = 0.0f;
float lookZ = cameraZ;

// Set the camera position and orientation

gluLookAt(cameraX, cameraY, cameraZ, lookX, lookY, lookZ,
0.0f, 1.0f, 0.0f);

glBegin(GL_QUADS);
glVertex3f(0.0f, 0.0f, -(32*4));
glVertex3f(32*4, 0.0f, -(32*4));
glVertex3f(32*4, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glEnd();

glFlush();
SwapBuffers(g_hDC);
}


Thankyou for you time.

Share this post


Link to post
Share on other sites
Getting the thread back on track (if anyone still wants to answer the anonymous poster then feel free) I am now stuck with a desire to size basic geometric objects (square / cube) in terms of pixel dimensions. Is there an openGL to do this?

A workaround that I have come up with is to get the distance between two points in openGL space that correspond to the same distance in terms of pixels and work with that, but it seems a bit or a work around in order to do it.

Later,
WebmasterMattD
http://www.webmastermattd.net


Why doth thou try to criticise me?
Doth thou not trust thine judgement?
Thou will only find clarity through faith,
of which faith is the extension of knowledge.
For if thou only knew what I knew,
thou would not have a single fear in the world.

Share this post


Link to post
Share on other sites
quote:
Original post by webmastermattd
Getting the thread back on track (if anyone still wants to answer the anonymous poster then feel free) I am now stuck with a desire to size basic geometric objects (square / cube) in terms of pixel dimensions. Is there an openGL to do this?

A workaround that I have come up with is to get the distance between two points in openGL space that correspond to the same distance in terms of pixels and work with that, but it seems a bit or a work around in order to do it.

Later,
WebmasterMattD
http://www.webmastermattd.net


Why doth thou try to criticise me?
Doth thou not trust thine judgement?
Thou will only find clarity through faith,
of which faith is the extension of knowledge.
For if thou only knew what I knew,
thou would not have a single fear in the world.


If you change your camera position your objects become greater or smaller...perhaps you should use an orthographic projection (gluOrtho2D()); so you can work directly in windows (client) coordinates.
But i have not understood exactly what you need...

Share this post


Link to post
Share on other sites