3D Spatial drawing and coordinates

Started by
1 comment, last by DaedalusOwnsYou 15 years, 10 months ago
Hi. This is not a specific gaming question. But, here I go. I have to draw a lot of spheres all around the scene. Each one of then, its on a different plane. For that, I'm using glPushMatrix() and glPopMatrix(), in one way that when I do one glRotate, the whole system will be safe. Doing this, I lost the origin referencial, so, every time I do one glRotate(), I have a new origin. If I wasn't clear enough, what I'm drawing its a sort of molecule. So, my problem is, I need to know where I am drawing these spheres, I mean, to get their coordinates(x,y,z). I'm looking for some way that I can get this coordinates even after all that transformations, or some other way to draw it using an absolute coordinate system. Here is a piece of code about how it is being done. glPushMatrix(); glRotated(-angle/2,0,0,1); // rotate to the new direction glPushMatrix(); // save the actual matrix glRotated(-90,1,0,0); // this piece its for drawing the tube glCallList(tube1); glPopMatrix(); // load the previous matrix glTranslated(0.0,bond,0.0); // move to the end of the tube glutSolidSphere(radius-0.02,50,50); // draw the sphere glPopMatrix(); // load the previous matrix // and so on So is it.
Advertisement
I have this problem too. I use trigonometry for converting the local position to global. With translate/rotate/translate pattern, this works well and is simple to implement.
I know that there are vectors and that I uoght to use them, but I don't have time for optimizing the code.
If your transformations are complex (some rotations, scales) and hard to trace, the global position may by calculated by multiplying the local position by final transformation matrix. Though I'm not sure about that, it's only thought.
You can get the current status of the modelview matrix, and from that get the vector that will represent the new origin of the scene when things are rendered. Some example code:

float matrix[16];glGetFloatv(GL_MODELVIEW_MATRIX,matrix);


The value x,y,z values
matrix[12],matrix[13],matrix[14]
will be the position of the origin after a matrix has been set up. In the code you provided, if you put this right before you start popping matrices these values will represent the center of the object.

This topic is closed to new replies.

Advertisement