glc_cone.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  *****************************************************************************/
00023 
00024 #include "glc_cone.h"
00025 
00026 // Class chunk id
00027 quint32 GLC_Cone::m_ChunkId= 0xA709;
00028 
00029 GLC_Cone::GLC_Cone(double dRadius, double dLength)
00030 :GLC_Mesh()
00031 , m_Radius(dRadius)
00032 , m_Length(dLength)
00033 , m_Discret(glc::GLC_POLYDISCRET)       // Default discretion
00034 {
00035         Q_ASSERT((m_Radius > 0.0) && (m_Length > 0.0));
00036 }
00037 
00038 GLC_Cone::GLC_Cone(const GLC_Cone& sourceCone)
00039 :GLC_Mesh(sourceCone)
00040 , m_Radius(sourceCone.m_Radius)
00041 , m_Length(sourceCone.m_Length)
00042 , m_Discret(sourceCone.m_Discret)
00043 {
00044 
00045 }
00046 
00047 GLC_Cone::~GLC_Cone()
00048 {
00049 
00050 }
00051 
00053 // Get Functions
00055 
00056 quint32 GLC_Cone::chunckID()
00057 {
00058         return m_ChunkId;
00059 }
00060 
00061 
00062 GLC_Geometry* GLC_Cone::clone() const
00063 {
00064         return new GLC_Cone(*this);
00065 }
00066 
00067 
00068 const GLC_BoundingBox& GLC_Cone::boundingBox()
00069 {
00070         if (GLC_Mesh::isEmpty())
00071         {
00072                 createMeshAndWire();
00073         }
00074         return GLC_Mesh::boundingBox();
00075 }
00076 
00078 // Set Functions
00080 
00081 void GLC_Cone::setLength(double Length)
00082 {
00083         Q_ASSERT(Length > 0.0);
00084         m_Length= Length;
00085 
00086         GLC_Mesh::clearMeshWireAndBoundingBox();
00087 }
00088 
00089 
00090 void GLC_Cone::setRadius(double Radius)
00091 {
00092         Q_ASSERT(Radius > 0.0);
00093         m_Radius= Radius;
00094 
00095         GLC_Mesh::clearMeshWireAndBoundingBox();
00096 }
00097 
00098 
00099 void GLC_Cone::setDiscretion(int TargetDiscret)
00100 {
00101         Q_ASSERT(TargetDiscret > 0);
00102         if (TargetDiscret != m_Discret)
00103         {
00104                 m_Discret= TargetDiscret;
00105                 if (m_Discret < 6) m_Discret= 6;
00106 
00107                 GLC_Mesh::clearMeshWireAndBoundingBox();
00108         }
00109 }
00110 
00112 // Private Opengl functions
00114 
00115 void GLC_Cone::glDraw(const GLC_RenderProperties& renderProperties)
00116 {
00117 
00118         if (GLC_Mesh::isEmpty())
00119         {
00120                 createMeshAndWire();
00121         }
00122 
00123         GLC_Mesh::glDraw(renderProperties);
00124 }
00125 
00126 
00127 void GLC_Cone::createMeshAndWire()
00128 {
00129         Q_ASSERT(GLC_Mesh::isEmpty());
00130         Q_ASSERT(m_WireData.isEmpty());
00131 
00132         // Create cosinus and sinus array according to the discretion and radius
00133         const int vertexNumber= m_Discret + 1;
00134         // Normals values
00135         QVector<float> cosNormalArray(vertexNumber);
00136         QVector<float> sinNormalArray(vertexNumber);
00137 
00138         QVector<float> cosArray(vertexNumber);
00139         QVector<float> sinArray(vertexNumber);
00140 
00141         const double angle= (2.0 * glc::PI) / static_cast<double>(m_Discret);
00142 
00143         // Normal Z value
00144         GLC_Vector3d normalVector(1.0, 0.0, 0.0);
00145         GLC_Matrix4x4 rotation(glc::Y_AXIS, -atan(m_Radius / m_Length));
00146         normalVector= rotation * normalVector;
00147         const float normalZ= static_cast<float>(normalVector.z());
00148         const double factor= normalVector.x(); // Normailsation factor
00149 
00150         for (int i= 0; i < vertexNumber; ++i)
00151         {
00152                 const double cosValue= cos(static_cast<double>(i) * angle);
00153                 const double sinValue= sin(static_cast<double>(i) * angle);
00154 
00155                 cosNormalArray[i]= static_cast<GLfloat>(factor * cosValue);
00156                 sinNormalArray[i]= static_cast<GLfloat>(factor * sinValue);
00157 
00158                 cosArray[i]= static_cast<GLfloat>(m_Radius * cosValue);
00159                 sinArray[i]= static_cast<GLfloat>(m_Radius * sinValue);
00160         }
00161 
00162 
00163         // Mesh Data
00164         GLfloatVector verticeVector;
00165         GLfloatVector normalsVector;
00166         GLfloatVector texelVector;
00167 
00168         // Wire Data
00169         GLfloatVector bottomWireData(vertexNumber * 3);
00170 
00171         const int size= vertexNumber * 3;
00172         verticeVector.resize(3 * size);
00173         normalsVector.resize(3 * size);
00174         texelVector.resize(2 * size);
00175 
00176         for (int i= 0; i < vertexNumber; ++i)
00177         {
00178                 // Bottom Mesh
00179                 verticeVector[3 * i]= cosArray[i];
00180                 verticeVector[3 * i + 1]= sinArray[i];
00181                 verticeVector[3 * i + 2]= 0.0f;
00182 
00183                 normalsVector[3 * i]= cosNormalArray[i];
00184                 normalsVector[3 * i + 1]= sinNormalArray[i];
00185                 normalsVector[3 * i + 2]= normalZ;
00186 
00187                 texelVector[2 * i]= static_cast<float>(i) / static_cast<float>(m_Discret);
00188                 texelVector[2 * i + 1]= 0.0f;
00189 
00190                 // Bottom Wire
00191                 bottomWireData[3 * i]= cosArray[i];
00192                 bottomWireData[3 * i + 1]= sinArray[i];
00193                 bottomWireData[3 * i + 2]= 0.0f;
00194 
00195                 // Top
00196                 verticeVector[3 * i + 3 * vertexNumber]= 0.0f;
00197                 verticeVector[3 * i + 1 + 3 * vertexNumber]= 0.0f;
00198                 verticeVector[3 * i + 2 + 3 * vertexNumber]= static_cast<float>(m_Length);
00199 
00200                 normalsVector[3 * i + 3 * vertexNumber]= cosNormalArray[i];
00201                 normalsVector[3 * i + 1 + 3 * vertexNumber]= sinNormalArray[i];
00202                 normalsVector[3 * i + 2 + 3 * vertexNumber]= normalZ;
00203 
00204                 texelVector[2 * i + 2 * vertexNumber]= texelVector[i];
00205                 texelVector[2 * i + 1 + 2 * vertexNumber]= 1.0f;
00206 
00207                 // Bottom Cap ends
00208                 verticeVector[3 * i + 2 * 3 * vertexNumber]= cosArray[i];
00209                 verticeVector[3 * i + 1 + 2 * 3 * vertexNumber]= sinArray[i];
00210                 verticeVector[3 * i + 2 + 2 * 3 * vertexNumber]= 0.0f;
00211 
00212                 normalsVector[3 * i + 2 * 3 * vertexNumber]= 0.0f;
00213                 normalsVector[3 * i + 1 + 2 * 3 * vertexNumber]= 0.0f;
00214                 normalsVector[3 * i + 2 + 2 * 3 * vertexNumber]= -1.0f;
00215 
00216                 texelVector[2 * i + 2 * 2 * vertexNumber]= texelVector[i];
00217                 texelVector[2 * i + 1 + 2 * 2 * vertexNumber]= 0.0f;
00218 
00219         }
00220 
00221         // Add bulk data in to the mesh
00222         GLC_Mesh::addVertice(verticeVector);
00223         GLC_Mesh::addNormals(normalsVector);
00224         GLC_Mesh::addTexels(texelVector);
00225 
00226         // Add polyline to wire data
00227         GLC_Geometry::addVerticeGroup(bottomWireData);
00228 
00229         // Set the material to use
00230         GLC_Material* pCylinderMaterial;
00231         if (hasMaterial())
00232         {
00233                 pCylinderMaterial= this->firstMaterial();
00234         }
00235         else
00236         {
00237                 pCylinderMaterial= new GLC_Material();
00238         }
00239 
00240         IndexList circumferenceStrips;
00241         // Create the index
00242         for (int i= 0; i < vertexNumber; ++i)
00243         {
00244                 circumferenceStrips.append(i + vertexNumber);
00245                 circumferenceStrips.append(i);
00246         }
00247         addTrianglesStrip(pCylinderMaterial, circumferenceStrips);
00248 
00249         {
00250                 IndexList bottomCap;
00251                 IndexList topCap;
00252                 int id1= 0;
00253                 int id2= m_Discret - 1;
00254                 const int size= m_Discret / 2 + (m_Discret % 2);
00255                 for (int i= 0; i < size; ++i)
00256                 {
00257                         bottomCap.append(id1 + 2 * vertexNumber);
00258                         bottomCap.append(id2 + 2 * vertexNumber);
00259 
00260                         id1+= 1;
00261                         id2-= 1;
00262                 }
00263                 addTrianglesStrip(pCylinderMaterial, bottomCap);
00264         }
00265 
00266         finish();
00267 }
00268 

SourceForge.net Logo

©2005-2011 Laurent Ribon