Repository navigation
Expand file tree
/
Copy pathmodel.h
More file actions
142 lines (124 loc) · 3.91 KB
/
Copy pathmodel.h
File metadata and controls
142 lines (124 loc) · 3.91 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#ifndef MODEL_LOADER_H
#define MODEL_LOADER_H
#include <GL/glew.h>
#include <vector>
#include <string>
#include <map>
#include <glm/glm.hpp>
static std::vector<unsigned int> VEC_UINT_DEFAUTL_VALUE{};
static std::vector<glm::vec3> VEC_VEC3_DEFAUTL_VALUE{};
static std::vector<glm::vec2> VEC_VEC2_DEFAUTL_VALUE{};
static std::map<std::string, GLuint> MAP_STRING_GLUINT_DEFAULT_VALUE{};
/**
* A very simple .obj loader. Use only for teaching purposes. Use loadOBJWithTiny()
* instead.
*/
void loadOBJ(
const std::string& path,
std::vector<glm::vec3>& vertices,
std::vector<glm::vec2>& uvs,
std::vector<glm::vec3>& normals,
std::vector<unsigned int>& indices = VEC_UINT_DEFAUTL_VALUE
);
/**
* A .vtp loader.
*/
void loadVTP(
const std::string& path,
std::vector<glm::vec3>& vertices,
std::vector<glm::vec2>& uvs,
std::vector<glm::vec3>& normals,
std::vector<unsigned int>& indices = VEC_UINT_DEFAUTL_VALUE
);
/**
* An .obj loader that uses tinyobjloader library. Any mesh (quad) is triangulated.
*
* https://github.com/syoyo/tinyobjloader
*/
void loadOBJWithTiny(
const std::string& path,
std::vector<glm::vec3>& verticies,
std::vector<glm::vec2>& uvs,
std::vector<glm::vec3>& normals,
std::vector<unsigned int>& indices = VEC_UINT_DEFAUTL_VALUE
);
/**
* Create VBO indexing.
* http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-9-vbo-indexing/
*/
void indexVBO(
const std::vector<glm::vec3>& in_vertices,
const std::vector<glm::vec2>& in_uvs,
const std::vector<glm::vec3>& in_normals,
std::vector<unsigned int> & out_indices,
std::vector<glm::vec3> & out_vertices,
std::vector<glm::vec2> & out_uvs,
std::vector<glm::vec3> & out_normals
);
class Drawable {
public:
Drawable(std::string path);
Drawable(
const std::vector<glm::vec3>& vertices,
const std::vector<glm::vec2>& uvs = VEC_VEC2_DEFAUTL_VALUE,
const std::vector<glm::vec3>& normals = VEC_VEC3_DEFAUTL_VALUE);
~Drawable();
void bind();
/* Bind VAO before calling draw */
void draw(int mode = GL_TRIANGLES);
public:
std::vector<glm::vec3> vertices, normals, indexedVertices, indexedNormals;
std::vector<glm::vec2> uvs, indexedUVS;
std::vector<unsigned int> indices;
GLuint VAO, verticesVBO, uvsVBO, normalsVBO, elementVBO;
private:
void createContext();
};
/*****************************************************************************/
namespace ogl {
struct Material {
glm::vec4 Ka;
glm::vec4 Kd;
glm::vec4 Ks;
float Ns;
GLuint texKa;
GLuint texKd;
GLuint texKs;
GLuint texNs;
};
class Mesh {
public:
Mesh(const std::vector<glm::vec3>& vertices,
const std::vector<glm::vec2>& uvs,
const std::vector<glm::vec3>& normals,
const Material& mtl);
Mesh(const Mesh&) = delete;
Mesh(Mesh&& other);
~Mesh();
void bind();
void draw(int mode = GL_TRIANGLES);
public:
std::vector<glm::vec3> vertices, normals, indexedVertices, indexedNormals;
std::vector<glm::vec2> uvs, indexedUVS;
std::vector<unsigned int> indices;
Material mtl;
GLuint VAO, verticesVBO, uvsVBO, normalsVBO, elementVBO;
private:
void createContext();
};
class Model {
public:
using MTLUploadFunction = void(const Material&);
Model(std::string path, MTLUploadFunction* uploader = nullptr);
~Model();
void draw();
private:
std::vector<Mesh> meshes;
std::map<std::string, GLuint> textures;
MTLUploadFunction* uploadFunction;
private:
void loadOBJWithTiny(const std::string& filename);
void loadTexture(const std::string& filename);
};
}
#endif