Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 (fixed function mode)
84  // use this function in compatibility profile
85  void draw_primitive();
86 
87  // activate vertex declaration + gl draw call (shader mode)
88  // _program may be nullptr, in that case the attribute locations are as follows.
89  // 0 : float3 position
90  // 1 : float3 normal
91  // 2 : float2 texcoord
92  void draw_primitive(GLSL::Program* _program);
93 
94  // add to deferred draw call to renderer
95  void addToRenderer_primitive(class IRenderer* _renderer, struct RenderObject* _ro);
96 
97  // Triangle or line count must be known before updateVBO.
98  // A GLPrimitive can consist of either only lines or only triangles.
99  // If getNumTriangles returns nonzero, its assumed to consist of tris only.
100  // Otherwise, getNumLines has to return nonzero and GLPrimitive is treated as a line-list.
101  virtual int getNumTriangles() = 0;
102  virtual int getNumLines() {return 0;}
103 
104  // get opengl vbo id
105  unsigned int getVBO();
106 
107  const VertexDeclaration* getVertexDecl() const;
108 
109  enum NormalOrientation {OUTSIDE, INSIDE};
110 
111 protected:
112 
113  // calls addTriangleToVBO to fill vertex buffer
114  virtual void updateVBO() = 0;
115 
116  void addTriangleToVBO(const ACG::Vec3f* _p, const ACG::Vec3f* _n, const ACG::Vec2f* _tex);
117  void addLineToVBO(const ACG::Vec3f* _p, const ACG::Vec3f* _n, const ACG::Vec2f* _tex);
118 
119  void bindVBO();
120 
121  bool checkVBO();
122 
123  void unBindVBO();
124 
125  bool vboDataInvalid_;
126 
127  NormalOrientation normalOrientation_;
128 
129 private:
130 
131  void updateVBOData();
132 
133  int numTris_;
134  int numLines_;
135  float* vboData_;
136  int curTriPtr_;
137 
138  VertexDeclaration vertexDecl_;
139 
140  unsigned int vbo_;
141 };
142 
143 //------------------------------------------------------------------------
144 
145 class ACGDLLEXPORT GLSphere: public GLPrimitive {
146 public:
147 
148  GLSphere(int _slices, int _stacks);
149  ~GLSphere();
150 
151  void draw(GLState& _state, float _radius, const ACG::Vec3f& _center = ACG::Vec3f(0.0f, 0.0f, 0.0f));
152 
153  void addToRenderer(class IRenderer* _renderer, const struct RenderObject* _base, float _radius, const ACG::Vec3f& _center = ACG::Vec3f(0.0f, 0.0f, 0.0f));
154 
155  int getNumTriangles();
156 
157 private:
158 
159  void updateVBO();
160 
161  void addTriangle(int sl0, int st0, int sl1, int st1, int sl2, int st2);
162 
163  ACG::Vec3f positionOnSphere(int _sliceNumber, int _stackNumber);
164  ACG::Vec2f texCoordOnSphere(int _sliceNumber, int _stackNumber);
165 
166 private:
167 
168  int slices_;
169  int stacks_;
170 
171 };
172 
173 //------------------------------------------------------------------------
174 
175 class ACGDLLEXPORT GLCone: public GLPrimitive {
176 public:
177 
178  GLCone(int _slices, int _stacks, float _bottomRadius, float _topRadius, bool _bottomCap_, bool _topCap);
179  ~GLCone();
180 
181  void draw(
182  GLState& _state,
183  float _height,
184  const ACG::Vec3f& _center = ACG::Vec3f(0.0f, 0.0f, 0.0f),
185  ACG::Vec3f _upDir = ACG::Vec3f(0.0f, 0.0f, 1.0f));
186 
187 
188  void addToRenderer(class IRenderer* _renderer, const struct RenderObject* _base,
189  float _height,
190  const ACG::Vec3f& _center = ACG::Vec3f(0.0f, 0.0f, 0.0f),
191  ACG::Vec3f _upDir = ACG::Vec3f(0.0f, 0.0f, 1.0f),
192  float _radiusScale = 1.0f);
193 
194  int getNumTriangles();
195 
196  void updateVBO();
197  void setBottomRadius(float _bottomRadius);
198  void setTopRadius(float _topRadius);
199  void setNormalOrientation(NormalOrientation orienation);
200 
201 private:
202 
203  void addTriangle(int sl0, int st0, int sl1, int st1, int sl2, int st2);
204 
205  ACG::Vec3f positionOnCone(int _sliceNumber, int _stackNumber);
206  ACG::Vec2f texCoordOnCone(int _sliceNumber, int _stackNumber);
207  ACG::Vec3f normalOnCone(int _sliceNumber, int _stackNumber);
208 
209 private:
210 
211  int slices_;
212  int stacks_;
213 
214  float bottomRadius_;
215  float topRadius_;
216 
217  bool bottomCap_;
218  bool topCap_;
219 
220 };
221 
222 //------------------------------------------------------------------------
223 
224 class ACGDLLEXPORT GLCylinder: public GLCone {
225 public:
226  GLCylinder(int _slices, int _stacks, float _radius, bool _bottomCap, bool _topCap);
227 };
228 
229 //------------------------------------------------------------------------
230 
231 class ACGDLLEXPORT GLPartialDisk: public GLPrimitive {
232 public:
233  GLPartialDisk(int _slices, int _loops, float _innerRadius, float _outerRadius, float _startAngle, float _sweepAngle);
234 
235  void setInnerRadius(float _innerRadius);
236  void setOuterRadius(float _outerRadius);
237  int getNumTriangles();
238 
239  void draw(
240  GLState& _state,
241  const ACG::Vec3f& _center = ACG::Vec3f(0.0f, 0.0f, 0.0f),
242  ACG::Vec3f _upDir = ACG::Vec3f(0.0f, 0.0f, 1.0f));
243 
244 protected:
245  void updateVBO();
246 
247 private:
248  int slices_;
249  int loops_;
250  float innerRadius_;
251  float outerRadius_;
252  float startAngle_;
253  float sweepAngle_;
254 };
255 
256 //------------------------------------------------------------------------
257 
258 class ACGDLLEXPORT GLDisk: public GLPartialDisk {
259 public:
260  GLDisk(int _slices, int _loops, float _innerRadius, float _outerRadius);
261 };
262 
263 //------------------------------------------------------------------------
264 
265 // axis-aligned unit cube centered at origin
266 class ACGDLLEXPORT GLBox: public GLPrimitive {
267 public:
268 
269  GLBox();
270  ~GLBox();
271 
272  int getNumTriangles();
273 
274 private:
275 
276  void updateVBO();
277 };
278 
279 //------------------------------------------------------------------------
280 
281 // axis-aligned unit cube centered at origin, only lines
282 class ACGDLLEXPORT GLLineBox: public GLPrimitive {
283 public:
284 
285  GLLineBox();
286  ~GLLineBox();
287 
288  int getNumTriangles();
289  int getNumLines();
290 
291 private:
292 
293  void updateVBO();
294 };
295 
296 //------------------------------------------------------------------------
297 
298 
299 } // namespace ACG
300 
301 #endif // ACG_GLPRIMITIVES_HH defined
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
GLSL program class.
Definition: GLSLShader.hh:217
Class to define the vertex input layout.
Interface class between scenegraph and renderer.