reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofMesh.h
Go to the documentation of this file.
1// This include guard is not a pragma once on purpose
2// so some IDEs are happy include this file back form the corresponding inl
3#ifndef OF_MESH_H
4#define OF_MESH_H
5
6#include "ofGLUtils.h"
7
8template<class V, class N, class C, class T>
9class ofMeshFace_;
10
77template<class V, class N, class C, class T>
78class ofMesh_{
79public:
80 using VertexType = V;
81 using NormalType = N;
82 using ColorType = C;
83 using TexCoordType = T;
86
89 ofMesh_();
90
96 ofMesh_(ofPrimitiveMode mode, const std::vector<V>& verts);
97
98 virtual ~ofMesh_(){}
99
100 void setFromTriangles( const std::vector<ofMeshFace_<V,N,C,T>>& tris, bool bUseFaceNormal=false );
101
105
111 void setMode(ofPrimitiveMode mode);
112
114 ofPrimitiveMode getMode() const;
115
119 static ofMesh_ plane(float width, float height, int columns=2, int rows=2,
121 static ofMesh_ sphere(float radius, int res=12,
123 static ofMesh_ icosahedron(float radius);
124 static ofMesh_ icosphere(float radius, std::size_t iterations=2);
139 static ofMesh_ cylinder(float radius, float height, int radiusSegments=12,
140 int heightSegments=6, int numCapSegments=2, bool bCapped = true,
142
154 static ofMesh_ cone(float radius, float height, int radiusSegments=12,
155 int heightSegments=6, int capSegments=2,
157
167 static ofMesh_ box(float width, float height, float depth, int resX=2,
168 int resY=2, int resZ=2);
169
171 static ofMesh_ axis(float size=1.0);
172
176
182 void addVertex(const V& v);
183
187 void addVertices(const std::vector<V>& verts);
188
193 void addVertices(const V* verts, std::size_t amt);
194
196 void removeVertex(ofIndexType index);
197
199 void removeVertices(ofIndexType startIndex, ofIndexType endIndex);
200
201 void setVertex(ofIndexType index, const V& v);
202
204 void clearVertices();
205
207 void clear();
208
209
212 std::size_t getNumVertices() const;
213
216
218 const V* getVerticesPointer() const;
219
221 V getVertex(ofIndexType i) const;
222
224 std::vector<V> & getVertices();
225
227 const std::vector<V> & getVertices() const;
228
230 bool hasVertices() const;
231
235 void append(const ofMesh_ & mesh);
236
238
240 V getCentroid() const;
241
242
244
247
249 N getNormal(ofIndexType i) const;
250
260 void addNormal(const N& n);
261
266 void addNormals(const std::vector<N>& norms);
267
272 void addNormals(const N* norms, std::size_t amt);
273
275 void removeNormal(ofIndexType index);
276
278 void removeNormals(ofIndexType startIndex, ofIndexType endIndex);
279
281 void setNormal(ofIndexType index, const N& n);
282
284 void clearNormals();
285
288 std::size_t getNumNormals() const;
289
292
294 const N* getNormalsPointer() const;
295
300 std::vector<N> & getNormals();
301
304 const std::vector<N> & getNormals() const;
305
307 bool hasNormals() const;
308
312 virtual void enableNormals();
315 virtual void disableNormals();
316 virtual bool usingNormals() const;
317
318 void smoothNormals( float angle );
319
321 void flatNormals();
322
326
329
335 std::vector<N> getFaceNormals( bool perVetex=false) const;
336
339 const std::vector<ofMeshFace_<V,N,C,T>> & getUniqueFaces() const;
340
344
347 C getColor(ofIndexType i) const;
348
351 void addColor(const C& c);
352
355 void addColors(const std::vector<C>& cols);
356
358 void addColors(const C* cols, std::size_t amt);
359
361 void removeColor(ofIndexType index);
362
364 void removeColors(ofIndexType startIndex, ofIndexType endIndex);
365
367 void setColor(ofIndexType index, const C& c);
368
370 void clearColors();
371
374 std::size_t getNumColors() const;
375
378 C* getColorsPointer();
379
381 const C* getColorsPointer() const;
382
385 std::vector<C> & getColors();
386
388 const std::vector<C> & getColors() const;
389
391 bool hasColors() const;
392
396 virtual void enableColors();
397
400 virtual void disableColors();
401 virtual bool usingColors() const;
402
404
405
408
412 T getTexCoord(ofIndexType i) const;
413
417 void addTexCoord(const T& t);
418
423 void addTexCoords(const std::vector<T>& tCoords);
424
430 void addTexCoords(const T* tCoords, std::size_t amt);
431
433 void removeTexCoord(ofIndexType index);
434
435 void removeTexCoords(ofIndexType startIndex, ofIndexType endIndex);
436
437 void setTexCoord(ofIndexType index, const T& t);
438
440 void clearTexCoords();
441
444 std::size_t getNumTexCoords() const;
445
448
450 const T* getTexCoordsPointer() const;
451
457 std::vector<T> & getTexCoords();
458
461 const std::vector<T> & getTexCoords() const;
462
464 bool hasTexCoords() const;
465
469 virtual void enableTextures();
470
473 virtual void disableTextures();
474 virtual bool usingTextures() const;
475
476
478
481
483 void setupIndicesAuto();
484
488 std::vector<ofIndexType> & getIndices();
489
490
493
494
514
517 void addIndex(ofIndexType i);
518
520 void addIndices(const std::vector<ofIndexType>& inds);
521
524 void addIndices(const ofIndexType* inds, std::size_t amt);
525
527 void removeIndex(ofIndexType index);
528 void removeIndices(ofIndexType startIndex, ofIndexType endIndex);
529
531 void setIndex(ofIndexType index, ofIndexType val);
532
535 void clearIndices();
536
539 std::size_t getNumIndices() const;
540
543
545 const ofIndexType* getIndexPointer() const;
546
547
549 const std::vector<ofIndexType> & getIndices() const;
550
552 bool hasIndices() const;
553
556 void addTriangle(ofIndexType index1, ofIndexType index2, ofIndexType index3);
557
561 virtual void enableIndices();
562
565 virtual void disableIndices();
566 virtual bool usingIndices() const;
567
568 void setColorForIndices( ofIndexType startIndex, ofIndexType endIndex, C color );
569
572 ofMesh_<V,N,C,T> getMeshForIndices( ofIndexType startIndex, ofIndexType endIndex ) const;
573 ofMesh_<V,N,C,T> getMeshForIndices( ofIndexType startIndex, ofIndexType endIndex, ofIndexType startVertIndex, ofIndexType endVertIndex ) const;
574
575
579
581 void drawVertices() const;
582
584 void drawWireframe() const;
585
587 void drawFaces() const;
588
591 void draw() const;
592
595 virtual void draw(ofPolyRenderMode renderType) const;
596
600
606 void load(const of::filesystem::path& path);
607
617 void save(const of::filesystem::path& path, bool useBinary = false) const;
618
620
621protected:
622
624 bool haveVertsChanged();
625
627 bool haveNormalsChanged();
628
630 bool haveColorsChanged();
631
634
636 bool haveIndicesChanged();
637
638private:
639
640 std::vector<V> vertices;
641 std::vector<C> colors;
642 std::vector<N> normals;
643 std::vector<T> texCoords;
644 std::vector<ofIndexType> indices;
645
646 // this variables are only caches and returned always as const
647 // mutable allows to change them from const methods
648 mutable std::vector<ofMeshFace_<V,N,C,T>> faces;
649 mutable bool bFacesDirty;
650
651 bool bVertsChanged, bColorsChanged, bNormalsChanged, bTexCoordsChanged,
652 bIndicesChanged;
653 ofPrimitiveMode mode;
654
655 bool useColors;
656 bool useTextures;
657 bool useNormals;
658 bool useIndices;
659
660// ofMaterial *mat;
661};
662
663
679template<class V, class N, class C, class T>
681public:
682 ofMeshFace_();
683
684 const N & getFaceNormal() const;
685
686 void setVertex( ofIndexType index, const V& v );
687 const V& getVertex( ofIndexType index ) const;
688
689 void setNormal( ofIndexType index, const N& n );
690 const N& getNormal( ofIndexType index ) const;
691
692 void setColor( ofIndexType index, const C& color );
693 const C& getColor(ofIndexType index) const;
694
695 void setTexCoord( ofIndexType index, const T& tCoord );
696 const T& getTexCoord( ofIndexType index ) const;
697
698 void setHasColors( bool bColors );
699 void setHasNormals( bool bNormals );
700 void setHasTexcoords( bool bTexcoords );
701
702 bool hasColors() const;
703 bool hasNormals() const;
704 bool hasTexcoords() const;
705
706private:
707 void calculateFaceNormal() const;
708 bool bHasNormals, bHasColors, bHasTexcoords;
709
710 // this variables are only caches and returned always as const
711 // mutable allows to change them from const methods
712 mutable bool bFaceNormalDirty;
713 mutable N faceNormal;
714 V vertices[3];
715 N normals[3];
716 C colors[3];
717 T texCoords[3];
718};
719
720#include "ofMesh.inl"
721
724
725#endif
Represents a set of vertices in 3D spaces with normals, colors, and texture coordinates at those poin...
Definition ofMesh.h:78
void setColorForIndices(ofIndexType startIndex, ofIndexType endIndex, C color)
Definition ofMesh.inl:1453
std::size_t getNumVertices() const
Definition ofMesh.inl:522
virtual void enableIndices()
Enable mesh indices. Use disableIndices() to turn indices off. Indices are enabled by default when th...
Definition ofMesh.inl:992
void clearNormals()
Remove all the normals.
Definition ofMesh.inl:887
void removeVertices(ofIndexType startIndex, ofIndexType endIndex)
Removes the vertices at the startIndex in the vector and the endindex specified.
Definition ofMesh.inl:365
void setMode(ofPrimitiveMode mode)
Allows you to set the ofPrimitiveMode. The available modes are OF_PRIMITIVE_TRIANGLES,...
Definition ofMesh.inl:803
void addTriangle(ofIndexType index1, ofIndexType index2, ofIndexType index3)
Adding a triangle means using three of the vertices that have already been added to create a triangle...
Definition ofMesh.inl:343
virtual void enableTextures()
Enable mesh textures. Use disableTextures() to turn textures off. Textures are enabled by default whe...
Definition ofMesh.inl:976
N getNormal(ofIndexType i) const
\returns the normal at the index in the normals vector.
Definition ofMesh.inl:498
T * getTexCoordsPointer()
Definition ofMesh.inl:604
void removeIndices(ofIndexType startIndex, ofIndexType endIndex)
Definition ofMesh.inl:465
T TexCoordType
Definition ofMesh.h:83
static ofMesh_ sphere(float radius, int res=12, ofPrimitiveMode mode=OF_PRIMITIVE_TRIANGLE_STRIP)
Definition ofMesh.inl:2006
virtual void enableNormals()
Enable mesh normals. Use disableNormals() to turn normals off. Normals are enabled by default when th...
Definition ofMesh.inl:984
void addIndex(ofIndexType i)
Definition ofMesh.inl:313
void drawFaces() const
This draws the mesh as faces, meaning that you'll have a collection of faces.
Definition ofMesh.inl:943
bool hasIndices() const
/returns Whether the mesh has any indices assigned to it.
Definition ofMesh.inl:174
void removeNormal(ofIndexType index)
Remove a normal.
Definition ofMesh.inl:379
bool hasTexCoords() const
/returns Whether the mesh has any textures assigned to it.
Definition ofMesh.inl:166
virtual bool usingNormals() const
Definition ofMesh.inl:1048
void addColors(const std::vector< C > &cols)
This adds colors using a reference to a vector of ofColors. For each color in the vector,...
Definition ofMesh.inl:224
bool haveColorsChanged()
Definition ofMesh.inl:89
void setIndex(ofIndexType index, ofIndexType val)
This sets the index at i.
Definition ofMesh.inl:853
static ofMesh_ plane(float width, float height, int columns=2, int rows=2, ofPrimitiveMode mode=OF_PRIMITIVE_TRIANGLE_STRIP)
Definition ofMesh.inl:1931
std::size_t getNumNormals() const
This will tell you how many normals are contained in the mesh.
Definition ofMesh.inl:538
ofIndexType getIndex(ofIndexType i) const
Definition ofMesh.inl:305
virtual void enableColors()
Enable mesh colors. Use disableColors() to turn colors off. Colors are enabled by default when they a...
Definition ofMesh.inl:968
static ofMesh_ cone(float radius, float height, int radiusSegments=12, int heightSegments=6, int capSegments=2, ofPrimitiveMode mode=OF_PRIMITIVE_TRIANGLE_STRIP)
A helper method that returns a cone made of triangles. The resolution settings for the radius,...
Definition ofMesh.inl:2521
bool haveVertsChanged()
Definition ofMesh.inl:76
void addNormal(const N &n)
Add a normal to the mesh as a 3D vector, typically perpendicular to the plane of the face....
Definition ofMesh.inl:244
void clearTexCoords()
Clear all the texture coordinates.
Definition ofMesh.inl:907
ofMesh_()
This creates the mesh, using OF_PRIMITIVE_TRIANGLES without any initial vertices.
Definition ofMesh.inl:17
bool hasNormals() const
/returnsWhether the mesh has any normals.
Definition ofMesh.inl:158
void clearVertices()
Removes all the vertices.
Definition ofMesh.inl:878
void draw() const
This draws the mesh using its primitive type, meaning that if you set them up to be triangles,...
Definition ofMesh.inl:951
bool hasColors() const
/returns Whether the mesh has any colors.
Definition ofMesh.inl:150
void mergeDuplicateVertices()
Definition ofMesh.inl:1538
void setupIndicesAuto()
Allow you to set up the indices automatically when you add a vertex.
Definition ofMesh.inl:863
void load(const of::filesystem::path &path)
Loads a mesh from a file located at the provided path into the mesh. This will replace any existing d...
Definition ofMesh.inl:1091
void append(const ofMesh_ &mesh)
Add the vertices, normals, texture coordinates and indices of one mesh onto another mesh....
Definition ofMesh.inl:1065
void removeNormals(ofIndexType startIndex, ofIndexType endIndex)
Remove normal's from index to end index from normals vector.
Definition ofMesh.inl:390
void addTexCoords(const std::vector< T > &tCoords)
Add a vector of texture coordinates to a mesh, allowing you to push out many at once rather than addi...
Definition ofMesh.inl:285
std::vector< ofIndexType > & getIndices()
Use this if you plan to change the indices as part of this call as it will force a reset of the cache...
Definition ofMesh.inl:691
V * getVerticesPointer()
Definition ofMesh.inl:580
virtual ~ofMesh_()
Definition ofMesh.h:98
bool haveTexCoordsChanged()
Definition ofMesh.inl:115
std::vector< C > & getColors()
Definition ofMesh.inl:667
virtual void disableNormals()
Disable mesh normals. Use enableNormals() to turn normals back on.
Definition ofMesh.inl:1016
std::size_t getNumTexCoords() const
This will tell you how many texture coordinates are contained in the mesh.
Definition ofMesh.inl:546
void setColor(ofIndexType index, const C &c)
Set the color at the index in the colors vector.
Definition ofMesh.inl:833
void removeColors(ofIndexType startIndex, ofIndexType endIndex)
Remove colors at the index to the end index of the colors vector.
Definition ofMesh.inl:414
const std::vector< ofMeshFace_< V, N, C, T > > & getUniqueFaces() const
Definition ofMesh.inl:1647
void smoothNormals(float angle)
Definition ofMesh.inl:1782
void removeTexCoord(ofIndexType index)
Remove a Vec2f representing the texture coordinate.
Definition ofMesh.inl:428
static ofMesh_ cylinder(float radius, float height, int radiusSegments=12, int heightSegments=6, int numCapSegments=2, bool bCapped=true, ofPrimitiveMode mode=OF_PRIMITIVE_TRIANGLE_STRIP)
A helper method that returns a cylinder made of triangles. The resolution settings for the radius,...
Definition ofMesh.inl:2328
void flatNormals()
Duplicates vertices and updates normals to get a low-poly look.
Definition ofMesh.inl:1883
void removeVertex(ofIndexType index)
Removes the vertex at the index in the vector.
Definition ofMesh.inl:354
virtual void disableIndices()
Disable mesh indices. Use enableIndices() to turn indices back on.
Definition ofMesh.inl:1024
std::size_t getNumIndices() const
This will tell you how many indices are contained in the mesh.
Definition ofMesh.inl:554
V getVertex(ofIndexType i) const
Definition ofMesh.inl:490
ofMeshFace_< V, N, C, T > getFace(ofIndexType faceId) const
Definition ofMesh.inl:1634
virtual bool usingTextures() const
Definition ofMesh.inl:1040
virtual bool usingColors() const
Definition ofMesh.inl:1032
std::vector< V > & getVertices()
Definition ofMesh.inl:659
void clear()
Removes all the vertices, colors, and indices from the mesh.
Definition ofMesh.inl:49
bool hasVertices() const
Definition ofMesh.inl:142
void addTexCoord(const T &t)
Add a Vec2f representing the texture coordinate. Because OF uses ARB textures these are in pixels rat...
Definition ofMesh.inl:274
std::size_t getNumColors() const
Definition ofMesh.inl:530
static ofMesh_ icosahedron(float radius)
Definition ofMesh.inl:2122
void addIndices(const std::vector< ofIndexType > &inds)
This adds a vector of indices.
Definition ofMesh.inl:323
T getTexCoord(ofIndexType i) const
Definition ofMesh.inl:514
virtual void disableTextures()
Disable mesh textures. Use enableTextures() to turn textures back on.
Definition ofMesh.inl:1008
virtual void disableColors()
Disable mesh colors. Use enableColors() to turn colors back on.
Definition ofMesh.inl:1000
V VertexType
Definition ofMesh.h:80
void removeColor(ofIndexType index)
Remove a color at the index in the colors vector.
Definition ofMesh.inl:403
void addVertex(const V &v)
Add a new vertex at the end of the current list of vertices. It is important to remember that the ord...
Definition ofMesh.inl:184
void setVertex(ofIndexType index, const V &v)
Definition ofMesh.inl:812
std::vector< N > & getNormals()
Definition ofMesh.inl:675
std::vector< N > getFaceNormals(bool perVetex=false) const
Get normals for each face As a default it only calculates the normal for the face as a whole but by s...
Definition ofMesh.inl:1690
void removeIndex(ofIndexType index)
Removes an index.
Definition ofMesh.inl:454
N * getNormalsPointer()
Definition ofMesh.inl:596
ofPrimitiveMode getMode() const
\returns the primitive mode that the mesh is using.
Definition ofMesh.inl:482
bool haveNormalsChanged()
Definition ofMesh.inl:102
void save(const of::filesystem::path &path, bool useBinary=false) const
Saves the mesh at the passed path in the PLY Format.
Definition ofMesh.inl:1331
ofIndexType * getIndexPointer()
Definition ofMesh.inl:612
void setTexCoord(ofIndexType index, const T &t)
Definition ofMesh.inl:843
void removeTexCoords(ofIndexType startIndex, ofIndexType endIndex)
Definition ofMesh.inl:439
static ofMesh_ icosphere(float radius, std::size_t iterations=2)
Definition ofMesh.inl:2134
C getColor(ofIndexType i) const
Get the color at the index in the colors vector.
Definition ofMesh.inl:506
void addNormals(const std::vector< N > &norms)
Add a vector of normals to a mesh, allowing you to push out many normals at once rather than adding o...
Definition ofMesh.inl:254
void setNormal(ofIndexType index, const N &n)
Definition ofMesh.inl:823
C ColorType
Definition ofMesh.h:82
C * getColorsPointer()
Definition ofMesh.inl:588
N NormalType
Definition ofMesh.h:81
void clearIndices()
Remove all the indices of the mesh. This means that your mesh will be a point cloud.
Definition ofMesh.inl:917
void addColor(const C &c)
This adds a color to the mesh, the color will be associated with the vertex in the same position.
Definition ofMesh.inl:214
bool haveIndicesChanged()
Definition ofMesh.inl:128
std::vector< T > & getTexCoords()
Get a vector representing the texture coordinates of the mesh Because OF uses ARB textures these are ...
Definition ofMesh.inl:683
V getCentroid() const
Definition ofMesh.inl:784
void setFromTriangles(const std::vector< ofMeshFace_< V, N, C, T > > &tris, bool bUseFaceNormal=false)
Definition ofMesh.inl:1725
void drawWireframe() const
This draws the mesh as GL_LINES, meaning that you'll have a wireframe.
Definition ofMesh.inl:935
virtual bool usingIndices() const
Definition ofMesh.inl:1056
void addVertices(const std::vector< V > &verts)
Add a vector of vertices to a mesh, allowing you to push out many at once rather than adding one at a...
Definition ofMesh.inl:194
ofMesh_< V, N, C, T > getMeshForIndices(ofIndexType startIndex, ofIndexType endIndex) const
Definition ofMesh.inl:1467
void clearColors()
Clear all the colors.
Definition ofMesh.inl:897
static ofMesh_ axis(float size=1.0)
Returns an ofMesh representing an XYZ coordinate system.
Definition ofMesh.inl:2927
void drawVertices() const
This allows you draw just the vertices, meaning that you'll have a point cloud.
Definition ofMesh.inl:927
static ofMesh_ box(float width, float height, float depth, int resX=2, int resY=2, int resZ=2)
A helper method that returns a box made of triangles. The resolution settings for the width and heigh...
Definition ofMesh.inl:2671
An ofMeshFace_ is a face on one of the ofPrimitive instances. In the ofPrimitive a face consists of 3...
Definition ofMesh.h:680
const V & getVertex(ofIndexType index) const
Definition ofMesh.inl:2997
const N & getFaceNormal() const
Definition ofMesh.inl:2970
bool hasTexcoords() const
Definition ofMesh.inl:3072
const T & getTexCoord(ofIndexType index) const
Definition ofMesh.inl:3036
void setColor(ofIndexType index, const C &color)
Definition ofMesh.inl:3016
void setHasColors(bool bColors)
Definition ofMesh.inl:3042
const C & getColor(ofIndexType index) const
Definition ofMesh.inl:3023
bool hasColors() const
Definition ofMesh.inl:3060
ofMeshFace_()
Definition ofMesh.inl:2960
void setHasNormals(bool bNormals)
Definition ofMesh.inl:3048
void setNormal(ofIndexType index, const N &n)
Definition ofMesh.inl:3003
bool hasNormals() const
Definition ofMesh.inl:3066
void setHasTexcoords(bool bTexcoords)
Definition ofMesh.inl:3054
void setVertex(ofIndexType index, const V &v)
Definition ofMesh.inl:2990
void setTexCoord(ofIndexType index, const T &tCoord)
Definition ofMesh.inl:3029
const N & getNormal(ofIndexType index) const
Definition ofMesh.inl:3010
unsigned int height
Definition ofAppEGLWindow.cpp:125
unsigned int width
Definition ofAppEGLWindow.cpp:124
TESSindex ofIndexType
Definition ofConstants.h:290
ofPrimitiveMode
Definition ofGraphicsConstants.h:20
@ OF_PRIMITIVE_TRIANGLE_STRIP
Definition ofGraphicsConstants.h:22
ofPolyRenderMode
Definition ofGraphicsConstants.h:13
#define c
ofMeshFace_< ofDefaultVertexType, ofDefaultNormalType, ofDefaultColorType, ofDefaultTexCoordType > ofMeshFace
Definition ofMesh.h:723