-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexagonalBipyramid.cpp
More file actions
149 lines (136 loc) · 4.14 KB
/
Copy pathHexagonalBipyramid.cpp
File metadata and controls
149 lines (136 loc) · 4.14 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
//--------------------------------------------------------------------------
// Developer -- Eddie Gyger
// Course ----- CS3233
// Project ---- Lighted Hexagonal Bipyramid
// Due date --- Nov. 9 2020
//
// Create a lighted hexangonal bipyramid through through the use of normal
// vectors and Eck's camera API.
//--------------------------------------------------------------------------
#ifdef _WIN32
#include <GL/glut.h>
#elif __linux__
#include <GL/glut.h>
#elif __APPLE__
#define GL_SILENCE_DEPRECATION
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
#endif
#include <cmath>
#include <cstdio>
#include "camera.h"
void init()
{
glEnable(GL_LIGHTING); // Enable lighting.
glEnable(GL_LIGHT0); // Turn on a light. Use default light attributes.
glEnable(GL_NORMALIZE); // OpenGL will make all normal vectors into unit normals
glutMouseFunc(trackballMouseFunction);//Register camera function with GLUT
glutMotionFunc(trackballMotionFunction);
return;
}
//Calculate Normal Vectors
void setNormal(float * a, float * o, float *b) {
float w[3] = { 0,0,0 };
float u[3] = { 0,0,0 };
float v[3] = { 0,0,0 };
//translate to origin
for (int i = 0; i < 3; i++)
{
u[i] = a[i] - o[i];
v[i] = b[i] - o[i];
}
//cross product
w[0] = ((u[1] * v[2]) - (u[2] * v[1])); //Wow the error here in the last assignment was
w[1] = ((u[2] * v[0]) - (u[0] * v[2]));//because I was using indexes 1,2,3 in a zero index world,
w[2] = ((u[0] * v[1]) - (u[1] * v[0]));//I'm not sure why the complier didnt throw an out of bounds error
//set normal vector
glNormal3fv(w);
}
const double PI = 3.141592653589793;
float hexagonX[6];
float hexagonZ[6];
void display() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
cameraSetLimits(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f);
cameraApply();
//Use trig to save coordinates of hexagon
int i = 0;
for (GLint degrees = 0; degrees <= 300; degrees += 60) {
GLfloat radians;
radians = degrees * PI / 180;
hexagonX[i] = cos(radians);
hexagonZ[i] = sin(radians);
i++;
}
float face_color[] = { 0.77f, 0.70f, 0.35f, 1.0f }; // Gold!
glMaterialfv(GL_FRONT, GL_DIFFUSE, face_color);
glMaterialfv(GL_FRONT, GL_SPECULAR, face_color);
glMaterialfv(GL_FRONT, GL_AMBIENT, face_color);
glMaterialf(GL_FRONT, GL_SHININESS, 75.0F);
//Create Bottom Faces
for (int i = 0; i < 6; i++) {
float currentFace[3][3];
glBegin(GL_TRIANGLES);
currentFace[0][0] = hexagonX[i];
currentFace[0][1] = 0.0f;
currentFace[0][2] = hexagonZ[i];
if (i < 5) {
currentFace[1][0] = hexagonX[i + 1];
currentFace[1][1] = 0.0f;
currentFace[1][2] = hexagonZ[i + 1];
}
else {
currentFace[1][0] = hexagonX[0];
currentFace[1][1] = 0.0f;
currentFace[1][2] = hexagonZ[0];
}
currentFace[2][0] = 0.0f;
currentFace[2][1] = -1.5f;
currentFace[2][2] = 0.0f;
setNormal(currentFace[2], currentFace[1], currentFace[0]);
glVertex3fv(currentFace[2]);
glVertex3fv(currentFace[1]);
glVertex3fv(currentFace[0]);
glEnd();
}
//Create Top Faces
for (int i = 0; i < 6; i++) {
float currentFace[3][3];
glBegin(GL_TRIANGLES);
currentFace[0][0] = hexagonX[i];
currentFace[0][1] = 0.0f;
currentFace[0][2] = hexagonZ[i];
if (i < 5) {
currentFace[1][0] = hexagonX[i + 1];
currentFace[1][1] = 0.0f;
currentFace[1][2] = hexagonZ[i + 1];
}
else {
currentFace[1][0] = hexagonX[0];
currentFace[1][1] = 0.0f;
currentFace[1][2] = hexagonZ[0];
}
currentFace[2][0] = 0.0f;
currentFace[2][1] = 1.5f;
currentFace[2][2] = 0.0f;
setNormal(currentFace[0], currentFace[1], currentFace[2]);
glVertex3fv(currentFace[2]);
glVertex3fv(currentFace[1]);
glVertex3fv(currentFace[0]);
glEnd();
}
glFlush(); // Render now
}
/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(640, 640);
glutInitWindowPosition(50, 50);
glutCreateWindow("Hexagonal Bipyramid");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}