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

SourceForge.net Logo

©2005 Laurent Ribon