GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_disc.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  http://glc-lib.sourceforge.net
6 
7  GLC-lib is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  GLC-lib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with GLC-lib; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  *****************************************************************************/
23 
24 #include "glc_disc.h"
25 
26 GLC_Disc::GLC_Disc(double radius, double angle, int discretization)
27 : GLC_Mesh()
28 , m_Radius(radius)
29 , m_Discret(discretization)
30 , m_Angle(angle)
31 , m_Step(0)
32 {
34 }
35 
37 : GLC_Mesh(disc)
38 , m_Radius(disc.m_Radius)
39 , m_Discret(disc.m_Discret)
40 , m_Angle(disc.m_Angle)
41 , m_Step(disc.m_Step)
42 {
44 }
45 
47 {
48 
49 }
50 
52 // Get Functions
54 
56 {
57  if (GLC_Mesh::isEmpty())
58  {
60  }
61  return GLC_Mesh::boundingBox();
62 }
63 
65 {
66  return new GLC_Disc(*this);
67 }
68 
70 // Set Functions
73 {
74  if (this != &disc)
75  {
76  // Call the operator of the super class
77  GLC_Mesh::operator=(disc);
78 
79  m_Radius= disc.m_Radius;
80  m_Discret= disc.m_Discret;
81  m_Angle= disc.m_Angle;
82  m_Step= disc.m_Step;
83  }
84  return *this;
85 }
86 
87 void GLC_Disc::setRadius(double radius)
88 {
89  Q_ASSERT(radius > 0.0);
91 
93 }
94 
95 void GLC_Disc::setDiscretion(int targetDiscret)
96 {
97  Q_ASSERT(targetDiscret > 0);
98  if (targetDiscret != m_Discret)
99  {
100  m_Discret= targetDiscret;
101  if (m_Discret < 6) m_Discret= 6;
102 
104  }
105 }
106 
107 void GLC_Disc::setAngle(double angle)
108 {
109  Q_ASSERT(angle > 0.0);
110  m_Angle= angle;
111 
113 }
115 // Private Opengl functions
117 void GLC_Disc::glDraw(const GLC_RenderProperties& renderProperties)
118 {
119 
120  if (GLC_Mesh::isEmpty())
121  {
123  }
124  GLC_Mesh::glDraw(renderProperties);
125 }
126 
127 // Create the cylinder mesh
129 {
130  Q_ASSERT(GLC_Mesh::isEmpty());
131  Q_ASSERT(m_WireData.isEmpty());
132 
133  m_Step= static_cast<GLuint>(static_cast<double>(m_Discret) * (m_Angle / (2 * glc::PI)));
134  if (m_Step < 2) m_Step= 2;
135 
136  // Create cosinus and sinus array according to the discretion and radius
137  const int vertexNumber= m_Step + 1;
138 
139  QVector<float> cosArray(vertexNumber);
140  QVector<float> sinArray(vertexNumber);
141 
142  const double angle= m_Angle / static_cast<double>(m_Step);
143  for (int i= 0; i < vertexNumber; ++i)
144  {
145  const double cosValue= cos(static_cast<double>(i) * angle);
146  const double sinValue= sin(static_cast<double>(i) * angle);
147 
148  cosArray[i]= static_cast<GLfloat>(m_Radius * cosValue);
149  sinArray[i]= static_cast<GLfloat>(m_Radius * sinValue);
150  }
151 
152  // Mesh Data
153  GLfloatVector verticeVector(vertexNumber * 3);
154  GLfloatVector normalsVector(vertexNumber * 3);
155  GLfloatVector texelVector(vertexNumber * 2);
156 
157  // Wire Data
158  GLfloatVector wireData(vertexNumber * 3);
159 
160  for (int i= 0; i < vertexNumber; ++i)
161  {
162  verticeVector[3 * i]= cosArray[i];
163  verticeVector[3 * i + 1]= sinArray[i];
164  verticeVector[3 * i + 2]= 0.0f;
165 
166  normalsVector[3 * i]= 0.0f;
167  normalsVector[3 * i + 1]= 0.0f;
168  normalsVector[3 * i + 2]= 1.0f;
169 
170  texelVector[2 * i]= texelVector[i];
171  texelVector[2 * i + 1]= 0.0f;
172 
173  wireData[3 * i]= cosArray[i];
174  wireData[3 * i + 1]= sinArray[i];
175  wireData[3 * i + 2]= 0.0f;
176  }
177  // Center Point
178  verticeVector << 0.0f << 0.0f << 0.0f;
179  normalsVector << 0.0f << 0.0f << 1.0f;
180  texelVector << 0.5f << 0.5f;
181 
182  if (!qFuzzyCompare(m_Angle, (2.0 * glc::PI)))
183  {
184  wireData << 0.0f << 0.0f << 0.0f;
185  wireData << wireData[0] << wireData[1] << wireData[2];
186  }
187 
188  // Add bulk data in to the mesh
189  GLC_Mesh::addVertice(verticeVector);
190  GLC_Mesh::addNormals(normalsVector);
191  GLC_Mesh::addTexels(texelVector);
192 
193  // Add polyline to wire data
195 
196  // Set the material to use
197  GLC_Material* pDiscMaterial;
198  if (hasMaterial())
199  {
200  pDiscMaterial= this->firstMaterial();
201  }
202  else
203  {
204  pDiscMaterial= new GLC_Material();
205  }
206 
207  IndexList discIndex;
208  discIndex << vertexNumber;
209  for (int i= 0; i < vertexNumber; ++i)
210  {
211  discIndex << i;
212  }
213 
214  addTrianglesFan(pDiscMaterial, discIndex);
215 
216  finish();
217 }

©2005-2013 Laurent Ribon