-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathklglutil.cpp
270 lines (242 loc) · 7.85 KB
/
klglutil.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
#include "klglutil.h"
KLGLUtil::KLGLUtil()
{
}
//adopted here: http://www.cburch.com/cs/490/sched/feb8/index.html
void KLGLUtil::drawSphere(double radius, int lats, int longs)
{
// int i, j;
// for(i = 0; i <= lats; i++) {
// double lat0 = M_PI * (-0.5 + (double) (i - 1) / lats);
// double z0 = sin(lat0);
// double zr0 = cos(lat0);
// double lat1 = M_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 * M_PI * (double) (j - 1) / longs;
// double x = cos(lng);
// double y = sin(lng);
// glNormal3f(radius * x * zr0, radius * y * zr0, radius * z0);
// glVertex3f(radius * x * zr0, radius * y * zr0, radius * z0);
// glNormal3f(radius * x * zr1, radius * y * zr1, radius * z1);
// glVertex3f(radius * x * zr1, radius * y * zr1, radius * z1);
// }
// glEnd();
// }
GLUquadricObj* quadratic;
quadratic = gluNewQuadric();
gluQuadricNormals(quadratic, GLU_SMOOTH);
gluQuadricTexture(quadratic, GL_TRUE);
gluSphere(quadratic, radius, lats, longs);
}
void KLGLUtil::drawAxis(double size)
{
//x red
glColor3f(1.0,0,0);
glBegin(GL_LINES);
glVertex3f(0,0,0);
glVertex3f(size,0,0);
glEnd();
glPushMatrix();
glTranslatef(size,0,0);
glRotatef(90,0,0,-1);
drawCylinder(size/15, size/128, size/5,16,2);
glPopMatrix();
//y green
glColor3f(0,1.0,0);
glBegin(GL_LINES);
glVertex3f(0,0,0);
glVertex3f(0,size,0);
glEnd();
glPushMatrix();
glTranslatef(0,size,0);
drawCylinder(size/15, size/128, size/5,16,2);
//glutSolidCone(size/12, size/4, 16,16);
glPopMatrix();
//z blue
glColor3f(0,0,1.0);
glBegin(GL_LINES);
glVertex3f(0,0,0);
glVertex3f(0,0,size);
glEnd();
glPushMatrix();
glTranslatef(0,0,size);
glRotatef(90, 1, 0, 0);
drawCylinder(size/15, size/128, size/5,16,2);
glPopMatrix();
//
glColor3f(1.0,1.0,1.0);
}
void KLGLUtil::drawCube(double size)
{
static const GLfloat vertex_list[][3] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
};
static const GLint index_list[][4] = {
0, 2, 3, 1,
0, 4, 6, 2,
0, 1, 5, 4,
4, 5, 7, 6,
1, 3, 7, 5,
2, 6, 7, 3,
};
glBegin(GL_QUADS);
for (int i=0; i<6; ++i)
for (int j=0; j<4; ++j){
glVertex3f(
vertex_list[index_list[i][j]][0] * size,
vertex_list[index_list[i][j]][1] * size,
vertex_list[index_list[i][j]][2] * size
);
}
glEnd();
}
void KLGLUtil::drawBox(double length, double width, double height)
{
glPushMatrix();
glScalef(length, height, width);
KLGLUtil::drawCube(1.0);
glPopMatrix();
}
void KLGLUtil::drawWiredPlane(double length, double width, int divideL, int divideW)
{
glBegin(GL_LINE_LOOP);
glVertex3f(-length / 2, 0, -width / 2);
glVertex3f(length / 2, 0, -width / 2);
glVertex3f(length / 2, 0, width / 2);
glVertex3f(-length / 2, 0, width / 2);
glEnd();
glBegin(GL_LINES);
for(int i = 1; i < divideL; i++){
glVertex3f(-length / 2 + i * length / divideL, 0, -width / 2);
glVertex3f(-length / 2 + i * length / divideL, 0, width / 2);
}
glEnd();
glBegin(GL_LINES);
for(int i = 1; i < divideW; i++){
glVertex3f(-length / 2, 0, -width / 2 + i * width / divideW);
glVertex3f(length / 2, 0, -width / 2 + i * width / divideW);
}
glEnd();
}
void KLGLUtil::drawCylinder(double baseRadius, double topRadius, double height, int divideR, int divideH)
{
GLUquadricObj *quadratic;
quadratic=gluNewQuadric();
gluQuadricNormals(quadratic, GLU_SMOOTH);
gluQuadricTexture(quadratic, GL_TRUE);
glPushMatrix();
glRotatef(-90, 1, 0, 0);
gluCylinder(quadratic,baseRadius,topRadius,height,divideR,divideH);
int STEP = divideR;
GLfloat rbase=baseRadius;
GLfloat rtop=topRadius;
glBegin(GL_POLYGON);
GLfloat n1[]={0.0,0.0,-1.0};
glNormal3fv(n1);
for(int i=0;i<=STEP+10;i++){
glVertex3f(sin(360/STEP*i*M_PI/180)*rbase,cos(360/STEP*i*M_PI/180)*rbase,0.0);
}
glEnd();
glBegin(GL_POLYGON);
GLfloat n2[]={0.0,0.0,1.0};
glNormal3fv(n2);
for(int i=0;i<=STEP+10;i++){
glVertex3f(sin(360/STEP*i*M_PI/180)*rtop,cos(360/STEP*i*M_PI/180)*rtop,height);
}
glEnd();
glPopMatrix();
}
//adopted here: http://www.cnblogs.com/xpvincent/archive/2013/02/16/2913537.html
void KLGLUtil::drawHeavySpring(QVector3D ptStart, QVector3D ptEnd, double Radius, double TubeRadius, double Coils, int Rings, int Sides)
{
double sideDelta = 2.0 * M_PI / Sides;
double ringDelta = 2.0 * M_PI / Rings;
double ringHeight;
double theta = 0;
double cosTheta = 1.0;
double sinTheta = 0.0;
double z;
double phi, sinPhi, cosPhi;
double dist;
double springLength;
QVector3D ptOldFrame, ptNewFrame;
QVector3D rotationAxis;
QMatrix4x4 rotationMatrix;
double rotationAngle;
ptOldFrame = QVector3D(0, 0, 1);
ptNewFrame = ptEnd - ptStart;
springLength = qSqrt(ptNewFrame.x() * ptNewFrame.x()
+ ptNewFrame.y() * ptNewFrame.y()
+ ptNewFrame.z() * ptNewFrame.z());
ringHeight = springLength / Coils / Rings;
QVector3D crossOldNew = QVector3D::crossProduct(ptOldFrame, ptNewFrame);
rotationAngle = qAsin((crossOldNew.length() / (ptOldFrame.length() * ptNewFrame.length())));
rotationAxis = crossOldNew;
rotationMatrix.rotate(rotationAngle * 180 / M_PI, rotationAxis);
glPushMatrix();
glTranslatef(ptStart.x(), ptStart.y(), ptStart.z());
float *temp = rotationMatrix.data();
GLdouble m[16];
for(int i = 0; i < 16; i++){
m[i] = temp[i];
}
glMultMatrixd(m);
z = 0;
for (int i = 0; i < Coils; i++)
{
for (int j = 0; j < Rings; j++)
{
double theta1 = theta + ringDelta;
double cosTheta1 = qCos(theta1);
double sinTheta1 = qSin(theta1);
glBegin(GL_QUAD_STRIP);
phi = 0;
for (int k = 0; k <= Sides; k++)
{
phi = phi + sideDelta;
cosPhi = qCos(phi);
sinPhi = qSin(phi);
dist = Radius + (TubeRadius * cosPhi);
GLfloat n1[]={cosTheta * (dist - Radius), sinTheta * (dist - Radius), TubeRadius * sinPhi};
glNormal3fv(n1);
glVertex3d(cosTheta * dist, sinTheta * dist, z + TubeRadius * sinPhi);
GLfloat n2[]={cosTheta1 * (dist - Radius), sinTheta1 * (dist - Radius), TubeRadius * sinPhi};
glNormal3fv(n2);
glVertex3d(cosTheta1 * dist, sinTheta1 * dist, z +ringHeight+ TubeRadius * sinPhi);
}
glEnd();
theta = theta1;
cosTheta = cosTheta1;
sinTheta = sinTheta1;
z += ringHeight;
}
}
glPopMatrix();
}
void KLGLUtil::drawCircle(double radius, int slices)
{
glBegin(GL_LINE_LOOP);
for(int i = 0; i < slices; i++){
glVertex3d(radius * qCos((360.0 / slices) * i / 180 * M_PI), radius * qSin((360.0 / slices) * i / 180 * M_PI), 0);
}
glEnd();
}
void KLGLUtil::drawParabola(double a, double b, double c, double startX, double endX, int slices)
{
glBegin(GL_LINE_STRIP);
for(int i = 0; i < slices; i++){
double x = startX + i * (endX - startX) / slices;
glVertex3d(x, a*x*x + b * x + c, 0);
}
glEnd();
}