glc_box.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  http://glc-lib.sourceforge.net
00006 
00007  GLC-lib is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU Lesser General Public License as published by
00009  the Free Software Foundation; either version 3 of the License, or
00010  (at your option) any later version.
00011 
00012  GLC-lib is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU Lesser General Public License for more details.
00016 
00017  You should have received a copy of the GNU Lesser General Public License
00018  along with GLC-lib; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 *****************************************************************************/
00022 
00024 
00025 #include "glc_box.h"
00026 #include "../glc_openglexception.h"
00027 #include "../glc_state.h"
00028 
00030 // Constructor Destructor
00032 
00033 GLC_Box::GLC_Box(double dLx, double dLy, double dlz)
00034 :GLC_Mesh()
00035 , m_dLgX(dLx)
00036 , m_dLgY(dLy)
00037 , m_dLgZ(dlz)
00038 {
00039         createMeshAndWire();
00040 }
00041 // Copy constructor
00042 GLC_Box::GLC_Box(const GLC_Box& box)
00043 :GLC_Mesh(box)
00044 , m_dLgX(box.m_dLgX)
00045 , m_dLgY(box.m_dLgY)
00046 , m_dLgZ(box.m_dLgZ)
00047 {
00048         createMeshAndWire();
00049 }
00050 GLC_Box::~GLC_Box()
00051 {
00052 
00053 }
00054 
00056 // Get Functions
00058 
00059 // return the box bounding box
00060 const GLC_BoundingBox& GLC_Box::boundingBox(void)
00061 {
00062         if (GLC_Mesh::isEmpty())
00063         {
00064                 createMeshAndWire();
00065         }
00066         return GLC_Mesh::boundingBox();
00067 }
00068 
00069 // Return a copy of the current geometry
00070 GLC_Geometry* GLC_Box::clone() const
00071 {
00072         return new GLC_Box(*this);
00073 }
00074 
00075 
00077 // Set Functions
00079 
00080 // Set X length
00081 void GLC_Box::setLgX(double LgX)
00082 {
00083         Q_ASSERT(LgX > 0);
00084         m_dLgX= LgX;
00085 
00086         GLC_Mesh::clearMeshWireAndBoundingBox();
00087 }
00088 
00089 // Set Y length
00090 void GLC_Box::setLgY(double LgY)
00091 {
00092         Q_ASSERT(LgY > 0);
00093         m_dLgY= LgY;
00094 
00095         GLC_Mesh::clearMeshWireAndBoundingBox();
00096 }
00097 
00098 // Set Z length
00099 void GLC_Box::setLgZ(double LgZ)
00100 {
00101         Q_ASSERT(LgZ > 0);
00102         m_dLgZ= LgZ;
00103 
00104         GLC_Mesh::clearMeshWireAndBoundingBox();
00105 }
00106 
00108 // Private OpenGL functions
00110 
00111 // Box Set Up
00112 void GLC_Box::glDraw(const GLC_RenderProperties& renderProperties)
00113 {
00114         if (GLC_Mesh::isEmpty())
00115         {
00116                 createMeshAndWire();
00117         }
00118 
00119         GLC_Mesh::glDraw(renderProperties);
00120 }
00121 // Create the box mesh
00122 void GLC_Box::createMeshAndWire()
00123 {
00124         Q_ASSERT(GLC_Mesh::isEmpty());
00125         createWire();
00126 
00127         const GLfloat lgX= static_cast<const GLfloat>(m_dLgX / 2.0);
00128         const GLfloat lgY= static_cast<const GLfloat>(m_dLgY / 2.0);
00129         const GLfloat lgZ= static_cast<const GLfloat>(m_dLgZ / 2.0);
00130 
00131         GLfloatVector verticeVector;
00132         GLfloatVector normalsVector;
00133         GLfloatVector texelVector;
00134 
00135         // Face 1
00136         verticeVector << -lgX; verticeVector << -lgY; verticeVector << lgZ;
00137         normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << 1.0f;
00138         texelVector << 0.0f; texelVector << 0.0f;
00139 
00140         verticeVector << lgX; verticeVector << -lgY; verticeVector << lgZ;
00141         normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << 1.0f;
00142         texelVector << 1.0f; texelVector << 0.0f;
00143 
00144         verticeVector << lgX; verticeVector << lgY; verticeVector << lgZ;
00145         normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << 1.0f;
00146         texelVector << 1.0f; texelVector << 1.0f;
00147 
00148         verticeVector << -lgX; verticeVector << lgY; verticeVector << lgZ;
00149         normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << 1.0f;
00150         texelVector << 0.0f; texelVector << 1.0f;
00151 
00152         // Face 2
00153         verticeVector << lgX; verticeVector << -lgY; verticeVector << lgZ;
00154         normalsVector << 1.0f; normalsVector << 0.0f; normalsVector << 0.0f;
00155         texelVector << 0.0f; texelVector << 0.0f;
00156 
00157         verticeVector << lgX; verticeVector << -lgY; verticeVector << -lgZ;
00158         normalsVector << 1.0f; normalsVector << 0.0f; normalsVector << 0.0f;
00159         texelVector << 1.0f; texelVector << 0.0f;
00160 
00161         verticeVector << lgX; verticeVector << lgY; verticeVector << -lgZ;
00162         normalsVector << 1.0f; normalsVector << 0.0f; normalsVector << 0.0f;
00163         texelVector << 1.0f; texelVector << 1.0f;
00164 
00165         verticeVector << lgX; verticeVector << lgY; verticeVector << lgZ;
00166         normalsVector << 1.0f; normalsVector << 0.0f; normalsVector << 0.0f;
00167         texelVector << 0.0f; texelVector << 1.0f;
00168 
00169         // Face 3
00170         verticeVector << -lgX; verticeVector << -lgY; verticeVector << -lgZ;
00171         normalsVector << -1.0f; normalsVector << 0.0f; normalsVector << 0.0f;
00172         texelVector << 0.0f; texelVector << 0.0f;
00173 
00174         verticeVector << -lgX; verticeVector << -lgY; verticeVector << lgZ;
00175         normalsVector << -1.0f; normalsVector << 0.0f; normalsVector << 0.0f;
00176         texelVector << 1.0f; texelVector << 0.0f;
00177 
00178         verticeVector << -lgX; verticeVector << lgY; verticeVector << lgZ;
00179         normalsVector << -1.0f; normalsVector << 0.0f; normalsVector << 0.0f;
00180         texelVector << 1.0f; texelVector << 1.0f;
00181 
00182         verticeVector << -lgX; verticeVector << lgY; verticeVector << -lgZ;
00183         normalsVector << -1.0f; normalsVector << 0.0f; normalsVector << 0.0f;
00184         texelVector << 0.0f; texelVector << 1.0f;
00185 
00186         // Face 4
00187         verticeVector << lgX; verticeVector << -lgY; verticeVector << -lgZ;
00188         normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << -1.0f;
00189         texelVector << 0.0f; texelVector << 0.0f;
00190 
00191         verticeVector << -lgX; verticeVector << -lgY; verticeVector << -lgZ;
00192         normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << -1.0f;
00193         texelVector << 1.0f; texelVector << 0.0f;
00194 
00195         verticeVector << -lgX; verticeVector << lgY; verticeVector << -lgZ;
00196         normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << -1.0f;
00197         texelVector << 1.0f; texelVector << 1.0f;
00198 
00199         verticeVector << lgX; verticeVector << lgY; verticeVector << -lgZ;
00200         normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << -1.0f;
00201         texelVector << 0.0f; texelVector << 1.0f;
00202 
00203         // Face 5
00204         verticeVector << -lgX; verticeVector << lgY; verticeVector << lgZ;
00205         normalsVector << 0.0f; normalsVector << 1.0f; normalsVector << 0.0f;
00206         texelVector << 0.0f; texelVector << 0.0f;
00207 
00208         verticeVector << lgX; verticeVector << lgY; verticeVector << lgZ;
00209         normalsVector << 0.0f; normalsVector << 1.0f; normalsVector << 0.0f;
00210         texelVector << 1.0f; texelVector << 0.0f;
00211 
00212         verticeVector << lgX; verticeVector << lgY; verticeVector << -lgZ;
00213         normalsVector << 0.0f; normalsVector << 1.0f; normalsVector << 0.0f;
00214         texelVector << 1.0f; texelVector << 1.0f;
00215 
00216         verticeVector << -lgX; verticeVector << lgY; verticeVector << -lgZ;
00217         normalsVector << 0.0f; normalsVector << 1.0f; normalsVector << 0.0f;
00218         texelVector << 0.0f; texelVector << 1.0f;
00219 
00220         // Face 6
00221         verticeVector << -lgX; verticeVector << -lgY; verticeVector << -lgZ;
00222         normalsVector << 0.0f; normalsVector << -1.0f; normalsVector << 0.0f;
00223         texelVector << 0.0f; texelVector << 0.0f;
00224 
00225         verticeVector << lgX; verticeVector << -lgY; verticeVector << -lgZ;
00226         normalsVector << 0.0f; normalsVector << -1.0f; normalsVector << 0.0f;
00227         texelVector << 1.0f; texelVector << 0.0f;
00228 
00229         verticeVector << lgX; verticeVector << -lgY; verticeVector << lgZ;
00230         normalsVector << 0.0f; normalsVector << -1.0f; normalsVector << 0.0f;
00231         texelVector << 1.0f; texelVector << 1.0f;
00232 
00233         verticeVector << -lgX; verticeVector << -lgY; verticeVector << lgZ;
00234         normalsVector << 0.0f; normalsVector << -1.0f; normalsVector << 0.0f;
00235         texelVector << 0.0f; texelVector << 1.0f;
00236 
00237         // Add bulk data in to the mesh
00238         GLC_Mesh::addVertice(verticeVector);
00239         GLC_Mesh::addNormals(normalsVector);
00240         GLC_Mesh::addTexels(texelVector);
00241 
00242         // Set the material to use
00243         GLC_Material* pMaterial;
00244         if (hasMaterial())
00245         {
00246                 pMaterial= this->firstMaterial();
00247         }
00248         else
00249         {
00250                 pMaterial= new GLC_Material();
00251         }
00252 
00253         IndexList index;
00254         // Face 1
00255         index << 0 << 1 << 3 << 2;
00256         GLC_Mesh::addTrianglesStrip(pMaterial, index);
00257         index.clear();
00258         // Face 2
00259         index << 4 << 5 << 7 << 6;
00260         GLC_Mesh::addTrianglesStrip(pMaterial, index);
00261         index.clear();
00262         // Face 3
00263         index << 8 << 9 << 11 << 10;
00264         GLC_Mesh::addTrianglesStrip(pMaterial, index);
00265         index.clear();
00266         // Face 4
00267         index << 12 << 13 << 15 << 14;
00268         GLC_Mesh::addTrianglesStrip(pMaterial, index);
00269         index.clear();
00270         // Face 5
00271         index << 16 << 17 << 19 << 18;
00272         GLC_Mesh::addTrianglesStrip(pMaterial, index);
00273         index.clear();
00274         // Face 6
00275         index << 20 << 21 << 23 << 22;
00276         GLC_Mesh::addTrianglesStrip(pMaterial, index);
00277         index.clear();
00278 
00279         GLC_Mesh::finish();
00280 }
00281 
00282 // Create the wire of the mesh
00283 void GLC_Box::createWire()
00284 {
00285         Q_ASSERT(m_WireData.isEmpty());
00286 
00287         const GLfloat lgX= static_cast<const GLfloat>(m_dLgX / 2.0);
00288         const GLfloat lgY= static_cast<const GLfloat>(m_dLgY / 2.0);
00289         const GLfloat lgZ= static_cast<const GLfloat>(m_dLgZ / 2.0);
00290 
00291         // Float vector
00292         GLfloatVector floatVector;
00293         floatVector << lgX << lgY << lgZ;
00294         floatVector << lgX << lgY << -lgZ;
00295         floatVector << -lgX << lgY << -lgZ;
00296         floatVector << -lgX << lgY << lgZ;
00297         floatVector << lgX << lgY << lgZ;
00298         GLC_Geometry::addVerticeGroup(floatVector);
00299         floatVector.clear();
00300 
00301         floatVector << lgX << -lgY << lgZ;
00302         floatVector << lgX << -lgY << -lgZ;
00303         floatVector << -lgX << -lgY << -lgZ;
00304         floatVector << -lgX << -lgY << lgZ;
00305         floatVector << lgX << -lgY << lgZ;
00306         GLC_Geometry::addVerticeGroup(floatVector);
00307         floatVector.clear();
00308 
00309         floatVector << lgX << lgY << lgZ;
00310         floatVector << lgX << -lgY << lgZ;
00311         GLC_Geometry::addVerticeGroup(floatVector);
00312         floatVector.clear();
00313 
00314         floatVector << lgX << lgY << -lgZ;
00315         floatVector << lgX << -lgY << -lgZ;
00316         GLC_Geometry::addVerticeGroup(floatVector);
00317         floatVector.clear();
00318 
00319         floatVector << -lgX << lgY << -lgZ;
00320         floatVector << -lgX << -lgY << -lgZ;
00321         GLC_Geometry::addVerticeGroup(floatVector);
00322         floatVector.clear();
00323 
00324         floatVector << -lgX << lgY << lgZ;
00325         floatVector << -lgX << -lgY << lgZ;
00326         GLC_Geometry::addVerticeGroup(floatVector);
00327         floatVector.clear();
00328 }
00329 

SourceForge.net Logo

©2005-2011 Laurent Ribon