-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglwriter.cxx
347 lines (296 loc) · 9.24 KB
/
glwriter.cxx
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* bzflag-model-writer
* Copyright (c) 2021 Scott Wichser
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the license found in the file
* named LICENSE.md that should have accompanied this file.
*
* THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "glwriter.h"
#include <glm/glm.hpp>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
static std::fstream modelFile;
static std::fstream materialFile;
static std::fstream logFile;
struct VertInfo
{
glm::vec3 vertice;
glm::vec3 normal;
glm::vec2 texCoord;
};
// Default state
static const glm::vec3 defaultNormal(0.0f, 0.0f, 1.0f);
static const glm::vec2 defaultTexCoord(0.0f, 0.0f);
// State
static bool shadeModelIsSmooth = true;
static GLenum faceMode = 0;
static glm::vec3 normal = defaultNormal;
static glm::vec2 texCoord = defaultTexCoord;
static std::vector<VertInfo> vertices;
static unsigned int groupCount = 0;
// Model state
static std::vector<glm::vec3> modelVertices;
static std::vector<glm::vec3> modelNormals;
static std::vector<glm::vec2> modelTexCoords;
// Materials
static std::map<std::string, MaterialInfo> materials;
void glWriterFileOpen(std::string baseFilename)
{
// Reset state
shadeModelIsSmooth = true;
faceMode = 0;
normal = defaultNormal;
texCoord = defaultTexCoord;
vertices.clear();
groupCount = 0;
modelVertices.clear();
modelNormals.clear();
modelTexCoords.clear();
// Open the files for writing
modelFile.open(baseFilename + ".obj", std::ios_base::out);
materialFile.open(baseFilename + ".mtl", std::ios_base::out);
logFile.open(baseFilename + ".log", std::ios_base::out);
if (!modelFile.is_open() || !materialFile.is_open() || !logFile.is_open())
{
std::cerr << "Failed to open model, material, or log for writing. TERMINATING!" << std::endl;
exit(-1);
}
// Write out which material file to use and start a named object
modelFile << "mtllib " << baseFilename << ".mtl" << std::endl;
modelFile << "o " << baseFilename << std::endl;
}
void glWriterFileClose()
{
// Write out the vertices
for (const auto &mv : modelVertices)
modelFile << "v " << mv.x << " " << mv.y << " " << mv.z << std::endl;
// Write out the normals
for (const auto &mn : modelNormals)
modelFile << "vn " << mn.x << " " << mn.y << " " << mn.z << std::endl;
// Write out the texcoords
for (const auto &mt : modelTexCoords)
modelFile << "vt " << mt.x << " " << mt.y << std::endl;
// Write out materials
for (auto &material : materials)
{
materialFile << "newmtl " << material.first << std::endl;
// The diffuse color values default to -1.0, so check if at least the first has been set to something.
if (material.second.diffuse[0] >= 0.0f)
materialFile << " Kd " << material.second.diffuse[0] << " " << material.second.diffuse[1] << " " <<
material.second.diffuse[2] << std::endl;
if (material.second.diffuseTexture.length() > 0)
materialFile << " map_Kd " << material.second.diffuseTexture << std::endl;
}
modelFile.flush();
modelFile.close();
materialFile.flush();
materialFile.close();
logFile.flush();
logFile.close();
}
void glWriterAddMaterial(std::string name, MaterialInfo material)
{
materials[name] = material;
}
void glWriterSetCurrentMaterial(std::string name)
{
modelFile << "usemtl " << name << std::endl;
}
// Utility function to find the index of a matching vertex from our list of unique vertices
int glWriterGetVertexIndex(glm::vec3 v)
{
int index = 1;
for (const auto &mv : modelVertices)
{
if (v == mv)
return index;
index++;
}
std::cerr << "FATAL ERROR: Unable to find vertex: << " << v.x << " " << v.y << " " << v.z << std::endl;
exit(-1);
}
// Utility function to find the index of a matching normal from our list of unique normals
int glWriterGetNormalIndex(glm::vec3 n)
{
int index = 1;
for (const auto &mn : modelNormals)
{
if (n == mn)
return index;
index++;
}
std::cerr << "FATAL ERROR: Unable to find normal: << " << n.x << " " << n.y << " " << n.z << std::endl;
exit(-1);
}
// Utility function to find the index of a matching texture coordinate from our list of unique texture coordinates
int glWriterGetTexCoordIndex(glm::vec2 t)
{
int index = 1;
for (const auto &mt : modelTexCoords)
{
if (t == mt)
return index;
index++;
}
std::cerr << "FATAL ERROR: Unable to find texCoord: << " << t.x << " " << t.y << std::endl;
exit(-1);
}
// Write out a single vetex for use in a face definition. This takes the form vetex/texcoord/normal, where each is an index. The last two are optional and can be left blank, or if neither are provided just the vetex can be provided without any slashes.
void writeFaceVertInfo(VertInfo vi)
{
modelFile << " " << glWriterGetVertexIndex(vi.vertice);
if (vi.texCoord != defaultTexCoord || vi.normal != defaultNormal)
{
modelFile << "/";
if (vi.texCoord != defaultTexCoord)
modelFile << glWriterGetTexCoordIndex(vi.texCoord);
modelFile << "/";
if (vi.normal != defaultNormal)
modelFile << glWriterGetNormalIndex(vi.normal);
}
}
void glShadeModel(GLenum mode)
{
logFile << "glShadeModel(" << mode << ")" << std::endl;
if (mode == GL_FLAT)
shadeModelIsSmooth = false;
else if (mode == GL_SMOOTH)
shadeModelIsSmooth = true;
else
logFile << "\tERROR: Invalid shade model mode: " << mode << std::endl;
}
void glBegin(GLenum mode)
{
logFile << "glBegin(" << mode << ")" << std::endl;
faceMode = mode;
modelFile << "g Group" << ++groupCount << std::endl;
if (shadeModelIsSmooth)
modelFile << "s " << groupCount << std::endl;
else
modelFile << "s off" << std::endl;
}
void glEnd()
{
logFile << "glEnd()" << std::endl;
// {0, 1, 2}, {3, 4, 5}, ...
if (faceMode == GL_TRIANGLES)
{
unsigned long index = 0;
for (const auto &v : vertices)
{
if (index % 3 == 0)
modelFile << "f";
writeFaceVertInfo(v);
if (index % 3 == 2)
modelFile << std::endl;
index++;
}
}
// {0, 1, 2}, {1, 2, 3}, ...
else if (faceMode == GL_TRIANGLE_STRIP)
{
bool flip = false;
for (unsigned long i = 0; i < vertices.size() - 2; ++i)
{
modelFile << "f";
if (flip)
{
writeFaceVertInfo(vertices.at(i+2));
writeFaceVertInfo(vertices.at(i+1));
writeFaceVertInfo(vertices.at(i));
}
else
{
writeFaceVertInfo(vertices.at(i));
writeFaceVertInfo(vertices.at(i+1));
writeFaceVertInfo(vertices.at(i+2));
}
modelFile << std::endl;
flip = !flip;
}
}
// {0, 1, 2}, {1, 2, 3}, ...
else if (faceMode == GL_TRIANGLE_FAN)
{
for (unsigned long i = 1; i < vertices.size() - 1; ++i)
{
modelFile << "f";
writeFaceVertInfo(vertices.at(0));
writeFaceVertInfo(vertices.at(i));
writeFaceVertInfo(vertices.at(i+1));
modelFile << std::endl;
}
}
else
{
std::cerr << "\tERROR: Unsupported face mode: " << faceMode << std::endl;
exit(-1);
}
faceMode = 0;
vertices.clear();
normal = defaultNormal;
texCoord = defaultTexCoord;
}
GLuint glGenLists(GLsizei range) // NO-OP
{
logFile << "glGenLists(" << range << ")" << std::endl;
return 1;
}
void glNewList(GLuint list, GLenum mode)
{
logFile << "glNewList(" << list << ", " << mode << ")" << std::endl;
}
void glEndList()
{
logFile << "glEndList()" << std::endl;
}
void glDeleteLists(GLuint list, GLsizei range) // NO-OP
{
logFile << "glDeleteLists(" << list << ", " << range << ")" << std::endl;
}
void glVertex3f(GLfloat x, GLfloat y, GLfloat z)
{
logFile << "\tglVertex3f(" << x << ", " << y << ", " << z << ")" << std::endl;
glm::vec3 vert(x, y, z);
VertInfo vi;
vi.normal = normal;
vi.texCoord = texCoord;
vi.vertice = vert;
vertices.push_back(vi);
for (const auto &mv : modelVertices)
{
if (mv == vert)
return;
}
modelVertices.push_back(vert);
}
void glNormal3f(GLfloat x, GLfloat y, GLfloat z)
{
logFile << "\tglNormal3f(" << x << ", " << y << ", " << z << ")" << std::endl;
normal = glm::vec3(x, y, z);
for (const auto &mn : modelNormals)
{
if (mn == normal)
return;
}
modelNormals.push_back(normal);
}
void glTexCoord2f(GLfloat x, GLfloat y)
{
logFile << "\tglTexCoord2f(" << x << ", " << y << ")" << std::endl;
texCoord = glm::vec2(x, y);
for (const auto &mt : modelTexCoords)
{
if (mt == texCoord)
return;
}
modelTexCoords.push_back(texCoord);
}