Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Drawing a corresponding coordinate system in the corner

This topic is 4164 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

hey there, i already made up a 3D-scene using OpenGL which I put on a stack. I am able to rotate it. I used Now i am trying to draw a corresponding coordinate system in the lower right corner so that I am able to see my objects orientation (while it is rotated). To do that I put my coordinate system on new stack: int w = width()/2; int h = height()/2; glPushMatrix(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-w, w, -h, h, -100.0, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); double whRatio = w / h; // aspect ratio double relW = w*0.2; relW = (w-relW);//+whRatio; double relH = h*0.2; relH = (h-relH);//+whRatio; glTranslatef(relW, -relH, 90.0); glMultMatrixd(MyRotationMatrix); // coordinate system glLineWidth(2); glBegin(GL_LINES); // X-Axis glColor3f(1.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 0.0); glVertex3f(50.0, 0.0, 0.0); // Y-Axis glColor3f(0.0, 1.0, 0.0); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 50.0, 0.0); // Z-Axis glColor3f(0.0, 0.0, 1.0); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 50.0); glEnd(); glLineWidth(1); glPopMatrix(); is that the right way of doing something like that? or are there other possibilities? the reason i am asking is that the translation of my coordinate system doesn't work for every kind of scene. but right now i cant see what is wrong. any experienced people out there? thx. :) Kerstin.

Share this post

Link to post
Share on other sites
Before reading your code, this was my idea.

Create a 3d model of a coordinate system (refered to as UCS from now on) or you could continue using your LINES method.

Draw the scene as you normally would.

If you were to draw the UCS model at the origin, it would be oriented correctly. But you want it in the corner. So you'll need to calculate that corner's position in 3d space. Apply this translation before you draw the UCS model.

If you aren't able to convert between your desired view space position to the world position. Take a look at this tutorial, it will give you some ideas on calculating the view space.

Unlike that tutorial though, I calculate my frustum only once when the camera configuration is changed (not position or orientation). I calculate the frustum in camera space. So the lower left corner would be some -x, -y, -z values. Then I'd just transform this point by the camera's transformation matrix (not the inverse transformation matrix like gluLookAt creates). And poof I'm in world space.

Share this post

Link to post
Share on other sites
the idea of using a 3d-model is quiet convenient. i will change that. :) but there are other issues i didnt address in my first post.

my scene is a bit more complex... all my 3d-models i can see on the screen are surrounded by a bounding sphere which diameter is as big as the biggest model in space. so if you delete the biggest model the sphere will get as big as the secound biggest model (so it will become smaller) and vice versa.

now i have my coordinate system (ucs) which has to be:
- outside of my bounding sphere otherwise it will get clipped.
- always in front of all my models even if i scale my scene. (so it may not be occluded or 'absorbed' by the 3d-models)
- always in the same distance to the window border
- and it shouldnt be distorted if i resize my window (it should be drawn in relation to the window border).

so is it right then to define a new projection matrix in which i put my ucs:

int w = width()/2;
int h = height()/2;
glOrtho(-w, w, -h, h, -100.0, 100.0);

and what about glOrtho()? do i use the parameters right? actually the other 3d-models are in a orthogonal proj-matrix already... i am still not very familiar in using glOrtho()?!

and how can i solve the problem that my ucs is updated to the aspect ratio of the window without any distortions?

thx again.

Share this post

Link to post
Share on other sites
you could do some minor changes to your code that should work:

(1) remove lines:

double whRatio = w / h; // aspect ratio
double relW = w*0.2;
relW = (w-relW);//+whRatio;
double relH = h*0.2;
relH = (h-relH);//+whRatio;

(2) replace:

glOrtho(-w, w, -h, h, -100.0, 100.0);


glOrtho(0, width(), 0, height(), -100.0, 100.0);

// The idea: to be lazy-> Its one way I use to conceptually
// make a 2D layer on top of 3D world.
// You can now use 2D pixel positions
// [with (0,0) at bottom left corner] to place your UCS thingy
// (I prefer axisIcon). It takes advantage of the
// fact that it is an orthographic projection the size of the window
// so depth (znear,zfar and anything in between, for our purposes,
// is superfluous!?[try saying that out loud, without spittin' in someone's eye]).


glTranslatef(relW, -relH, 90.0);


glTranslatef(xpos,ypos, zpos);
// where (xpos,ypos) is the pixel position you have in mind for the UCS
// [with (0,0) at bottom left corner]
// and zpos is not going to make any difference (say just use 0.0f)

Try it out.

[Edited by - steven katic on July 26, 2007 3:16:31 AM]

Share this post

Link to post
Share on other sites
you are right. that works fine... thanks a lot. :)

the only thing which doesnt work yet: in my case the z-coordinate is important. i have to position my ucs very far away from (in front of) my model so that there wont be any occlusions or conflicts between these two while rotating or scaling the scene.

right now, i kind of hard-coded but i think i always have to update the z-position depending on the size of my sphere. or is there a better way of doing it?

Share this post

Link to post
Share on other sites

try: glDisable(GL_DEPTH_TEST);// ensures zpos above is superfluous
before rendering your lines...

//That way our "2D layer" always stays on top.

then renable...as you do

[Edited by - steven katic on July 26, 2007 5:41:38 AM]

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!