-
Notifications
You must be signed in to change notification settings - Fork 4
/
mesh.hpp
85 lines (73 loc) · 2.15 KB
/
mesh.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef MESH_HPP
#define MESH_HPP
#include <math.h>
#include <iostream>
#include <string>
#include <cstdint>
#include <vector>
#include <functional>
#include "glm/glm.hpp"
using namespace std;
using namespace glm;
const float pi = 3.1415926f;
/*
* A vertex read from a .mesh file.
*/
struct MeshVertex {
// Position coordinates
union {
float position[3];
struct {
float vx, vy, vz;
};
};
// Texture coordinates
union {
float tex_coord[2];
struct {
float tx, ty;
};
};
// Normal coordinates
union {
float normal[3]; // normal
struct {
float nx, ny, nz;
};
};
};
/*
* Triangle in a mesh from a .mesh file. Note that triangles are
* stored in CW order. The meshTriangle struct stores the index of
* a vertex in the vertexArray of its triangleMesh.
*/
struct MeshTriangle {
uint32_t i0; // ID of first vertex in vertexArray
uint32_t i1; // ID of second vertex in vertexArray
uint32_t i2; // ID of third vertex in vertexArray
};
struct TriangleMesh
{
uint32_t nv; // number of vertices
uint32_t nt; // number of triangles
MeshVertex* vertexArray; // contains every vertex in the mesh
MeshTriangle* triangleArray; // contains every triangle in the mesh
TriangleMesh()
: nv(0), nt(0), vertexArray(nullptr), triangleArray(nullptr)
{}
~TriangleMesh()
{
if (vertexArray) { free(vertexArray); }
if (triangleArray) { free(triangleArray); }
}
};
void ReadMesh(TriangleMesh &tm, const string &fname);
void WriteMesh(const TriangleMesh &tm, const string &fname);
void GeneratePoints(vector<MeshVertex> &vlist,
const int &rings, const int &slices,
function<vec3(float, float)> pt_fn,
const float pstep, const float tstep);
void GenerateFaces(vector<MeshTriangle> &tlist, const int &nrings, const int &nslices);
void GenerateVertexNormals(vector<MeshVertex> &vlist, const vector<MeshTriangle> &tlist);
void GenerateSphereVertexNormals(vector<MeshVertex> &vlist);
#endif // MESH_HPP