Advertisement

camera defined in GL_PROJECTION

Started by
1 comment, last by JARVIS_UK 18 years, 11 months ago
After working on creating one of my first computer games now for two weeks, i have hit a problem that i can not resolve :( I stupidly put my camera gluLookAt call under GL_PROJECTION MATRIX. Cantained within my application i also use commands such as glGetDoublev(GL_MODELVIEW_MATRIX,myMat) to get the exact loaction of points in my scene. When i try to rearrange my gluLookAt call into the GL_MODELVIEWMATRIX the position of the calculated points from myMat are incorrect as they are at a different position due to being relative to the camera. If anyone can guide me to a way to place the calls that they believe will work i would be most greatful. Thank you Jarvis Here is my display function: void myGlutDisplayCallback(void){ if(LockFrameRate(100)){ // CalculateFrameRate(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(0.7,0.9,1.0,1); setup_3D(); cam.Look(); //gluLookAt contained in here glMatrixMode(GL_MODELVIEW); // Modeling transformation glLoadIdentity(); // Initialize the model matrix as identity CheckForMovement(); CheckDirectInput(); glEnable(GL_LIGHTING); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer drawObjects(AnimStage); glDisable(GL_LIGHTING); glPushMatrix(); glColor3f(1,1,1); glutWireSphere(0.1,5,5); glPopMatrix(); //TEST SECTION WITH A POINT WHICH HAS GLTRANSLATEF,GLROTATE ETC APPLIED TO ITS MATRIX MATRIXSTRUCT myMatTest; glGetDoublev(GL_MODELVIEW_MATRIX,myMatTest.myRotMatArrray); printf("\n1 0=%f,1=%f,2=%f,3=%f,4=%f,5=%f,6=%f,7=%f,8=%f,9=%f,10=%f,11=%f,12=%f,13=%f,14=%f,15=%f\n", myMatTest.myRotMatArrray[0],myMatTest.myRotMatArrray[1],myMatTest.myRotMatArrray[2],myMatTest.myRotMatArrray[3], myMatTest.myRotMatArrray[4],myMatTest.myRotMatArrray[5],myMatTest.myRotMatArrray[6],myMatTest.myRotMatArrray[7], myMatTest.myRotMatArrray[8],myMatTest.myRotMatArrray[9],myMatTest.myRotMatArrray[10],myMatTest.myRotMatArrray[11], myMatTest.myRotMatArrray[12],myMatTest.myRotMatArrray[13],myMatTest.myRotMatArrray[14],myMatTest.myRotMatArrray[15]); MYCAMPOINTS my1,my2; my1 = drawCamPos(1); //MAIN SECTION WHERE ALL TRANSFORMATIONS ARE APPLIED TO MATRICES gCircuit_Display_I(&gCir,fakeAngle,fakeAngle1,floorLevel); my2 = drawCamPos(2); drawSpheres(); glEnable(GL_LIGHTING); drawGround(); setup_2D(); glFlush(); glutSwapBuffers(); } }
Okay ur code is a bit dirty... Tell me if i wrong but here is what u are doing
Display{  glClear(..  setup_3D(); // In here u Call ViewPort and Set Current Matrix to Projection  cam.look() --> gluLookAt(..)  glMatrixMode(GL_MODELVIEW); // Modeling transformation  glLoadIdentity(); // Initialize the model matrix as identity  // I dont know why u call GL_CLEAR AGAIN...  ... Frame Update Stuff here  ///  drawObjects(AnimStage); // Okay Here u render Stuff // glPushMatirx() ... No need.. glutWireSphere doesnt edit the matrix  glutWireSphere(0.1,5,5);  // glPopMatrix() if u didnt save then dont load...  float m[16];  getDoublefv(GL_MODEL_VIEW,m); printf }


Now i dont kow what you want to do with the Matrix or what ever but GL_MODEL_VIEW in this case will always be an Identity Matrix (unless DrawObjects() edited it) and so
this matrix wont help you much... The Matrix in this case that you want would be GL_PROJECTION_VIEW but even better is to re-arrange ur code like this

Display{  glClear(..  setup_3D(); // In here u Call ViewPort and Set Current Matrix to Projection// NOTE THAT GL_PROJECTION MATRIX holds only the View Frustum now... ;-)  glMatrixMode(GL_MODELVIEW); // Modeling transformation  glLoadIdentity(); // Initialize the model matrix as identity  cam.look() --> gluLookAt(..)  UpdatePhysics(); // or what ever it is u do  drawObjects(AnimStage); // Okay Here u render Stuff  glutWireSphere(0.1,5,5);   float m[16];  getDoublefv(GL_MODEL_VIEW,m);  setup_2D(); // This Call Prob has a glOrtho in... make sure the projection matrix is selected}




Lastly if u want anything back in the exact same coordinates as u passed it in you need to have all the matrices which is why gluUnProject is usefull..

eg

glMatrixMode(GL_PROJECTION)
gluPerspective(45....) // Set for Fish eye

glMatrixMode(GL_MODEL_VIEW);
glLoadIdentity()
RenderTerrain()
glTranslatef(Posx,posy,posz)
glDrawEnemyModel();

// Now if u want the Screen to Terrain Coordinates u need to take the matrix there...
// By Adding getDoublefv() after RenderTerrain() (and u need to get both the Projection and Model view Matrix)

// Then if u want the enemy model coordinates u need to take it right after u translated.

REMEMBER the end result u see on the screen is = PROJECTION MATRIX * MODEL MATRIX
and a couple others which we dont care about.........
----------------------------

http://djoubert.co.uk
Advertisement
hi, thank you ever so much for your reply.
Im a bit of an amature so bare with me.
in the setup_3d i now have

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,scrWid,scrHei);
gluPerspective(vAng,asp,nearD,farD);

and i moved the gluLookAt... below the
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();.

The problem that occurs now is that points that i have obtained using
gltranslatef(...);
etc...
glGetDoublev(GL_MODELVIEW...);
DoubleArrayToMatrix(...);

are manipulated by the movement of the viewport.
Originally a point would be multiplied by the obtained matrix from DoubleArrayToMatrix(...) and produces a point in the world that is not affected by the movement of the viewport.

now the gluUnProject may resolve this i don't know if you think it might could you possibly help me to implement it.

Thank you for your time.

This topic is closed to new replies.

Advertisement