-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
299 lines (236 loc) · 7.2 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
295
296
297
298
299
#ifdef __WIN32
#include <windows.h>
#include <GL/glut.h>
#elif __APPLE__
// Compile on OSX with clang
// $ clang main.cpp -framework OpenGL -framework GLUT -Wno-deprecated-declarations -o demo
#include <GLUT/glut.h>
#elif __LINUX__
// Untested
#include <GLUT/glut.h>
#endif
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Based on the code from programming-technique.blogspot.com
using namespace std;
bool redFirst;
bool rotateFirst;
float t = 0.0f;
float dt = 0.001f;
//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 'r': // use 'r' to control order of the rectangles to be drawn in drawSceneAlpha()
redFirst = !redFirst;
glutPostRedisplay();
break;
case 't': // use 't' to control order of the transformations in drawTriangleWhite()
rotateFirst = !rotateFirst;
glutPostRedisplay();
break;
case 27: //Escape key
exit(0);
}
}
//Initializes 3D rendering
void initRendering() {
redFirst = true;
rotateFirst = true;
// //Makes 3D drawing work when something is in front of something else
glEnable(GL_DEPTH_TEST);
}
//Called when the window is resized
void handleResize(int w, int h) {
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity(); //Reset the camera
gluPerspective(45.0, //The field of view angle, in degrees, in the y direction
(double) w / (double) h, //The width-to-height ratio
1.0, //The near z clipping coordinate
200.0); //The far z clipping coordinate
}
//Draws the 3D scene
void drawCube() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
glTranslatef(1.5f, 0.0f, -7.0f);
glScalef(0.5f, 0.5f, 0.5f);
glPushMatrix();
glTranslatef(sin(t), 0, 0);
glRotatef(t * 1000, 0, 1, 1);
glBegin(GL_QUADS); // Begin drawing the color cube with 6 quads
// Top face (y = 1.0f)
// Define vertices in counter-clockwise (CCW) order with normal pointing out
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
// Bottom face (y = -1.0f)
glColor3f(1.0f, 0.5f, 0.0f); // Orange
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
// Front face (z = 1.0f)
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
// Back face (z = -1.0f)
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd(); // End of drawing color-cube
glPopMatrix();
glutSwapBuffers(); //Send the 3D scene to the screen
t += dt; // Move the time forward
glutPostRedisplay(); // Invoke drawing another frame
}
void drawTriangleWhite() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); //Reset the drawing perspective
glPushMatrix();
glScalef(0.08, 0.1, 0.1);
if (rotateFirst) {
glRotatef(90, 0, 0, 1);
glTranslatef(-2, -2, 0);
} else {
glTranslatef(-2, -2, 0);
glRotatef(90, 0, 0, 1);
}
glBegin(GL_TRIANGLES);
glVertex2f(-2.0, -1.0);
glVertex2f(1.0, -1.0);
glVertex2f(0.0, 2.0);
glVertex2f(4.0, -4.0);
glVertex2f(4.0, 5.0);
glVertex2f(3.0, 5.0);
glEnd();
glPopMatrix();
glutSwapBuffers(); //Send the 3D scene to the screen
}
void drawDotsLines() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); //Reset the drawing perspective
glPushMatrix();
glScalef(0.1, 0.1, 0.1);
glBegin(GL_POINTS);
glVertex2f(-3, 2);
glVertex2f(2, 1);
glEnd();
glLineWidth(2.0); // Specify line width
glBegin(GL_LINES); // Draw line
glVertex2f(-5, 7);
glVertex2f(5, -7);
glEnd();
glPopMatrix();
glutSwapBuffers(); //Send the 3D scene to the screen
}
void drawTriangleColored() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); //Reset the drawing perspective
glPushMatrix();
glScalef(0.1, 0.1, 0.1);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0, 0); // specify color for one vertex
glVertex2f(-2.0, -1.0);
glColor3f(0, 1.0, 0);
glVertex2f(1.0, -1.0);
glColor3f(0, 0.0, 1.0);
glVertex2f(0.0, 2.0);
glEnd();
glPopMatrix();
glutSwapBuffers(); //Send the 3D scene to the screen
}
void drawGreenSquare() {
glColor4f(0.5, 1, 0.5, 0.5); // alpha!!!
glPushMatrix();
glScalef(0.25, 0.25, 0.25);
glTranslatef(-0.25, 0.25, 0);
glBegin(GL_QUADS);
glVertex2f(-1, -1);
glVertex2f(1, -1);
glVertex2f(1, 1);
glVertex2f(-1, 1);
glEnd();
glPopMatrix();
}
void drawRedSquare() {
glColor4f(1, 0.5, 0.5, 0.5); // alpha!!!
glPushMatrix();
glScalef(0.25, 0.25, 0.25);
glTranslatef(0.25, -0.25, 0);
glBegin(GL_QUADS);
glVertex2f(-1, -1);
glVertex2f(1, -1);
glVertex2f(1, 1);
glVertex2f(-1, 1);
glEnd();
glPopMatrix();
}
void drawSceneAlpha() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // many possible blend modes here!
if (redFirst) {
drawRedSquare();
glTranslatef(0, 0, -0.01);
drawGreenSquare();
} else {
drawGreenSquare();
glTranslatef(0, 0, -0.01);
drawRedSquare();
}
glDisable(GL_BLEND);
glutSwapBuffers();
}
int main(int argc, char **argv) {
//Initialize GLUT
glutInit(&argc, argv);
// TOGGLE ME!
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
// glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
//Create the window
glutInitWindowSize(1280, 800); //Set the window size
glutCreateWindow("15-462 OpenGL Tutorial");
initRendering(); //Initialize rendering
// Main draw routine(s)
// glutDisplayFunc(drawTriangleWhite);
// glutDisplayFunc(drawDotsLines);
// glutDisplayFunc(drawTriangleColored);
glutDisplayFunc(drawCube);
// glutDisplayFunc(drawSceneAlpha);
//Set handler functions for keypresses, and window resizes
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMainLoop(); //Start the main loop
return 0;
}