-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCube.cpp
140 lines (119 loc) · 4.97 KB
/
Cube.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
#include "Cube.hpp"
#include "Camera.hpp"
#include "Light.hpp"
#include "Vector.hpp"
extern Camera myCamera;
extern Light myLight;
extern GLint isShading;
extern GLint shadingType;
Cube::Cube()
{
//8 vertices, each with x,y,z
vertex[0][0] = -1;vertex[0][1] = -1;vertex[0][2] = -1;
vertex[1][0] = -1;vertex[1][1] = 1;vertex[1][2] = -1;
vertex[2][0] = 1;vertex[2][1] = -1;vertex[2][2] = -1;
vertex[3][0] = 1;vertex[3][1] = 1;vertex[3][2] = -1;
vertex[4][0] = -1;vertex[4][1] = -1;vertex[4][2] = 1;
vertex[5][0] = -1;vertex[5][1] = 1;vertex[5][2] = 1;
vertex[6][0] = 1;vertex[6][1] = -1;vertex[6][2] = 1;
vertex[7][0] = 1;vertex[7][1] = 1;vertex[7][2] = 1;
//6 faces, defined by 4 of the above vertices
face[0][0] = 0;face[0][1] = 1;face[0][2] = 3;face[0][3] = 2;
face[1][0] = 3;face[1][1] = 7;face[1][2] = 6;face[1][3] = 2;
face[2][0] = 7;face[2][1] = 5;face[2][2] = 4;face[2][3] = 6;
face[3][0] = 4;face[3][1] = 5;face[3][2] = 1;face[3][3] = 0;
face[4][0] = 5;face[4][1] = 7;face[4][2] = 3;face[4][3] = 1;
face[5][0] = 6;face[5][1] = 4;face[5][2] = 0;face[5][3] = 2;
// the face color setting
faceColor[0][0] = 1.0, faceColor[0][1] = 0.0; faceColor[0][2] = 0.0; //red
faceColor[1][0] = 0.0, faceColor[1][1] = 1.0; faceColor[1][2] = 0.0; //green
faceColor[2][0] = 0.0, faceColor[2][1] = 0.0; faceColor[2][2] = 1.0; //blue
faceColor[3][0] = 1.0, faceColor[3][1] = 1.0; faceColor[3][2] = 0.0; //yellow
faceColor[4][0] = 1.0, faceColor[4][1] = 0.0; faceColor[4][2] = 1.0; //purple
faceColor[5][0] = 0.0, faceColor[5][1] = 1.0; faceColor[5][2] = 1.0; //cyan
// face normal setting
cube_face_norm_mc[0][0] = 0.0, cube_face_norm_mc[0][1] = 0.0, cube_face_norm_mc[0][2] = -1.0;
cube_face_norm_mc[1][0] = 1.0, cube_face_norm_mc[1][1] = 0.0, cube_face_norm_mc[1][2] = 0.0;
cube_face_norm_mc[2][0] = 0.0, cube_face_norm_mc[2][1] = 0.0, cube_face_norm_mc[2][2] = 1.0;
cube_face_norm_mc[3][0] = -1.0, cube_face_norm_mc[3][1] = 0.0, cube_face_norm_mc[3][2] = 0.0;
cube_face_norm_mc[4][0] = 0.0, cube_face_norm_mc[4][1] = 1.0, cube_face_norm_mc[4][2] = 0.0;
cube_face_norm_mc[5][0] = 0.0, cube_face_norm_mc[5][1] = -1.0, cube_face_norm_mc[5][2] = 0.0;
}
bool Cube::isBackface(int faceindex) {
GLfloat v[4];
v[0] = cube_face_norm_mc[faceindex][0];
v[1] = cube_face_norm_mc[faceindex][1];
v[2] = cube_face_norm_mc[faceindex][2];
v[3] = 0.0;
this->MC.multiply_vector(v);
Vector vec = Vector(v[0],v[1],v[2]);
vec.normalize();
GLfloat x = myCamera.ref.x - myCamera.eye.x;
GLfloat y = myCamera.ref.y - myCamera.eye.y;
GLfloat z = myCamera.ref.z - myCamera.eye.z;
// return true if faceface false otherwise
return((x * vec.x + y * vec.y + z * vec.z )>= 0);
}
/*
*
* Use the lighting model that defines the reflected intensity Id as Id = Rd* I *vector(n) . vector(s) + Ra*Iam,
* where:
* Rd is diffusion reflection coefficient,
* I is the the point light intensity,
* Vector(n) is the unit surface normal,
* Vector(s) is the unit vector in the direction of the light source,
* Ra is the ambient reflection coefficient,
* Iam is the ambient light intensity.
*/
//I = 1.0; //point light intensity
//Ia = 1.0; //ambient light intensity
//Rd = 0.5; //diffusion reflection coefficient
//Ra = 0.5; //ambient reflection coefficient
GLfloat Cube::getFaceShade(int faceindex) {
GLfloat intensity;
GLfloat IpKd = myLight.Rd*myLight.I;
GLfloat v[4];
v[0] = cube_face_norm_mc[faceindex][0];
v[1] = cube_face_norm_mc[faceindex][1];
v[2] = cube_face_norm_mc[faceindex][2];
v[3] = 0.0;
this->MC.multiply_vector(v);
Vector vec = Vector(v[0],v[1],v[2]);
vec.normalize();
//middle points on face
GLfloat mx = (vertex[face[faceindex][0]][0] + vertex[face[faceindex][1]][0] + vertex[face[faceindex][2]][0] + vertex[face[faceindex][3]][0])/4;
GLfloat my = (vertex[face[faceindex][0]][1] + vertex[face[faceindex][1]][1] + vertex[face[faceindex][2]][1] + vertex[face[faceindex][3]][1])/4;
GLfloat mz = (vertex[face[faceindex][0]][2] + vertex[face[faceindex][1]][2] + vertex[face[faceindex][2]][2] + vertex[face[faceindex][3]][2])/4;
//camera points
GLfloat lx = myLight.x;
GLfloat ly = myLight.y;
GLfloat lz = myLight.z;
Vector L = Vector(lx-mx,ly-my,lz-mz);
L.normalize();
intensity = vec.x*L.x + vec.y*L.y + vec.z*L.z;
intensity = intensity * IpKd;
return(intensity);
}
void Cube::draw_face(int i)
{
glBegin(GL_POLYGON);
glVertex3fv(&vertex[face[i][0]][0]);
glVertex3fv(&vertex[face[i][1]][0]);
glVertex3fv(&vertex[face[i][2]][0]);
glVertex3fv(&vertex[face[i][3]][0]);
glEnd();
}
void Cube::draw()
{
glPushMatrix();
this->ctm_multiply(); // update the CTM
glScalef(s, s, s);
for (int i = 0; i < 6; i++){
draw_face(i);
// if (!isBackface(i)){
// //Without shading
// draw_face(i);
// }
}
glPopMatrix();
}