-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
245 lines (209 loc) · 7.02 KB
/
Copy pathModel.cpp
File metadata and controls
245 lines (209 loc) · 7.02 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
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
#include "Model.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "Color.h"
#include "Utils.h"
#include <zlib.h>
// This weird syntax initializes vertices and faces
// It's called an "Initializer List"
Model::Model(const char *filename) : vertices(), faces(), textures(), normals(), texture_image() {
std::ifstream file_stream;
file_stream.open(MODELS_DIR + std::string(filename), std::ifstream::in);
if (file_stream.fail()) return;
// This var refers to our current input line.
std::string line;
while (!file_stream.eof()) {
// We've now opened a stream to the file, so we need a line reader
std::getline(file_stream, line);
// This thing is turning our line string object into a "stream" that
// we can use C++'s janky stream syntax on.
std::istringstream line_stream(line.c_str());
std::string tag;
// Lol it's /dev/null
char trash;
// Read the tag char on the line.
line_stream >> tag;
line_stream >> std::ws;
if (tag == "v") {
Vector3f vertex;
// If it's a vector, we trash tag element and put the other 3 in a vector3.
for (int i=0;i<3;i++) line_stream >> vertex[i];
vertices.push_back(vertex);
} else if (tag == "f") {
std::vector<Vector3i> face;
int32_t idx, tex, normal;
// Lines come in like x/y/z, this stores x in the idx variable, and
// discards the other two values (using trash and itrash to tell
// it to trash chars and ints specifically).
//
// What a gross syntax.
while (line_stream >> idx >> trash >> tex >> trash >> normal) {
idx--; // in wavefront obj all indices start at 1, not zero
tex--;
normal--;
face.push_back(Vector3i(idx, tex, normal));
}
faces.push_back(face);
} else if (tag == "vt") {
float u, v, ftrash;
line_stream >> u >> v >> ftrash;
textures.push_back(Vector2f(u, v));
} else if (tag == "vn") {
float x, y , z;
line_stream >> x >> y >> z;
normals.push_back(Vector3f(x, y, z));
}
}
#ifdef DEBUG
std::cerr << "Model Counts -- \n v: " << vertices.size() << " f: " << faces.size() << " vt: " << textures.size() << " vn: " << normals.size() << std::endl;
#endif
}
Model::~Model() {
}
bool Model::load_image(std::string path, TGAImage &dst) {
uint32_t uncompressed_size = 0;
const uint32_t chunk_size = 1024 * 256;
bool load_successful = true;
std::string basename, ext;
// Find the file extension and decompress based on that.
uint32_t pivot = path.rfind(".", path.length());
basename = path.substr(0, pivot);
ext = path.substr(pivot, path.length() - pivot);
if (ext == ".gz") {
FILE* file;
uint32_t file_size;
int32_t zlib_result;
uint8_t* zlib_input = 0;
uint8_t zlib_output[chunk_size];
std::stringstream* decompressed = new std::stringstream;
z_stream zstream;
/////////// Read the file into 'compressed'
file = fopen((MODELS_DIR + path).c_str(), "r");
if (!file || ferror(file)) {
std::cerr << "Error opening file.\n";
load_successful = false;
goto cleanup;
}
fseek(file, 0, SEEK_END);
file_size = ftell(file);
rewind(file);
zlib_input = new uint8_t[file_size];
fread(zlib_input, 1, file_size, file);
fclose(file);
/////////////////////////////////////////////////////
/////////// Prep zlib for decompression loop
zstream.zalloc = Z_NULL;
zstream.zfree = Z_NULL;
zstream.opaque = Z_NULL;
zstream.next_in = zlib_input;
zstream.avail_in = file_size;
zlib_result = inflateInit2(&zstream, 16 + MAX_WBITS);
if (zlib_result != Z_OK) {
std::cerr << "Failed to read zip.\n";
load_successful = false;
goto cleanup;
}
if (ferror(file)) {
std::cerr << "Error reading gzip.\n";
load_successful = false;
goto cleanup;
}
if (zstream.avail_in == file_size) {
std::cerr << "successfully read valid gzip.\n";
}
zstream.next_in = zlib_input;
//////////////////////////////////////////////////////
////////////// Main decompression spin loop.
do {
zstream.avail_out = chunk_size;
zstream.next_out = zlib_output;
zlib_result = inflate(&zstream, Z_NO_FLUSH);
if (zlib_result == Z_MEM_ERROR) {
std::cerr << "Ran out of memory decompressing. Load failed.";
load_successful = false;
goto cleanup;
}
if (zlib_result == Z_DATA_ERROR) {
std::cerr << "Bad zip file.\n";
load_successful = false;
goto cleanup;
}
if (zlib_result == Z_BUF_ERROR) {
continue;
}
// Here is where we copy from zlib_output buffer into our vector that will hold the decompressed file.
decompressed->write((const char *)zlib_output, chunk_size - zstream.avail_out);
uncompressed_size += (chunk_size - zstream.avail_out);
assert(zlib_result != Z_STREAM_ERROR);
} while (zstream.avail_out == 0);
inflateEnd(&zstream);
////////////////////////////////////////////////////////
dump_stream(*decompressed, MODELS_DIR + "test.out");
load_successful = dst.read_tga_data(*decompressed);
cleanup:
if (decompressed != NULL) delete(decompressed);
if (zlib_input != NULL) delete[](zlib_input);
}
else {
load_successful = dst.read_tga_file((MODELS_DIR + path).c_str());
}
if (load_successful) {
dst.flip_vertically();
}
return load_successful;
}
bool Model::load_texture(std::string path) {
return load_image(path, texture_image);
}
bool Model::load_normal_texture(std::string path) {
return load_image(path, normal_image);
}
bool Model::load_specular_texture(std::string path) {
return load_image(path, specular_image);
}
Color Model::diffuse_color(const Vector2f &uv) const {
return Color(texture_image.get(
(uint32_t) (uv[0] * texture_image.get_width()),
(uint32_t) (uv[1] * texture_image.get_height())
));
}
Vector3f Model::normal(const Vector2f &uv) const {
TGAColor col = normal_image.get(uv[0] * normal_image.get_width(), uv[1] * normal_image.get_height());
Vector3f result;
for (int i = 0; i < 3; i++) {
// RGB -> XYZ, but we have the order as BGR, so BGR -> ZYX.
// Also we want to normalize to the range (-1, +1)
result[2 - i] = col[i] / 255.0f - 0.5f;
}
return result;
}
float Model::specular(const Vector2f &uv) const {
TGAColor col = normal_image.get(uv[0] * specular_image.get_width(), uv[1] * specular_image.get_height());
return (col[0] / 1.0f);
}
int Model::vertex_count() {
return (int)vertices.size();
}
int Model::face_count() {
return (int)faces.size();
}
int Model::tex_count() {
return (int)textures.size();
}
int Model::normals_count() {
return (int)normals.size();
}
std::vector<Vector3i> Model::face(int idx) {
return faces[idx];
}
Vector3f Model::vertex(int idx) {
return vertices[idx];
}
Vector2f Model::texture(int idx) {
return textures[idx];
}
Vector3f Model::normal(int idx) {
return normals[idx];
}