-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwfobj.h
191 lines (122 loc) · 3.32 KB
/
wfobj.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* File: wfobj.h
* Author: gg
*
* Created on 4 de dezembro de 2019, 21:45
*/
#ifndef WFOBJ_H
#define WFOBJ_H
#include <map>
#include <vector>
#include "xmlutils.h"
#include "shapes.h"
#include "filepath.h"
using namespace std;
struct WFCommand;
typedef void (*WFFunction)(const GLfloat*, WFCommand*);
struct WFCommand {
WFFunction apply;
const char* name;
GLuint texture;
};
class wf_object_loader_t;
#define MATERIAL_COMMANDS_COUNT 6
struct wf_material_t {
private:
friend wf_object_loader_t;
string name;
WFCommand commands[MATERIAL_COMMANDS_COUNT];
float arguments[MATERIAL_COMMANDS_COUNT * 4];
int n = 0;
string* texture = NULL;
public:
wf_material_t() {
}
~wf_material_t() {
}
};
class wf_object_t;
class wf_command_t {
public:
virtual void apply(wf_object_t& obj) = 0;
};
/**
* An object model that is parsed from a .obj file.
*/
class wf_object_t {
private:
WFCommand* commands;
float* arguments;
int n;
float s = 1;
wf_object_t(WFCommand* commands, float* arguments, int n) :
commands(commands), arguments(arguments), n(n) {
}
public:
~wf_object_t() {
delete (commands);
free(arguments);
}
void draw();
void scale(float s1) {
s = s1;
}
private:
// commands
friend wf_object_loader_t;
static void ambient(const GLfloat* coords, WFCommand* data);
static void diffuse(const GLfloat* coords, WFCommand* data);
static void specular(const GLfloat* coords, WFCommand* data);
static void shininess(const GLfloat* coords, WFCommand* data);
static void vertex(const GLfloat* coords, WFCommand* data);
static void normal(const GLfloat* coords, WFCommand* data);
static void tex(const GLfloat* coords, WFCommand* data);
static void begin(const GLfloat* ignore, WFCommand* data);
static void end(const GLfloat* ignore, WFCommand* data);
static void bindTexture(const GLfloat* ignore, WFCommand* data);
};
/**
* A class that loads an object per time.
*/
class wf_object_loader_t {
private:
map<string, wf_material_t> materials;
vector<float> vertices;
vector<float> normals;
vector<float> textures;
vector<WFCommand> commands;
vector<float> arguments;
wf_material_t currentMaterial;
file_path* filepath = NULL;
vector<string*> garbage;
float mScale = 1;
public:
wf_object_t* load(const char* filename);
void loadResOnly(const char* filename);
wf_object_t* loadRes(const char* name);
~wf_object_loader_t() {
}
private:
wf_object_t* load();
void loadOnly();
void loadMaterial(const char* filename);
void loadMTL(char*);
void loadOBJ(char*);
void useMaterial(const char*);
void put(WFFunction c, const char* name, vector<float>& v, int index);
void put(WFFunction c, const char* name);
void parseFace(char* line);
static void loadMTL(char* line, wf_object_loader_t& obj) {
obj.loadMTL(line);
}
static void loadOBJ(char* line, wf_object_loader_t& l) {
l.loadOBJ(line);
}
void forEachLine(const char* filename, void (*)(char*, wf_object_loader_t&));
public:
vector3f getMostDistantVertex() const;
void scale(float s);
void normalize();
wf_object_t* build();
};
#endif /* WFOBJ_H */