Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TriMesh.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include <vector>
26 #include "cinder/Vector.h"
27 #include "cinder/AxisAlignedBox.h"
28 #include "cinder/DataSource.h"
29 #include "cinder/DataTarget.h"
30 #include "cinder/Matrix.h"
31 #include "cinder/Color.h"
32 #include "cinder/Rect.h"
33 
34 namespace cinder {
35 
66 class TriMesh {
67  public:
68 
69  void clear();
70 
71  bool hasNormals() const { return ! mNormals.empty(); }
72  bool hasColorsRGB() const { return ! mColorsRGB.empty(); }
73  bool hasColorsRGBA() const { return ! mColorsRGBA.empty(); }
74  bool hasTexCoords() const { return ! mTexCoords.empty(); }
75 
77  void appendVertex( const Vec3f &v ) { mVertices.push_back( v ); }
79  void appendVertices( const Vec3f *verts, size_t num );
81  void appendVertices( const Vec4d *verts, size_t num );
83  void appendNormal( const Vec3f &v ) { mNormals.push_back( v ); }
85  void appendNormals( const Vec3f *normals, size_t num );
87  void appendNormals( const Vec4d *normals, size_t num );
89  void appendColorRgb( const Color &rgb ) { mColorsRGB.push_back( rgb ); }
91  void appendColorRgba( const ColorA &rgba ) { mColorsRGBA.push_back( rgba ); }
93  void appendTexCoord( const Vec2f &v ) { mTexCoords.push_back( v ); }
94 
96  void appendColorsRgb( const Color *rgbs, size_t num );
98  void appendColorsRgba( const ColorA *rgbas, size_t num );
100  void appendTexCoords( const Vec2f *texcoords, size_t num );
101 
105  void appendTriangle( uint32_t v0, uint32_t v1, uint32_t v2 )
106  { mIndices.push_back( v0 ); mIndices.push_back( v1 ); mIndices.push_back( v2 ); }
108  void appendIndices( const uint32_t *indices, size_t num );
109 
111  size_t getNumIndices() const { return mIndices.size(); }
113  size_t getNumTriangles() const { return mIndices.size() / 3; }
115  size_t getNumVertices() const { return mVertices.size(); }
116 
118  void getTriangleVertices( size_t idx, Vec3f *a, Vec3f *b, Vec3f *c ) const;
119 
121  std::vector<Vec3f>& getVertices() { return mVertices; }
123  const std::vector<Vec3f>& getVertices() const { return mVertices; }
125  std::vector<Vec3f>& getNormals() { return mNormals; }
127  const std::vector<Vec3f>& getNormals() const { return mNormals; }
129  std::vector<Color>& getColorsRGB() { return mColorsRGB; }
131  const std::vector<Color>& getColorsRGB() const { return mColorsRGB; }
133  std::vector<ColorA>& getColorsRGBA() { return mColorsRGBA; }
135  const std::vector<ColorA>& getColorsRGBA() const { return mColorsRGBA; }
137  std::vector<Vec2f>& getTexCoords() { return mTexCoords; }
139  const std::vector<Vec2f>& getTexCoords() const { return mTexCoords; }
141  std::vector<uint32_t>& getIndices() { return mIndices; }
143  const std::vector<uint32_t>& getIndices() const { return mIndices; }
144 
149 
151  void read( DataSourceRef in );
153  void write( DataTargetRef out ) const;
154 
156  void recalculateNormals();
157 
158  private:
159  std::vector<Vec3f> mVertices;
160  std::vector<Vec3f> mNormals;
161  std::vector<Color> mColorsRGB;
162  std::vector<ColorA> mColorsRGBA;
163  std::vector<Vec2f> mTexCoords;
164  std::vector<uint32_t> mIndices;
165 };
166 
167 class TriMesh2d {
168  public:
169  void clear();
170 
171  bool hasColorsRgb() const { return ! mColorsRgb.empty(); }
172  bool hasColorsRgba() const { return ! mColorsRgba.empty(); }
173  bool hasTexCoords() const { return ! mTexCoords.empty(); }
174 
176  void appendVertex( const Vec2f &v ) { mVertices.push_back( v ); }
178  void appendVertices( const Vec2f *verts, size_t num );
180  void appendColorRgb( const Color &rgb ) { mColorsRgb.push_back( rgb ); }
182  void appendColorRgba( const ColorA &rgba ) { mColorsRgba.push_back( rgba ); }
184  void appendTexCoord( const Vec2f &v ) { mTexCoords.push_back( v ); }
185 
187  void appendColorsRgb( const Color *rgbs, size_t num );
189  void appendColorsRgba( const ColorA *rgbas, size_t num );
191  void appendTexCoords( const Vec2f *texcoords, size_t num );
192 
196  void appendTriangle( uint32_t v0, uint32_t v1, uint32_t v2 )
197  { mIndices.push_back( v0 ); mIndices.push_back( v1 ); mIndices.push_back( v2 ); }
199  void appendIndices( const uint32_t *indices, size_t num );
200 
202  size_t getNumIndices() const { return mIndices.size(); }
204  size_t getNumTriangles() const { return mIndices.size() / 3; }
206  size_t getNumVertices() const { return mVertices.size(); }
207 
209  void getTriangleVertices( size_t idx, Vec2f *a, Vec2f *b, Vec2f *c ) const;
210 
212  std::vector<Vec2f>& getVertices() { return mVertices; }
214  const std::vector<Vec2f>& getVertices() const { return mVertices; }
216  std::vector<Color>& getColorsRGB() { return mColorsRgb; }
218  const std::vector<Color>& getColorsRGB() const { return mColorsRgb; }
220  std::vector<ColorA>& getColorsRGBA() { return mColorsRgba; }
222  const std::vector<ColorA>& getColorsRGBA() const { return mColorsRgba; }
224  std::vector<Vec2f>& getTexCoords() { return mTexCoords; }
226  const std::vector<Vec2f>& getTexCoords() const { return mTexCoords; }
228  std::vector<uint32_t>& getIndices() { return mIndices; }
230  const std::vector<uint32_t>& getIndices() const { return mIndices; }
231 
233  Rectf calcBoundingBox() const;
234 
235  private:
236  std::vector<Vec2f> mVertices;
237  std::vector<Color> mColorsRgb;
238  std::vector<ColorA> mColorsRgba;
239  std::vector<Vec2f> mTexCoords;
240  std::vector<uint32_t> mIndices;
241 };
242 
243 } // namespace cinder
const std::vector< uint32_t > & getIndices() const
Trimesh indices are ordered such that the indices of triangle T are { indices[T*3+0], indices[T*3+1], indices[T*3+2] }.
Definition: TriMesh.h:230
Rectf calcBoundingBox() const
Calculates the bounding box of all vertices.
Definition: TriMesh.cpp:279
void appendTriangle(uint32_t v0, uint32_t v1, uint32_t v2)
after creating three vertices, pass the indices of the three vertices to create a triangle from them...
Definition: TriMesh.h:196
AxisAlignedBox3f calcBoundingBox() const
Calculates the bounding box of all vertices.
Definition: TriMesh.cpp:90
void appendColorRgba(const ColorA &rgba)
this sets the color used by a triangle generated by the TriMesh
Definition: TriMesh.h:91
size_t getNumVertices() const
Returns the total number of indices contained by a TriMesh. This should be number of triangles/3...
Definition: TriMesh.h:206
size_t getNumTriangles() const
Returns the total number of triangles contained by a TriMesh. This should be number of indices*3...
Definition: TriMesh.h:204
const std::vector< Vec3f > & getNormals() const
Returns all the normals for a mesh in a std::vector as Vec3f objects. There will be one of these for ...
Definition: TriMesh.h:127
std::vector< Vec2f > & getVertices()
Returns all the vertices for a mesh in a std::vector as Vec2f objects.
Definition: TriMesh.h:212
void recalculateNormals()
Adds or replaces normals by calculating them from the vertices and faces.
Definition: TriMesh.cpp:210
void read(DataSourceRef in)
This reads a TriMesh in from a data file that was serialized using the write() function.
Definition: TriMesh.cpp:142
void clear()
Definition: TriMesh.cpp:238
void appendTexCoords(const Vec2f *texcoords, size_t num)
Appends multiple texcoords to the TriMesh.
Definition: TriMesh.cpp:78
std::vector< Vec3f > & getVertices()
Returns all the vertices for a mesh in a std::vector as Vec3f objects.
Definition: TriMesh.h:121
Definition: Vector.h:691
void write(DataTargetRef out) const
This writes a TriMesh to a proprietary file format to be read using the read() function.
Definition: TriMesh.cpp:181
void appendNormal(const Vec3f &v)
Appends a normal.
Definition: TriMesh.h:83
void appendColorsRgba(const ColorA *rgbas, size_t num)
Appends multiple RGBA colors to the TriMesh.
Definition: TriMesh.cpp:73
bool hasColorsRGBA() const
Definition: TriMesh.h:73
void appendTexCoord(const Vec2f &v)
appends a texture coordinate in [-1,1] space to be applied to generated triangles.
Definition: TriMesh.h:93
const std::vector< Color > & getColorsRGB() const
Returns a std::vector of RGB colors of the triangles faces. There will be one of these for each trian...
Definition: TriMesh.h:218
size_t getNumVertices() const
Returns the total number of indices contained by a TriMesh. This should be number of triangles/3...
Definition: TriMesh.h:115
size_t getNumIndices() const
Returns the total number of indices contained by a TriMesh. This should be number of triangles/3...
Definition: TriMesh.h:111
void appendColorRgba(const ColorA &rgba)
this sets the color used by a triangle generated by the TriMesh
Definition: TriMesh.h:182
const std::vector< Vec2f > & getVertices() const
Returns all the vertices for a mesh in a std::vector as Vec2f objects.
Definition: TriMesh.h:214
void appendColorRgb(const Color &rgb)
this sets the color used by a triangle generated by the TriMesh
Definition: TriMesh.h:89
const std::vector< Vec2f > & getTexCoords() const
Returns a std::vector of Texture coordinates as Vec2fs. There will be one texture coord for each vert...
Definition: TriMesh.h:139
GLfloat GLfloat v1
Definition: GLee.h:2445
std::vector< uint32_t > & getIndices()
Trimesh indices are ordered such that the indices of triangle T are { indices[T*3+0], indices[T*3+1], indices[T*3+2] }.
Definition: TriMesh.h:141
bool hasTexCoords() const
Definition: TriMesh.h:173
std::vector< Vec2f > & getTexCoords()
Returns a std::vector of Texture coordinates as Vec2fs. There will be one texture coord for each vert...
Definition: TriMesh.h:137
void appendColorsRgba(const ColorA *rgbas, size_t num)
Appends multiple RGBA colors to the TriMesh.
Definition: TriMesh.cpp:262
bool hasTexCoords() const
Definition: TriMesh.h:74
bool hasColorsRgb() const
Definition: TriMesh.h:171
void appendVertices(const Vec2f *verts, size_t num)
Appends multiple vertices which can be referred to with appendTriangle() or appendIndices() ...
Definition: TriMesh.cpp:247
std::vector< ColorA > & getColorsRGBA()
Returns a std::vector of RGBA colors of the triangles faces. There will be one of these for each tria...
Definition: TriMesh.h:220
GLuint GLuint GLsizei GLenum const GLvoid * indices
Definition: GLee.h:963
void getTriangleVertices(size_t idx, Vec3f *a, Vec3f *b, Vec3f *c) const
Puts the 3 vertices of triangle number idx into a, b and c.
Definition: TriMesh.cpp:83
void appendVertices(const Vec3f *verts, size_t num)
Appends multiple vertices to the TriMesh which can be referred to with appendTriangle() or appendIndi...
Definition: TriMesh.cpp:41
GLuint GLenum GLenum transform
Definition: GLee.h:10032
std::vector< Vec3f > & getNormals()
Returns all the normals for a mesh in a std::vector as Vec3f objects. There will be one of these for ...
Definition: TriMesh.h:125
std::vector< Vec2f > & getTexCoords()
Returns a std::vector of Texture coordinates as Vec2fs. There will be one texture coord for each vert...
Definition: TriMesh.h:224
void appendTexCoords(const Vec2f *texcoords, size_t num)
Appends multiple texcoords to the TriMesh.
Definition: TriMesh.cpp:267
void getTriangleVertices(size_t idx, Vec2f *a, Vec2f *b, Vec2f *c) const
Puts the 3 vertices of triangle number idx into a, b and c.
Definition: TriMesh.cpp:272
GLfloat GLfloat GLfloat v2
Definition: GLee.h:2451
std::vector< Color > & getColorsRGB()
Returns a std::vector of RGB colors of the triangles faces. There will be one of these for each trian...
Definition: TriMesh.h:216
const std::vector< ColorA > & getColorsRGBA() const
Returns a std::vector of RGBA colors of the triangles faces. There will be one of these for each tria...
Definition: TriMesh.h:222
void appendIndices(const uint32_t *indices, size_t num)
Appends num vertices to the TriMesh2d pointed to by indices.
Definition: TriMesh.cpp:252
void appendColorsRgb(const Color *rgbs, size_t num)
Appends multiple RGB colors to the TriMesh.
Definition: TriMesh.cpp:68
std::vector< uint32_t > & getIndices()
Trimesh indices are ordered such that the indices of triangle T are { indices[T*3+0], indices[T*3+1], indices[T*3+2] }.
Definition: TriMesh.h:228
const GLdouble * v
Definition: GLee.h:1384
GLuint GLuint num
Definition: GLee.h:10873
GLboolean GLboolean GLboolean b
Definition: GLee.h:2964
bool hasColorsRGB() const
Definition: TriMesh.h:72
void appendTexCoord(const Vec2f &v)
appends a texture coordinate in [-1,1] space to be applied to generated triangles.
Definition: TriMesh.h:184
void appendNormals(const Vec3f *normals, size_t num)
appendNormals functions similarly to the appendVertices method, appending multiple normals to be asso...
Definition: TriMesh.cpp:57
void appendVertex(const Vec3f &v)
Creates a vertex which can be referred to with appendTriangle() or appendIndices() ...
Definition: TriMesh.h:77
const GLubyte * c
Definition: GLee.h:8491
size_t getNumTriangles() const
Returns the total number of triangles contained by a TriMesh. This should be number of indices*3...
Definition: TriMesh.h:113
bool hasNormals() const
Definition: TriMesh.h:71
void appendColorRgb(const Color &rgb)
this sets the color used by a triangle generated by the TriMesh
Definition: TriMesh.h:180
GLuint in
Definition: GLee.h:10861
std::vector< ColorA > & getColorsRGBA()
Returns a std::vector of RGBA colors of the triangles faces. There will be one of these for each tria...
Definition: TriMesh.h:133
GLboolean GLboolean GLboolean GLboolean a
Definition: GLee.h:2964
std::shared_ptr< class DataTarget > DataTargetRef
Definition: DataTarget.h:33
const std::vector< Vec3f > & getVertices() const
Returns all the vertices for a mesh in a std::vector as Vec3f objects.
Definition: TriMesh.h:123
GLfloat v0
Definition: GLee.h:2439
const std::vector< ColorA > & getColorsRGBA() const
Returns a std::vector of RGBA colors of the triangles faces. There will be one of these for each tria...
Definition: TriMesh.h:135
void appendColorsRgb(const Color *rgbs, size_t num)
Appends multiple RGB colors to the TriMesh.
Definition: TriMesh.cpp:257
void clear()
Definition: TriMesh.cpp:31
void appendIndices(const uint32_t *indices, size_t num)
Appends num vertices to the TriMesh pointed to by indices.
Definition: TriMesh.cpp:52
Definition: AxisAlignedBox.h:31
const std::vector< Vec2f > & getTexCoords() const
Returns a std::vector of Texture coordinates as Vec2fs. There will be one texture coord for each vert...
Definition: TriMesh.h:226
std::shared_ptr< class DataSource > DataSourceRef
Definition: DataSource.h:35
Definition: TriMesh.h:167
const std::vector< uint32_t > & getIndices() const
Trimesh indices are ordered such that the indices of triangle T are { indices[T*3+0], indices[T*3+1], indices[T*3+2] }.
Definition: TriMesh.h:143
void appendVertex(const Vec2f &v)
Creates a vertex which can be referred to with appendTriangle() or appendIndices() ...
Definition: TriMesh.h:176
size_t getNumIndices() const
Returns the total number of indices contained by a TriMesh. This should be number of triangles/3...
Definition: TriMesh.h:202
std::vector< Color > & getColorsRGB()
Returns a std::vector of RGB colors of the triangles faces. There will be one of these for each trian...
Definition: TriMesh.h:129
const std::vector< Color > & getColorsRGB() const
Returns a std::vector of RGB colors of the triangles faces. There will be one of these for each trian...
Definition: TriMesh.h:131
Definition: TriMesh.h:66
bool hasColorsRgba() const
Definition: TriMesh.h:172
void appendTriangle(uint32_t v0, uint32_t v1, uint32_t v2)
after creating three vertices, pass the indices of the three vertices to create a triangle from them...
Definition: TriMesh.h:105