00001
00002 #include <blort/TomGine/tgFrustum.h>
00003
00004 using namespace TomGine;
00005
00006 void tgFrustum::ExtractFrustum(){
00007 float clip[16];
00008 float t;
00009 float mv[16];
00010 float pj[16];
00011
00012
00013 glGetFloatv( GL_PROJECTION_MATRIX, pj );
00014
00015
00016 glGetFloatv( GL_MODELVIEW_MATRIX, mv );
00017
00018 m_intrinsic = mat4(pj);
00019 m_extrinsic = mat4(mv);
00020
00021
00022 clip[ 0] = mv[ 0] * pj[ 0] + mv[ 1] * pj[ 4] + mv[ 2] * pj[ 8] + mv[ 3] * pj[12];
00023 clip[ 1] = mv[ 0] * pj[ 1] + mv[ 1] * pj[ 5] + mv[ 2] * pj[ 9] + mv[ 3] * pj[13];
00024 clip[ 2] = mv[ 0] * pj[ 2] + mv[ 1] * pj[ 6] + mv[ 2] * pj[10] + mv[ 3] * pj[14];
00025 clip[ 3] = mv[ 0] * pj[ 3] + mv[ 1] * pj[ 7] + mv[ 2] * pj[11] + mv[ 3] * pj[15];
00026
00027 clip[ 4] = mv[ 4] * pj[ 0] + mv[ 5] * pj[ 4] + mv[ 6] * pj[ 8] + mv[ 7] * pj[12];
00028 clip[ 5] = mv[ 4] * pj[ 1] + mv[ 5] * pj[ 5] + mv[ 6] * pj[ 9] + mv[ 7] * pj[13];
00029 clip[ 6] = mv[ 4] * pj[ 2] + mv[ 5] * pj[ 6] + mv[ 6] * pj[10] + mv[ 7] * pj[14];
00030 clip[ 7] = mv[ 4] * pj[ 3] + mv[ 5] * pj[ 7] + mv[ 6] * pj[11] + mv[ 7] * pj[15];
00031
00032 clip[ 8] = mv[ 8] * pj[ 0] + mv[ 9] * pj[ 4] + mv[10] * pj[ 8] + mv[11] * pj[12];
00033 clip[ 9] = mv[ 8] * pj[ 1] + mv[ 9] * pj[ 5] + mv[10] * pj[ 9] + mv[11] * pj[13];
00034 clip[10] = mv[ 8] * pj[ 2] + mv[ 9] * pj[ 6] + mv[10] * pj[10] + mv[11] * pj[14];
00035 clip[11] = mv[ 8] * pj[ 3] + mv[ 9] * pj[ 7] + mv[10] * pj[11] + mv[11] * pj[15];
00036
00037 clip[12] = mv[12] * pj[ 0] + mv[13] * pj[ 4] + mv[14] * pj[ 8] + mv[15] * pj[12];
00038 clip[13] = mv[12] * pj[ 1] + mv[13] * pj[ 5] + mv[14] * pj[ 9] + mv[15] * pj[13];
00039 clip[14] = mv[12] * pj[ 2] + mv[13] * pj[ 6] + mv[14] * pj[10] + mv[15] * pj[14];
00040 clip[15] = mv[12] * pj[ 3] + mv[13] * pj[ 7] + mv[14] * pj[11] + mv[15] * pj[15];
00041
00042
00043 frustum[0][0] = clip[ 3] - clip[ 0];
00044 frustum[0][1] = clip[ 7] - clip[ 4];
00045 frustum[0][2] = clip[11] - clip[ 8];
00046 frustum[0][3] = clip[15] - clip[12];
00047
00048
00049 t = sqrt( frustum[0][0] * frustum[0][0] + frustum[0][1] * frustum[0][1] + frustum[0][2] * frustum[0][2] );
00050 frustum[0][0] /= t;
00051 frustum[0][1] /= t;
00052 frustum[0][2] /= t;
00053 frustum[0][3] /= t;
00054
00055
00056 frustum[1][0] = clip[ 3] + clip[ 0];
00057 frustum[1][1] = clip[ 7] + clip[ 4];
00058 frustum[1][2] = clip[11] + clip[ 8];
00059 frustum[1][3] = clip[15] + clip[12];
00060
00061
00062 t = sqrt( frustum[1][0] * frustum[1][0] + frustum[1][1] * frustum[1][1] + frustum[1][2] * frustum[1][2] );
00063 frustum[1][0] /= t;
00064 frustum[1][1] /= t;
00065 frustum[1][2] /= t;
00066 frustum[1][3] /= t;
00067
00068
00069 frustum[2][0] = clip[ 3] + clip[ 1];
00070 frustum[2][1] = clip[ 7] + clip[ 5];
00071 frustum[2][2] = clip[11] + clip[ 9];
00072 frustum[2][3] = clip[15] + clip[13];
00073
00074
00075 t = sqrt( frustum[2][0] * frustum[2][0] + frustum[2][1] * frustum[2][1] + frustum[2][2] * frustum[2][2] );
00076 frustum[2][0] /= t;
00077 frustum[2][1] /= t;
00078 frustum[2][2] /= t;
00079 frustum[2][3] /= t;
00080
00081
00082 frustum[3][0] = clip[ 3] - clip[ 1];
00083 frustum[3][1] = clip[ 7] - clip[ 5];
00084 frustum[3][2] = clip[11] - clip[ 9];
00085 frustum[3][3] = clip[15] - clip[13];
00086
00087
00088 t = sqrt( frustum[3][0] * frustum[3][0] + frustum[3][1] * frustum[3][1] + frustum[3][2] * frustum[3][2] );
00089 frustum[3][0] /= t;
00090 frustum[3][1] /= t;
00091 frustum[3][2] /= t;
00092 frustum[3][3] /= t;
00093
00094
00095 frustum[4][0] = clip[ 3] - clip[ 2];
00096 frustum[4][1] = clip[ 7] - clip[ 6];
00097 frustum[4][2] = clip[11] - clip[10];
00098 frustum[4][3] = clip[15] - clip[14];
00099
00100
00101 t = sqrt( frustum[4][0] * frustum[4][0] + frustum[4][1] * frustum[4][1] + frustum[4][2] * frustum[4][2] );
00102 frustum[4][0] /= t;
00103 frustum[4][1] /= t;
00104 frustum[4][2] /= t;
00105 frustum[4][3] /= t;
00106
00107
00108 frustum[5][0] = clip[ 3] + clip[ 2];
00109 frustum[5][1] = clip[ 7] + clip[ 6];
00110 frustum[5][2] = clip[11] + clip[10];
00111 frustum[5][3] = clip[15] + clip[14];
00112
00113
00114 t = sqrt( frustum[5][0] * frustum[5][0] + frustum[5][1] * frustum[5][1] + frustum[5][2] * frustum[5][2] );
00115 frustum[5][0] /= t;
00116 frustum[5][1] /= t;
00117 frustum[5][2] /= t;
00118 frustum[5][3] /= t;
00119 }
00120
00121 bool tgFrustum::PointInFrustum( float x, float y, float z ){
00122 int p;
00123
00124 for( p = 0; p < 6; p++ ){
00125 if( frustum[p][0] * x + frustum[p][1] * y + frustum[p][2] * z + frustum[p][3] <= 0 )
00126 return false;
00127 }
00128
00129 return true;
00130 }
00131
00132 bool tgFrustum::SphereInFrustum( float x, float y, float z, float radius ){
00133 int p;
00134
00135 for( p = 0; p < 6; p++ ){
00136 if( frustum[p][0] * x + frustum[p][1] * y + frustum[p][2] * z + frustum[p][3] <= -radius )
00137 return false;
00138 }
00139 return true;
00140 }
00141
00142 void tgFrustum::DrawFrustum(){
00143
00144
00145 float depthRange[2];
00146 glGetFloatv(GL_DEPTH_RANGE, &depthRange[0]);
00147 float near = -depthRange[0];
00148 float far = -depthRange[1];
00149
00150
00151 const float nLeft = near * (m_intrinsic[2] - 1.0f) / m_intrinsic[0];
00152 const float nRight = near * (1.0f + m_intrinsic[2]) / m_intrinsic[0];
00153 const float nTop = near * (1.0f + m_intrinsic[6]) / m_intrinsic[5];
00154 const float nBottom = near * (m_intrinsic[6] - 1.0f) / m_intrinsic[5];
00155
00156
00157 const float fLeft = far * (m_intrinsic[2] - 1.0f) / m_intrinsic[0];
00158 const float fRight = far * (1.0f + m_intrinsic[2]) / m_intrinsic[0];
00159 const float fTop = far * (1.0f + m_intrinsic[6]) / m_intrinsic[5];
00160 const float fBottom = far * (m_intrinsic[6] - 1.0f) / m_intrinsic[5];
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 glDisable(GL_LIGHTING);
00174 glDisable(GL_TEXTURE_2D);
00175
00176
00177 glMatrixMode(GL_MODELVIEW);
00178
00179 glPushMatrix();
00180 glBegin(GL_LINES);
00181
00182 glVertex3f(nLeft, nBottom, -near);
00183 glVertex3f(fLeft, fBottom, -far);
00184
00185 glVertex3f(nRight, nBottom, -near);
00186 glVertex3f(fRight, fBottom, -far);
00187
00188 glVertex3f(nRight, nTop, -near);
00189 glVertex3f(fRight, fTop, -far);
00190
00191 glVertex3f(nLeft, nTop, -near);
00192 glVertex3f(fLeft, fTop, -far);
00193
00194
00195 glVertex3f(fLeft, fBottom, -far);
00196 glVertex3f(fRight, fBottom, -far);
00197
00198 glVertex3f(fRight, fTop, -far);
00199 glVertex3f(fLeft, fTop, -far);
00200
00201 glVertex3f(fRight, fTop, -far);
00202 glVertex3f(fRight, fBottom, -far);
00203
00204 glVertex3f(fLeft, fTop, -far);
00205 glVertex3f(fLeft, fBottom, -far);
00206
00207
00208 glVertex3f(nLeft, nBottom, -near);
00209 glVertex3f(nRight, nBottom, -near);
00210
00211 glVertex3f(nRight, nTop, -near);
00212 glVertex3f(nLeft, nTop, -near);
00213
00214 glVertex3f(nLeft, nTop, -near);
00215 glVertex3f(nLeft, nBottom, -near);
00216
00217 glVertex3f(nRight, nTop, -near);
00218 glVertex3f(nRight, nBottom, -near);
00219
00220 glEnd();
00221 glPopMatrix();
00222 }
00223