-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cube.h
executable file
·90 lines (77 loc) · 2.92 KB
/
Cube.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
#ifndef CUBE_H
#define CUBE_H
#include "Utility.h"
#include "Geometry.h"
#include "Shape.h"
class Cube : public Shape {
public:
Cube() {};
~Cube() {};
double Intersect(Point eyePointP, Vector rayV, Matrix transformMatrix) {
return 0;
};
Vector findIsectNormal(Point eyePoint, Vector ray, double dist) {
Vector v;
return v;
};
protected:
PVList getPoints(int segX, int segY) {
if ((int)pvs.size() != 6 * (segX + 1) * (segY + 1)) {
pvs.clear();
PVList().swap(pvs); // free memory
/* only calculate first face*/
PVList xyFace;
for (int x = 0; x < segX + 1; x++) {
for (int y = 0; y < segY + 1; y++) {
Point p(
-DEFAULT_LENGTH + x / (double)segX,
DEFAULT_LENGTH - y / (double)segY,
DEFAULT_LENGTH
);
Vector v(0.0f, 0.0f, 0.5f);
PV pv = {p, v};
xyFace.push_back(pv);
}
}
/* rotate around y axis for 4 faces */
for (int a = 0; a < 4; a++) {
Matrix mtx = rotY_mat(DEG_TO_RAD(a * 90));
PVList sidepvs = rotatePVs(mtx, xyFace);
pvs.insert(pvs.end(), sidepvs.begin(), sidepvs.end());
}
/* rotate around x axis for top bot face*/
for (int a = -1; a <= 1; a++) {
if (a == 0) continue; // don't double count 0
Matrix mtx = rotX_mat(DEG_TO_RAD(a * 90));
PVList sidepvs = rotatePVs(mtx, xyFace);
pvs.insert(pvs.end(), sidepvs.begin(), sidepvs.end());
}
}
return pvs;
}
Surface getSurface(int segX, int segY) {
if ((int)surface.size() != 12 * segX * segY) {
surface.clear();
Surface().swap(surface); // free memory
for (int s = 0; s < 6; s++) { // side
for (int x = 0; x < segX; x++) {
for (int y = 0; y < segY; y++) {
int li = x, ri = x + 1;
surface.push_back(toTriangle(
toSideIndex(s, ri, y, segX + 1, segY + 1),
toSideIndex(s, li, y, segX + 1, segY + 1),
toSideIndex(s, ri, y + 1, segX + 1, segY + 1)
));
surface.push_back(toTriangle(
toSideIndex(s, ri, y + 1, segX + 1, segY + 1),
toSideIndex(s, li, y, segX + 1, segY + 1),
toSideIndex(s, li, y + 1, segX + 1, segY + 1)
));
}
}
}
}
return surface;
}
};
#endif