![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsRegularPolyhedra.cpp 00003 ******************************************************************************/ 00007 /****************************************************************************** 00008 SUKITTI PUNAK (09/09/2010) 00009 UPDATE (03/28/2011) 00010 ******************************************************************************/ 00011 #include "STAPsRegularPolyhedra.hpp" 00012 // Using Inclusion Model (i.e. definitions are included in declarations) 00013 // (this name.cpp is included in name.hpp) 00014 // Each friend is defined directly inside its declaration. 00015 00016 BEGIN_NAMESPACE_TAPs 00017 //============================================================================= 00018 // Constructors 00019 //----------------------------------------------------------------------------- 00020 template <typename T> 00021 RegularPolyhedra<T>::RegularPolyhedra () 00022 { 00023 if ( !Init() ) { 00024 exit( -1 ); 00025 } 00026 } 00027 //----------------------------------------------------------------------------- 00028 template <typename T> 00029 RegularPolyhedra<T>::~RegularPolyhedra () 00030 { 00031 Clear(); 00032 } 00033 //----------------------------------------------------------------------------- 00034 template <typename T> 00035 std::string RegularPolyhedra<T>::StrInfo () const 00036 { 00037 std::ostringstream ss; 00038 ss << "RegularPolyhedra"; 00039 ss << "\n"; 00040 return ss.str(); 00041 } 00042 //----------------------------------------------------------------------------- 00043 template <typename T> 00044 bool RegularPolyhedra<T>::Init () 00045 { 00046 // Tetrahedron -- center at the origin 00047 // 1 00048 // / \ 00049 // / y \ 00050 // / |/ \ 00051 // / O--x\ 00052 // / /z \ 00053 // / 0 \ 00054 // 2-------------3 00055 float h = 0.5f; 00056 float a = sqrt( 0.75f ); 00057 float t = a * 2.0f/3.0f; 00058 float b = a - t; 00059 00060 // Hexahedron -- center at the origin 00061 // 3-------------2 00062 // /| /| 00063 // / y / | 00064 // / | | / | 00065 // 7-------------6 | 00066 // | | O---x | | 00067 // | 0- / - - -|- -1 00068 // | / z | / 00069 // | | / 00070 // |/ |/ 00071 // 4-------------5 00072 00073 // Octahedron -- center at the origin 00074 // |y 00075 // | / 00076 // 4 / 00077 // /- 1 00078 // / |/ \ 00079 // ----2----0----0-------------->x 00080 // \ /| / 00081 // 3 -/ 00082 // / 5 00083 // / | 00084 // /z 00085 00086 const int faces[] = { 00087 00088 //* 00089 // Tetrahedron (4 faces -- 4 triangles -- CCW) 00090 0, 3, 1, 00091 0, 1, 2, 00092 0, 2, 3, 00093 3, 2, 1, 00094 //*/ 00095 00096 // Hexahedron (6 faces -- 12 triangles -- CCW) 00097 00098 4+3, 4+2, 4+0, 00099 4+0, 4+2, 4+1, 00100 4+7, 4+4, 4+6, 00101 4+6, 4+4, 4+5, 00102 4+0, 4+1, 4+4, 00103 4+4, 4+1, 4+5, 00104 4+3, 4+7, 4+2, 00105 4+2, 4+7, 4+6, 00106 4+0, 4+4, 4+3, 00107 4+3, 4+4, 4+7, 00108 4+2, 4+6, 4+1, 00109 4+1, 4+6, 4+5, 00110 00111 // Octahedron (8 faces -- 8 triangles -- CCW) 00112 12+4, 12+0, 12+1, 00113 12+4, 12+1, 12+2, 00114 12+4, 12+2, 12+3, 00115 12+4, 12+3, 12+0, 00116 12+5, 12+1, 12+0, 00117 12+5, 12+2, 12+1, 00118 12+5, 12+3, 12+2, 00119 12+5, 12+0, 12+3, 00120 }; 00121 00122 const float vertices[] = { 00123 00124 //* 00125 // Tetrahedron (4 vertices, normals, colors) 00126 0, 0, h, 0, 0, h, 1, 1, 1, 1, 00127 0, t, -b, 0, t, -b, 1, 0, 0, 1, 00128 -h, -b, -b, -h, -b, -b, 0, 1, 0, 1, 00129 h, -b, -b, h, -b, -b, 0, 0, 1, 1, 00130 //*/ 00131 00132 // Hexahedron (8 vertices) 00133 -h, -h, -h, -h, -h, -h, 1, 0, 0, 1, 00134 h, -h, -h, h, -h, -h, 0, 1, 0, 1, 00135 h, h, -h, h, h, -h, 0, 0, 1, 1, 00136 -h, h, -h, -h, h, -h, 1, 1, 0, 1, 00137 -h, -h, h, -h, -h, h, 0, 1, 1, 1, 00138 h, -h, h, h, -h, h, 1, 0, 1, 1, 00139 h, h, h, h, h, h, .5,.5,.5,1, 00140 -h, h, h, -h, h, h, 1, 1, 1, 1, 00141 00142 // Octahedron (6 vertices) 00143 h, 0, 0, h, 0, 0, 1, 0, 0, 1, 00144 0, 0, -h, 0, 0, -h, 0, 0, .5,1, 00145 -h, 0, 0, -h, 0, 0, .5,0, 0, 1, 00146 0, 0, h, 0, 0, h, 0, 0, 1, 1, 00147 0, h, 0, 0, h, 0, 0, 1, 0, 1, 00148 0, -h, 0, 0, -h, 0, 0, .5,0, 1, 00149 }; 00150 00151 //* 00152 std::cout << "sizeof(faces): " << sizeof(faces) << "\n"; 00153 std::cout << "sizeof(int): " << sizeof(int) << "\n"; 00154 m__pFacesSize = sizeof(faces)/sizeof(int); 00155 m__pVerticesSize = sizeof(vertices)/sizeof(float); 00156 m__pFaces = new int[ m__pFacesSize ]; 00157 m__pVertices = new float[ m__pVerticesSize ]; 00158 memcpy( m__pFaces, faces, sizeof(faces) ); 00159 memcpy( m__pVertices, vertices, sizeof(vertices) ); 00160 00161 std::cout << "m__pFacesSize: " << m__pFacesSize << "\n"; 00162 //*/ 00163 00164 #if defined(__gl_h_) || defined(__GL_H__) 00165 if ( InitGL( faces, sizeof(faces), vertices, sizeof(vertices) ) ) { 00166 if ( InitGLSL() ) { 00167 return true; 00168 } 00169 else { 00170 Clear(); 00171 return false; 00172 } 00173 } 00174 else { 00175 Clear(); 00176 return false; 00177 } 00178 #endif//defined(__gl_h_) || defined(__GL_H__) 00179 } 00180 //----------------------------------------------------------------------------- 00181 template <typename T> 00182 void RegularPolyhedra<T>::Clear () 00183 { 00184 #if defined(__gl_h_) || defined(__GL_H__) 00185 ClearGL(); 00186 #endif//defined(__gl_h_) || defined(__GL_H__) 00187 } 00188 //----------------------------------------------------------------------------- 00189 //============================================================================= 00190 00191 00192 //============================================================================= 00193 // OpenGL 00194 #if defined(__gl_h_) || defined(__GL_H__) 00195 //----------------------------------------------------------------------------- 00196 template <typename T> 00197 void RegularPolyhedra<T>::Draw () 00198 { 00199 static T angle; 00200 angle += 0.01; 00201 if ( angle > 360 ) angle = 0; 00202 m_Trx.RefToMatrixTransform() = m_Trx.RefToMatrixTransform().CreateRotation( Vector3<T>(1,1,1), angle ); 00203 00204 Matrix4x4<T> Mscale = CGMath<T>::MatrixScale( Vector3<T>(0.5,0.25,0.25) ); 00205 00206 static char* offsetToHexahedronData = (char*)NULL + sizeof(GLuint)*(4)*3; 00207 static char* offsetToOctahedronData = (char*)NULL + sizeof(GLuint)*(4+12)*3; 00208 00209 00210 GLfloat LD[3] = { 0, 0, -1 }; 00211 00212 00213 //* 00214 // Draw tetrahedron 00215 00216 Matrix4x4<T> mat; 00217 00218 mat = CGMath<T>::MatrixTranslation(Vector3<T>(-0.25,-0.25,0.0)) * m_Trx.GetMatrixTransform() * Mscale; 00219 glBindBuffer( GL_UNIFORM_BUFFER, m_Uniform_Block_BO ); 00220 glBufferSubData( GL_UNIFORM_BUFFER, 0, sizeof(GLfloat)*16, mat.GetDataFloat() ); 00221 glBindBuffer( GL_UNIFORM_BUFFER, 0 ); 00222 00223 GL().glslProgObj->BeginGLSL(); 00224 //glUniformMatrix4fv( m_Uniform_MVP, 1, GL_TRUE, ( CGMath<T>::MatrixTranslation(Vector3<T>(-0.25,-0.25,0.0)) * m_Trx.GetMatrixTransform() * Mscale ).GetDataFloat() ); 00225 glUniformMatrix4fv( m_ModelViewMatrix_Uniform, 1, GL_TRUE, ( CGMath<T>::MatrixTranslation(Vector3<T>(-0.25,-0.25,0.0)) * m_Trx.GetMatrixTransform() * Mscale ).GetDataFloat() ); 00226 00227 glUniform3fv( m_Uniform_LightDir, 1, LD ); 00228 00230 //GLfloat TestLD[3]; 00231 //GLint loc = glGetUniformLocation( GL().glslProgObj->GetProgramObject(), "LightDirection" ); 00232 //glGetUniformfv( GL().glslProgObj->GetProgramObject(), loc, TestLD ); 00233 //std::cout << "TestLD: " << TestLD[0] << "\n"; 00234 //std::cout << "TestLD: " << TestLD[1] << "\n"; 00235 //std::cout << "TestLD: " << TestLD[2] << "\n"; 00236 00237 glBindVertexArray( mGL_VAO ); 00238 glDrawElements( GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0 ); 00239 //glDrawArrays( GL_POINTS, 0, 4 ); 00240 glBindVertexArray( 0 ); 00241 GL().glslProgObj->EndGLSL(); 00242 //*/ 00243 00244 //* 00245 // Draw hexahedron 00246 00247 mat = CGMath<T>::MatrixTranslation(Vector3<T>(0,0,0)) * m_Trx.GetMatrixTransform() * Mscale; 00248 glBindBuffer( GL_UNIFORM_BUFFER, m_Uniform_Block_BO ); 00249 glBufferSubData( GL_UNIFORM_BUFFER, 0, sizeof(GLfloat)*16, mat.GetDataFloat() ); 00250 glBindBuffer( GL_UNIFORM_BUFFER, 0 ); 00251 00252 GL().glslProgObj->BeginGLSL(); 00253 //glUniformMatrix4fv( m_Uniform_MVP, 1, GL_TRUE, ( CGMath<T>::MatrixTranslation(Vector3<T>(0,0,0)) * m_Trx.GetMatrixTransform() * Mscale ).GetDataFloat() ); 00254 glUniformMatrix4fv( m_ModelViewMatrix_Uniform, 1, GL_TRUE, ( CGMath<T>::MatrixTranslation(Vector3<T>(0,0,0)) * m_Trx.GetMatrixTransform() * Mscale ).GetDataFloat() ); 00255 00256 glUniform3fv( m_Uniform_LightDir, 1, LD ); 00257 00258 glBindVertexArray( mGL_VAO ); 00259 glDrawElements( GL_TRIANGLES, 36, GL_UNSIGNED_INT, offsetToHexahedronData ); 00260 //glDrawElementsBaseVertex( GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0, 0 ); 00261 //glDrawArrays( GL_POINTS, 0, 4 ); 00262 glBindVertexArray( 0 ); 00263 GL().glslProgObj->EndGLSL(); 00264 //*/ 00265 00266 //* 00267 // Draw octahedron 00268 00269 mat = CGMath<T>::MatrixTranslation(Vector3<T>(0.25,0.25,0.0)) * m_Trx.GetMatrixTransform() * Mscale; 00270 glBindBuffer( GL_UNIFORM_BUFFER, m_Uniform_Block_BO ); 00271 glBufferSubData( GL_UNIFORM_BUFFER, 0, sizeof(GLfloat)*16, mat.GetDataFloat() ); 00272 glBindBuffer( GL_UNIFORM_BUFFER, 0 ); 00273 00274 GL().glslProgObj->BeginGLSL(); 00275 //glUniformMatrix4fv( m_Uniform_MVP, 1, GL_TRUE, ( CGMath<T>::MatrixTranslation(Vector3<T>(0.25,0.25,0.0)) * m_Trx.GetMatrixTransform() * Mscale ).GetDataFloat() ); 00276 glUniformMatrix4fv( m_ModelViewMatrix_Uniform, 1, GL_TRUE, ( CGMath<T>::MatrixTranslation(Vector3<T>(0.25,0.25,0.0)) * m_Trx.GetMatrixTransform() * Mscale ).GetDataFloat() ); 00277 00278 glUniform3fv( m_Uniform_LightDir, 1, LD ); 00279 00280 glBindVertexArray( mGL_VAO ); 00281 glDrawElements( GL_TRIANGLES, 24, GL_UNSIGNED_INT, offsetToOctahedronData ); 00282 //glDrawElementsBaseVertex( GL_TRIANGLES, 24, GL_UNSIGNED_INT, 0, 12 ); 00283 //glDrawArrays( GL_POINTS, 0, 4 ); 00284 glBindVertexArray( 0 ); 00285 GL().glslProgObj->EndGLSL(); 00286 00287 //*/ 00288 00289 00290 00291 //* 00292 // Draw hexahedron 00293 00294 mat = CGMath<T>::MatrixTranslation(Vector3<T>(-0.5,-0.25,0.0)) * m_Trx.GetMatrixTransform() * Mscale; 00295 glBindBuffer( GL_UNIFORM_BUFFER, m_Uniform_Block_BO ); 00296 glBufferSubData( GL_UNIFORM_BUFFER, 0, sizeof(GLfloat)*16, mat.GetDataFloat() ); 00297 glBindBuffer( GL_UNIFORM_BUFFER, 0 ); 00298 00299 GL().glslProgObj->BeginGLSL(); 00300 //glUniformMatrix4fv( m_Uniform_MVP, 1, GL_TRUE, ( CGMath<T>::MatrixTranslation(Vector3<T>(-0.5,-0.25,0.0)) * m_Trx.GetMatrixTransform() * Mscale ).GetDataFloat() ); 00301 glUniformMatrix4fv( m_ModelViewMatrix_Uniform, 1, GL_TRUE, ( CGMath<T>::MatrixTranslation(Vector3<T>(-0.5,-0.25,0.0)) * m_Trx.GetMatrixTransform() * Mscale ).GetDataFloat() ); 00302 00303 glUniform3fv( m_Uniform_LightDir, 1, LD ); 00304 00305 //glBindVertexArray( mGL_VAO ); 00306 //glDrawElements( GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0 ); 00307 00308 GLsizei stride = 3 * sizeof( float ); 00309 GLsizei vStride = 10 * sizeof( float ); 00310 mGL_VertexAttribIdx_Positions = 0; 00311 mGL_VertexAttribIdx_Normals = 1; 00312 mGL_VertexAttribIdx_Colors = 2; 00313 glBindBuffer( GL_ARRAY_BUFFER, mGL_VBO_Positions ); 00314 glEnableVertexAttribArray( mGL_VertexAttribIdx_Positions ); 00315 glVertexAttribPointer( mGL_VertexAttribIdx_Positions, 3, GL_FLOAT, GL_FALSE, vStride, (char *)NULL + 0 ); 00316 glEnableVertexAttribArray( mGL_VertexAttribIdx_Normals ); 00317 glVertexAttribPointer( mGL_VertexAttribIdx_Normals, 3, GL_FLOAT, GL_FALSE, vStride, (char *)NULL + stride ); 00318 glEnableVertexAttribArray( mGL_VertexAttribIdx_Colors ); 00319 glVertexAttribPointer( mGL_VertexAttribIdx_Colors, 4, GL_FLOAT, GL_FALSE, vStride, (char *)NULL + stride*2 ); 00320 00321 glDrawElements( GL_TRIANGLES, 36, GL_UNSIGNED_INT, m__pFaces + 4*3 ); 00322 //glDrawElementsBaseVertex( GL_TRIANGLES, 36, GL_UNSIGNED_INT, m__pFaces + 4*3, 0 ); 00323 00324 00325 //glDrawArrays( GL_POINTS, 0, 4 ); 00326 00327 static bool first = true; 00328 if ( first ) { 00329 for ( int i = 0; i < m__pFacesSize; ++i ) { 00330 std::cout << m__pFaces[i] << " "; 00331 } 00332 first = false; 00333 } 00334 GL().glslProgObj->EndGLSL(); 00335 //*/ 00336 } 00337 //----------------------------------------------------------------------------- 00338 template <typename T> 00339 void RegularPolyhedra<T>::DrawTetrahedron ( Matrix4x4<T> MVP, Vector3<T> LightDirection ) 00340 { 00341 static char* offsetToTetrahedron = (char*)NULL + sizeof(GLuint)*0; 00342 // Draw tetrahedron 00343 GL().glslProgObj->BeginGLSL(); 00344 //glUniformMatrix4fv( m_Uniform_MVP, 1, GL_TRUE, MVP.GetDataFloat() ); 00345 glUniformMatrix4fv( m_ModelViewMatrix_Uniform, 1, GL_TRUE, MVP.GetDataFloat() ); 00346 glUniform3fv( m_Uniform_LightDir, 1, LightDirection.GetDataFloat() ); 00347 glBindVertexArray( mGL_VAO ); 00348 glDrawElements( GL_TRIANGLES, 12, GL_UNSIGNED_INT, offsetToTetrahedron ); 00349 glBindVertexArray( 0 ); 00350 GL().glslProgObj->EndGLSL(); 00351 } 00352 //----------------------------------------------------------------------------- 00353 template <typename T> 00354 void RegularPolyhedra<T>::DrawHexahedron ( Matrix4x4<T> MVP, Vector3<T> LightDirection ) 00355 { 00356 static char* offsetToHexahedronData = (char*)NULL + sizeof(GLuint)*(4)*3; 00357 // Draw hexahedron 00358 GL().glslProgObj->BeginGLSL(); 00359 //glUniformMatrix4fv( m_Uniform_MVP, 1, GL_TRUE, MVP.GetDataFloat() ); 00360 glUniformMatrix4fv( m_ModelViewMatrix_Uniform, 1, GL_TRUE, MVP.GetDataFloat() ); 00361 glUniform3fv( m_Uniform_LightDir, 1, LightDirection.GetDataFloat() ); 00362 glBindVertexArray( mGL_VAO ); 00363 glDrawElements( GL_TRIANGLES, 36, GL_UNSIGNED_INT, offsetToHexahedronData ); 00364 glBindVertexArray( 0 ); 00365 GL().glslProgObj->EndGLSL(); 00366 } 00367 //----------------------------------------------------------------------------- 00368 template <typename T> 00369 void RegularPolyhedra<T>::DrawOctahedron ( Matrix4x4<T> MVP, Vector3<T> LightDirection ) 00370 { 00371 static char* offsetToOctahedronData = (char*)NULL + sizeof(GLuint)*(4+12)*3; 00372 // Draw octahedron 00373 GL().glslProgObj->BeginGLSL(); 00374 //glUniformMatrix4fv( m_Uniform_MVP, 1, GL_TRUE, MVP.GetDataFloat() ); 00375 glUniformMatrix4fv( m_ModelViewMatrix_Uniform, 1, GL_TRUE, MVP.GetDataFloat() ); 00376 glUniform3fv( m_Uniform_LightDir, 1, LightDirection.GetDataFloat() ); 00377 glBindVertexArray( mGL_VAO ); 00378 glDrawElements( GL_TRIANGLES, 24, GL_UNSIGNED_INT, offsetToOctahedronData ); 00379 glBindVertexArray( 0 ); 00380 GL().glslProgObj->EndGLSL(); 00381 } 00382 //----------------------------------------------------------------------------- 00383 template <typename T> 00384 bool RegularPolyhedra<T>::InitGL ( 00385 const int faces[], int sizeOfFaces, 00386 const float vertices[], int sizeOfVertices 00387 ) 00388 { 00389 // Initial values 00390 mGL_VAO = 0; 00391 mGL_VBO_Positions = 0; 00392 mGL_VBO_Normals = 0; 00393 mGL_VBO_Colors = 0; 00394 mGL_VBO_Indices = 0; 00395 mGL_VertexAttribIdx_Positions = 0; 00396 mGL_VertexAttribIdx_Normals = 1; 00397 mGL_VertexAttribIdx_Colors = 2; 00398 //m_Uniform_MVP = 0; 00399 m_Uniform_LightDir = 0; 00400 00401 m_ModelViewMatrix_Uniform = 0; 00402 g_BindingIndex = 1; 00403 00404 00405 // Create the vertex buffer object for positions 00406 GLsizei stride = 3 * sizeof( float ); 00407 glGenBuffers( 1, &mGL_VBO_Positions ); 00408 glBindBuffer( GL_ARRAY_BUFFER, mGL_VBO_Positions ); 00409 glBufferData( GL_ARRAY_BUFFER, sizeOfVertices, vertices, GL_STATIC_DRAW ); 00410 //glBindBuffer( GL_ARRAY_BUFFER, 0 ); 00411 00412 // Create the vertex buffer object for indices 00413 glGenBuffers( 1, &mGL_VBO_Indices ); 00414 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mGL_VBO_Indices ); 00415 glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeOfFaces, faces, GL_STATIC_DRAW ); 00416 //glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); 00417 00418 // Create the vertex array object 00419 GLsizei vStride = 10 * sizeof( float ); 00420 glGenVertexArrays( 1, &mGL_VAO ); 00421 glBindVertexArray( mGL_VAO ); 00422 glBindBuffer( GL_ARRAY_BUFFER, mGL_VBO_Positions ); 00423 glEnableVertexAttribArray( mGL_VertexAttribIdx_Positions ); 00424 glVertexAttribPointer( mGL_VertexAttribIdx_Positions, 3, GL_FLOAT, GL_FALSE, vStride, (char *)NULL + 0 ); 00425 glEnableVertexAttribArray( mGL_VertexAttribIdx_Normals ); 00426 glVertexAttribPointer( mGL_VertexAttribIdx_Normals, 3, GL_FLOAT, GL_FALSE, vStride, (char *)NULL + stride ); 00427 glEnableVertexAttribArray( mGL_VertexAttribIdx_Colors ); 00428 glVertexAttribPointer( mGL_VertexAttribIdx_Colors, 4, GL_FLOAT, GL_FALSE, vStride, (char *)NULL + stride*2 ); 00429 glBindBuffer( GL_ARRAY_BUFFER, 0 ); 00430 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mGL_VBO_Indices ); 00431 glBindVertexArray( 0 ); 00432 00433 // Clear bindings 00434 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); 00435 00436 // Create uniform block buffer 00437 glGenBuffers( 1, &m_Uniform_Block_BO ); 00438 glBindBuffer( GL_UNIFORM_BUFFER, m_Uniform_Block_BO ); 00439 glBufferData( GL_UNIFORM_BUFFER, sizeof(GLSL_S_Transform), NULL, GL_STREAM_DRAW ); 00440 glBindBuffer( GL_UNIFORM_BUFFER, 0 ); 00441 00442 // DEBUG 00443 //std::cout << "m_Uniform_Block_BO: " << m_Uniform_Block_BO << "\n"; 00444 //std::cout << "sizeof(GLSL_S_Transform): " << sizeof(GLSL_S_Transform) << "\n"; 00445 //std::cout << "sizeof(GLfloat)*16: " << sizeof(GLfloat)*16 << "\n"; 00446 00447 // DEBUG 00448 //std::cout << "mGL_VAO: " << mGL_VAO << "\n"; 00449 //std::cout << "mGL_VBO_Positions: " << mGL_VBO_Positions << "\n"; 00450 //std::cout << "mGL_VBO_Indices: " << mGL_VBO_Indices << "\n"; 00451 //std::cout << "mGL_VertexAttribIdx: " << mGL_VertexAttribIdx << "\n"; 00452 00453 return true; 00454 } 00455 //----------------------------------------------------------------------------- 00456 template <typename T> 00457 bool RegularPolyhedra<T>::InitGLSL () 00458 { 00459 if ( GL().GetVersionMajorNumber() < 3 ) { 00460 return false; 00461 } 00462 00463 //bool result = GL().InitShaders( "Minimal", true, false, true ); 00464 //bool result = GL().InitShaders( "FlatColor", true, false, true ); 00465 //bool result = GL().InitShaders( "Color", true, false, true ); 00466 bool result = GL().InitShaders( "DirectionalLight", true, false, true ); 00467 00468 if ( result ) { 00469 00470 //m_Uniform_MVP = glGetUniformLocation( GL().glslProgObj->GetProgramObject(), "MVP" ); 00471 m_ModelViewMatrix_Uniform = glGetUniformLocation( GL().glslProgObj->GetProgramObject(), "ModelViewMatrix" ); 00472 m_Uniform_LightDir = glGetUniformLocation( GL().glslProgObj->GetProgramObject(), "LightDirection" ); 00473 00474 // Bind the uniform block buffer 00475 GLint idx = glGetUniformBlockIndex( GL().glslProgObj->GetProgramObject(), "Transform" ); 00476 glUniformBlockBinding( GL().glslProgObj->GetProgramObject(), idx, g_BindingIndex ); 00477 glBindBufferRange( GL_UNIFORM_BUFFER, g_BindingIndex, m_Uniform_Block_BO, 0, sizeof(GLSL_S_Transform) ); 00478 OpenGL::Fn::CHECK_GL_ERROR(); 00479 00480 00481 00482 00483 int pos = glGetAttribLocation( GL().glslProgObj->GetProgramObject(), "Position" ); 00484 int nor = glGetAttribLocation( GL().glslProgObj->GetProgramObject(), "Normal" ); 00485 int col = glGetAttribLocation( GL().glslProgObj->GetProgramObject(), "Color" ); 00486 00487 std::cout << "pos: " << pos << "\n"; 00488 std::cout << "col: " << col << "\n"; 00489 std::cout << "nor: " << nor << "\n"; 00490 } 00491 00492 return result; 00493 } 00494 //----------------------------------------------------------------------------- 00495 template <typename T> 00496 void RegularPolyhedra<T>::ClearGL () 00497 { 00498 // 00499 // Clear buffers 00500 // 00501 if ( mGL_VAO != 0 ) { 00502 glDeleteVertexArrays( 1, &mGL_VAO ); 00503 mGL_VAO = 0; 00504 glBindVertexArray( 0 ); 00505 } 00506 if ( mGL_VBO_Positions != 0 ) { 00507 glDeleteBuffers( 1, &mGL_VBO_Positions ); 00508 mGL_VBO_Positions = 0; 00509 glBindBuffer( GL_ARRAY_BUFFER, 0 ); 00510 } 00511 if ( mGL_VBO_Colors != 0 ) { 00512 glDeleteBuffers( 1, &mGL_VBO_Colors ); 00513 mGL_VBO_Colors = 0; 00514 glBindBuffer( GL_ARRAY_BUFFER, 0 ); 00515 } 00516 if ( mGL_VBO_Normals != 0 ) { 00517 glDeleteBuffers( 1, &mGL_VBO_Normals ); 00518 mGL_VBO_Normals = 0; 00519 glBindBuffer( GL_ARRAY_BUFFER, 0 ); 00520 } 00521 if ( mGL_VBO_Indices != 0 ) { 00522 glDeleteBuffers( 1, &mGL_VBO_Indices ); 00523 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); 00524 } 00525 00526 // uniform block buffer 00527 if ( m_Uniform_Block_BO != 0 ) { 00528 glDeleteBuffers( 1, &m_Uniform_Block_BO ); 00529 glBindBuffer( GL_UNIFORM_BUFFER, 0 ); 00530 } 00531 00532 } 00533 //----------------------------------------------------------------------------- 00534 #endif // OpenGL 00535 //============================================================================= 00536 00537 00538 //============================================================================= 00539 END_NAMESPACE_TAPs 00540 //34567890123456789012345678901234567890123456789012345678901234567890123456789 00541 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----