Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Geometry.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef Geometry_INCLUDE_ONCE
33 #define Geometry_INCLUDE_ONCE
34 
37 #include <vlCore/Vector2.hpp>
38 #include <vlCore/Vector4.hpp>
40 #include <vlCore/vlnamespace.hpp>
41 #include <vlCore/Log.hpp>
42 #include <vlCore/Say.hpp>
43 #include <vlCore/Colors.hpp>
46 #include <vlCore/Collection.hpp>
47 
48 namespace vl
49 {
50  class OpenGLContext;
51 
52  //------------------------------------------------------------------------------
53  // Geometry
54  //------------------------------------------------------------------------------
67  {
69 
70  // use deepCopy() and shallowCopy() instead
71  // Geometry(const Geometry& other): Renderable(other) { }
72  Geometry& operator=(const Geometry&) { return *this; }
73 
74  public:
76  Geometry();
77 
79  virtual ~Geometry();
80 
84  ref<Geometry> shallowCopy() const;
85 
89  Geometry& shallowCopyFrom(const Geometry&);
90 
94  ref<Geometry> deepCopy() const;
95 
99  Geometry& deepCopyFrom(const Geometry&);
100 
102  Collection<DrawCall>& drawCalls() { return mDrawCalls; }
103 
105  const Collection<DrawCall>& drawCalls() const { return mDrawCalls; }
106 
108  void setColorArray(const fvec4& color)
109  {
110  u32 vert_count = (u32)(vertexArray() ? vertexArray()->size() : 0);
111  VL_CHECK( vert_count )
112 
113  // try to minimize reallocations
114  ref<ArrayFloat4> color_array = colorArray()->as<ArrayFloat4>();
115  if ( ! color_array ) {
116  color_array = new ArrayFloat4;
117  }
118  if ( color_array->size() != vert_count ) {
119  color_array->resize(vert_count);
120  }
121  for( u32 i = 0; i < color_array->size(); ++i ) {
122  color_array->at( i ) = color;
123  }
124  color_array->setBufferObjectDirty();
125  setBufferObjectDirty();
126 
127  setColorArray(color_array.get());
128  }
129 
131  virtual void clearArrays(bool clear_draw_calls=true);
132 
133  // --- Renderable interface implementation ---
134 
136  virtual void updateDirtyBufferObject(EBufferObjectUpdateMode mode);
137 
139  virtual void deleteBufferObject();
140 
141  // ------------------------------------------------------------------------
142  // Geometry Tools
143  // ------------------------------------------------------------------------
144 
157  void computeNormals(bool verbose=false);
158 
162  bool flipNormals();
163 
165  void fixTriangleWinding();
166 
177  void transform(const mat4&matr, bool normalize = true);
178 
180  void convertDrawCallToDrawArrays();
181 
184  DrawCall* mergeTriangleStrips();
185 
187  void mergeDrawCallsWithPrimitiveRestart(EPrimitiveType primitive_type);
188 
190  void mergeDrawCallsWithMultiDrawElements(EPrimitiveType primitive_type);
191 
194  void mergeDrawCallsWithTriangles(EPrimitiveType primitive_type);
195 
198  void triangulateDrawCalls();
199 
203  void shrinkDrawCalls();
204 
207  void makeGLESFriendly();
208 
213  bool sortVertices();
214 
217  void regenerateVertices(const std::vector<u32>& map_new_to_old);
218 
220  void colorizePrimitives();
221 
230  // Based on:
231  // Lengyel, Eric. “Computing Tangent Space Basis Vectors for an Arbitrary Mesh”. Terathon Software 3D Graphics Library, 2001.
232  // http://www.terathon.com/code/tangent.html
233  static void computeTangentSpace(
234  u32 vert_count,
235  const vl::fvec3 *vertex,
236  const vl::fvec3* normal,
237  const vl::fvec2 *texcoord,
238  const vl::DrawCall* primitives,
239  vl::fvec3 *tangent,
240  vl::fvec3 *bitangent );
241 
242  // ------------------------------------------------------------------------
243  // IVertexAttribSet Interface Implementation
244  // ------------------------------------------------------------------------
245 
246  void setVertexArray(ArrayAbstract* data);
247 
248  const ArrayAbstract* vertexArray() const { return mVertexAttribArrays[VA_Position].get(); }
249 
250  ArrayAbstract* vertexArray() { return mVertexAttribArrays[VA_Position].get(); }
251 
252  void setNormalArray(ArrayAbstract* data);
253 
254  const ArrayAbstract* normalArray() const { return mVertexAttribArrays[VA_Normal].get(); }
255 
256  ArrayAbstract* normalArray() { return mVertexAttribArrays[VA_Normal].get(); }
257 
258  void setColorArray(ArrayAbstract* data);
259 
260  const ArrayAbstract* colorArray() const { return mVertexAttribArrays[VA_Color].get(); }
261 
262  ArrayAbstract* colorArray() { return mVertexAttribArrays[VA_Color].get(); }
263 
264  void setSecondaryColorArray(ArrayAbstract* data);
265 
266  const ArrayAbstract* secondaryColorArray() const { return mVertexAttribArrays[VA_SecondaryColor].get(); }
267 
268  ArrayAbstract* secondaryColorArray() { return mVertexAttribArrays[VA_SecondaryColor].get(); }
269 
270  void setFogCoordArray(ArrayAbstract* data);
271 
272  const ArrayAbstract* fogCoordArray() const { return mVertexAttribArrays[VA_FogCoord].get(); }
273 
274  ArrayAbstract* fogCoordArray() { return mVertexAttribArrays[VA_FogCoord].get(); }
275 
276  void setTexCoordArray(int tex_unit, ArrayAbstract* data);
277 
278  const ArrayAbstract* texCoordArray(int tex_unit) const { return mVertexAttribArrays[VA_TexCoord0 + tex_unit].get(); }
279 
280  ArrayAbstract* texCoordArray(int tex_unit) { return mVertexAttribArrays[VA_TexCoord0 + tex_unit].get(); }
281 
282  void setVertexAttribArray(int attrib_location, const ArrayAbstract* info);
283 
284  const ArrayAbstract* vertexAttribArray(int attrib_location) const;
285 
286  ArrayAbstract* vertexAttribArray(int attrib_location);
287 
288  protected:
289  virtual void computeBounds_Implementation();
290 
291  virtual void render_Implementation(const Actor* actor, const Shader* shader, const Camera* camera, OpenGLContext* gl_context) const;
292 
293  // render calls
295 
296  // vertex attributes
297  ref<ArrayAbstract> mVertexAttribArrays[VA_MaxAttribCount];
298  };
299  //------------------------------------------------------------------------------
300 }
301 
302 #endif
The ArrayAbstract class defines an abstract interface to conveniently manipulate data stored in a Buf...
Definition: Array.hpp:58
Associates a Renderable object to an Effect and Transform.
Definition: Actor.hpp:130
Abstract interface to manipulate OpenGL&#39;s vertex attribute arrays.
An array of vl::fvec4.
Definition: Array.hpp:416
const T * get() const
Definition: Object.hpp:128
const ArrayAbstract * vertexArray() const
Conventional vertex array.
Definition: Geometry.hpp:248
Represents an OpenGL context, possibly a widget or a pbuffer, which can also respond to keyboard...
ArrayAbstract * normalArray()
Conventional normal array.
Definition: Geometry.hpp:256
ArrayAbstract * texCoordArray(int tex_unit)
Conventional texture coords arrays.
Definition: Geometry.hpp:280
void setColorArray(const fvec4 &color)
Fills the color array with the given color.
Definition: Geometry.hpp:108
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
The Geometry class is a Renderable that implements a polygonal mesh made of polygons, lines and points.
Definition: Geometry.hpp:66
EPrimitiveType
const ArrayAbstract * colorArray() const
Conventional color array.
Definition: Geometry.hpp:260
Visualization Library main namespace.
unsigned int u32
32 bits unsigned integer
Definition: std_types.hpp:51
An abstract class that represents all the objects that can be rendered.
Definition: Renderable.hpp:58
EBufferObjectUpdateMode
ArrayAbstract * fogCoordArray()
Conventional fog array.
Definition: Geometry.hpp:274
Manages most of the OpenGL rendering states responsible of the final aspect of the rendered objects...
Definition: Shader.hpp:1830
ArrayAbstract * colorArray()
Conventional color array.
Definition: Geometry.hpp:262
The base class of DrawArrays, DrawElements, MultiDrawElements and DrawRangeElements.
Definition: DrawCall.hpp:90
It&#39;s basically an std::vector for Objects that is itself an Object so it can be reference counted and...
Definition: Collection.hpp:49
ArrayAbstract * vertexArray()
Conventional vertex array.
Definition: Geometry.hpp:250
const Collection< DrawCall > & drawCalls() const
Returns the list of DrawCall objects bound to a Geometry.
Definition: Geometry.hpp:105
const ArrayAbstract * secondaryColorArray() const
Conventional secondary color array.
Definition: Geometry.hpp:266
ArrayAbstract * secondaryColorArray()
Conventional secondary color array.
Definition: Geometry.hpp:268
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
const ArrayAbstract * texCoordArray(int tex_unit) const
Conventional texture coords arrays.
Definition: Geometry.hpp:278
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:49
const ArrayAbstract * fogCoordArray() const
Conventional fog array.
Definition: Geometry.hpp:272
#define VL_CHECK(expr)
Definition: checks.hpp:73
Visualization Library&#39;s enums in the &#39;vl&#39; namespace.
Collection< DrawCall > mDrawCalls
Definition: Geometry.hpp:294
const ArrayAbstract * normalArray() const
Conventional normal array.
Definition: Geometry.hpp:254
T normalize(T)
Definition: glsl_math.hpp:1128
Collection< DrawCall > & drawCalls()
Returns the list of DrawCall objects bound to a Geometry.
Definition: Geometry.hpp:102