-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes.h
140 lines (118 loc) · 3.9 KB
/
shapes.h
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
#ifndef SHAPES_H
#define SHAPES_H // Header guards ...
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <math.h>
const int PI = 4*atan(1);
void draw_cylinder(float radius, float height, int R, int G, int B) {
float x = 0.0, y = 0.0, angle = 0.0, angle_stepsize = 0.1;
glColor3f(R/255.0, G/255.0, B/255.0); // Setting color for cylinder ...
// Draw the tube
glBegin(GL_QUAD_STRIP);
angle = 0.0;
while( angle < 2*PI ) {
x = radius * cos(angle);
y = radius * sin(angle);
glNormal3d(cos(angle), sin(angle), 0.0); // Setting outword normal for each strip ...
glVertex3f(x, y , height);
glVertex3f(x, y , 0.0);
angle = angle + angle_stepsize;
}
glNormal3d(1.0, 0.0, 0.0);
glVertex3f(radius, 0.0, height);
glVertex3f(radius, 0.0, 0.0);
glEnd();
// Draw the circle on top of cylinder
glBegin(GL_POLYGON);
angle = 0.0;
glNormal3d(0.0, 0.0, 1.0); // Setting normal for top of cylinder ...
while( angle < 2*PI ) {
x = radius * cos(angle);
y = radius * sin(angle);
glVertex3f(x, y , height);
angle = angle + angle_stepsize;
}
glVertex3f(radius, 0.0, height);
glEnd();
// Draw the circle on bottom of cylinder
glBegin(GL_POLYGON);
angle = 0.0;
glNormal3d(0.0, 0.0, -1.0); // Setting normal for top of cylinder ...
while( angle < 2*PI ) {
x = radius * cos(angle);
y = radius * sin(angle);
glVertex3f(x, y , 0.0);
angle = angle + angle_stepsize;
}
glVertex3f(radius, 0.0, 0.0);
glEnd();
}
void draw_open_cylinder(float radius, float height, int R, int G, int B) {
float x = 0.0, y = 0.0, angle = 0.0, angle_stepsize = 0.1;
glColor3f(R/255.0, G/255.0, B/255.0);
// Draw the tube
glBegin(GL_QUAD_STRIP);
angle = 0.0;
while( angle < 2*PI ) {
x = radius * cos(angle);
y = radius * sin(angle);
glNormal3d(-cos(angle), -sin(angle), 0.0); // Setting inwards normal for each strip ...
glVertex3f(x, y , height);
glVertex3f(x, y , 0.0);
angle = angle + angle_stepsize;
}
glVertex3f(radius, 0.0, height);
glVertex3f(radius, 0.0, 0.0);
glEnd();
}
void draw_circle(float cx, float cy, float radius) {
float x, y;
float angle = 0.0, angle_stepsize=0.1;
glBegin(GL_POLYGON);
glNormal3d(0.0, 0.0, 1.0); // Setting normal for circular plate ...
while( angle < 2*PI ) {
x = radius * cos(angle);
y = radius * sin(angle);
glVertex3f(cx + x, cy + y , 0);
angle = angle + angle_stepsize;
}
glVertex3f(cx + radius, cy + 0.0, 0.0);
glEnd();
}
void draw_cube(int l, int b, int h, int R, int G, int B) {
glBegin(GL_QUADS);
glColor3f(R/255.0, G/255.0, B/255.0);
glNormal3d(0, 0, -1);
glVertex3f(0, 0, 0);
glVertex3f(0, b, 0);
glVertex3f(l, b, 0);
glVertex3f(l, 0, 0);
glNormal3d(0, 0, 1);
glVertex3f(0, 0, h);
glVertex3f(l, 0, h);
glVertex3f(l, b, h);
glVertex3f(0, b, h);
glNormal3d(-1, 0, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, h);
glVertex3f(0, b, h);
glVertex3f(0, b, 0);
glNormal3d(1, 0, 0);
glVertex3f(l, 0, 0);
glVertex3f(l, b, 0);
glVertex3f(l, b, h);
glVertex3f(l, 0, h);
glNormal3d(0, -1, 0);
glVertex3f(0, 0, 0);
glVertex3f(l, 0, 0);
glVertex3f(l, 0, h);
glVertex3f(0, 0, h);
glNormal3d(0, 1, 0);
glVertex3f(0, b, 0);
glVertex3f(0, b, h);
glVertex3f(l, b, h);
glVertex3f(l, b, 0);
glEnd();
};
#endif