-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgl_util.h
executable file
·223 lines (185 loc) · 5.55 KB
/
gl_util.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
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
#ifndef _GL_UTIL_H
#define _GL_UTIL_H
#include <GL/glut.h>
#include <GL/glu.h>
#include <cmath>
#define RADPERDEG 0.0174533
int NoColor = false;
void drawArrow(float x1, float y1, float z1, float x2, float y2,
float z2, float D) {
float x = x2 - x1;
float y = y2 - y1;
float z = z2 - z1;
float L = sqrt(x * x + y * y + z * z);
GLUquadricObj *quadObj;
glPushMatrix();
glTranslated(x1, y1, z1);
if ((x != 0.) || (y != 0.)) {
glRotated(atan2(y, x) / RADPERDEG, 0., 0., 1.);
glRotated(atan2(sqrt(x * x + y * y), z) / RADPERDEG, 0., 1., 0.);
}
else if (z < 0) {
glRotated(180, 1., 0., 0.);
}
glTranslatef(0, 0, L - 4 * D);
quadObj = gluNewQuadric();
gluQuadricDrawStyle(quadObj, GLU_FILL);
gluQuadricNormals(quadObj, GLU_SMOOTH);
gluCylinder(quadObj, 2 * D, 0.0, 4 * D, 32, 4);
gluDeleteQuadric(quadObj);
quadObj = gluNewQuadric();
gluQuadricDrawStyle(quadObj, GLU_FILL);
gluQuadricNormals(quadObj, GLU_SMOOTH);
gluDisk(quadObj, 0.0, 2 * D, 32, 4);
gluDeleteQuadric(quadObj);
glTranslatef(0, 0, -L + 4 * D);
quadObj = gluNewQuadric();
gluQuadricDrawStyle(quadObj, GLU_FILL);
gluQuadricNormals(quadObj, GLU_SMOOTH);
gluCylinder(quadObj, D, D, L - 4 * D, 32, 4);
gluDeleteQuadric(quadObj);
quadObj = gluNewQuadric();
gluQuadricDrawStyle(quadObj, GLU_FILL);
gluQuadricNormals(quadObj, GLU_SMOOTH);
gluDisk(quadObj, 0.0, D, 32, 4);
gluDeleteQuadric(quadObj);
glPopMatrix();
}
void drawCircle(float r) {
glPushMatrix();
GLUquadricObj *quadObj;
quadObj = gluNewQuadric();
gluQuadricDrawStyle(quadObj, GLU_SILHOUETTE);
gluQuadricNormals(quadObj, GLU_SMOOTH);
gluDisk(quadObj, 0.0, r, 32, 4);
gluDeleteQuadric(quadObj);
glPopMatrix();
}
void drawCylinder(float h, float r) {
glPushMatrix();
GLUquadricObj *quadObj;
quadObj = gluNewQuadric();
gluQuadricDrawStyle(quadObj, GLU_FILL);
gluQuadricNormals(quadObj, GLU_SMOOTH);
gluCylinder(quadObj, r, r, h, 32, 4);
gluQuadricDrawStyle(quadObj, GLU_FILL);
gluQuadricNormals(quadObj, GLU_SMOOTH);
gluDisk(quadObj, 0.0, r, 32, 4);
glTranslatef(0, 0, h);
gluQuadricDrawStyle(quadObj, GLU_FILL);
gluQuadricNormals(quadObj, GLU_SMOOTH);
gluDisk(quadObj, 0.0, r, 32, 4);
gluDeleteQuadric(quadObj);
glPopMatrix();
}
void drawCone(float h, float r0, float r1) {
glPushMatrix();
GLUquadricObj *quadObj;
quadObj = gluNewQuadric();
gluQuadricDrawStyle(quadObj, GLU_FILL);
gluQuadricNormals(quadObj, GLU_SMOOTH);
gluCylinder(quadObj, r0, r1, h, 32, 4);
gluDeleteQuadric(quadObj);
glPopMatrix();
}
void drawSphere(float r, float slices , float stacks) {
GLUquadricObj *quadObj;
quadObj = gluNewQuadric();
gluSphere(quadObj, r, slices, stacks);
gluDeleteQuadric(quadObj);
}
void draw_axes_arrow(float width, float height, float depth, float r)
{
glColor3f(1, 0, 0);
drawArrow(-width / 2, 0, 0, width / 2, 0, 0, r);
glColor3f(0, 1, 0);
drawArrow(0, -height / 2, 0, 0, height / 2, 0, r);
glColor3f(0, 0, 1);
drawArrow(0, 0, -depth / 2, 0, 0, depth / 2, r);
}
void draw_axes(float width, float height, float depth)
{
glColor3f(1, 0, 0);
glBegin(GL_LINES);
glVertex3f(-width / 2, 0, 0);
glVertex3f(width / 2, 0, 0);
glEnd();
glColor3f(0, 0, 1);
glBegin(GL_LINES);
glVertex3f(0, -height / 2, 0);
glVertex3f(0, height / 2, 0);
glEnd();
glColor3f(0, 1, 0);
glBegin(GL_LINES);
glVertex3f(0, 0, -depth / 2);
glVertex3f(0, 0, depth / 2);
glEnd();
}
void draw_axes_positive(float width, float height, float depth)
{
glColor3f(1, 0, 0);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(width, 0, 0);
glEnd();
glColor3f(0, 0, 1);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(0, height, 0);
glEnd();
glColor3f(0, 1, 0);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, depth);
glEnd();
}
void drawCube(float dx, float dy, float dz) {
glPushMatrix();
glTranslatef(-dx / 2, -dy / 2, -dz / 2);
glBegin(GL_QUADS);
// back face
glNormal3f(0, 0, -1);
glVertex3f(0, 0, 0);
glVertex3f(0, dy, 0);
glVertex3f(dx, dy, 0);
glVertex3f(dx, 0, 0);
// flront face
glNormal3f(0, 0, 1);
glVertex3f(0, 0, dz);
glVertex3f(0, dy, dz);
glVertex3f(dx, dy, dz);
glVertex3f(dx, 0, dz);
// bottom face (z = 1.0f)
glNormal3f(0, -1, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, dz);
glVertex3f(dx, 0, dz);
glVertex3f(dx, 0, 0);
// top face (z = -1.0f)
glNormal3f(0, 1, 0);
glVertex3f(0, dy, 0);
glVertex3f(0, dy, dz);
glVertex3f(dx, dy, dz);
glVertex3f(dx, dy, 0);
// Left face (x = -1.0f)
glNormal3f(-1, 0, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, dz);
glVertex3f(0, dy, dz);
glVertex3f(0, dy, 0);
// // Right face (x = 1.0f)
glNormal3f(1, 0, 0);
glVertex3f(dx, 0, 0);
glVertex3f(dx, 0, dz);
glVertex3f(dx, dy, dz);
glVertex3f(dx, dy, 0);
glEnd(); // End of drawing color-cube
glPopMatrix();
}
void renderString(float x, float y, const char* string, float r = 1, float g = 1, float b = 1)
{
glColor3f(r, g, b);
glRasterPos2f(x, y);
glutBitmapString(GLUT_BITMAP_9_BY_15, (const unsigned char*)string);
}
#endif