This repository has been archived by the owner on Oct 13, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomer.cpp
373 lines (305 loc) · 7.65 KB
/
homer.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <stdlib.h>
#include <GL/glut.h>
#define _USE_MATH_DEFINES
#include <cmath>
#include<string>
// sets the gl color by giving the function the colorname as a string
// if given color not defined, white is used
void setColor(const std::string& color) {
if (color == "blue") {
glColor3ub(0, 0, 255);
} else if (color == "red") {
glColor3ub(255, 0, 0);
} else if (color == "yellow") {
glColor3ub(254, 217, 15);
} else if (color == "green") {
glColor3ub(0, 255, 0);
} else if (color == "black") {
glColor3ub(0, 0, 0);
} else if (color == "brown") {
glColor3ub(208, 165, 96);
} else if (color == "grey") {
glColor3ub(122, 122, 122);
} else {
glColor3ub(255, 255, 255);
}
}
void drawQuad(const std::string& color)
{
setColor(color);
glBegin(GL_POLYGON);
glVertex3f(-0.5, -0.5, 0.0);
glVertex3f(-0.5, 0.5, 0.0);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(0.5, -0.5, 0.0);
glEnd();
}
void drawTriangle(const std::string& color)
{
setColor(color);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5, -0.5, 0);
glVertex3f(0.5, -0.5, 0);
glVertex3f(0, 0.5, 0);
glEnd();
}
void drawLine(const std::string& color)
{
setColor(color);
glBegin(GL_LINES);
glVertex3f(-0.5, 0, 0);
glVertex3f(0.5, 0, 0);
glEnd();
}
void drawCircle(const std::string& color)
{
setColor(color);
glBegin(GL_TRIANGLE_FAN); // create triangle fan
glVertex2f(0, 0); // set the first vertex in the center
for (int i = 0; i <= 50; i++)
{
float angle = float(i) / 50.0 * 2.0 * M_PI; // calculate angle
float x = sinf(angle) / 2.0; // to get a radius of 0.5 -> division
float y = cosf(angle) / 2.0;
glVertex2f(x, y); // set the vertex
}
glEnd();
}
void drawPolygon(const std::string& color)
{
setColor(color);
glBegin(GL_POLYGON);
glVertex3f(-0.2, 0.2, 0.0);
glVertex3f(0.4, 0.1, 0.0);
glVertex3f(-0.2, -0.3, 0.0);
glVertex3f(-0.6, -0.1, 0.0);
glEnd();
}
void drawToes()
{
glScalef(0.25, 0.25, 0);
glPushMatrix();
glTranslatef(-2.2, -0.6, 0); // single toe
glRotatef(35, 0, 0, 1);
drawLine("black");
glPopMatrix();
glPushMatrix(); // single toe
glTranslatef(-1.7, -0.6, 0); // single toe
glRotatef(35, 0, 0, 1);
drawLine("black");
glPopMatrix();
glPushMatrix(); // single toe
glTranslatef(-1.3, -0.8, 0); // single toe
glRotatef(35, 0, 0, 1);
drawLine("black");
glPopMatrix();
glPushMatrix(); // single toe
glTranslatef(-0.8, -0.8, 0); // single toe
glRotatef(35, 0, 0, 1);
drawLine("black");
glPopMatrix();
}
void drawFeet()
{
glPushMatrix(); // feet
glTranslatef(0, -1.4, 0);
glPushMatrix();
glScalef(1.2, 1.3, 0);
drawQuad("yellow");
glRotatef(90, 0, 0, 1);
drawLine("black");
glPopMatrix(); // undo scaling
glPushMatrix(); //toes
glPushMatrix(); // left toes
glTranslatef(-0.4, -0.74, 0);
drawPolygon("yellow");
drawToes();
glPopMatrix(); // end left toe
glPushMatrix(); //right toes
glTranslatef(0.4, -0.74, 0);
glScalef(-1, 1, 0);
drawPolygon("yellow");
drawToes();
glPopMatrix(); // end right toe
glPopMatrix(); // end toes
glPopMatrix(); // end feet
}
void drawArms()
{
glPushMatrix(); //arms
glScalef(0.5, 0.5, 0);
glTranslatef(0, -1.5, 0);
glPushMatrix(); // left hand
glRotatef(-10, 0, 0, 1);
glTranslatef(-1.5, 0, 0);
drawPolygon("yellow");
glPopMatrix(); // end left hand
glPushMatrix(); // right hand
glRotatef(10, 0, 0, 1);
glTranslatef(1.5, 0, 0);
glScalef(-1, 1, 0);
drawPolygon("yellow");
glPopMatrix(); // end right hand
glPopMatrix(); // end arms
}
void drawBody()
{
glPushMatrix(); // body
glScalef(2.2, 1.8, 0);
drawCircle("yellow");
glPushMatrix();
glScalef(0.8, 1.2, 0);
glTranslatef(0, -0.05, 0);
drawCircle("white");
glPopMatrix();
glScalef(0.73, 1, 0);
glTranslatef(0, -0.3, 0);
drawLine("black");
glPopMatrix(); // end body
}
void drawHair()
{
glPushMatrix(); //hair left
glTranslatef(0, 0.9, 0);
glRotatef(75, 0, 0, 1);
drawLine("black");
glPopMatrix();
glPushMatrix(); //hair right
glTranslatef(0.4, 0.8, 0);
glRotatef(55, 0, 0, 1);
drawLine("black");
glPopMatrix();
}
void drawEyes()
{
glPushMatrix(); // eye
// left eye
glScalef(0.4, 0.4, 0);
glPushMatrix(); //start left eye
//left eye
glTranslatef(-0.5, 1, 0);
drawCircle("white");
// left pupille
glScalef(0.2, 0.2, 0);
drawCircle("black");
glPopMatrix(); // end left eye
glPushMatrix(); //start right eye
// right eye
glTranslatef(0.5, 1, 0);
drawCircle("white");
//right pupille
glScalef(0.2, 0.2, 0);
drawCircle("black");
glPopMatrix(); // end right eye
glPopMatrix(); // end eyes
}
void drawEars()
{
glPushMatrix(); // ears
glScalef(0.2, 0.2, 0);
glPushMatrix(); // left ear
glTranslatef(-1.8, 0.8, 0);
drawCircle("yellow");
glRotatef(155, 0, 0, 1);
glScalef(0.5, 0.2, 0);
glTranslatef(0, 0.5, 0);
drawTriangle("black");
glPopMatrix();
glPushMatrix(); // right ear
glTranslatef(1.8, 0.8, 0);
drawCircle("yellow");
glRotatef(-155, 0, 0, 1);
glScalef(0.5, 0.2, 0);
glTranslatef(0, 0.5, 0);
drawTriangle("black");
glPopMatrix();
glPopMatrix(); // end ears
}
void drawMouth()
{
glPushMatrix(); // mouth
glScalef(0.8, 0.7, 0);
glTranslatef(0, -0.2, 0);
drawCircle("brown");
glRotatef(155, 0, 0, 1);
glScalef(0.3, 0.1, 0);
glTranslatef(0, 1.5, 0);
drawTriangle("black");
glPopMatrix(); // end mouth
}
void drawHead()
{
glPushMatrix(); // head
glTranslatef(0, 1.5, 0);
glPushMatrix(); // save translation of head before scaling
glScalef(0.7, 1.3, 0);
drawQuad("yellow");
glPushMatrix(); // set top and bottom circle of head
glScalef(1, 0.6, 0);
glPushMatrix(); //top of head
glTranslatef(0, 0.8, 0);
drawCircle("yellow");
drawHair();
glPopMatrix();
glPushMatrix(); // bottom of head
glTranslatef(0, -0.8, 0);
drawCircle("yellow");
glPopMatrix();
glPopMatrix(); // end of drawing head-circles
glPopMatrix(); // end of head scaling
drawMouth();
drawEyes();
drawEars();
glPushMatrix();
glTranslatef(0, 0.2, 0);
glScalef(0.25, 0.25, 0);
drawCircle("yellow");
glPopMatrix();
glPopMatrix(); // end head
}
void drawHomer()
{
drawFeet();
drawArms();
drawBody();
drawHead();
}
//render method (callback-function)
void display()
{
//clear all pixels
glClear(GL_COLOR_BUFFER_BIT);
drawHomer();
//don't wait! process buffered OpenGL routines
glFlush();
}
//initialization of the application. only started once.
void init()
{
//select clearing color (color that is used as 'background')
glClearColor(0.0, 0.0, 0.0, 0.0);
//initialize view
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.6, 2.6, -2.6, 2.6, -1.0, 1.0); // normal front camera, size halved
// glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
//set matrix-mode back to model-view for rendering
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
//initialize display mode (single buffer and RGBA)
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//initialize window size and position. open window
glutInitWindowSize(350, 350);
glutInitWindowPosition(100, 100);
glutCreateWindow("Homer in OpenGL");
//call initialization routine
init();
//register callback function to display graphics
glutDisplayFunc(display);
//enter main loop and process events
glutMainLoop();
return 0;
}