-
Notifications
You must be signed in to change notification settings - Fork 2
/
basics.cpp
341 lines (298 loc) · 13.1 KB
/
basics.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
#include "basics.hpp"
#include <cmath>
//The following two functions set the material of the object
void setMaterial(GLfloat spec[], GLfloat amb[], GLfloat diff[], GLfloat shin[])
{
glMaterialfv(GL_FRONT, GL_SPECULAR, spec);
glMaterialfv(GL_FRONT, GL_SHININESS, shin);
glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diff);
}
void setMaterialColors(float r, float g, float b, float shin)
{
GLfloat mat_specularRGB[] ={r*shin/128.0,g*shin/128.0,b*shin/128.0,1.0};
GLfloat mat_ambientRGB[] ={r,g,b,1.0};
GLfloat mat_diffuseRGB[] ={r,g,b,1.0};
GLfloat mat_shininessRGB[] ={shin};
setMaterial(mat_specularRGB, mat_ambientRGB, mat_diffuseRGB, mat_shininessRGB);
}
//Next on are the basic shapes required for the transformer
//Cube
void drawCube(float r, float g, float b) {
glColor4f(r, g, b, 1.0);
glBegin(GL_QUADS);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glEnd();
}
//Line
void drawLine(float r, float g, float b) {
glColor4f(r, g, b, 1.0);
glBegin(GL_LINES);
glVertex3f(-0.5f, 0.0f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
glEnd();
}
//A box
void drawBox(float r, float g, float b) {
glColor4f(r, g, b, 1.0);
glBegin(GL_QUADS);
glVertex3f(0.5f, 0.5f, 0.0f);
glVertex3f(-0.5f, 0.5f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glEnd();
}
//Sphere
void drawSphere(int lats, int longs, float r, float g, float b) {
glColor4f(r, g, b, 1.0);
int i, j;
for(i = 0; i <= lats; i++) {
double lat0 = PI * (-0.5 + (double) (i - 1) / lats);
double z0 = sin(lat0);
double zr0 = cos(lat0);
double lat1 = PI * (-0.5 + (double) i / lats);
double z1 = sin(lat1);
double zr1 = cos(lat1);
glBegin(GL_QUAD_STRIP);
for(j = 0; j <= longs; j++) {
double lng = 2 * PI * (double) (j - 1) / longs;
double x = cos(lng);
double y = sin(lng);
glNormal3f(x * zr0, y * zr0, z0);
glVertex3f(x * zr0, y * zr0, z0);
glNormal3f(x * zr1, y * zr1, z1);
glVertex3f(x * zr1, y * zr1, z1);
}
glEnd();
}
}
//Cylinder
void drawCylinder(float r, float g, float b) {
setMaterialColors(r, g, b);
glColor4f(r, g, b, 1.0);
/* top triangle */
double i, resolution = 0.1;
double height = 1, radius = 0.5;
glPushMatrix();
glTranslatef(0, -0.5, 0);
glBegin(GL_TRIANGLE_FAN);
/* center */
glVertex3f(0, height, 0);
for (i = 0; i <= 2 * PI; i += resolution)
glVertex3f(radius * cos(i), height, radius * sin(i));
glEnd();
/* bottom triangle: note: for is in reverse order */
glBegin(GL_TRIANGLE_FAN);
/* center */
glVertex3f(0, 0, 0);
for (i = 2 * PI; i >= 0; i -= resolution)
glVertex3f(radius * cos(i), 0, radius * sin(i));
/* close the loop back to 0 degrees */
glVertex3f(radius, height, 0);
glEnd();
/* middle tube */
glBegin(GL_QUAD_STRIP);
for (i = 0; i <= 2 * PI; i += resolution)
{
glVertex3f(radius * cos(i), 0, radius * sin(i));
glVertex3f(radius * cos(i), height, radius * sin(i));
}
/* close the loop back to zero degrees */
glVertex3f(radius, 0, 0);
glVertex3f(radius, height, 0);
glEnd();
glPopMatrix();
}
void drawCone(float r, float g, float b) {
setMaterialColors(r, g, b);
glColor4f(r, g, b, 1.0);
/* top triangle */
double i, resolution = 0.1;
double height = 1, radius = 0.5;
glPushMatrix();
glTranslatef(0, -0.5, 0);
glBegin(GL_TRIANGLE_FAN);
/* center */
glVertex3f(0, height, 0);
for (i = 0; i <= 2 * PI; i += resolution)
glVertex3f(radius * cos(i), height, radius * sin(i));
glEnd();
/* middle tube */
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0, 0, 0);
for (i = 0; i <= 2 * PI; i += resolution)
{
glVertex3f(radius * cos(i), height, radius * sin(i));
}
glVertex3f(radius, height, 0);
glEnd();
glPopMatrix();
}
//Next up are the textured versions of the basic shapes
void texcube(int i, float r, float g, float b, bool override)
{
glEnable(GL_TEXTURE_2D);
// choose the texture to use.
glBindTexture(GL_TEXTURE_2D, texture[i]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
// begin drawing a cube
glBegin(GL_QUADS);
if(!override)
setMaterialColors(r, g, b, 40);
else
setMaterialColors(r, g, b, 0);
// Front Face "+z"
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, 0.5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, -0.5f, 0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, 0.5f, 0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f, 0.5f); // Top Left Of The Texture and Quad
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f, 0.5f, -0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.5f, 0.5f, -0.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.5f, -0.5f, -0.5f); // Bottom Left Of The Texture and Quad
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f, -0.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, 0.5f, 0.5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, 0.5f, 0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, 0.5f, -0.5f); // Top Right Of The Texture and Quad
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f, -0.5f, -0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.5f, -0.5f, -0.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.5f, -0.5f, 0.5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.5f, -0.5f, 0.5f); // Bottom Right Of The Texture and Quad
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, -0.5f, -0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, 0.5f, -0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.5f, 0.5f, 0.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.5f, -0.5f, 0.5f); // Bottom Left Of The Texture and Quad
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -0.5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.5f, -0.5f, 0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f, 0.5f, 0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f, -0.5f); // Top Left Of The Texture and Quad
glEnd();
// done with the polygon.
glDisable(GL_TEXTURE_2D);
}
void texCylinder(float r, float g, float b, int texindex) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[texindex]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
setMaterialColors(r, g, b);
glColor4f(r, g, b, 1.0);
/* top triangle */
double i, resolution = 0.1;
double height = 1, radius = 0.5;
glPushMatrix();
glTranslatef(0, -0.5, 0);
glBegin(GL_TRIANGLE_FAN);
/* center */
glTexCoord2f( 0.5, 0.5 );
glVertex3f(0, height, 0);
for (i = 0; i <= 2 * PI; i += resolution)
{
glTexCoord2f( 0.5f * cos(i) + 0.5f, 0.5f * sin(i) + 0.5f );
glVertex3f(radius * cos(i), height, radius * sin(i));
}
glEnd();
/* bottom triangle: note: for is in reverse order */
glBegin(GL_TRIANGLE_FAN);
/* center */
glTexCoord2f( 0.5, 0.5 );
glVertex3f(0, 0, 0);
for (i = 2 * PI; i >= 0; i -= resolution)
{
glTexCoord2f( 0.5f * cos(i) + 0.5f, 0.5f * sin(i) + 0.5f );
glVertex3f(radius * cos(i), 0, radius * sin(i));
}
/* close the loop back to 0 degrees */
glVertex3f(radius, height, 0);
glEnd();
/* middle tube */
glBegin(GL_QUAD_STRIP);
for (i = 0; i <= 2 * PI; i += resolution)
{
glVertex3f(radius * cos(i), 0, radius * sin(i));
glVertex3f(radius * cos(i), height, radius * sin(i));
}
/* close the loop back to zero degrees */
glVertex3f(radius, 0, 0);
glVertex3f(radius, height, 0);
glEnd();
glPopMatrix();
}
//Sky requires a special u-v map, so taken as a separate case.
void skybox(float r, float g, float b, bool override, int texindex)
{
// Enable Texture Mapping
glEnable(GL_TEXTURE_2D);
// choose the texture to use.
glBindTexture(GL_TEXTURE_2D, texture[texindex]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
// begin drawing a cube
glBegin(GL_QUADS);
if(!override)
setMaterialColors(r, g, b, 40);
else
setMaterialColors(r, g, b, 0);
// Front Face
glTexCoord2f(0.0f, 0.33f); glVertex3f(-0.5f, -0.5f, 0.5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(0.25f, 0.33f); glVertex3f( 0.5f, -0.5f, 0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(0.25f, 0.66f); glVertex3f( 0.5f, 0.5f, 0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 0.66f); glVertex3f(-0.5f, 0.5f, 0.5f); // Top Left Of The Texture and Quad
// Right face
glTexCoord2f(0.5f, 0.33f); glVertex3f( 0.5f, -0.5f, -0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(0.5f, 0.66f); glVertex3f( 0.5f, 0.5f, -0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.25f, 0.66f); glVertex3f( 0.5f, 0.5f, 0.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.25f, 0.33f); glVertex3f( 0.5f, -0.5f, 0.5f); // Bottom Left Of The Texture and Quad
// Back Face
glTexCoord2f(0.75f, 0.33f); glVertex3f(-0.5f, -0.5f, -0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(0.75f, 0.66f); glVertex3f(-0.5f, 0.5f, -0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.5f, 0.66f); glVertex3f( 0.5f, 0.5f, -0.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.5f, 0.33f); glVertex3f( 0.5f, -0.5f, -0.5f); // Bottom Left Of The Texture and Quad
// Left Face
glTexCoord2f(0.75f, 0.33f); glVertex3f(-0.5f, -0.5f, -0.5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.33f); glVertex3f(-0.5f, -0.5f, 0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 0.66f); glVertex3f(-0.5f, 0.5f, 0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.75f, 0.66f); glVertex3f(-0.5f, 0.5f, -0.5f); // Top Left Of The Texture and Quad
// Top Face
glTexCoord2f(0.5f, 1.0f); glVertex3f(-0.5f, 0.5f, -0.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.25f, 1.0f); glVertex3f(-0.5f, 0.5f, 0.5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(0.25f, 0.66f); glVertex3f( 0.5f, 0.5f, 0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(0.5f, 0.66f); glVertex3f( 0.5f, 0.5f, -0.5f); // Top Right Of The Texture and Quad
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f, -0.5f, -0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.5f, -0.5f, -0.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.5f, -0.5f, 0.5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.5f, -0.5f, 0.5f); // Bottom Right Of The Texture and Quad
glEnd();
// done with the polygon.
glDisable(GL_TEXTURE_2D);
}