00001 #include "MercuryCamera.h" 00002 #include "MercuryDisplay.h" 00003 00004 MercuryCamera::MercuryCamera() 00005 :MercuryObject() 00006 { 00007 SetDrawOrder(-32768); //give camera highest draw priority 00008 00009 /* set the up and direction so that by default both old style camera 00010 and quaternion based camera default to same orientation.*/ 00011 m_up = MercuryPoint( 0, 1, 0 ); //Y axis is up 00012 00013 m_recalcAxis = false; 00014 m_drawable = true; 00015 m_pyr[0] = m_pyr[1] = m_pyr[2] = 0; 00016 m_lai = new LookAtInternals; 00017 } 00018 00019 void MercuryCamera::Prerender() 00020 { 00021 //no need to do base prerender stuff 00022 if (m_taintedMatrix) 00023 { 00024 *(LookAtInternals*)m_lai = CalculateLookAtMatrix( GetPosition(), m_target, m_up, m_finalMatrix); 00025 m_taintedMatrix = false; 00026 } 00027 } 00028 00029 void MercuryCamera::Render() 00030 { 00031 //Skip all the base rendering stuff for the camera and just set the position 00032 DISPLAY->SetView(GetPosition(), m_finalMatrix, *(LookAtInternals*)m_lai); 00033 } 00034 00035 /* OpenGL says Z axis goes into and out of the screen, 00036 Y is up, and X is left and right. That is how these 00037 quaternions work. 00038 */ 00039 00040 /* 00041 void MercuryCamera::Roll(const float degree) 00042 { 00043 float rad = DEGRAD * degree; 00044 00045 static MQuaternion v; 00046 static MQuaternion r; 00047 static MQuaternion q; 00048 00049 v.x = m_up.x; 00050 v.y = m_up.y; 00051 v.z = m_up.z; 00052 v.w = 0; 00053 00054 // r.CreateFromAxisAngle(m_direction, rad); 00055 //Rotate about Z axis (0 - 2PI) 00056 m_pyr[2] += rad; 00057 00058 q = r * v * r.conjugate(); 00059 m_up.x = q.x; 00060 m_up.y = q.y; 00061 m_up.z = q.z; 00062 00063 //Recalculate X axis 00064 if (m_recalcAxis) 00065 { 00066 // m_x = m_direction.CrossProduct(m_up); 00067 // m_x.NormalizeSelf(); 00068 } 00069 } 00070 00071 void MercuryCamera::Pitch(const float degree) 00072 { 00073 float rad = DEGRAD * degree; 00074 static MercuryPoint oldpos; 00075 static MercuryPoint p; 00076 static MQuaternion v; 00077 static MQuaternion r; 00078 static MQuaternion q; 00079 00080 if (oldpos != GetPosition()) 00081 { 00082 // p = m_direction - GetPosition(); 00083 oldpos = GetPosition(); 00084 } 00085 // else 00086 // p = m_direction; 00087 v.x = p.x; 00088 v.y = p.y; 00089 v.z = p.z; 00090 v.w = 0; 00091 00092 //Rotate about X axis 00093 // r.CreateFromAxisAngle(m_x, rad); 00094 m_pyr[0] += rad; 00095 00096 q = r * v * r.conjugate(); 00097 // m_direction.x = q.x; 00098 // m_direction.y = q.y; 00099 // m_direction.z = q.z; 00100 // 00101 //Recalculate UP axis 00102 if (m_recalcAxis) 00103 { 00104 // m_up = m_x.CrossProduct(m_direction); 00105 m_up.NormalizeSelf(); 00106 } 00107 } 00108 00109 void MercuryCamera::Yaw(const float degree) 00110 { 00111 float rad = DEGRAD * degree; 00112 00113 static MQuaternion v; 00114 static MQuaternion r; 00115 static MQuaternion q; 00116 00117 // v.x = m_direction.x; 00118 // v.y = m_direction.y; 00119 // v.z = m_direction.z; 00120 v.w = 0; 00121 00122 //Rotate about "up" (Y) axis 00123 r.CreateFromAxisAngle(m_up, rad); 00124 m_pyr[1] += rad; 00125 00126 //by conjugate also so that rotation is correct not 1/2 degree or whatever 00127 q = r * v * r.conjugate(); 00128 // m_direction.x = q.x; 00129 // m_direction.y = q.y; 00130 // m_direction.z = q.z; 00131 00132 //Recalculate X 00133 if (m_recalcAxis) 00134 { 00135 // m_x = m_direction.CrossProduct(m_up); 00136 // m_x.NormalizeSelf(); 00137 } 00138 } 00139 */ 00140 MercuryPoint MercuryCamera::GetRight() const 00141 { 00142 MercuryPoint dir = GetDirection(); 00143 dir.NormalizeSelf(); 00144 00145 MercuryPoint xaxis = dir.CrossProduct( m_up ); 00146 xaxis.NormalizeSelf(); 00147 00148 return xaxis; 00149 } 00150 00151 00152 /* 00153 * (c) 2006 Anthony Varner, Josh Allen 00154 * All rights reserved. 00155 * 00156 * Permission is hereby granted, free of charge, to any person obtaining a 00157 * copy of this software and associated documentation files (the 00158 * "Software"), to deal in the Software without restriction, including 00159 * without limitation the rights to use, copy, modify, merge, publish, 00160 * distribute, and/or sell copies of the Software, and to permit persons to 00161 * whom the Software is furnished to do so, provided that the above 00162 * copyright notice(s) and this permission notice appear in all copies of 00163 * the Software and that both the above copyright notice(s) and this 00164 * permission notice appear in supporting documentation. 00165 * 00166 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00167 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00168 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF 00169 * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS 00170 * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT 00171 * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 00172 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 00173 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00174 * PERFORMANCE OF THIS SOFTWARE. 00175 */ 00176