-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcube_system.cpp
More file actions
214 lines (150 loc) · 4.94 KB
/
cube_system.cpp
File metadata and controls
214 lines (150 loc) · 4.94 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
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
#include "cube_system.h"
// Initialize variables
cube_system::cube_system()
{
angle = 0;
}
// Setup the scene and render cube system
void cube_system::render()
{
// Enable depth testing
glEnable(GL_DEPTH_TEST);
// Disable lighting
glDisable(GL_LIGHTING);
// Clear the screen and depth buffer
glClearColor(1,1,1,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set the projection and view
setup_projection();
// Render the system
render_system();
}
// Advance one frame by increasing the rotation angle
void cube_system::advance_frame()
{
// Increase angle and perform a modulo 360
angle = (angle+1)%360;
}
// Render the cube system
void cube_system::render_system()
{
/********
Task 2.1.2. Program the transformation tree from the exercise sheet. To store the
active aggregated transformation matrix on top of a stack use glPushMatrix
and to reactivate and remove the top element of the transformation stack
use glPopMatrix. For animation a variable "angle" (which has values
between 0 and 359) is defined. Use it for rotations as defined in
the transformation tree.
Aufgabe 2.1.2. Programmieren Sie den Transformationsbaum aus dem Uebungsblatt.
Nutzen Sie glPushMatrix um die aktuelle Gesamttransformations-Matrix an die
Spitze eines Stacks zu speichern und glPopMatrix um die vorderste Matrix
im Stack zu entfernen und zu reaktivieren. Fuer Animationen steht eine Variable
"angle" zur Verfuegung (die Werte zwischen 0 und 359 enthält). Nutzen Sie diese
Variable für Rotationen analog zu den Angaben im gegebenen Transformationsbaum.
*********/
// Remove the following statement (if wanted) as it just serves 2.1.1.
// Entfernen Sie die folgende Anweisung gegebenenfalls, da sie lediglich dem Testen
// von Aufgabe 2.1.1 dient.
// render_cube();
glPushMatrix();
glRotated(angle, 0, 1, 0);
render_cube();
glPopMatrix();
glRotated(-angle, 0, 1, 0);
glTranslated(5, 0, 0);
glScaled(0.6, 0.6, 0.6);
render_cube();
glPushMatrix();
glRotated(2 * angle, 0, 0, 1);
glTranslated(3, 0, 0);
glScaled(0.5, 0.5, 0.5);
render_cube();
glPopMatrix();
glRotated(2 * angle + 180, 0, 0, 1);
glTranslated(3, 0, 0);
glScaled(0.5, 0.5, 0.5);
render_cube();
}
// Render one single cube
void cube_system::render_cube()
{
/********
Task 2.1.1. Tesellate a cube by specifying vertices and colors via the commands
glVertex3d and glColor3d. Choose appropriate primitives (parameter to
glBegin) and provide vertices and colors for all 6 sides. The cube shall
range from (-1, -1, -1) to (1, 1, 1).
Aufgabe 2.1.1. Tesellieren Sie einen Wuerfel, indem Sie Vertices und Farben mittels
der Kommandos glVertex3d und glColor3d spezifizieren. Waehlen Sie
zunaechst das passende Zeichenprimitiv (Parameter von glBegin) und
erstellen Sie Vertices und Farben für alle 6 Seiten. Der Wuerfel soll
von (-1, -1, -1) bis (1, 1, 1) reichen.
*********/
// Vorderseite
glBegin(GL_QUADS);
glColor3d( 0, 0, 0 );
glVertex3d( -1, -1, -1); // P1
glVertex3d( -1, 1, -1); // P2
glVertex3d( 1, 1, -1); // P3
glVertex3d( 1, -1, -1); // P4
glEnd();
//Rückseite - weiß (grau)
glBegin(GL_QUADS);
glColor3d( 0.8, 0.8, 0.8 );
glVertex3d( 1, -1, 1 );
glVertex3d( 1, 1, 1 );
glVertex3d( -1, 1, 1 );
glVertex3d( -1, -1, 1 );
glEnd();
//rechte Seite - lila (einfarbig)
glBegin(GL_QUADS);
glColor3d( 1.0, 0.0, 1.0 );
glVertex3d( 1, -1, -1 );
glVertex3d( 1, 1, -1 );
glVertex3d( 1, 1, 1 );
glVertex3d( 1, -1, 1 );
glEnd();
//linke Seite - grün (einfarbig)
glBegin(GL_QUADS);
glColor3d( 0.0, 1.0, 0.0 );
glVertex3d( -1, -1, 1 );
glVertex3d( -1, 1, 1 );
glVertex3d( -1, 1, -1 );
glVertex3d( -1, -1, -1 );
glEnd();
//obere Seite - blau (einfarbig)
glBegin(GL_QUADS);
glColor3d( 0.0, 0.0, 1.0 );
glVertex3d( 1, 1, 1 );
glVertex3d( 1, 1, -1 );
glVertex3d( -1, 1, -1 );
glVertex3d( -1, 1, 1 );
glEnd();
//untere Seite - rot (einfarbig)
glBegin(GL_QUADS);
glColor3d( 1.0, 0.0, 0.0 );
glVertex3d( 1, -1, -1 );
glVertex3d( 1, -1, 1 );
glVertex3d( -1, -1, 1 );
glVertex3d( -1, -1, -1 );
glEnd();
}
// Set the projection and the view
void cube_system::setup_projection()
{
// For projection choose a perspective matrix with an aperture angle of
// 45deg, an aspect ratio that corresponds to the width and height of the
// window, z_near at 0.01 and z_far at 100.0
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, glutGet(GLUT_WINDOW_WIDTH) / (double)glutGet(GLUT_WINDOW_HEIGHT), 0.01, 100.0);
// For the modelview matrix choose a view from the position (10.0, 6.0, -10.0) to
// the position (0, 0, 0) where the up-direction is (0, 1, 0).
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(10.0,6.0,-10.0, 0,0,0, 0,1,0);
}
// Set debug text
void cube_system::set_text(stringstream &stream)
{
stream<<"Showing cube system (with rotation parameter at "<<angle<<"°)";
}