This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes.c
280 lines (249 loc) · 5.96 KB
/
shapes.c
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
#include "project.h"
/*
* BEGIN SHAPE DEFINITION FUNCTIONS
*/
/*
* square
* ------
* Draw a square of type t, up to four sides
* This can be refactored to be much nicer
*/
void square(int s, int a, int b, int c, int d)
{
glBegin(GL_POLYGON);
/* front */
if (s == 1) { glNormal3f(0,0,1); }
/* back */
else if (s == 2) { glNormal3f(0,0,-1); }
/* right */
else if (s == 3) { glNormal3f(1,0,0); }
/* left */
else if (s == 4) { glNormal3f(-1,0,0); }
/* top */
else if (s == 5) { glNormal3f(0,1,0); }
/* bottom */
else if (s == 6) { glNormal3f(0,-1,0); }
glTexCoord2f(0,0); glVertex3fv(cube_v[a]);
glTexCoord2f(1,0); glVertex3fv(cube_v[b]);
glTexCoord2f(1,1); glVertex3fv(cube_v[c]);
glTexCoord2f(0,1); glVertex3fv(cube_v[d]);
glEnd();
}
/*
* cube
* ------
* Draw a cube
* at (x,y,z)
* dimensions (dx,dy,dz)
* rotated th about the y axis
*/
void cube(double x,double y,double z,
double dx,double dy,double dz,
double th)
{
float emissions[] = {0,0,0.01*emission,1};
glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,shinyvec);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white);
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,emissions);
/* enable textures */
if (renderMode == DEF_RENDER) {
glEnable(GL_TEXTURE_2D);
/* using the current texture */
glBindTexture(GL_TEXTURE_2D,currentTexture);
}
glPushMatrix();
/* Transform cube */
glTranslated(x,y,z);
glRotated(th,0,1,0);
glScaled(dx,dy,dz);
/* front */
square(1, 4,5,6,7);
/* back */
square(2, 3,0,1,2);
/* right */
square(3, 5,6,2,1);
/* left */
square(4, 7,4,0,3);
/* top */
square(5, 6,7,3,2);
/* bottom */
square(6, 0,4,5,1);
glPopMatrix();
/* disable textures */
glDisable(GL_TEXTURE_2D);
}
/*
* vertex
* ------
* Draw vertex in polar coordinates with normal
*/
void vertex(double th,double ph)
{
double x = Sin(th)*Cos(ph);
double y = Cos(th)*Cos(ph);
double z = Sin(ph);
/* For a sphere at the origin, the position
and normal vectors are the same */
glNormal3d(x,y,z);
glTexCoord2d(th/360.0,ph/180.0+0.5);
glVertex3d(x,y,z);
}
/*
* circle
* ------
* Draw a circle of radius r
*/
void circle(int r)
{
int th;
glRotated(90,1,0,0);
glScaled(r,r,r);
glBegin(GL_QUAD_STRIP);
for (th=0;th<=360;th+=2*DEF_D) {
vertex(th,0);
vertex(th,1);
}
glEnd();
}
/*
* Draw a sphere
* at (x,y,z)
* radius (r)
* rotated rot around the y axis
*/
void sphere(double x,double y,double z,double r,double rot)
{
int th,ph;
float yellow[] = {1.0,1.0,0.0,1.0};
float emissions[] = {0.0,0.0,0.01*emission,1.0};
glMaterialfv(GL_FRONT,GL_SHININESS,shinyvec);
glMaterialfv(GL_FRONT,GL_SPECULAR,yellow);
glMaterialfv(GL_FRONT,GL_EMISSION,emissions);
if (renderMode == DEF_RENDER) {
/* enable textures */
glEnable(GL_TEXTURE_2D);
/* using the current texture */
glBindTexture(GL_TEXTURE_2D,currentTexture);
}
glPushMatrix();
/* Offset, scale and rotate */
glTranslated(x,y,z);
glScaled(r,r,r);
glRotated(rot,0,1,0);
/* Bands of latitude */
for (ph=-90;ph<90;ph+=DEF_D) {
glBegin(GL_QUAD_STRIP);
for (th=0;th<=360;th+=2*DEF_D) {
vertex(th,ph);
vertex(th,ph+DEF_D);
}
glEnd();
}
glPopMatrix();
/* disable textures */
glDisable(GL_TEXTURE_2D);
}
/*
* cone
* ------
* Draws a cone
* at (x,y,z)
* with radius r and height h
* with 360/deg sides
*/
void cone(double x,double y,double z,
double r,double h,int deg)
{
int k;
if (renderMode == DEF_RENDER) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,currentTexture);
}
glPushMatrix();
/* Transformation */
glTranslated(x,y,z);
glScaled(r,h,r);
/* we're drawing this sideways
because for some reason drawing upright doesn't work for normals */
glRotated(-90,1,0,0);
/* sides */
glBegin(GL_TRIANGLES);
for (k=0;k<=360;k+=deg){
glNormal3f(0,0,1);
/* center of cone is always center of texture */
glTexCoord2f(0.5,0.5);
glVertex3f(0,0,1);
glNormal3f(Cos(k),Sin(k),r);
/* need to cast to double or lose precision */
glTexCoord2f((double)1/2*Cos(k)+0.5,(double)1/2*Sin(k)+0.5);
glVertex3f(Cos(k),Sin(k),0);
glNormal3f(Cos(k+deg),Sin(k+deg),r);
/* need to cast to double or lose precision */
glTexCoord2f((double)1/2*Cos(k+deg)+0.5,(double)1/2*Sin(k+deg)+0.5);
glVertex3f(Cos(k+deg),Sin(k+deg),0);
}
glEnd();
/* bottom circle */
/* rotate back */
glRotated(90,1,0,0);
glBegin(GL_TRIANGLES);
glNormal3f(0,-1,0);
for (k=0;k<=360;k+=deg) {
glTexCoord2f(0.5,0.5);
glVertex3f(0,0,0);
glTexCoord2f(0.5*Cos(k)+0.5,0.5*Sin(k)+0.5);
glVertex3f(Cos(k),0,Sin(k));
glTexCoord2f(0.5*Cos(k+deg)+0.5,0.5*Sin(k+deg)+0.5);
glVertex3f(Cos(k+deg),0,Sin(k+deg));
}
glEnd();
glPopMatrix();
/* disable textures */
glDisable(GL_TEXTURE_2D);
}
/*
* cylinder
* ------
* Draw a cylinder
* at (x, y, z)
* with radius r and height h
*/
void cylinder(double x,double y,double z,
double r,double h)
{
int i,k;
if (renderMode == DEF_RENDER) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,currentTexture);
}
glPushMatrix();
/* Transformation */
glTranslated(x,y,z);
glScaled(r,h,r);
/* sides */
glBegin(GL_QUAD_STRIP);
for (k=0;k<=360;k+=DEF_D) {
/* These textures aren't exactly what I want, but I think they look cool */
glNormal3f(Cos(k),0,Sin(k));
glTexCoord2f(-Cos(k),Sin(k));
glVertex3f(Cos(k),+1,Sin(k));
glTexCoord2f(Cos(k),Sin(k));
glVertex3f(Cos(k),-1,Sin(k));
}
glEnd();
/* top and bottom circles */
/* reuse the currentTexture on top and bottom) */
for (i=1;i>=-1;i-=2) {
glBegin(GL_TRIANGLE_FAN);
glNormal3f(0,i,0);
glTexCoord2f(0.5,0.5);
glVertex3f(0,i,0);
for (k=0;k<=360;k+=DEF_D) {
glTexCoord2f(0.5*Cos(k)+0.5,0.5*Sin(k)+0.5);
glVertex3f(i*Cos(k),i,Sin(k));
}
glEnd();
}
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}