Where is the Object?

Started by
-1 comments, last by luck_bird_bj 20 years, 9 months ago
On www.gameturorials.com there is a Billborading tutorial,the following is the Section: ********************************************************************************************* Where is the Object? When an object is placed in the world using translations and rotations it becomes hard to find its world coordinates. In this section a simple technique to find the whereabouts in world coordinates of an object is presented. It is assumed that the object is rendered in the local origin. The technique tells you where the local origin is in world coordinates. Again we have to look at the modelview matrix. The position of the local origin in camera''s coordinates is know, it is the v vector of the figure bellow. But since the local origin''s position is dependent on both the camera''s position and orientation, one can''t just add that position to the camera position. What is needed is to reverse the orientation of the camera back to a coordinate system with axis aligned with the world coordinate system, then we can add the camera position to the object''s position relative to the camera. This sum will provide the position of the object in world coordinates. In order to reverse the orientation of the camera we use the inverse of M1. As mentioned before, the inverse of M1 is the transpose. The object''s position in world coordinates is therefore given by the expression objPosWC = camPos + M1T * V where camPos is the camera position in world coordinates, and M1T is the transpose of M1. ********************************************************************************************* The following is my code: ////////////////////////////////////////////////////////// typedef struct _vector3d { float x; float y; float z; }vector3d; RenderScene() { glClearColor(0.5,0.5,0.5,1); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Define the modelview transformation. glMatrixMode (GL_MODELVIEW); glLoadIdentity (); vector3d camPos;//assign world coordinate to the camera,for example camPos.x=10; camPos.y=10; camPos.z=10; vector3d objPos; //assign world coordinate to the obj,for example objPos.x=0; objPos.y=0; objPos.z=0; float modelview[16]; // save the current modelview matrix glPushMatrix(); gluLookAt(camPos.x,camPos.y,camPos.z, objPos.x,objPos.y,objPos.z, 0.0f,1.0f,0.0f); /////////////////////////perform geometry transformation glTranslatef (0.0f, 0.0f, -3.0f); static int incz=2; incz+=2; glRotatef ((GLfloat) incz, 0.0f, 1.0f,0.0f); //and more geometry transformation ....... ........ // get the current modelview matrix glGetFloatv(GL_MODELVIEW_MATRIX , modelview);// vector3d objPosWC; // compute the world coordinate of a object after translationsand rotations, objPosWC.x=modelview[0]*modelview[12]+modelview[1]*modelview[13]+modelview[2]*modelview[14]+camPos.x; objPosWC.y=modelview[4]*modelview[12]+modelview[5]*modelview[13]+modelview[6]*modelview[14]+camPos.y; objPosWC.z=modelview[8]*modelview[12]+modelview[9]*modelview[13]+modelview[10]*modelview[14]+camPos.z; //////////////////////////////// draw an object in local origin /////////////////////////////// ..... glPopMatrix(); } But it doest''t work properly!!! similarly is the rotation in world coordinate. Can you help me ? sample code is preferred. Tnank you! Luck_bird_bj@sina.com

This topic is closed to new replies.

Advertisement