-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mesh.cpp
87 lines (60 loc) · 2.69 KB
/
Mesh.cpp
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
86
87
#include "Mesh.h"
#include <vector>
Mesh::Mesh( const std::string& filename ) {
IndexedModel model = OBJModel( filename ).ToIndexedModel();
InitMesh( model );
}
Mesh::Mesh( Vertex* vertices, unsigned int vertice_count, unsigned int* indices, unsigned int indice_count ) {
IndexedModel model;
for ( unsigned int i = 0; i < vertice_count; i++ ) {
model.positions.push_back( *vertices[ i ].GetPos() );
model.texCoords.push_back( *vertices[ i ].GetTexCoord() );
model.normals.push_back( *vertices[ i ].GetNormal() );
}
for ( unsigned int i = 0; i < indice_count; i++ ) {
model.indices.push_back( indices[ i ] );
}
InitMesh( model );
}
Mesh::Mesh( ObjectLoader obj ) {
IndexedModel model;
for ( unsigned int i = 0; i < obj.vertices.size(); i++ ) {
model.positions.push_back( obj.vertices[i].pos );
model.texCoords.push_back( obj.vertices[i].texCoord);
model.normals.push_back( obj.vertices[i].normal );
}
for ( unsigned int i = 0; i < obj.indices.size(); i++ ) {
model.indices.push_back( obj.indices[ i ] );
}
InitMesh( model );
}
void Mesh::InitMesh( const IndexedModel& model ) {
this->drawCount = model.indices.size();
glGenVertexArrays( 1, &this->vertexArrayObject );
glBindVertexArray( this->vertexArrayObject );
glGenBuffers( this->NUM_BUFFERS, this->vertexArrayBuffers );
glBindBuffer( GL_ARRAY_BUFFER, this->vertexArrayBuffers[ this->POSITION_VB ] );
glBufferData( GL_ARRAY_BUFFER, model.positions.size() * sizeof( model.positions[0] ), &model.positions[0], GL_STATIC_DRAW );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, 0 );
glBindBuffer( GL_ARRAY_BUFFER, this->vertexArrayBuffers[ this->TEXCOORD_VB ] );
glBufferData( GL_ARRAY_BUFFER, model.positions.size() * sizeof( model.texCoords[0] ), &model.texCoords[0], GL_STATIC_DRAW );
glEnableVertexAttribArray( 1 );
glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, 0, 0 );
glBindBuffer( GL_ARRAY_BUFFER, this->vertexArrayBuffers[ this->NORMAL_VB ] );
glBufferData( GL_ARRAY_BUFFER, model.normals.size() * sizeof( model.normals[0] ), &model.normals[0], GL_STATIC_DRAW );
glEnableVertexAttribArray( 2 );
glVertexAttribPointer( 2, 3, GL_FLOAT, GL_FALSE, 0, 0 );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, this->vertexArrayBuffers[ this->INDEX_VB ] );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, model.positions.size() * sizeof( model.indices[0] ), &model.indices[0], GL_STATIC_DRAW );