-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMesh.h
79 lines (59 loc) · 2.48 KB
/
Mesh.h
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
#pragma once
#include <vector>
#include "Math.h"
#include "Utl.h"
class TransformState;
class Mesh
{
public:
Mesh();
~Mesh();
enum class DataType {
kVertexPos,
kVertexNormal,
kVertexColor,
kNumVertexDataTypes
};
int NumVertexDataTypes() const { return raw_enum( DataType::kNumVertexDataTypes ); }
Vector3f& Data( int idx, DataType type ) {
size_t raw_idx = NumVertexDataTypes() * idx + raw_enum( type );
return mVertices[raw_idx];
}
Vector3f& Vertex( int idx ) { return Data( idx, DataType::kVertexPos ); }
Vector3f& Normal( int idx ) { return Data( idx, DataType::kVertexNormal ); }
Vector3f& Color( int idx ) { return Data( idx, DataType::kVertexColor ); }
// Returns the index of the vertex just added
int AddVertex( const Vector3f& pos, const Vector3f& color );
int AddVertex( const Vector3f& pos, const Vector3f& normal, const Vector3f& color );
const Vector3f* VertexData() const { return &mVertices[0]; }
int VertexDataSize() const { return mVertices.size() * sizeof( Vector3f ); }
int VertexDataCount() const { return mVertices.size(); }
int NumVertices() const { return mVertices.size() / NumVertexDataTypes(); }
int Index( int idx ) const { return mIndices[idx]; }
void AddTriangle( int i0, int i1, int i2 );
const int* IndexData() const { return &mIndices[0]; }
int IndexDataSize() const { return mIndices.size() * sizeof( int ); }
int IndexDataCount() const { return mIndices.size(); }
int TriangleCount() const { return IndexDataCount() / 3; }
void RemoveTriangle( int tri_idx );
void Subdivide();
void glVertexAttribPointer( int idx, DataType type );
TransformState& GetTransformState() { return *mTransformState; }
void SetScale( const Vector3f& scale );
void SetTranslation( const Vector3f& translation );
void SetRotation( const Vector3f& rotation );
const Vector3f& GetScale();
const Vector3f& GetTranslation();
const Vector3f& GetRotation();
void AddSquare( const Matrix3f& rot, const Vector3f& color );
friend std::ostream& operator<< ( std::ostream &out, const Mesh& mesh );
private:
int AddAveragedVertex( int i, int j );
void SubdivideTriangle( int tri_idx );
std::vector<Vector3f> mVertices; // interleaved vertex/normal/color
std::vector<int> mIndices; // vertex indices of successive triangles
TransformState* mTransformState;
};
std::ostream& operator<< ( std::ostream &out, const Mesh& mesh );
Mesh* NewSphere();
Mesh* NewCube();