-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
190 lines (144 loc) · 6.51 KB
/
main.cpp
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
#include <iostream>
#include <algorithm>
#include "headers/vectormath.h"
#include "headers/homogenVector.h"
#include "headers/color.h"
#include "headers/image.h"
#include "headers/scene.h"
#include "headers/objParser.h"
#include "headers/camera.h"
#include "headers/zBuffer.h"
#include "headers/lightSrc.h"
#include "headers/transform.h"
#include "headers/matrix.h"
using namespace std;
int main(int argc, char** argv) {
// -------------------------------------------------------------------------------
// command line arguments:
// main camPosX camPosY camPosZ lightX lightY lightZ rotX rotY rotZ
// Note:if not provided they take on the default values
// -------------------------------------------------------------------------------
const int width = 1080;
const int height = 720;
bool cameraPositionSet = false;
Point cameraPosition(0, 0, 0);
Point lightPosition(0, 0, 0);
if (argc >= 5) {
stringstream ss;
ss << argv[2];
ss >> cameraPosition.x;
ss.clear();
ss << argv[3];
ss >> cameraPosition.y;
ss.clear();
ss << argv[4];
ss >> cameraPosition.z;
ss.clear();
cameraPositionSet = true;
if (argc >= 8) {
stringstream ss;
ss << argv[5];
ss >> lightPosition.x;
ss.clear();
ss << argv[6];
ss >> lightPosition.y;
ss.clear();
ss << argv[7];
ss >> lightPosition.z;
ss.clear();
}
}
Image image(width, height);
Camera camera(cameraPosition, width, height);
LightSrc light(lightPosition, 1300.0f);
Scene scene;
ObjParser objParser;
objParser.parse(argv[1]);
// automatically adjust camera to look at the object
// by taking average coordinates of the vertices as the camera position
// but if camera position is given as command line args don't set it automatically
Point towardsObject;
for(auto t: objParser.triangles) {
if (!cameraPositionSet) {
towardsObject.x += (t.v0.x + t.v1.x + t.v2.x) / (3.0 * objParser.triangles.size());
towardsObject.y += (t.v0.y + t.v1.y + t.v2.y) / (3.0 * objParser.triangles.size());
towardsObject.z += (t.v0.z + t.v1.z + t.v2.z) / (3.0 * objParser.triangles.size());
}
scene.addTriangle(t);
}
if (!cameraPositionSet) {
const float CAMERA_DIST = 500;
towardsObject.x *= NORMALIZED_2_RASTER_EXPANSION_SCALE;
towardsObject.y *= NORMALIZED_2_RASTER_EXPANSION_SCALE;
towardsObject.z *= NORMALIZED_2_RASTER_EXPANSION_SCALE;
cout << "\n--- Average coordinate of the vertices in scene ----\n";
cout << "avg x: " << towardsObject.x << endl;
cout << "avg y: " << towardsObject.y << endl;
cout << "avg z: " << towardsObject.z << endl;
towardsObject.z += CAMERA_DIST;
camera.position = towardsObject;
// set light position automatically as well
light.position = towardsObject;
light.position.x += 50;
light.position.y += 50;
light.position.z -= CAMERA_DIST;
light.position.z += 100;
}
cout << "\n\n-------- before transform -------------" << endl;
cout << "camera position: " << camera.position.x << ", " << camera.position.y
<< ", " << camera.position.z << endl << endl;
cout << "light position: " << light.position.x << ", " << light.position.y
<< ", " << light.position.z << endl << endl;
cout << "One of the face before:" << endl;
cout << scene.triangles[0].v0.x << ", " << scene.triangles[0].v0.y << ", " << scene.triangles[0].v0.z << endl;
cout << scene.triangles[0].v1.x << ", " << scene.triangles[0].v1.y << ", " << scene.triangles[0].v1.z << endl;
cout << scene.triangles[0].v2.x << ", " << scene.triangles[0].v2.y << ", " << scene.triangles[0].v2.z << endl;
cout << "normals: " << endl;
cout << scene.triangles[0].n0.x << ", " << scene.triangles[0].n0.y << ", " << scene.triangles[0].n0.z << endl;
cout << scene.triangles[0].n1.x << ", " << scene.triangles[0].n1.y << ", " << scene.triangles[0].n1.z << endl;
cout << scene.triangles[0].n2.x << ", " << scene.triangles[0].n2.y << ", " << scene.triangles[0].n2.z << endl;
world2view(scene, camera, light);
if (argc >= 11) {
// rotation along x y z is given as the command line args
float rx, ry, rz;
stringstream ss;
ss << argv[8];
ss >> rx;
ss.clear();
ss << argv[9];
ss >> ry;
ss.clear();
ss << argv[10];
ss >> rz;
ss.clear();
rotateCameraX(rx, scene, light);
rotateCameraY(ry, scene, light);
rotateCameraZ(rz, scene, light);
}
cout << "\n\n------ after transform -----------" << endl;
cout << "camera position: " << camera.position.x << ", " << camera.position.y
<< ", " << camera.position.z << endl << endl;
cout << "light position: " << light.position.x << ", " << light.position.y
<< ", " << light.position.z << endl << endl;
cout << "One of the face after:" << endl;
cout << scene.triangles[0].v0.x << ", " << scene.triangles[0].v0.y << ", " << scene.triangles[0].v0.z << endl;
cout << scene.triangles[0].v1.x << ", " << scene.triangles[0].v1.y << ", " << scene.triangles[0].v1.z << endl;
cout << scene.triangles[0].v2.x << ", " << scene.triangles[0].v2.y << ", " << scene.triangles[0].v2.z << endl;
cout << "normals: " << endl;
cout << scene.triangles[0].n0.x << ", " << scene.triangles[0].n0.y << ", " << scene.triangles[0].n0.z << endl;
cout << scene.triangles[0].n1.x << ", " << scene.triangles[0].n1.y << ", " << scene.triangles[0].n1.z << endl;
cout << scene.triangles[0].n2.x << ", " << scene.triangles[0].n2.y << ", " << scene.triangles[0].n2.z << endl;
// for (auto t: scene.triangles) {
// cout << t.v0.x << ", " << t.v0.y << ", " << t.v0.z << endl;
// cout << t.v1.x << ", " << t.v1.y << ", " << t.v1.z << endl;
// cout << t.v2.x << ", " << t.v2.y << ", " << t.v2.z << endl;
// cout << "normals: " << endl;
// cout << t.n0.x << ", " << t.n0.y << ", " << t.n0.z << endl;
// cout << t.n1.x << ", " << t.n1.y << ", " << t.n1.z << endl;
// cout << t.n2.x << ", " << t.n2.y << ", " << t.n2.z << endl;
// }
// findShadow(light, camera, scene, image);
zBuffer(light, camera, scene, image);
cout << endl;
image.saveImagePPM("rastarized.ppm");
}