-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_handler.h
More file actions
265 lines (255 loc) · 5.86 KB
/
file_handler.h
File metadata and controls
265 lines (255 loc) · 5.86 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#ifndef FILE_HANDLER_H
#define FILE_HANDLER_H
#include "drawing_window.h"
#include "model_triangle.h"
#include "canvas_triangle.h"
#include "texture_triangle.h"
#include "intersection.h"
#include "object.h"
#include "utils.h"
#include <glm/glm.hpp>
#include <fstream>
using namespace std;
using namespace glm;
#define DELTA 25
#define THETA 0.1
#define WIDTH 480
#define HEIGHT 640
#define DEPTH 1150
#define FOCAL_LENGTH 625
OBJ obj;
int mode = 0;
int w = WIDTH / 2;
int h = HEIGHT / 2;
vector<vec3> lights;
vec3 cameraPosition(0, 0, DEPTH);
mat3 cameraRotation(1, 0, 0, 0, 1, 0, 0, 0, 1);
DrawingWindow window = DrawingWindow(WIDTH, HEIGHT, false);
void ppmWrite(int n);
PPM ppmRead(string filename);
OBJ mtlRead(string filename);
OBJ objRead(string filename);
void ppmWrite(int n)
{
char red;
char green;
char blue;
uint32_t pixel;
string filename = "./outputs/frame" + to_string(n) + ".ppm";
std::ofstream out(filename, std::ios::out | std::ios::binary);
if (!out)
{
std::cerr << "Cannot open " << filename << std::endl;
exit(1);
}
out << "P6\n";
out << "# Created by Alex & Dylan\n";
out << to_string(WIDTH) << " " << to_string(HEIGHT) << "\n";
out << "255\n";
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
pixel = window.pixelBuffer[WIDTH * i + j];
red = char((pixel & 0x00FF0000) >> 16);
green = char((pixel & 0x0000FF00) >> 8);
blue = char((pixel & 0x000000FF));
out << red << green << blue;
}
}
out.close();
}
PPM ppmRead(string filename)
{
int red;
int green;
int blue;
string line;
PPM ppm;
std::ifstream in(filename, std::ios::in | std::ios::binary);
if (!in)
{
std::cerr << "Cannot open " << filename << std::endl;
exit(1);
}
std::getline(in, line);
std::getline(in, line);
in >> line;
ppm.width = std::stoi(line);
in >> line;
ppm.height = std::stoi(line);
in >> line;
ppm.colour = std::stoi(line);
std::getline(in, line);
for (int i = 0; i < ppm.height; i++)
{
vector<uint32_t> row;
for (int j = 0; j < ppm.width; j++)
{
red = int(in.get());
green = int(in.get());
blue = int(in.get());
uint32_t pixel = (int(255) << 24) + (int(red) << 16) + (int(green) << 8) + int(blue);
row.push_back(pixel);
}
ppm.image.push_back(row);
}
in.close();
return ppm;
}
OBJ mtlRead(string filename)
{
OBJ obj;
MTL mtl = MTL("none");
string line;
std::ifstream in(filename, std::ios::in);
if (!in)
{
std::cerr << "Cannot open " << filename << std::endl;
exit(1);
}
string key;
while (std::getline(in, line))
{
if (line.size() > 4)
{
if (line.substr(0, 6) == "newmtl")
{
if (mtl.name != "none")
{
obj.addMaterial(mtl, mtl.name);
}
string *token = split(line, ' ');
key = token[1];
mtl = MTL(key);
}
else if (line.substr(0, 2) == "Ka")
{
string *token = split(line, ' ');
int r = 255 * stod(token[1]);
int g = 255 * stod(token[2]);
int b = 255 * stod(token[3]);
mtl.setKaToColour(vec3(r, g, b));
}
else if (line.substr(0, 2) == "Kd")
{
string *token = split(line, ' ');
int r = 255 * stod(token[1]);
int g = 255 * stod(token[2]);
int b = 255 * stod(token[3]);
mtl.setKdToColour(vec3(r, g, b));
}
else if (line.substr(0, 2) == "Ks")
{
string *token = split(line, ' ');
int r = 255 * stod(token[1]);
int g = 255 * stod(token[2]);
int b = 255 * stod(token[3]);
mtl.setKsToColour(vec3(r, g, b));
}
else if (line.substr(0, 2) == "Ns")
{
string *token = split(line, ' ');
mtl.setSpecularity(stof(token[1]));
}
else if (line.substr(0, 3) == "Mir")
{
string *token = split(line, ' ');
float mi = stof(token[1]);
mtl.mirrorness = mi;
}
else if (line.substr(0, 6) == "map_Ka")
{
string *token = split(line, ' ');
PPM ppm = ppmRead(token[1]);
PPM *ptr = obj.addTexture(ppm);
mtl.setKaToTexture(ptr);
}
else if (line.substr(0, 6) == "map_Kd")
{
string *token = split(line, ' ');
PPM ppm = ppmRead(token[1]);
PPM *ptr = obj.addTexture(ppm);
mtl.setKdToTexture(ptr);
}
else if (line.substr(0, 6) == "map_Ks")
{
string *token = split(line, ' ');
PPM ppm = ppmRead(token[1]);
PPM *ptr = obj.addTexture(ppm);
mtl.setKsToTexture(ptr);
}
}
}
obj.addMaterial(mtl, mtl.name);
return obj;
}
OBJ objRead(string filename)
{
OBJ obj;
MTL mtl;
vector<vec3> vertices;
vector<TexturePoint> texturePoints;
string line;
std::ifstream in(filename, std::ios::in);
if (!in)
{
std::cerr << "Cannot open " << filename << std::endl;
exit(1);
}
int n = 0;
while (std::getline(in, line))
{
//check v for vertices
if (line.size() > 4)
{
if (line.substr(0, 6) == "mtllib")
{
string *token = split(line, ' ');
string mtlFname = token[1];
obj = mtlRead(mtlFname);
}
if (line.substr(0, 6) == "usemtl")
{
string *token = split(line, ' ');
mtl = obj.mtls.at(token[1]);
}
else if (line.substr(0, 2) == "v ")
{
string *token = split(line, ' ');
double x = stod(token[1]);
double y = stod(token[2]);
double z = stod(token[3]);
vertices.push_back(vec3(x, y, z));
}
else if (line.substr(0, 3) == "vt ")
{
string *token = split(line, ' ');
double xt = stod(token[1]);
double yt = stod(token[2]);
texturePoints.push_back(TexturePoint(xt, yt));
}
//check for faces
else if (line.substr(0, 2) == "f ")
{
int A, B, C; //to store vertex indices
int At, Bt, Ct; //to store texture indices
const char *chh = line.c_str();
sscanf(chh, "f %i/%i %i/%i %i/%i", &A, &At, &B, &Bt, &C, &Ct);
At--;
Bt--;
Ct--;
A--;
B--;
C--;
ModelTriangle tri = ModelTriangle(vertices.at(A), vertices.at(B), vertices.at(C), mtl);
TextureTriangle tex_tri = TextureTriangle(texturePoints.at(At), texturePoints.at(Bt), texturePoints.at(Ct));
obj.addTextureTri(tex_tri);
obj.addFace(tri);
}
}
n++;
}
return obj;
}
#endif