-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
294 lines (216 loc) · 7.4 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* Soccerball with OpenGL, Copyright (c) 2020 Yuriy Yakimenko
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*/
#include <GL/glut.h>
#include "math.h"
#include <stdio.h>
#include <vector>
#include <assert.h>
#include "SoccerBallGL.h"
static double rotate_y = 0;
static double rotate_x = 0;
static std::vector<Hextagon> pentagons, hexagons;
static void Init (void)
{
float position[] = { -5.0, 5.0, 5.0, 0.0 };
float local_view[] = { 0.0 };
glEnable (GL_DEPTH_TEST);
glDepthFunc (GL_LESS);
glLightfv (GL_LIGHT0, GL_POSITION, position);
glLightModelfv (GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
glFrontFace (GL_CW);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glEnable (GL_AUTO_NORMAL);
glEnable (GL_NORMALIZE);
glClearColor (0.0, 0.65, 0.1, 1.0); // green
}
static void setPentColors ()
{
float ambient[] = { 0.01175, 0.01175, 0.01175 };
float diffuse[] = { 0.04136, 0.04136, 0.04136 };
float specular[] = { 0.626959, 0.626959, 0.626959 };
glMaterialfv (GL_FRONT, GL_AMBIENT, ambient);
glMaterialfv (GL_FRONT, GL_DIFFUSE, diffuse);
glMaterialfv (GL_FRONT, GL_SPECULAR, specular);
glMaterialf (GL_FRONT, GL_SHININESS, 0.6*128.0);
}
static void setHexColors ()
{
float ambient[] = { 1.2175, 1.2175, 1.2175 };
float diffuse[] = { 0.7136, 0.7136, 1.2136 };
float specular[] = { 0.626959, 0.626959, 0.626959 };
glMaterialfv (GL_FRONT, GL_AMBIENT, ambient);
glMaterialfv (GL_FRONT, GL_DIFFUSE, diffuse);
glMaterialfv (GL_FRONT, GL_SPECULAR, specular);
glMaterialf (GL_FRONT, GL_SHININESS, 0.7*128.0);
}
/////////////////////////////////////////////////////////////////////////////
//
// recursive call.
// when level > 0, split triangle into four smaller triangles and call this
// function recursively for each of them.
// norm is normal to "seam" line, which is used to "dent" the surface
//
/////////////////////////////////////////////////////////////////////////////
static void DisplayTriangle (int level, DPoint a, DPoint b, DPoint c, DPoint & norm)
{
const int ADDED_DETAIL_ALONG_SEAMS = 1; // keep this number between [0 .. 2] inclusive.
if (level <= 0)
{
const double R = 0.022; // this value controls how deep and thick seams are.
double coef_a = 1, coef_b = 1, coef_c = 1;
bool is_edge = false;
double angle = a.getAngleAsin (norm);
if (angle < R)
{
coef_a = 1 - R + sqrt (R * R - (angle - R) * (angle - R));
is_edge = true;
}
angle = b.getAngleAsin (norm);
if (angle < R)
{
coef_b = 1 - R + sqrt (R * R - (angle - R) * (angle - R));
is_edge = true;
}
angle = c.getAngleAsin (norm);
if (angle < R)
{
coef_c = 1 - R + sqrt (R * R - (angle - R) * (angle - R));
is_edge = true;
}
if (is_edge && (level > -ADDED_DETAIL_ALONG_SEAMS))
{
DPoint ab = a.midpointTo (b);
DPoint ac = a.midpointTo (c);
DPoint bc = b.midpointTo (c);
DisplayTriangle (level - 1, a, ab, ac, norm);
DisplayTriangle (level - 1, b, ab, bc, norm);
DisplayTriangle (level - 1, c, ac, bc, norm);
DisplayTriangle (level - 1, ab, ac, bc, norm);
return;
}
else // level < 0 or is_edge is false
{
if (level == -ADDED_DETAIL_ALONG_SEAMS)
{
a.MultiplyBy (coef_a);
b.MultiplyBy (coef_b);
c.MultiplyBy (coef_c);
}
DPoint norm = a.getNormal (b, c);
glNormal3f (norm.x, norm.y, norm.z);
glVertex3f ( a.x, a.y, a.z );
glVertex3f (b.x, b.y, b.z);
glVertex3f (c.x, c.y, c.z);
}
}
else
{
// create 4 smaller triangles.
// compute median points on all three sides
DPoint ab = a.midpointTo (b);
DPoint ac = a.midpointTo (c);
DPoint bc = b.midpointTo (c);
DisplayTriangle (level - 1, a, ab, ac, norm);
DisplayTriangle (level - 1, b, ab, bc, norm);
DisplayTriangle (level - 1, c, ac, bc, norm);
DisplayTriangle (level - 1, ab, ac, bc, norm);
}
}
static void DisplayBall ()
{
glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
glColorMaterial (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glRotatef (rotate_x, 1.0, 0.0, 0.0);
glRotatef (rotate_y, 0.0, 1.0, 0.0);
const int DETAIL_LEVEL = 5;
setPentColors ();
glBegin (GL_TRIANGLES);
for (auto & pent : pentagons )
{
for (size_t i = 0; i < pent.ordered_vertices.size(); i++)
{
auto & a = pent.ordered_vertices[i];
auto & b = pent.ordered_vertices[ (i + 1) % 5];
DPoint seam_normal (0.0,0.0,0.0);
seam_normal = seam_normal.getNormal (a, b);
DisplayTriangle (DETAIL_LEVEL, pent.center, a, b, seam_normal);
}
}
setHexColors ();
for (auto & pent : hexagons )
{
for (size_t i = 0; i < pent.ordered_vertices.size(); i++)
{
auto & a = pent.ordered_vertices [i];
auto & b = pent.ordered_vertices [ (i + 1) % 6];
DPoint seam_normal (0.0,0.0,0.0);
seam_normal = seam_normal.getNormal (a, b);
DisplayTriangle (DETAIL_LEVEL, pent.center, a, b, seam_normal);
}
}
glEnd ();
glutSwapBuffers (); // glFlush is done implicitly inside this call.
}
// ----------------------------------------------------------
// specialKeys() Callback Function
// ----------------------------------------------------------
static void specialKeys( int key, int x, int y )
{
// Right arrow - increase rotation by 5 degree
if (key == GLUT_KEY_RIGHT)
{
rotate_y += 5;
}
// Left arrow - decrease rotation by 5 degree
else if (key == GLUT_KEY_LEFT)
{
rotate_y -= 5;
}
else if (key == GLUT_KEY_UP)
{
rotate_x += 5;
}
else if (key == GLUT_KEY_DOWN)
{
rotate_x -= 5;
}
// Request display update
glutPostRedisplay();
}
static void Reshape(int w, int h)
{
glViewport (0, 0, (GLint)w, (GLint)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
// this is to avoid stretching the image:
float aspect = (float)w / (float)h;
glOrtho(-2 * aspect, 2 * aspect, -2.0, 2.0, -1.0, 6.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(800, 800);
GLenum type = GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE;
glutInitDisplayMode (type);
glutCreateWindow("Soccer ball. Use keyboard arrow keys to rotate");
Init ();
SoccerBallGL soccerBall;
soccerBall.Init (pentagons, hexagons);
glutReshapeFunc (Reshape);
glutDisplayFunc (DisplayBall);
glutSpecialFunc (specialKeys);
glutMainLoop ();
return EXIT_SUCCESS;
}