Developer Documentation
GLPrimitives.hh
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 
51 
52 //=============================================================================
53 //
54 // CLASS GLPrimitive
55 //
56 //=============================================================================
57 
58 #ifndef ACG_GLPRIMITIVES_HH
59 #define ACG_GLPRIMITIVES_HH
60 
61 
62 //== INCLUDES =================================================================
63 
64 
65 #include <ACG/Config/ACGDefines.hh>
66 #include <ACG/GL/GLState.hh>
67 #include <ACG/Math/VectorT.hh>
68 #include <ACG/GL/VertexDeclaration.hh>
69 
70 
71 //== NAMESPACES ===============================================================
72 
73 namespace ACG {
74 
75 //== CLASS DEFINITION =========================================================
76 
77 
78 class ACGDLLEXPORT GLPrimitive {
79 public:
80  GLPrimitive();
81  virtual ~GLPrimitive();
82 
83  // bind vbo + gl draw call
84  void draw_primitive();
85 
86  // add to deferred draw call to renderer
87  void addToRenderer_primitive(class IRenderer* _renderer, struct RenderObject* _ro);
88 
89  // Triangle or line count must be known before updateVBO.
90  // A GLPrimitive can consist of either only lines or only triangles.
91  // If getNumTriangles returns nonzero, its assumed to consist of tris only.
92  // Otherwise, getNumLines has to return nonzero and GLPrimitive is treated as a line-list.
93  virtual int getNumTriangles() = 0;
94  virtual int getNumLines() {return 0;}
95 
96  // get opengl vbo id
97  unsigned int getVBO();
98 
99  const VertexDeclaration* getVertexDecl() const;
100 
101  enum NormalOrientation {OUTSIDE, INSIDE};
102 
103 protected:
104 
105  // calls addTriangleToVBO to fill vertex buffer
106  virtual void updateVBO() = 0;
107 
108  void addTriangleToVBO(const ACG::Vec3f* _p, const ACG::Vec3f* _n, const ACG::Vec2f* _tex);
109  void addLineToVBO(const ACG::Vec3f* _p, const ACG::Vec3f* _n, const ACG::Vec2f* _tex);
110 
111  void bindVBO();
112 
113  bool checkVBO();
114 
115  void unBindVBO();
116 
117  bool vboDataInvalid_;
118 
119  NormalOrientation normalOrientation_;
120 
121 private:
122 
123  void updateVBOData();
124 
125  int numTris_;
126  int numLines_;
127  float* vboData_;
128  int curTriPtr_;
129 
130  VertexDeclaration vertexDecl_;
131 
132  unsigned int vbo_;
133 };
134 
135 //------------------------------------------------------------------------
136 
137 class ACGDLLEXPORT GLSphere: public GLPrimitive {
138 public:
139 
140  GLSphere(int _slices, int _stacks);
141  ~GLSphere();
142 
143  void draw(GLState& _state, float _radius, const ACG::Vec3f& _center = ACG::Vec3f(0.0f, 0.0f, 0.0f));
144 
145  void addToRenderer(class IRenderer* _renderer, const struct RenderObject* _base, float _radius, const ACG::Vec3f& _center = ACG::Vec3f(0.0f, 0.0f, 0.0f));
146 
147  int getNumTriangles();
148 
149 private:
150 
151  void updateVBO();
152 
153  void addTriangle(int sl0, int st0, int sl1, int st1, int sl2, int st2);
154 
155  ACG::Vec3f positionOnSphere(int _sliceNumber, int _stackNumber);
156  ACG::Vec2f texCoordOnSphere(int _sliceNumber, int _stackNumber);
157 
158 private:
159 
160  int slices_;
161  int stacks_;
162 
163 };
164 
165 //------------------------------------------------------------------------
166 
167 class ACGDLLEXPORT GLCone: public GLPrimitive {
168 public:
169 
170  GLCone(int _slices, int _stacks, float _bottomRadius, float _topRadius, bool _bottomCap_, bool _topCap);
171  ~GLCone();
172 
173  void draw(
174  GLState& _state,
175  float _height,
176  const ACG::Vec3f& _center = ACG::Vec3f(0.0f, 0.0f, 0.0f),
177  ACG::Vec3f _upDir = ACG::Vec3f(0.0f, 0.0f, 1.0f));
178 
179 
180  void addToRenderer(class IRenderer* _renderer, const struct RenderObject* _base,
181  float _height,
182  const ACG::Vec3f& _center = ACG::Vec3f(0.0f, 0.0f, 0.0f),
183  ACG::Vec3f _upDir = ACG::Vec3f(0.0f, 0.0f, 1.0f),
184  float _radiusScale = 1.0f);
185 
186  int getNumTriangles();
187 
188  void updateVBO();
189  void setBottomRadius(float _bottomRadius);
190  void setTopRadius(float _topRadius);
191  void setNormalOrientation(NormalOrientation orienation);
192 
193 private:
194 
195  void addTriangle(int sl0, int st0, int sl1, int st1, int sl2, int st2);
196 
197  ACG::Vec3f positionOnCone(int _sliceNumber, int _stackNumber);
198  ACG::Vec2f texCoordOnCone(int _sliceNumber, int _stackNumber);
199  ACG::Vec3f normalOnCone(int _sliceNumber, int _stackNumber);
200 
201 private:
202 
203  int slices_;
204  int stacks_;
205 
206  float bottomRadius_;
207  float topRadius_;
208 
209  bool bottomCap_;
210  bool topCap_;
211 
212 };
213 
214 //------------------------------------------------------------------------
215 
216 class ACGDLLEXPORT GLCylinder: public GLCone {
217 public:
218  GLCylinder(int _slices, int _stacks, float _radius, bool _bottomCap, bool _topCap);
219 };
220 
221 //------------------------------------------------------------------------
222 
223 class ACGDLLEXPORT GLPartialDisk: public GLPrimitive {
224 public:
225  GLPartialDisk(int _slices, int _loops, float _innerRadius, float _outerRadius, float _startAngle, float _sweepAngle);
226 
227  void setInnerRadius(float _innerRadius);
228  void setOuterRadius(float _outerRadius);
229  int getNumTriangles();
230 
231  void draw(
232  GLState& _state,
233  const ACG::Vec3f& _center = ACG::Vec3f(0.0f, 0.0f, 0.0f),
234  ACG::Vec3f _upDir = ACG::Vec3f(0.0f, 0.0f, 1.0f));
235 
236 protected:
237  void updateVBO();
238 
239 private:
240  int slices_;
241  int loops_;
242  float innerRadius_;
243  float outerRadius_;
244  float startAngle_;
245  float sweepAngle_;
246 };
247 
248 //------------------------------------------------------------------------
249 
250 class ACGDLLEXPORT GLDisk: public GLPartialDisk {
251 public:
252  GLDisk(int _slices, int _loops, float _innerRadius, float _outerRadius);
253 };
254 
255 //------------------------------------------------------------------------
256 
257 // axis-aligned unit cube centered at origin
258 class ACGDLLEXPORT GLBox: public GLPrimitive {
259 public:
260 
261  GLBox();
262  ~GLBox();
263 
264  int getNumTriangles();
265 
266 private:
267 
268  void updateVBO();
269 };
270 
271 //------------------------------------------------------------------------
272 
273 // axis-aligned unit cube centered at origin, only lines
274 class ACGDLLEXPORT GLLineBox: public GLPrimitive {
275 public:
276 
277  GLLineBox();
278  ~GLLineBox();
279 
280  int getNumTriangles();
281  int getNumLines();
282 
283 private:
284 
285  void updateVBO();
286 };
287 
288 //------------------------------------------------------------------------
289 
290 
291 } // namespace ACG
292 
293 #endif // ACG_GLPRIMITIVES_HH defined
Class to define the vertex input layout.
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
Interface class between scenegraph and renderer.