#include "GeoMeshView.h" #include using namespace Geometry; using namespace std; //--------------------------------------------------------------------------- // Hanle events. //--------------------------------------------------------------------------- int GeoMeshView::handle(int event) { int key; int button; int x; int y; if (geoMesh != NULL) { draw(); } switch(event) { case fltk::KEY: case fltk::PUSH: button = fltk::event_key(); mMouseX = fltk::event_x(); mMouseY = fltk::event_y(); return 1; case fltk::DRAG: button = fltk::event_state(); x = fltk::event_x(); y = fltk::event_y(); switch (button) { // Button 1 Pushed. case 0x01100000: if (x > mMouseX) { if (mPan) { xshift = xshift + (x - mMouseX); } else { hAng = hAng + (x - mMouseX); } } else { if (x < mMouseX) { if (mPan) { xshift = xshift - (mMouseX - x); } else { hAng = hAng - (mMouseX - x); } } } if (y > mMouseY) { if (mPan) { yshift = yshift - (y - mMouseY); } else { vAng = vAng + (y - mMouseY); } } else { if (y < mMouseY) { if (mPan) { yshift = yshift + (mMouseY - y); } else { vAng = vAng - (mMouseY - y); } } } hAng = float(int(hAng) % 360); vAng = float(int(vAng) % 360); mMouseX = x; mMouseY = y; draw(); return 1; case 0x04100000: if (y > mSizeY) { size++; } else { if (y < mSizeY) { size--; } } mSizeY = y; draw(); return 1; } case fltk::MOUSEWHEEL: size = size + fltk::event_dy(); draw(); return 1; } return fltk::GlWindow::handle(event); } //--------------------------------------------------------------------------- // Get the number of frames per second. //--------------------------------------------------------------------------- size_t GeoMeshView::getFPS() { return mFPS; } //--------------------------------------------------------------------------- // Calculate the number of frames per second. //--------------------------------------------------------------------------- void GeoMeshView::calcFPS() { double duration; clock_t finish_time; // Gets the time. finish_time = clock(); // Calc the duration respect the start time. duration = (double)((finish_time - mStartTime) / CLOCKS_PER_SEC); // Increments the number of frames per second. mFPS++; // If a second have passed. if (duration >= 1.0) { // Repaint the FPS. mGeoMeshViewUI->refreshFPS(mFPS); mFPS = 0; mStartTime = clock(); } } //--------------------------------------------------------------------------- // Enable color strips. //--------------------------------------------------------------------------- void GeoMeshView::enableColorStrips() { colorStripsFlag = true; } //--------------------------------------------------------------------------- // Disable color strips. //--------------------------------------------------------------------------- void GeoMeshView::disableColorStrips() { colorStripsFlag = false; } //--------------------------------------------------------------------------- // Gets the color strips state. //--------------------------------------------------------------------------- bool GeoMeshView::getColorStrips() { return colorStripsFlag; } //--------------------------------------------------------------------------- // Set the color to the submesh sumbmeshIndex //--------------------------------------------------------------------------- void GeoMeshView::setSubMeshSelected(int submeshIndex) { mSubMeshSelected = submeshIndex; } //--------------------------------------------------------------------------- // Sets the leaves submesh index. //--------------------------------------------------------------------------- void GeoMeshView::setLeavesSubMesh(int submeshIndex) { leavesSubMesh = submeshIndex; } //--------------------------------------------------------------------------- // Gets the leaves submesh index. //--------------------------------------------------------------------------- int GeoMeshView::getLeavesSubMesh() { return leavesSubMesh; } //--------------------------------------------------------------------------- // Set the list of strip colors. //--------------------------------------------------------------------------- void GeoMeshView::setStripColors() { SubMesh *geosubmesh; // Free memory of the strip colors array. for (int submesh = 0; submesh < mStripColorsCount; submesh++) { delete []mStripColors[submesh]; } delete []mStripColors; // Initialize the Colors array. mStripColors = new ColorElement*[geoMesh->mSubMeshCount]; // Count the total number of strips y the submeshes. for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++) { // Gets the actual submesh. geosubmesh = &geoMesh->mSubMesh[submesh]; if (geosubmesh->mType == GEO_TRIANGLE_STRIPS) { mStripColors[submesh] = new ColorElement[geosubmesh->mStripCount]; } else { mStripColors[submesh] = NULL; } } // Sets random colors to each strip. for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++) { // Gets the actual submesh. geosubmesh = &geoMesh->mSubMesh[submesh]; if (geosubmesh->mType == GEO_TRIANGLE_STRIPS) { for (int strip = 0; strip < geosubmesh->mStripCount; strip++) { mStripColors[submesh][strip].r = rand()/((float)RAND_MAX); mStripColors[submesh][strip].g = rand()/((float)RAND_MAX); mStripColors[submesh][strip].b = rand()/((float)RAND_MAX); } } } // Sets the submesh count. mStripColorsCount = geoMesh->mSubMeshCount; } /* //--------------------------------------------------------------------------- // Get the bounding box of the object. //--------------------------------------------------------------------------- GeometryBounds GeoMeshView::getBoundingBox() { SubMesh *geosubmesh; Index position; Vector3 vector3; GeometryBounds bounds; float x,y,z; float xMin,yMin,zMin; float xMax,yMax,zMax; // For each submesh. for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++) { // Gets the actual submesh. geosubmesh = &geoMesh->mSubMesh[submesh]; // For each index of the strip. for (int index = 0; index < geosubmesh->mIndexCount; index++) { position = geosubmesh->mIndex[index]; // Gets the vertex coordinates. vector3 = geosubmesh->mVertexBuffer->mPosition[position]; x = vector3[0]; y = vector3[1]; z = vector3[2]; // If is the first iteation. if ((submesh == 0) && (index == 0)) { xMin = xMax = x; yMin = yMax = y; zMin = zMax = z; } else { if (x < xMin) { xMin = x; } else if (x > xMax) { xMax = x; } if (y < yMin) { yMin = y; } else if (y > yMax) { yMax = y; } if (z < zMin) { zMin = z; } else if (z > zMax) { zMax = z; } } } } xMed = (xMax + xMin) / 2; yMed = (yMax + yMin) / 2; zMed = (zMax + zMin) / 2; if ((xMax - xMin) > (yMax - yMin)) { if ((xMax - xMin) > (zMax - zMin)) { mScaleFactor = xMax - xMin; } else { mScaleFactor = zMax - zMin; } } else { if ((yMax - yMin) > (zMax - zMin)) { mScaleFactor = yMax - yMin; } else { mScaleFactor = zMax - zMin; } } // Set the bounds. bounds.minx = xMin; bounds.miny = yMin; bounds.minz = zMin; bounds.maxx = xMax; bounds.maxy = yMax; bounds.maxz = zMax; bounds.radius = mScaleFactor; return bounds; } */ //--------------------------------------------------------------------------- // Constructor. //--------------------------------------------------------------------------- GeoMeshView::GeoMeshView( int x, int y, int w, int h, const char *l, GeoMeshViewUI *geoMeshViewUI) : fltk::GlWindow(x,y,w,h,l) { mStripColors = NULL; mSharedPosArray = NULL; mSharedNorArray = NULL; mSharedTexCoordArray= NULL; mPosArray = NULL; mNorArray = NULL; mTexCoordArray = NULL; mIndexArray = NULL; vAng = 0.0; hAng = 0.0; size = 0.0; mSizeY = 0; xshift = 0.0; yshift = 0.0; geoMesh = 0; mRotate = true; mPan = false; mLodStrip = false; mLodTree = false; mIdVisualList = 0; mSubMeshCount = 0; mStripColorsCount = 0; // Initialize the model view. deactiveWire(); deactiveSolid(); activeRotate(); deactiveCW(); activeCCW(); // Initialize start time for FPS. mStartTime = clock(); // Cross Reference. mGeoMeshViewUI = geoMeshViewUI; current_texture = 0; current_texture_submesh=NULL; use_texture_mapping = true; } // The color used for the edges of the bounding cube. #define CUBECOLOR 255,255,255,255 /* Draw a colored cube */ #define ALPHA 0.5 //--------------------------------------------------------------------------- // Sets the Mesh to display. //--------------------------------------------------------------------------- void GeoMeshView::setMesh(Mesh *geomesh) { GLfloat color_blanco[] = {1.0f,1.0f,1.0f,1.0f}; this->geoMesh = geomesh; mSubMeshSelected = -1; leavesSubMesh = -1; mScaleFactor = geomesh->mMeshBounds.scaleFactor; // Refresh vertices to the vertex array. refreshVertices(geomesh); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_blanco); if (current_texture_submesh) delete[] current_texture_submesh; current_texture_submesh=new GLuint[getSubMeshCount()]; for (int i=0; i 0) { for (int submesh = 0; submesh < mSubMeshCount; submesh++) { delete [] mPosArray[submesh]; delete [] mNorArray[submesh]; delete [] mIndexArray[submesh]; delete [] mTexCoordArray[submesh]; } delete [] mSharedPosArray; delete [] mSharedNorArray; delete [] mSharedTexCoordArray; delete [] mPosArray; delete [] mNorArray; delete [] mIndexArray; } // If there is shared vertex. if (geomesh->mVertexBuffer != NULL) { vertex_buffer = geomesh->mVertexBuffer; // Allocate memory. mSharedPosArray = new GLfloat[vertex_buffer->mVertexCount * 3]; mSharedNorArray = new GLfloat[vertex_buffer->mVertexCount * 3]; mSharedTexCoordArray = new GLfloat[vertex_buffer->mVertexCount * 2]; for (int vertex = 0; vertex < vertex_buffer->mVertexCount; vertex++) { mSharedPosArray[3 * vertex] = vertex_buffer->mPosition[vertex].x; mSharedPosArray[(3 * vertex) + 1] = vertex_buffer->mPosition[vertex].y; mSharedPosArray[(3 * vertex) + 2] = vertex_buffer->mPosition[vertex].z; mSharedNorArray[3 * vertex] = vertex_buffer->mNormal[vertex].x; mSharedNorArray[(3 * vertex) + 1] = vertex_buffer->mNormal[vertex].y; mSharedNorArray[(3 * vertex) + 2] = vertex_buffer->mNormal[vertex].z; mSharedTexCoordArray[2 * vertex] = vertex_buffer->mTexCoords[vertex].x; mSharedTexCoordArray[(2 * vertex) + 1] = vertex_buffer->mTexCoords[vertex].y; } } // Build the arrays of vertices and indices. mPosArray = new GLfloat*[geomesh->mSubMeshCount]; mNorArray = new GLfloat*[geomesh->mSubMeshCount]; mTexCoordArray = new GLfloat*[geomesh->mSubMeshCount]; mIndexArray = new GLuint*[geomesh->mSubMeshCount]; // For each submesh. for (int submesh = 0; submesh < geomesh->mSubMeshCount; submesh++) { // Gets the actual submesh. geosubmesh = &geomesh->mSubMesh[submesh]; // Allocate memory for index array of the actual submesh. mIndexArray[submesh] = new GLuint[geosubmesh->mIndexCount]; // Gets the indices of the actual submesh. for (int index = 0; index < geosubmesh->mIndexCount; index++) { mIndexArray[submesh][index] = geosubmesh->mIndex[index]; } // If the submesh is NOT shared vertex. if (!geosubmesh->mSharedVertexBuffer) { vertex_buffer = geosubmesh->mVertexBuffer; // Allocate memory for vertices of the actual submesh. mPosArray[submesh] = new GLfloat[vertex_buffer->mVertexCount * 3]; mNorArray[submesh] = new GLfloat[vertex_buffer->mVertexCount * 3]; mTexCoordArray[submesh] = new GLfloat[vertex_buffer->mVertexCount * 2]; // For each one of the vertex. for (int vertex = 0; vertex < vertex_buffer->mVertexCount; vertex++) { mPosArray[submesh][3 * vertex] = vertex_buffer->mPosition[vertex].x; mPosArray[submesh][(3 * vertex) + 1] = vertex_buffer->mPosition[vertex].y; mPosArray[submesh][(3 * vertex) + 2] = vertex_buffer->mPosition[vertex].z; mNorArray[submesh][3 * vertex] = vertex_buffer->mNormal[vertex].x; mNorArray[submesh][(3 * vertex) + 1] = vertex_buffer->mNormal[vertex].y; mNorArray[submesh][(3 * vertex) + 2] = vertex_buffer->mNormal[vertex].z; mTexCoordArray[submesh][2 * vertex] = vertex_buffer->mTexCoords[vertex].x; mTexCoordArray[submesh][(2 * vertex) + 1] = vertex_buffer->mTexCoords[vertex].y; }// End for each vertex. } // If the submesh is shared vertex. else { mPosArray[submesh] = NULL; mNorArray[submesh] = NULL; mTexCoordArray[submesh] = NULL; } }// End for each submesh. mSubMeshCount = geoMesh->mSubMeshCount; } //--------------------------------------------------------------------------- // Gets the triangle count of the current lod. //--------------------------------------------------------------------------- size_t GeoMeshView::getLodStripsTriangleCount() { size_t triangle_count; // Initialize triangle count. triangle_count = 0; // For each strip. for (int strip = 0; strip < lodStripsLib->GetStripCount(); strip++) { triangle_count += lodStripsLib->mStrips[strip].size() - 2; } return triangle_count; } //--------------------------------------------------------------------------- // Repaint the Mesh. //--------------------------------------------------------------------------- void GeoMeshView::drawGeoMesh(bool paintWire) { SubMesh *geosubmesh; bool submesh_selected; if (geoMesh) { if (mLodStrip) { drawLodStrip(); } else if (mLodTree) { drawLodTree(); } else { for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++) { // Gets the actual submesh. geosubmesh = &geoMesh->mSubMesh[submesh]; if (geosubmesh->mType == GEO_TRIANGLE_LIST) { if (mSubMeshSelected == submesh) { submesh_selected = true; } else { submesh_selected = false; } // Draw the actual submesh in triangle list. drawTriangleList(submesh,submesh_selected,paintWire); } else { // Draw the actual submesh in triangle strip. drawTriangleStrip(submesh); } } } } }// End drawGeoMesh. //--------------------------------------------------------------------------- // Paint eah strip separately. //--------------------------------------------------------------------------- void GeoMeshView::drawTriangleStrip(int submesh) { SubMesh *geosubmesh; Index position; Vector3 vector3; Vector2 vector2; float x,y,z; float r,g,b; Index *index; Index *indexBegin; Index *indexEnd; int color_index; color_index = 0; // Gets the actual submesh. geosubmesh = &geoMesh->mSubMesh[submesh]; // bind current texture to use GLuint usetex = 0; if (use_texture_mapping) { if (current_texture_submesh[submesh]) usetex=current_texture_submesh[submesh]; else if (current_texture) usetex=current_texture; } glBindTexture(GL_TEXTURE_2D,usetex); // For each one of the strips. for (int strip = 0; strip < geosubmesh->mStripCount; strip++) { // Paint the current strip. glBegin(GL_TRIANGLE_STRIP); // First index of the strip. indexBegin = geosubmesh->mStrip[strip]; // If the strips is not the first. //if (strip != 0) //{ // indexBegin++; //} // If is the final strip if (strip == (geosubmesh->mStripCount - 1)) { // The end of the index array. indexEnd = &geosubmesh->mIndex[geosubmesh->mIndexCount]; } else { // The beginning of the next strip. indexEnd = geosubmesh->mStrip[strip + 1]; // Remove degenerated //indexEnd--; } int i; i = 0; if (getColorStrips()) { // Gets the color of the strip. r = mStripColors[submesh][color_index].r; g = mStripColors[submesh][color_index].g; b = mStripColors[submesh][color_index].b; // Change to the next color. color_index++; // Paint a new color. glColor3f(r,g,b); } // For each index of the strip. for (index = indexBegin; index < indexEnd; index++) { position = indexBegin[i]; // Gets the vertex normal. vector3 = geosubmesh->mVertexBuffer->mNormal[position]; x = vector3[0]; y = vector3[1]; z = vector3[2]; // Sets the vertex normal. glNormal3f(x,y,z); // set the texture coordinates if needed if (usetex) { vector2 = geosubmesh->mVertexBuffer->mTexCoords[position]; x = vector2[0]; y = vector2[1]; glTexCoord2f(x,y); } // Gets the vertex coordinates. vector3 = geosubmesh->mVertexBuffer->mPosition[position]; x = vector3[0]; y = vector3[1]; z = vector3[2]; // Sets the vertex position. glVertex3f(x,y,z); // Increments i. i++; } glEnd(); } }//End drawTriangleStrip. //--------------------------------------------------------------------------- // Paint the array of idices. //--------------------------------------------------------------------------- void GeoMeshView::drawTriangleList( int submesh, bool selectedSubMesh, bool paintWire) { SubMesh *geosubmesh; Index position; Vector3 vector3; float x,y,z; int idx_offset; int vrt_offset; // Index offset. idx_offset = 0; // Vertex offset. vrt_offset = 0; // Gets the actual submesh. geosubmesh = &geoMesh->mSubMesh[submesh]; // If wire is not selected. if (!paintWire) { // If a mesh is selected. if (selectedSubMesh) { glColor3d(1.0,0.0,0.0); GLfloat red[] = {1.0f,0.0f,0.0f,1.0f}; glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,red); } // If is a tree. else if (leavesSubMesh >= 0) { glColor3d(1.0, 0.5, 0.5); GLfloat brown[] = {1.0f,0.5f,0.5f,1.0f}; glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,brown); // For each index of the strip. if (submesh == leavesSubMesh) { glColor3d(0.0,1.0,0.0); GLfloat green[] = {0.0f,1.0f,0.0f,1.0f}; glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,green); } } // If is NOT a tree. else { // Set white color to the object. glColor3d(1.0, 1.0, 1.0); GLfloat white[] = {1.0f,1.0f,1.0f,1.0f}; glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,white); } } // bind current texture to use GLuint usetex = 0; if (use_texture_mapping) { if (current_texture_submesh[submesh]) usetex=current_texture_submesh[submesh]; else if (current_texture) usetex=current_texture; } glBindTexture(GL_TEXTURE_2D,usetex); // Enable arrays. glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); if (usetex) glEnableClientState(GL_TEXTURE_COORD_ARRAY); else glDisableClientState(GL_TEXTURE_COORD_ARRAY); // If the submesh is shared vertex. if (geosubmesh->mSharedVertexBuffer) { glVertexPointer(3, GL_FLOAT, 0, mSharedPosArray); glNormalPointer(GL_FLOAT, 0, mSharedNorArray); if (usetex) glTexCoordPointer(2, GL_FLOAT, 0, mSharedTexCoordArray); } else { glVertexPointer(3, GL_FLOAT, 0, mPosArray[submesh]); glNormalPointer(GL_FLOAT, 0, mNorArray[submesh]); if (usetex) glTexCoordPointer(2, GL_FLOAT, 0, mTexCoordArray[submesh]); } glDrawElements( GL_TRIANGLES, geosubmesh->mIndexCount, GL_UNSIGNED_INT, mIndexArray[submesh]); }//End drawTriangleList //--------------------------------------------------------------------------- // Paint lodstrip object. //--------------------------------------------------------------------------- void GeoMeshView::drawLodStrip() { SubMesh *geosubmesh; Index position; Vector3 vector3; Vector2 vector2; float x,y,z; float r,g,b; int color_index; int current_strip; // Initialize current strip. current_strip = 0; // For each submesh. for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++) { // bind current texture to use GLuint usetex = 0; if (use_texture_mapping) { if (current_texture_submesh[submesh]) usetex=current_texture_submesh[submesh]; else if (current_texture) usetex=current_texture; } glBindTexture(GL_TEXTURE_2D,usetex); color_index = 0; // Gets the actual submesh. geosubmesh = &geoMesh->mSubMesh[submesh]; // For each one of the strips. for (int strip = 0; strip < geosubmesh->mStripCount; strip++) { // Paint the current strip. glBegin(GL_TRIANGLE_STRIP); if (getColorStrips()) { // Gets the color of the strip. r = mStripColors[submesh][color_index].r; g = mStripColors[submesh][color_index].g; b = mStripColors[submesh][color_index].b; // Change to the next color. color_index++; // Paint a new color. glColor3f(r,g,b); } // For each index of the strip. for (int index = 0; index < lodStripsLib->mStrips[current_strip].size(); index++) { position = lodStripsLib->mStrips[current_strip][index]; // Gets the vertex normal. vector3 = geosubmesh->mVertexBuffer->mNormal[position]; x = vector3[0]; y = vector3[1]; z = vector3[2]; // Sets the vertex normal. glNormal3f(x,y,z); // set the texture coordinates if needed if (usetex) { vector2 = geosubmesh->mVertexBuffer->mTexCoords[position]; x = vector2[0]; y = vector2[1]; glTexCoord2f(x,y); } // Gets the vertex coordinates. vector3 = geosubmesh->mVertexBuffer->mPosition[position]; x = vector3[0]; y = vector3[1]; z = vector3[2]; // Sets the vertex position. glVertex3f(x,y,z); } // Increments current strip. current_strip++; // A strip is generated. glEnd(); } } }//End drawTriangleStrip. //--------------------------------------------------------------------------- // Paint lodtree object. //--------------------------------------------------------------------------- void GeoMeshView::drawLodTree() { SubMesh *geosubmesh; Index position; Vector3 vector3; Vector2 vector2; float x,y,z; float r,g,b; int color_index; int current_strip; // Initialize current strip. current_strip = 0; glEnable(GL_ALPHA_TEST); // glEnable(GL_BLEND); // DRAW THE TRUNK AS A LODSTRIP OBJECT // For each submesh. for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++) { // bind current texture to use GLuint usetex = 0; if (use_texture_mapping) { if (current_texture_submesh[submesh]) usetex=current_texture_submesh[submesh]; else if (current_texture) usetex=current_texture; } glBindTexture(GL_TEXTURE_2D,usetex); color_index = 0; // Gets the actual submesh. geosubmesh = &geoMesh->mSubMesh[submesh]; // For each one of the strips. for (int strip = 0; strip < geosubmesh->mStripCount; strip++) { // Paint the current strip. glBegin(GL_TRIANGLE_STRIP); if (getColorStrips()) { // Gets the color of the strip. r = mStripColors[submesh][color_index].r; g = mStripColors[submesh][color_index].g; b = mStripColors[submesh][color_index].b; // Change to the next color. color_index++; // Paint a new color. glColor3f(r,g,b); } // For each index of the strip. for (int index = 0; index < lodTreeLib->GetStrip(current_strip).size(); index++) { position = lodTreeLib->GetStrip(current_strip)[index]; // Gets the vertex normal. vector3 = geosubmesh->mVertexBuffer->mNormal[position]; x = vector3[0]; y = vector3[1]; z = vector3[2]; // Sets the vertex normal. glNormal3f(x,y,z); // set the texture coordinates if needed if (usetex) { vector2 = geosubmesh->mVertexBuffer->mTexCoords[position]; x = vector2[0]; y = vector2[1]; glTexCoord2f(x,y); } // Gets the vertex coordinates. vector3 = geosubmesh->mVertexBuffer->mPosition[position]; x = vector3[0]; y = vector3[1]; z = vector3[2]; // Sets the vertex position. glVertex3f(x,y,z); } // Increments current strip. current_strip++; // A strip is generated. glEnd(); } } // DRAW THE LEAVES AS A TRIANGLE SOUP glColor3f(0,1,1); glDisable(GL_LIGHTING); glBegin(GL_TRIANGLES); const Geometry::VertexData *foliage_verts = lodTreeLib->Get_Foliage_VertexData(); const Geometry::IndexData *foliage_inds = lodTreeLib->CurrentLOD_Foliage_Indices(); for (int j=0; jCurrentLOD_Foliage_IndexCount(); j++) { float vx,vy,vz; foliage_verts->GetVertexCoord(foliage_inds->GetIndex(j),vx,vy,vz); glVertex3f(vx,vy,vz); } glEnd(); glEnable(GL_LIGHTING); glColor3f(1,1,1); }//End drawTriangleStrip. //--------------------------------------------------------------------------- // Sets the lodstripslibrary object. //--------------------------------------------------------------------------- void GeoMeshView::setLodStripsLibrary(LodStripsLibrary *lodstrips) { lodStripsLib = lodstrips; activeLodStrip(); } //--------------------------------------------------------------------------- // Sets the lodtreeslibrary object. //--------------------------------------------------------------------------- void GeoMeshView::setLodTreesLibrary(LodTreeLibrary *lodtrees) { lodTreeLib = lodtrees; activeLodTree(); } //--------------------------------------------------------------------------- // Change de Level of detail of the object. //--------------------------------------------------------------------------- void GeoMeshView::GoToLod_LodStrip(unsigned int lod) { if (mLodStrip) lodStripsLib->GoToLod(lod); if (mLodTree) lodTreeLib->GoToTrunkLod(lod); draw(); } //--------------------------------------------------------------------------- // Change de Level of detail of the object. //--------------------------------------------------------------------------- void GeoMeshView::GoToLod_LodTree(unsigned int lod) { if (mLodTree) lodTreeLib->GoToFoliageLod(lod); draw(); } //--------------------------------------------------------------------------- // Draw the object in the window. //--------------------------------------------------------------------------- void GeoMeshView::draw() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (current_texture && use_texture_mapping) glEnable(GL_TEXTURE_2D); else glDisable(GL_TEXTURE_2D); glEnable(GL_LIGHTING); // Frustrum. glViewport(0,0,w(),h()); gluPerspective(60,(float)w()/(float)h(),0.1,1000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); GLfloat light_ambient[] = {0.3,0.3,0.3,1.0}; GLfloat luzpos[] = {0.0,0.0,1.0,0.0}; GLfloat luzcolor[] = {1.0,1.0,1.0,1.0}; // glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, luzpos); glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, luzcolor); glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1); glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0); glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0); glEnable (GL_POLYGON_OFFSET_LINE); glPolygonOffset(-1,-1); glEnable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gluLookAt(0,0,2/*mScaleFactor*/ + size * 0.25, 0,0,0, 0,1,0); glTranslatef(xshift,yshift,0); glRotatef(hAng,0,1,0); glRotatef(vAng,1,0,0); //glTranslatef(-xMed,-yMed,-zMed); // Set white color to the object. glColor3d(1.0, 1.0, 1.0); GLfloat color_blanco[] = {1.0f,1.0f,1.0f,1.0f}; glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_blanco); glClearColor(0,0,0,0); if (mCW) { glFrontFace(GL_CW); glEnable(GL_CULL_FACE); } else { if (mCCW) { glFrontFace(GL_CCW); glEnable(GL_CULL_FACE); } else { glDisable(GL_CULL_FACE); } } if (mSolid) { enableColorStrips(); glDisable(GL_LIGHTING); glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); drawGeoMesh(false); } else { glEnable(GL_LIGHTING); } if (mWire) { disableColorStrips(); glDisable(GL_LIGHTING); glColor3d(0.0, 0.0, 1.0); glPolygonMode (GL_FRONT_AND_BACK, GL_LINE); drawGeoMesh(true); } else { if (!mSolid) { enableColorStrips(); glEnable(GL_LIGHTING); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); drawGeoMesh(false); } } swap_buffers(); // Calculate the FPS. calcFPS(); } #include void GeoMeshView::LoadTexture(const char *imgfile) { ilInit(); ilutEnable(ILUT_OPENGL_CONV); ilutRenderer(ILUT_OPENGL); current_texture = ilutGLLoadImage((const ILstring)imgfile); if (!current_texture) fltk::alert("Error loading texture!"); ilShutDown(); } void GeoMeshView::LoadTextureSubMesh(int isubmesh, const char *imgfile) { ilInit(); ilutEnable(ILUT_OPENGL_CONV); ilutRenderer(ILUT_OPENGL); current_texture_submesh[isubmesh] = ilutGLLoadImage((const ILstring)imgfile); if (!current_texture_submesh[isubmesh]) fltk::alert("Error loading texture!"); ilShutDown(); } void GeoMeshView::resetTextures(void) { if (current_texture) { glDeleteTextures(1,¤t_texture); current_texture=0; } if (current_texture_submesh) { glDeleteTextures(getSubMeshCount(),current_texture_submesh); for (int i=0; i