Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

MarchingCubesData.cpp

Go to the documentation of this file.
00001 
00015 #include "MarchingCubesData.h"
00016 
00017 // Actual Level: 6
00018 #define DEBUG_MODE 99
00019 //#define DEBUG_GL
00020 //#define DEBUG_CONST
00021 //#define DEBUG_DEST
00022 #include "General/vuMarchingCubes/debug.h"
00023 
00024 #include "MarchingCubesGeneral.h"
00025 
00026 
00027 // {{{ struct MCMech::MCData
00028 
00029 inline void MCMesh::MCData::copyNormalFrom(const float *n) // {{{
00030 {
00031         DEBUG0("inline void MCMesh::MCData::copyNormalFrom(float *n)\n");
00032         memcpy((void*)normal, (void*)n, sizeof(float)*3);
00033 } // }}} inline void MCMesh::MCData::copyNormalFrom(float *n)
00034 
00035 inline void MCMesh::MCData::copyVertexFrom(const float *v) // {{{
00036 {
00037         DEBUG0("inline void MCMesh::MCData::copyVertexFrom(float *v)\n");
00038         memcpy((void*)vertex, (void*)v, sizeof(float)*3);
00039 } // }}} inline void MCMesh::MCData::copyVertexFrom(float *v)
00040 
00041 // }}} MCMech::MCData
00042 
00043 
00044 // {{{ class MCMesh::MCVertex
00045 
00046 MCMesh::MCVertex::MCVertex(int idx) // {{{
00047 : _idx(idx),
00048   _indexed(false)
00049 {
00050         DEBUGC("MCMesh::MCVertex::MCVertex(int idx="<<idx<<")\n");
00051 } // }}} MCMesh::MCVertex::MCVertex(int idx)
00052 
00053 MCMesh::MCVertex::~MCVertex() // {{{
00054 {
00055         DEBUGD("virtual MCMesh::MCVertex::~MCVertex()\n");
00056 } // }}} MCMesh::MCVertex::~MCVertex()
00057 
00059 void MCMesh::MCVertex::AddNeigbour(MCVertex *v) // {{{
00060 {
00061         DEBUG0("void MCMesh::MCVertex::AddNeigbour(MCVertex *v)\n");
00062         assert(v);      // We do not want to push back a NULL pointer :)
00063         _neighbours.push_back(v);
00064 } // }}} void MCMesh::MCVertex::AddNeigbour(MCVertex *v)
00065 
00066 void MCMesh::MCVertex::Vertex(vuVector &v) // {{{
00067 {
00068         DEBUG0("void MCMesh::MCVertex::Vertex(vuVector &v)\n");
00069         _vertex = v;
00070 } // }}} void MCMesh::MCVertex::Vertex(vuVector &v)
00071 
00072 vuVector& MCMesh::MCVertex::Vertex() // {{{
00073 {
00074         DEBUG0("vuVector MCMesh::MCVertex::Vertex()\n");
00075         return _vertex;
00076 } // }}} vuVector MCMesh::MCVertex::Vertex()
00077 
00078 void MCMesh::MCVertex::Idx(MCMesh::index_t idx) // {{{
00079 {
00080         DEBUG0("void MCMesh::MCVertex::Idx(MCMesh::index_t idx)\n");
00081         _idx = idx;
00082         _indexed = true;
00083 } // }}} void MCMesh::MCVertex::Idx(MCMesh::index_t idx)
00084 
00085 MCMesh::index_t MCMesh::MCVertex::Idx() // {{{
00086 {
00087         DEBUG0("MCMesh::index_t MCMesh::MCVertex::Idx()\n");
00088         return _idx;
00089 } // }}} MCMesh::index_t MCMesh::MCVertex::Idx()
00090 
00091 bool MCMesh::MCVertex::Indexed() // {{{
00092 {
00093         DEBUG0("bool MCMesh::MCVertex::Indexed()\n");
00094         return _indexed;
00095 } // }}} bool MCMesh::MCVertex::Indexed()
00096 
00097 void MCMesh::MCVertex::resetIndexing() // {{{
00098 {
00099         DEBUG0("void MCMesh::MCVertex::resetIndexing()\n");
00100         _indexed = false;
00101 } // }}} void MCMesh::MCVertex::resetIndexing()
00102 
00103 // }}} MCMesh::MCVertex
00104 
00105 
00106 // {{{ class MCMesh::MCTriangle
00107 
00108 MCMesh::MCTriangle::MCTriangle(int type) // {{{
00109 : _type(type),
00110   _vertCnt(0)
00111 {
00112         DEBUGC("MCMesh::MCTriangle::MCTriangle()\n");
00113         _vertices[0] = _vertices[1] = _vertices[2] = 0;
00114 } // }}} MCMesh::MCTriangle::MCTriangle()
00115 
00116 MCMesh::MCTriangle::~MCTriangle() // {{{
00117 {
00118         DEBUGD("virtual MCMesh::MCTriangle::~MCTriangle()\n");
00119 } // }}} MCMesh::MCTriangle::~MCTriangle()
00120 
00121 int MCMesh::MCTriangle::AddVertex(MCVertex *v) // {{{
00122 {
00123         DEBUG0("int MCMesh::MCTriangle::AddVertex(MCVertex *v)\n");
00124         if (3==_vertCnt) return -1;
00125         _vertices[_vertCnt] = v;
00126         //DEBUG5("Added vertex "<<i<<"\n");
00127         if (2 == _vertCnt) {
00129                 _vertices[0]->AddNeigbour(_vertices[1]);
00130                 _vertices[0]->AddNeigbour(_vertices[2]);
00131                 _vertices[1]->AddNeigbour(_vertices[0]);
00132                 _vertices[1]->AddNeigbour(_vertices[2]);
00133                 _vertices[2]->AddNeigbour(_vertices[0]);
00134                 _vertices[2]->AddNeigbour(_vertices[1]);
00135         }
00136         return _vertCnt++;
00137 } // }}} int MCMesh::MCTriangle::AddVertex(MCVertex *v)
00138 
00139 MCMesh::MCVertex* MCMesh::MCTriangle::GetVertex(int n) // {{{
00140 {
00141         DEBUG0("MCMesh::MCVertex* MCMesh::MCTriangle::GetVertex(int n)\n");
00142         assert(n >= 0 && n < 3);
00143         return _vertices[n];
00144 } // }}} MCMesh::MCVertex* MCMesh::MCTriangle::GetVertex(int n)
00145 
00147 vuVector MCMesh::MCTriangle::GetNormal() // {{{
00148 {
00149         DEBUG0("vuVector MCMesh::MCTriangle::GetNormal(int n)\n");
00150         assert(Complete());
00151         vuVector v0 = _vertices[1]->Vertex() - _vertices[0]->Vertex();
00152         vuVector v1 = _vertices[1]->Vertex() - _vertices[2]->Vertex();
00153         v0.makeUnit(); v1.makeUnit();
00154         //return v0.cross(v1).makeUnit();
00155         return v1.cross(v0).makeUnit();
00156 } // }}} vuVector MCMesh::MCTriangle::GetNormal()
00157 
00158 bool MCMesh::MCTriangle::Complete() // {{{
00159 {
00160         DEBUG0("bool MCMesh::MCTriangle::Complete()\n");
00161         return _vertCnt == 3;
00162 } // }}} bool MCMesh::MCTriangle::Complete()
00163 
00164 MCMesh::index_t MCMesh::MCTriangle::GetVertexIdx(int v) // {{{
00165 {
00166         DEBUG0("MCMesh::index_t MCMesh::MCTriangle::GetVertexIdx(int v)\n");
00167         assert(v < 3);
00168         assert(_vertices[v] != 0);
00169         return _vertices[v]->Idx();
00170 } // }}} MCMesh::index_t MCMesh::MCTriangle::GetVertexIdx(int v)
00171 
00172 int MCMesh::MCTriangle::Type() // {{{
00173 {
00174         DEBUG0("int MCMesh::MCTriangle::Type()\n");
00175         return _type;
00176 } // }}} int MCMesh::MCTriangle::Type()
00177 
00178 // }}} MCMesh::MCTriangle
00179 
00180 
00181 // {{{ class MCMesh
00182 
00183 const int MCMesh::_edgeMapping[12][3][4] = { // {{{
00184         { //  0:   x   y   z  edge         {{{
00185                 {  0,  0, -1,  4 },
00186                 {  0, -1, -1,  6 },
00187                 {  0, -1,  0,  2 }
00188         },                              // }}}
00189         { //  1:   x   y   z  edge         {{{
00190                 {  0,  0, -1,  5 },
00191                 {  1,  0, -1,  7 },
00192                 {  1,  0,  0,  3 }
00193         },                              // }}}
00194         { //  2:   x   y   z  edge         {{{
00195                 {  0,  1,  0,  0 },
00196                 {  0,  1, -1,  4 },
00197                 {  0,  0, -1,  6 }
00198         },                              // }}}
00199         { //  3:   x   y   z  edge         {{{
00200                 { -1,  0,  0,  1 },
00201                 { -1,  0, -1,  5 },
00202                 {  0,  0, -1,  7 }
00203         },                              // }}}
00204         { //  4:   x   y   z  edge         {{{
00205                 {  0, -1,  0,  6 },
00206                 {  0, -1,  1,  2 },
00207                 {  0,  0,  1,  0 }
00208         },                              // }}}
00209         { //  5:   x   y   z  edge         {{{
00210                 {  1,  0,  0,  7 },
00211                 {  1,  0,  1,  3 },
00212                 {  0,  0,  1,  1 }
00213         },                              // }}}
00214         { //  6:   x   y   z  edge         {{{
00215                 {  0,  0,  1,  2 },
00216                 {  0,  1,  1,  0 },
00217                 {  0,  1,  0,  4 }
00218         },                              // }}}
00219         { //  7:   x   y   z  edge         {{{
00220                 {  0,  0,  1,  3 },
00221                 { -1,  0,  1,  1 },
00222                 { -1,  0,  0,  5 }
00223         },                              // }}}
00224         { //  8:   x   y   z  edge         {{{
00225                 {  0, -1,  0, 11 },
00226                 { -1, -1,  0, 10 },
00227                 { -1,  0,  0,  9 }
00228         },                              // }}}
00229         { //  9:   x   y   z  edge         {{{
00230                 {  1,  0,  0,  8 },
00231                 {  1, -1,  0, 11 },
00232                 {  0, -1,  0, 10 }
00233         },                              // }}}
00234         { // 10:   x   y   z  edge         {{{
00235                 {  0,  1,  0,  9 },
00236                 {  1,  1,  0,  8 },
00237                 {  1,  0,  0, 11 }
00238         },                              // }}}
00239         { // 11:   x   y   z  edge         {{{
00240                 { -1,  0,  0, 10 },
00241                 { -1,  1,  0,  9 },
00242                 {  0,  1,  0,  8 }
00243         }                               // }}}
00244 }; // }}}
00245 
00246 MCMesh::MCMesh(MCGlobalData *gd, MCBProgressCallback *cb) // {{{
00247 : _data(0),
00248   _dataSize(0),
00249   _normalsArray(0),
00250   _normalsArraySize(0),
00251   _normalsPrepared(false),
00252   _gd(gd),
00253   _progCB(cb)
00254 {
00255         DEBUGC("MCMesh::MCMesh(MCGlobalData *gd, MCBProgressCallback *cb)\n");
00256         assert(gd != NULL);
00257         glDisableClientState(GL_VERTEX_ARRAY);
00258         glDisableClientState(GL_NORMAL_ARRAY);
00259         glDisableClientState(GL_COLOR_ARRAY);
00260         _gd->compileNormals = true;
00261         for(int i = 0; i < 256; ++i) {
00262                 _indices[i] = 0;
00263                 _indexSize[i] = 0;
00264         }
00265         _vertexSize = sizeof(MCVertex) + 2*sizeof(MCVertex*);
00266         _triangleSize = sizeof(MCTriangle) + sizeof(MCTriangle*);
00267         _gd->meshSize = 0;
00268 } // }}} MCMesh::MCMesh(MCGlobalData *gd, MCBProgressCallback *cb)
00269 
00270 MCMesh::~MCMesh() // {{{
00271 {
00272         DEBUGD("virtual MCMesh::~MCMesh()\n");
00273         for(std::list<MCVertex *>::iterator i = _vertexList.begin(); i!=_vertexList.end(); ++i)
00274                         delete *i;
00275         for(unsigned int i = 0; i < _triangles.size(); ++i) delete _triangles[i];
00276         if (_data) delete _data;
00277         for(int i = 0; i < 256; ++i) {
00278                 if (_indices[i]) delete _indices[i];
00279         }
00280         if (_normalsArray) delete _normalsArray;
00281 } // }}} MCMesh::~MCMesh()
00282 
00283 MCMesh::index_t MCMesh::_getIndex(int x, int y, int z, int edge) // {{{
00284 {
00285         DEBUG0("MCMesh::index_t MCMesh::_getIndex(int x="<<x<<", int y="
00286                <<y<<", int z="<<z<<", int edge="<<edge<<")\n");
00287         return (x<<MCD_X_SHIFT) + (y<<MCD_Y_SHIFT) + (z<<MCD_Z_SHIFT) + edge;
00288 } // }}} MCMesh::index_t MCMesh::_getIndex(int x, int y, int z, int edge)
00289 
00290 void MCMesh::_compileData() // {{{
00291 {
00292         DEBUG0("void MCMesh::_compileData()\n");
00293         _dataSize = numVertices();
00294         if (_progCB) {
00295                 _progCB->SetText("Compiling scene data:");
00296                 _progCB->Start();
00297         }
00300         try {
00301                 if (_data) delete _data;
00302                 for(int i = 0; i < 256; ++i) {
00303                         if (_indices[i]) delete _indices[i];
00304                         _indices[i] = 0;
00305                         _indexSize[i] = 0;
00306                 }
00307                 for(std::list<MCVertex *>::iterator i = _vertexList.begin();
00308                     i!=_vertexList.end(); ++i)
00309                         (*i)->resetIndexing();
00310         } catch(...) {
00311                 MC_FAIL_EXIT("Error while cleaning up during the data compilation.", -1);
00312         }
00314         index_t maxIndexSize = numTriangles()*3;        // The extreme value.
00315         DEBUG5("_dataSize = "<<_dataSize<<", _indexSize = "<<_indexSize<<"\n");
00316         try {
00317                 _data = new MCData[_dataSize];
00318                 if (_gd->doMeshStatistics) _gd->meshSize += sizeof(MCData)*_dataSize;
00319                 DEBUG5("Allocated "<<_dataSize*sizeof(MCData)
00320                        <<" Bytes (MCData: "<<sizeof(MCData)<<")\n");
00321         } catch (...) {
00322                 MC_FAIL_EXIT("could not allocate "<<_dataSize<<" data elements ("
00323                              <<_dataSize*sizeof(MCData)<<")", -1);
00324         }
00328         int update = _triangles.size()/50;
00329         update = (update>0) ? update : 1;
00330         DEBUG5("update = "<<update<<"\n");
00331         index_t triSize = _triangles.size();
00332         index_t vertCnt = 0;
00333         DEBUG5("cubeSize: "<<_gd->cubeSize[0]<<", "<<_gd->cubeSize[1]<<", "<<_gd->cubeSize[1]<<"\n");
00334         for(unsigned int t = 0; t < triSize; ++t) {
00335                 if (!_triangles[t]->Complete()) continue;
00336                 DEBUG5("Adding triangle "<<t<<"\n");
00337                 vuVector n = _triangles[t]->GetNormal();
00338                 int type = _triangles[t]->Type();
00339                 for(int v = 0; v < 3; ++v) {
00340                         MCVertex* vert = _triangles[t]->GetVertex(v);
00341                         vuVector theNormal;
00342                         index_t theIdx;
00343                         if (!vert->Indexed()) {
00344                                 _data[vertCnt].vertex[0] = vert->Vertex()[0]*_gd->cubeSize[0];
00345                                 _data[vertCnt].vertex[1] = vert->Vertex()[1]*_gd->cubeSize[1];
00346                                 _data[vertCnt].vertex[2] = vert->Vertex()[2]*_gd->cubeSize[2];
00347                                 //_data[vertCnt].copyVertexFrom(vert->Vertex().getData());
00348                                 DEBUG5("vertex: "<<_data[vertCnt].vertex[0]
00349                                        <<", "<<_data[vertCnt].vertex[1]
00350                                        <<", "<<_data[vertCnt].vertex[3]<<"\n");
00351                                 theNormal = vuVector(0.0, 0.0, 0.0);
00352                                 vert->Idx(vertCnt);
00353                                 theIdx = vertCnt;
00354                                 ++vertCnt;              // <-- vertCnt
00355                         } else {
00356                                 theIdx = vert->Idx();
00357                                 theNormal = vuVector(_data[theIdx].normal[0],
00358                                                      _data[theIdx].normal[1],
00359                                                      _data[theIdx].normal[2]);
00360                         }
00361                         theNormal += n;
00362                         theNormal.makeUnit();
00363                         /*
00364                         _data[theIdx].normal[0] = theNormal[0];
00365                         _data[theIdx].normal[1] = theNormal[1];
00366                         _data[theIdx].normal[2] = theNormal[2];
00367                         */
00368                         _data[theIdx].copyNormalFrom(theNormal.getData());
00369                         ++_indexSize[type];             // <-- indexSize
00370                         assert(vertCnt <= _dataSize);
00371                         DEBUG4("_indexSize[type]: "<<_indexSize[type]
00372                                <<", maxIndexSize: "<<maxIndexSize<<"\n");
00373                         assert(_indexSize[type] <= maxIndexSize);
00374                 }
00375                 DEBUG4("_progCB: "<<(void*)_progCB<<"\n");
00376                 if (!(t%update) && _progCB) {
00377                         DEBUG4("progress: "<<((t*100/triSize)>>1)<<"\n");
00378                         _progCB->Update((t*100/triSize)>>1);
00379                         if (_gd->termProgress)
00380                                 std::cerr << ((float)t/(float)triSize*100.0)
00381                                         << "% added to array.\n";
00382                 }
00383         }
00384         if (_progCB) _progCB->Update(50);
00385         index_t idxCnt[255];
00386         for(unsigned int t = 0; t < triSize; ++t) {
00387                 if (!_triangles[t]->Complete()) continue;
00388                 int type = _triangles[t]->Type();
00389                 DEBUG4("calc indices: t = "<<t<<", type = "<<type<<"\n");
00390                 for(int v = 0; v < 3; ++v) {
00391                         if (!_indices[type]) {
00392                                 idxCnt[type] = 0;
00393                                 try {
00394                                         DEBUG5("Creating index array for type "<<type
00395                                                <<", size: "<<_indexSize[type]<<"\n");
00396                                         _indices[type] = new index_t[_indexSize[type]];
00397                                         if (_gd->doMeshStatistics)
00398                                                 _gd->meshSize += sizeof(index_t)*_indexSize[type];
00399                                 } catch (...) {
00400                                         MC_FAIL_EXIT("could not allocate "<<maxIndexSize
00401                                                      <<" index elements for type "<<type<<" ("
00402                                                      <<_dataSize*sizeof(MCData)<<")", -1);
00403                                 }
00404                         }
00405                         _indices[type][idxCnt[type]++] =
00406                                 _triangles[t]->GetVertex(v)->Idx();     // <-- indexSize
00407                         DEBUG4("idxCnt["<<type<<"] = "<<idxCnt[type]<<"\n");
00408                         assert(idxCnt[type] <= _indexSize[type]);
00409                 }
00410                 if (!(t%update) && _progCB) {
00411                         DEBUG5("progress: "<<(50+((t*100/triSize)>>1))<<"\n");
00412                         _progCB->Update(50+((t*100/triSize)>>1));
00413                         if (_gd->termProgress)
00414                                 std::cerr << ((float)t/(float)triSize*100.0) << "% indexed.\n";
00415                 }
00416         }
00417         if (_progCB) _progCB->End();
00418         DEBUG5("meshSize = "<<_gd->meshSize<<"\n");
00419         _gd->compileData = false;
00420 } // }}} void MCMesh::_compileData()
00421 
00422 void MCMesh::_compileNormalsArray() // {{{
00423 {
00424         DEBUG0("void MCMesh::_compileNormalsArray()\n");
00425         if (_normalsArray) delete _normalsArray;
00426         _normalsArraySize = _dataSize*2;
00427         try {
00428                 DEBUG3("normals array size: "<<_normalsArraySize*3*sizeof(float)<<"\n");
00429                 _normalsArray = new float[_normalsArraySize*3];
00430         } catch (...) {
00431                 MC_FAIL_EXIT("could not allocate "<<_normalsArraySize
00432                              <<" elements for the normals array ("
00433                              <<_normalsArraySize*3*sizeof(float)<<")", -1);
00434         }
00435         index_t cnt = 0;
00436         for(index_t i = 0; i < _dataSize; ++i) {
00437                 vuVector v = vuVector(_data[i].vertex);
00438                 vuVector n = vuVector(_data[i].normal);
00439                 _normalsArray[cnt*3]   = _data[i].vertex[0];
00440                 _normalsArray[cnt*3+1] = _data[i].vertex[1];
00441                 _normalsArray[cnt*3+2] = _data[i].vertex[2];
00442                 ++cnt;
00443                 DEBUG1("normalsLength = "<<_gd->normalsLength<<"\n");
00444                 vuVector v2 = v+n*_gd->normalsLength;
00445                 _normalsArray[cnt*3]   = v2[0];
00446                 _normalsArray[cnt*3+1] = v2[1];
00447                 _normalsArray[cnt*3+2] = v2[2];
00448                 ++cnt;
00449                 assert(cnt <= _normalsArraySize);
00450         }
00451         _gd->compileNormals = false;
00452         DEBUG3("Normals are compiled!!!\n");
00453 } // }}} void MCMesh::_compileNormalsArray()
00454 
00455 MCMesh::index_t MCMesh::AddVertex(int x, int y, int z, int edge, int triangle, vuVector& v) // {{{
00456 {
00457         DEBUG0("MCMesh::index_t MCMesh::AddVertex(int x, int y, int z, "
00458                <<"int edge, int triangle, vuVector& v)\n");
00459         index_t idx = _getIndex(x, y, z, edge);
00460         if (_vertices.find(idx) == _vertices.end()) {
00461                 MCVertex *theVert = new MCVertex(-1);
00462                 if (_gd->doMeshStatistics) _gd->meshSize += _vertexSize;
00463                 _vertexList.push_back(theVert);
00464                 _vertices[idx] = theVert;
00465                 theVert->Vertex(v);
00466                 DEBUG1(">>> added index: "<<idx<<"\n");
00468                 for(int i = 0; i < 3; ++i) {
00470                         int otherX = x+_edgeMapping[edge][i][0];
00471                         int otherY = y+_edgeMapping[edge][i][1];
00472                         int otherZ = z+_edgeMapping[edge][i][2];
00473                         int otherEdge = _edgeMapping[edge][i][3];
00474                         DEBUG1("other: "<<otherX<<", "<<otherY<<", "<<otherZ<<", "<<otherEdge<<"\n");
00475                         if (otherX<0 || otherY<0 || otherZ<0) continue; // there is no index < 0.
00476                         index_t otherIdx = _getIndex(otherX, otherY, otherZ, otherEdge);
00481                         assert(_vertices.find(otherIdx) == _vertices.end());
00483                         _vertices[otherIdx] = theVert;
00484                         DEBUG1(">>>> added index: "<<otherIdx<<".\n");
00485                 }
00486         }
00488         return AddVertex(idx, triangle);
00489 } // }}} MCMesh::index_t MCMesh::AddVertex(int x, int y, int z, int edge, int triangle, vuVector& v)
00490 
00491 MCMesh::index_t MCMesh::AddVertex(int idx, int triangle) // {{{
00492 {
00493         DEBUG0("MCMesh::index_t MCMesh::AddVertex(int idx, int triangle)\n");
00494         _normalsPrepared = false;
00495         assert(triangle != -1);         // This is realy bad :)
00496         if (_vertices.find(idx) == _vertices.end()) return MCD_ERROR;   // Vertex does not exist.
00497         if (_triangles[triangle]->AddVertex(_vertices[idx]) == -1) return MCD_ERROR; // Error while
00498                                                                         // adding vertex to triangle.
00499         return idx;
00500 } // }}} MCMesh::index_t MCMesh::AddVertex(int idx, int triangle)
00501 
00502 MCMesh::index_t MCMesh::GetVertex(int x, int y, int z, int edge) // {{{
00503 {
00504         DEBUG0("MCMesh::index_t MCMesh::GetVertex(int x, int y, int z, int edge)\n");
00505         int idx = _getIndex(x, y, z, edge);
00506         if (_vertices.find(idx) != _vertices.end())
00507                 return idx;
00508         return MCD_ERROR;
00509 } // }}} MCMesh::index_t MCMesh::GetVertex(int x, int y, int z, int edge)
00510 
00512 int MCMesh::AddTriangle(int type) // {{{
00513 {
00514         DEBUG0("int MCMesh::AddTriangle(int type)\n");
00515         _normalsPrepared = false;
00516         _triangles.push_back(new MCTriangle(type));
00517         if (_gd->doMeshStatistics) _gd->meshSize += _triangleSize;
00518         return _triangles.size() - 1;
00519 } // }}} int MCMesh::AddTriangle(int type)
00520 
00521 void MCMesh::DebugPrint(void) // {{{
00522 {
00523         DEBUG0("void MCMesh::DebugPrint(void)\n");
00524 #if DEBUG_MODE < 99
00525         /*
00526         for(int i=0; i<_indices.GetSize(); ++i) {
00527                 if (i%3 == 0) {
00528                         cerr << DEBUG_LOCATION << "Triange:\n";
00529                 }
00530                 int idx = *_indices[i];
00531                 cerr << DEBUG_LOCATION << "\t(" << _vertexData[idx][0] << ", "
00532                         << _vertexData[idx][1] << ", " << _vertexData[idx][2] << ")\n";
00533         }
00534         */
00535 #endif
00536 } // }}} void MCMesh::DebugPrint(void)
00537 
00538 void MCMesh::prepare(void) // {{{
00539 {
00540         DEBUG0("void MCMesh::prepare(void)\n");
00541         if (!_data || _gd->compileData) _compileData();
00542         DEBUG3("drawNormals = "<<_gd->drawNormals<<", compileNormals = "<<_gd->compileNormals<<"\n");
00543         if (_gd->drawNormals && _gd->compileNormals) _compileNormalsArray();
00544 } // }}} void MCMesh::prepare(void)
00545 
00546 void MCMesh::render() // {{{
00547 {
00548         DEBUG0("void MCMesh::render()\n");
00549         prepare();
00550         GLboolean oldCullMode;
00551         glGetBooleanv(GL_CULL_FACE, &oldCullMode);
00552         GLint oldFaceMode;
00553         glGetIntegerv(GL_CULL_FACE_MODE, &oldFaceMode);
00554 
00555         glInterleavedArrays(GL_N3F_V3F, 0, (void*)_data);
00556         glEnableClientState(GL_VERTEX_ARRAY);
00557         glEnableClientState(GL_NORMAL_ARRAY);
00558 
00559         for(int i = 1; i < 255; ++i) {
00560                 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, _gd->objectColor[i].getData());
00561                 glDrawElements(GL_TRIANGLES, _indexSize[i], GL_UNSIGNED_INT, (void*)_indices[i]);
00562         }
00563 
00564         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, _gd->lineColor.getData());
00565         if (_gd->linesMode) {
00566                 GLboolean enabled;
00567                 glGetBooleanv(GL_CULL_FACE, &enabled);
00568                 GLint mode;
00569                 glGetIntegerv(GL_CULL_FACE_MODE, &mode);
00570                 if (_gd->linesMode == GL_FRONT_AND_BACK) {
00571                         glDisable(GL_CULL_FACE);
00572                 } else if (_gd->linesMode == GL_BACK) {
00573                         glCullFace(GL_FRONT);
00574                         glEnable(GL_CULL_FACE);
00575                 } else if (_gd->linesMode == GL_FRONT) {
00576                         glCullFace(GL_BACK);
00577                         glEnable(GL_CULL_FACE);
00578                 }
00579                 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, _gd->lineColor.getData());
00580                 glPolygonMode(_gd->linesMode, GL_LINE);
00581                 for(int i = 1; i < 255; ++i) {
00582                         glDrawElements(GL_TRIANGLES, _indexSize[i],
00583                                        GL_UNSIGNED_INT, (void*)_indices[i]);
00584                 }
00585                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00586                 if (!mode) {
00587                         glDisable(GL_CULL_FACE);
00588                 } else {
00589                         glEnable(GL_CULL_FACE);
00590                 }
00591                 glCullFace(mode);
00592         }
00593         glDisableClientState(GL_VERTEX_ARRAY);
00594         glDisableClientState(GL_NORMAL_ARRAY);
00595 
00596         if (_gd->drawNormals) {
00597                 DEBUG3("Rendering Normals ...\n");
00598                 glVertexPointer(3, GL_FLOAT, 0, (void*)_normalsArray);
00599                 glEnableClientState(GL_VERTEX_ARRAY);
00600                 glDrawArrays(GL_LINES, 0, _normalsArraySize);
00601                 glDisableClientState(GL_VERTEX_ARRAY);
00602                 DEBUG3("rendering done!!!\n");
00603         }
00604 
00605         if (oldCullMode) {
00606                 glDisable(GL_CULL_FACE);
00607         } else {
00608                 glEnable(GL_CULL_FACE);
00609         }
00610         glCullFace(oldFaceMode);
00611 } // }}} void MCMesh::render()
00612 
00613 MCMesh::index_t MCMesh::numTriangles() // {{{
00614 {
00615         DEBUG0("MCMesh::index_t MCMesh::numTriangles()\n");
00616         return _triangles.size();
00617 } // }}} MCMesh::index_t MCMesh::numTriangles()
00618 
00619 MCMesh::index_t MCMesh::numVertices() // {{{
00620 {
00621         DEBUG0("MCMesh::index_t MCMesh::numVertices()\n");
00622         //return _vertices.size();
00623         return _vertexList.size();
00624 } // }}} MCMesh::index_t MCMesh::numVertices()
00625 
00626 MCMesh::index_t MCMesh::numIndices() // {{{
00627 {
00628         DEBUG0("MCMesh::index_t MCMesh::numIndices()\n");
00629         index_t cnt = 0;
00630         for(int i = 0; i < 255; ++i) {
00631                 cnt += _indexSize[i];
00632         }
00633         return cnt;
00634 } // }}} MCMesh::index_t MCMesh::numIndices()
00635 
00636 // }}} MCMesh
00637 
00638 
00639 // vim:fdm=marker:fdc=3:tw=100

Generated on Wed Dec 15 21:20:29 2004 for vuVolume by  doxygen 1.3.9.1