1 #ifndef INCLUDE_AL_GRAPHICS_MESH_HPP
2 #define INCLUDE_AL_GRAPHICS_MESH_HPP
49 #include "al/graphics/al_OpenGL.hpp"
50 #include "al/math/al_Mat.hpp"
51 #include "al/math/al_Vec.hpp"
52 #include "al/types/al_Color.hpp"
64 enum Primitive :
unsigned int {
67 LINE_STRIP = GL_LINE_STRIP,
68 LINE_LOOP = GL_LINE_LOOP,
69 TRIANGLES = GL_TRIANGLES,
70 TRIANGLE_STRIP = GL_TRIANGLE_STRIP,
71 TRIANGLE_FAN = GL_TRIANGLE_FAN,
72 LINES_ADJACENCY = GL_LINES_ADJACENCY,
73 LINE_STRIP_ADJACENCY = GL_LINE_STRIP_ADJACENCY,
74 TRIANGLES_ADJACENCY = GL_TRIANGLES_ADJACENCY,
75 TRIANGLE_STRIP_ADJACENCY = GL_TRIANGLE_STRIP_ADJACENCY
80 typedef float TexCoord1;
83 typedef unsigned int Index;
85 typedef std::vector<Vertex> Vertices;
86 typedef std::vector<Normal> Normals;
87 typedef std::vector<Color> Colors;
88 typedef std::vector<TexCoord1> TexCoord1s;
89 typedef std::vector<TexCoord2> TexCoord2s;
90 typedef std::vector<TexCoord3> TexCoord3s;
91 typedef std::vector<Index> Indices;
94 Mesh(Primitive p = TRIANGLES);
98 void copy(
Mesh const &m);
140 return scale(v[0], v[1], v[2]);
159 Mesh &
transform(
const Mat<4, T> &m,
int begin = 0,
int end = -1);
194 void ribbonize(
float width = 0.04,
bool faceBinormal =
false) {
209 bool faceBinormal =
false);
219 void smooth(
float amount = 1,
int weighting = 0);
221 Primitive primitive()
const {
return mPrimitive; }
222 const std::vector<Vertex> &vertices()
const {
return mVertices; }
223 const std::vector<Normal> &normals()
const {
return mNormals; }
224 const std::vector<Color> &colors()
const {
return mColors; }
225 const std::vector<TexCoord1> &texCoord1s()
const {
return mTexCoord1s; }
226 const std::vector<TexCoord2> &texCoord2s()
const {
return mTexCoord2s; }
227 const std::vector<TexCoord3> &texCoord3s()
const {
return mTexCoord3s; }
228 const std::vector<Index> &indices()
const {
return mIndices; }
241 void index(
unsigned int i) { indices().push_back(i); }
244 template <
class Tindex>
245 void index(
const Tindex *buf,
int size, Tindex indexOffset = 0) {
246 for (
int i = 0; i < size; ++i)
247 index((Index)(buf[i] + indexOffset));
250 template <
class... Indices>
void index(
unsigned i, Indices... indices) {
259 void color(
const HSV &v) { colors().push_back(v); }
262 void color(
const RGB &v) { colors().push_back(v); }
265 void color(
float r,
float g,
float b,
float a = 1) {
271 color(v[0], v[1], v[2], v[3]);
275 template <
class T>
void color(
const T *src,
int numColors) {
276 for (
int i = 0; i < numColors; ++i)
277 color(src[4 * i + 0], src[4 * i + 1], src[4 * i + 2], src[4 * i + 3]);
292 template <
class T>
void normal(
const T *src,
int numNormals) {
293 for (
int i = 0; i < numNormals; ++i)
294 normal(src[3 * i + 0], src[3 * i + 1], src[3 * i + 2]);
298 void texCoord(
float u) { texCoord1s().push_back(TexCoord1(u)); }
308 texCoord3s().push_back(
TexCoord3(u, v, w));
328 template <
class T>
void vertex(
const T *src,
int numVerts) {
329 for (
int i = 0; i < numVerts; ++i)
330 vertex(src[3 * i + 0], src[3 * i + 1], src[3 * i + 2]);
335 for (
int i = 0; i < numVerts; ++i)
336 vertex(src[i][0], src[i][1], src[i][2]);
339 Vertices &vertices() {
return mVertices; }
340 Normals &normals() {
return mNormals; }
341 Colors &colors() {
return mColors; }
342 TexCoord1s &texCoord1s() {
return mTexCoord1s; }
343 TexCoord2s &texCoord2s() {
return mTexCoord2s; }
344 TexCoord3s &texCoord3s() {
return mTexCoord3s; }
345 Indices &indices() {
return mIndices; }
355 bool save(
const char *filePath,
const char *solidName =
"",
356 bool binary =
true)
const;
368 bool saveSTL(
const char *filePath,
const char *solidName =
"")
const;
378 bool savePLY(
const char *filePath,
const char *solidName =
"",
379 bool binary =
true)
const;
382 void print(FILE *dst = stderr)
const;
389 TexCoord1s mTexCoord1s;
390 TexCoord2s mTexCoord2s;
391 TexCoord3s mTexCoord3s;
394 Primitive mPrimitive;
400 end += vertices().size() + 1;
401 for (
int i = begin; i < end; ++i) {
402 Vertex &v = vertices()[i];
Fixed-size n-by-n square matrix.
Stores buffers related to rendering graphical objects.
void unitize(bool proportional=true)
Scale all vertices to lie in [-1,1].
Mesh & transform(const Mat< 4, T > &m, int begin=0, int end=-1)
Transform vertices by projective transform matrix.
void smooth(float amount=1, int weighting=0)
Smooths a triangle mesh.
void color(const Vec< 4, T > &v)
Append color to color buffer.
bool saveSTL(const char *filePath, const char *solidName="") const
Save mesh to an STL file.
void index(unsigned int i)
Append index to index buffer.
void decompress()
Convert indices (if any) to flat vertex buffers.
void texCoord(float u)
Append texture coordinate to 1D texture coordinate buffer.
void vertex(const Vec< 3, T > *src, int numVerts)
Append vertices to vertex buffer.
void normal(const Normal &v)
Append normal to normal buffer.
void vertex(const T *src, int numVerts)
Append vertices from flat array.
void texCoord(const Vec< 2, T > &v)
Append texture coordinate to 2D texture coordinate buffer.
Vec3f getCenter() const
Get center of vertices.
Mesh & primitive(Primitive p)
Set geometric primitive.
Mesh & repeatLast()
Repeat last vertex element(s)
void vertex(float x, float y, float z=0)
Append vertex to vertex buffer.
void color(const HSV &v)
Append color to color buffer.
void invertNormals()
Invert direction of normals.
void print(FILE *dst=stderr) const
Print information about Mesh.
void ribbonize(float width=0.04, bool faceBinormal=false)
Ribbonize curve.
void normal(const T *src, int numNormals)
Append normals from flat array.
void toTriangles()
Convert triangle strip to triangles.
void color(const T *src, int numColors)
Append colors from flat array.
void texCoord(float u, float v)
Append texture coordinate to 2D texture coordinate buffer.
void compress()
Generates indices for a set of vertices.
bool savePLY(const char *filePath, const char *solidName="", bool binary=true) const
Save mesh to a PLY file.
void getBounds(Vec3f &min, Vec3f &max) const
Get corners of bounding box of vertices.
void index(const Tindex *buf, int size, Tindex indexOffset=0)
Append indices to index buffer.
Mesh(Primitive p=TRIANGLES)
void normal(float x, float y, float z=0)
Append normal to normal buffer.
bool save(const char *filePath, const char *solidName="", bool binary=true) const
Save mesh to file.
void vertex(const Vec< 2, T > &v, float z=0)
Append vertex to vertex buffer.
Mesh & scale(float x, float y, float z)
Scale all vertices.
void equalizeBuffers()
Extend buffers to match number of vertices.
void texCoord(float u, float v, float w)
Append texture coordinate to 3D texture coordinate buffer.
void texCoord(const Vec< 3, T > &v)
Append texture coordinate to 3D texture coordinate buffer.
void merge(const Mesh &src)
Append buffers from another mesh:
void normal(const Vec< 2, T > &v, float z=0)
Append normal to normal buffer.
void color(const RGB &v)
Append color to color buffer.
Mesh & reset()
Reset all buffers.
Mesh & translate(float x, float y, float z)
Translate all vertices.
void color(float r, float g, float b, float a=1)
Append color to color buffer.
void generateNormals(bool normalize=true, bool equalWeightPerFace=false)
Generates normals for a set of vertices.
void createNormalsMesh(Mesh &mesh, float length=0.1, bool perFace=false)
Creates a mesh filled with lines for each normal of the source.
void ribbonize(float *widths, int widthsStride=1, bool faceBinormal=false)
Ribbonize curve.
void color(const Color &v)
Append color to color buffer.
void vertex(const Vertex &v)
Append vertex to vertex buffer.
Vec & set(const Vec< N, T2 > &v)
Set elements from another vector.
T min(const T &v1, const T &v2, const T &v3)
T max(const T &v1, const T &v2, const T &v3)
Color represented by hue, saturation, and value.
Color represented by red, green, and blue components.