src/camera.cpp

Go to the documentation of this file.
00001 // *** CAMERA.CPP ***
00002 // Definitions for CAMERA class
00003 
00004 #include "camera.h"
00005 
00006 CAMERA::CAMERA()
00007 {
00008         dir.LoadMultIdent();
00009         dir_vel.LoadMultIdent();
00010         
00011         //pos and pos_vel are VERTEX types, and so they init themselves to 0 on their own
00012         
00013         cam_mode = "free";
00014 }
00015 
00016 void CAMERA::MoveRelative(float mx, float my, float mz)
00017 {
00018         //old system
00019         /*glPushMatrix();
00020         
00021         float transform_matrix[16];
00022         dir.GetMat(transform_matrix);
00023         
00024         glLoadMatrixf(transform_matrix);
00025         
00026         glTranslatef(mx, my, mz);
00027         
00028         glGetFloatv(GL_MODELVIEW_MATRIX, transform_matrix);
00029         
00030         //now, put the new x,y,z vals into our x,y,z
00031         
00032         glPopMatrix();*/
00033         
00034         
00035         //new system
00036         QUATERNION dirconj;
00037 
00038         dirconj = dir.ReturnConjugate();
00039         QUATERNION qtemp;
00040         qtemp.w = 0;
00041         qtemp.x = mx;
00042         qtemp.y = my;
00043         qtemp.z = mz;
00044         QUATERNION qout;
00045         qout = dirconj * qtemp * dir;
00046         
00047         position.x += qout.x;
00048         position.y += qout.y;
00049         position.z += qout.z;
00050 }
00051 
00052 void CAMERA::MoveRelativeConstrainY(float mx, float my, float mz)
00053 {
00054         //new system
00055         QUATERNION dirconj;
00056 
00057         dirconj = dir.ReturnConjugate();
00058         QUATERNION qtemp;
00059         qtemp.w = 0;
00060         qtemp.x = mx;
00061         qtemp.y = my;
00062         qtemp.z = mz;
00063         QUATERNION qout;
00064         qout = dirconj * qtemp * dir;
00065         
00066         //adjust to constrain Y
00067         VERTEX origmove;
00068         origmove.x = mx; origmove.y = my; origmove.z = mz;
00069         float origlength = origmove.len();
00070         VERTEX newmove;
00071         newmove.x = qout.x;
00072         newmove.y = 0;
00073         newmove.z = qout.z;
00074         if (origlength > 0)
00075                 newmove.Scale(origlength/newmove.len());
00076         
00077         position.x += newmove.x;
00078         position.y += newmove.y;
00079         position.z += newmove.z;
00080 }
00081 
00082 void CAMERA::Move(float mx, float my, float mz)
00083 {
00084         //simply move the object (in world coords)
00085         position.x += mx;
00086         position.y += my;
00087         position.z += mz;
00088 }
00089 
00090 void CAMERA::Rotate(float a, float ax, float ay, float az)
00091 {
00092         //NOTE: ax, ay, az is assumed to be a UNIT VECTOR!!
00093         
00094         QUATERNION qtemp;
00095         qtemp.SetAxisAngle(a, ax, ay, az);
00096         //dir.Multiply(qtemp);
00097         dir.PostMultiply(qtemp);
00098 }
00099 
00100 /*void CAMERA::RotateX(float theta)
00101 {
00102         rotation.x += theta;
00103 
00104         GenerateTransform();
00105 }
00106 
00107 void CAMERA::RotateY(float theta)
00108 {
00109         rotation.y += theta;
00110 
00111         GenerateTransform();
00112 }
00113 
00114 void CAMERA::RotateZ(float theta)
00115 {
00116         rotation.z += theta;
00117 
00118         GenerateTransform();
00119 }*/
00120 
00121 void CAMERA::GenerateTransform()
00122 {
00123         //we don't really need to do anything here anymore
00124         
00125         /*glPushMatrix();
00126         glLoadIdentity();
00127         glRotatef(rotation.y, 0.0, -1.0, 0.0);
00128         glRotatef(rotation.x, -1.0, 0.0, 0.0);
00129         glTranslatef(-position.x, -position.y, -position.z);
00130         glGetDoublev(GL_MODELVIEW_MATRIX, transform_matrix);
00131         glPopMatrix();
00132 
00133         glPushMatrix();
00134         glLoadIdentity();
00135         glTranslatef(-pos_vel.x, -pos_vel.y, -pos_vel.z);
00136         glGetDoublev(GL_MODELVIEW_MATRIX, velocity_matrix);
00137         glPopMatrix();*/
00138 }
00139 
00140 void CAMERA::Update()
00141 {
00142         position = position + pos_vel;
00143         
00144         /*if (strcmp(cam_mode.c_str(), "chase") == 0)
00145                 Chase(entities.entity_array[chase_ent]);
00146         else if (strcmp(cam_mode.c_str(), "cockpit") == 0)
00147         {
00148                 dir = entities.entity_array[chase_ent].dir.ReturnConjugate();
00149                 position.x = -entities.entity_array[chase_ent].x;
00150                 position.y = -entities.entity_array[chase_ent].y;
00151                 position.z = -entities.entity_array[chase_ent].z;
00152         }*/
00153         
00154         //position.DebugPrint();
00155         
00156         GenerateTransform();
00157 }
00158 
00159 void CAMERA::PutTransformInto(GLdouble *matrix16)
00160 {               
00161         int i;
00162         float transform_matrix[16];
00163         QUATERNION qtemp;
00164         qtemp = dir_coupled.ReturnConjugate();
00165         qtemp.PostMultiply(dir);
00166         qtemp.GetMat(transform_matrix);
00167         
00168         for (i=0;i<16;i++)
00169         {
00170                 matrix16[i] = transform_matrix[i];
00171         }
00172 }
00173 
00174 void CAMERA::PutVelocityInto(GLdouble *matrix16)
00175 {
00176         int i;
00177         float transform_matrix[16];
00178         
00179         dir_vel.GetMat(transform_matrix);
00180         
00181         for (i=0;i<16;i++)
00182         {
00183                 matrix16[i] = transform_matrix[i];
00184         }
00185 }
00186 
00187 void CAMERA::LoadVelocityIdentity()
00188 {
00189         dir_vel.LoadMultIdent();
00190 
00191         pos_vel.x = 0;
00192         pos_vel.y = 0;
00193         pos_vel.z = 0;
00194 }
00195 
00196 VERTEX CAMERA::GetPosition()
00197 {
00198         VERTEX temp_vec;
00199 
00200         temp_vec.x = (float) position.x;
00201         temp_vec.y = (float) position.y;
00202         temp_vec.z = (float) position.z;
00203 
00204         return temp_vec;
00205 }
00206 
00207 //never want to do this anymore....
00208 /*VERTEX CAMERA::GetRotation()
00209 {
00210         VERTEX temp_vec;
00211 
00212         temp_vec.x = (float) rotation.x;
00213         temp_vec.y = (float) rotation.y;
00214         temp_vec.z = (float) rotation.z;
00215 
00216         return temp_vec;
00217 }*/
00218 
00219 VERTEX CAMERA::GetVelocity()
00220 {
00221         VERTEX temp_vec;
00222 
00223         temp_vec.x = (float) pos_vel.x;
00224         temp_vec.y = (float) pos_vel.y;
00225         temp_vec.z = (float) pos_vel.z;
00226 
00227         return temp_vec;
00228 }
00229 
00230 void CAMERA::SetVelocity(VERTEX new_velocity)
00231 {
00232         pos_vel = new_velocity;
00233 }
00234 
00235 void CAMERA::Chase()
00236 {
00237 
00238         /*dir = chasethis.dir.ReturnConjugate();
00239         
00240         position.x = -chasethis.x;
00241         position.y = -chasethis.y;
00242         position.z = -chasethis.z;
00243 
00244         MoveRelative(0,-4.0,-15.0);*/
00245         //Move(0,-4.0,-15.0);
00246 }
00247 
00248 void CAMERA::SetPosition(VERTEX newpos)
00249 {
00250         position = newpos;
00251 }
00252 
00253 void CAMERA::ExtractFrustum()
00254 {
00255    float   proj[16];
00256    float   modl[16];
00257    float   clip[16];
00258    float   t;
00259 
00260    /* Get the current PROJECTION matrix from OpenGL */
00261    glGetFloatv( GL_PROJECTION_MATRIX, proj );
00262 
00263    /* Get the current MODELVIEW matrix from OpenGL */
00264    glGetFloatv( GL_MODELVIEW_MATRIX, modl );
00265 
00266    /* Combine the two matrices (multiply projection by modelview) */
00267    clip[ 0] = modl[ 0] * proj[ 0] + modl[ 1] * proj[ 4] + modl[ 2] * proj[ 8] + modl[ 3] * proj[12];
00268    clip[ 1] = modl[ 0] * proj[ 1] + modl[ 1] * proj[ 5] + modl[ 2] * proj[ 9] + modl[ 3] * proj[13];
00269    clip[ 2] = modl[ 0] * proj[ 2] + modl[ 1] * proj[ 6] + modl[ 2] * proj[10] + modl[ 3] * proj[14];
00270    clip[ 3] = modl[ 0] * proj[ 3] + modl[ 1] * proj[ 7] + modl[ 2] * proj[11] + modl[ 3] * proj[15];
00271 
00272    clip[ 4] = modl[ 4] * proj[ 0] + modl[ 5] * proj[ 4] + modl[ 6] * proj[ 8] + modl[ 7] * proj[12];
00273    clip[ 5] = modl[ 4] * proj[ 1] + modl[ 5] * proj[ 5] + modl[ 6] * proj[ 9] + modl[ 7] * proj[13];
00274    clip[ 6] = modl[ 4] * proj[ 2] + modl[ 5] * proj[ 6] + modl[ 6] * proj[10] + modl[ 7] * proj[14];
00275    clip[ 7] = modl[ 4] * proj[ 3] + modl[ 5] * proj[ 7] + modl[ 6] * proj[11] + modl[ 7] * proj[15];
00276 
00277    clip[ 8] = modl[ 8] * proj[ 0] + modl[ 9] * proj[ 4] + modl[10] * proj[ 8] + modl[11] * proj[12];
00278    clip[ 9] = modl[ 8] * proj[ 1] + modl[ 9] * proj[ 5] + modl[10] * proj[ 9] + modl[11] * proj[13];
00279    clip[10] = modl[ 8] * proj[ 2] + modl[ 9] * proj[ 6] + modl[10] * proj[10] + modl[11] * proj[14];
00280    clip[11] = modl[ 8] * proj[ 3] + modl[ 9] * proj[ 7] + modl[10] * proj[11] + modl[11] * proj[15];
00281 
00282    clip[12] = modl[12] * proj[ 0] + modl[13] * proj[ 4] + modl[14] * proj[ 8] + modl[15] * proj[12];
00283    clip[13] = modl[12] * proj[ 1] + modl[13] * proj[ 5] + modl[14] * proj[ 9] + modl[15] * proj[13];
00284    clip[14] = modl[12] * proj[ 2] + modl[13] * proj[ 6] + modl[14] * proj[10] + modl[15] * proj[14];
00285    clip[15] = modl[12] * proj[ 3] + modl[13] * proj[ 7] + modl[14] * proj[11] + modl[15] * proj[15];
00286 
00287    /* Extract the numbers for the RIGHT plane */
00288    frustum[0][0] = clip[ 3] - clip[ 0];
00289    frustum[0][1] = clip[ 7] - clip[ 4];
00290    frustum[0][2] = clip[11] - clip[ 8];
00291    frustum[0][3] = clip[15] - clip[12];
00292 
00293    /* Normalize the result */
00294    t = sqrt( frustum[0][0] * frustum[0][0] + frustum[0][1] * frustum[0][1] + frustum[0][2] * frustum[0][2] );
00295    frustum[0][0] /= t;
00296    frustum[0][1] /= t;
00297    frustum[0][2] /= t;
00298    frustum[0][3] /= t;
00299 
00300    /* Extract the numbers for the LEFT plane */
00301    frustum[1][0] = clip[ 3] + clip[ 0];
00302    frustum[1][1] = clip[ 7] + clip[ 4];
00303    frustum[1][2] = clip[11] + clip[ 8];
00304    frustum[1][3] = clip[15] + clip[12];
00305 
00306    /* Normalize the result */
00307    t = sqrt( frustum[1][0] * frustum[1][0] + frustum[1][1] * frustum[1][1] + frustum[1][2] * frustum[1][2] );
00308    frustum[1][0] /= t;
00309    frustum[1][1] /= t;
00310    frustum[1][2] /= t;
00311    frustum[1][3] /= t;
00312 
00313    /* Extract the BOTTOM plane */
00314    frustum[2][0] = clip[ 3] + clip[ 1];
00315    frustum[2][1] = clip[ 7] + clip[ 5];
00316    frustum[2][2] = clip[11] + clip[ 9];
00317    frustum[2][3] = clip[15] + clip[13];
00318 
00319    /* Normalize the result */
00320    t = sqrt( frustum[2][0] * frustum[2][0] + frustum[2][1] * frustum[2][1] + frustum[2][2] * frustum[2][2] );
00321    frustum[2][0] /= t;
00322    frustum[2][1] /= t;
00323    frustum[2][2] /= t;
00324    frustum[2][3] /= t;
00325 
00326    /* Extract the TOP plane */
00327    frustum[3][0] = clip[ 3] - clip[ 1];
00328    frustum[3][1] = clip[ 7] - clip[ 5];
00329    frustum[3][2] = clip[11] - clip[ 9];
00330    frustum[3][3] = clip[15] - clip[13];
00331 
00332    /* Normalize the result */
00333    t = sqrt( frustum[3][0] * frustum[3][0] + frustum[3][1] * frustum[3][1] + frustum[3][2] * frustum[3][2] );
00334    frustum[3][0] /= t;
00335    frustum[3][1] /= t;
00336    frustum[3][2] /= t;
00337    frustum[3][3] /= t;
00338 
00339    /* Extract the FAR plane */
00340    frustum[4][0] = clip[ 3] - clip[ 2];
00341    frustum[4][1] = clip[ 7] - clip[ 6];
00342    frustum[4][2] = clip[11] - clip[10];
00343    frustum[4][3] = clip[15] - clip[14];
00344 
00345    /* Normalize the result */
00346    t = sqrt( frustum[4][0] * frustum[4][0] + frustum[4][1] * frustum[4][1] + frustum[4][2] * frustum[4][2] );
00347    frustum[4][0] /= t;
00348    frustum[4][1] /= t;
00349    frustum[4][2] /= t;
00350    frustum[4][3] /= t;
00351 
00352    /* Extract the NEAR plane */
00353    frustum[5][0] = clip[ 3] + clip[ 2];
00354    frustum[5][1] = clip[ 7] + clip[ 6];
00355    frustum[5][2] = clip[11] + clip[10];
00356    frustum[5][3] = clip[15] + clip[14];
00357 
00358    /* Normalize the result */
00359    t = sqrt( frustum[5][0] * frustum[5][0] + frustum[5][1] * frustum[5][1] + frustum[5][2] * frustum[5][2] );
00360    frustum[5][0] /= t;
00361    frustum[5][1] /= t;
00362    frustum[5][2] /= t;
00363    frustum[5][3] /= t;
00364 }

Generated on Thu Oct 19 04:05:48 2006 by  doxygen 1.4.6