19 bool fastCrossTest(
float p0_x,
float p0_y,
float p1_x,
float p1_y,
20 float p2_x,
float p2_y,
float p3_x,
float p3_y,
float *i_x,
float *i_y);
29 out <<
"(" <<
loc.
x <<
", " <<
loc.
y <<
", " <<
loc.z <<
")";
58 return sqrt(
x *
x +
y *
y +
z *
z);
70 double maxx1, maxx2, maxy1, maxy2;
71 double minx1, minx2, miny1, miny2;
83 { maxx2 = which.
start.
x; minx2 = which.
end.
x; }
85 { maxx2 = which.
end.
x; minx2 = which.
start.
x; }
88 { maxy2 = which.
start.
y; miny2 = which.
end.
y; }
90 { maxy2 = which.
end.
y; miny2 = which.
start.
y; }
103 if ((maxy2 < maxy1) && (miny2 > miny1))
109 double y = m*here.
start.
x+b;
118 if ((maxy1 < maxy2) && (miny1 > miny2))
124 double y = m*which.
start.
x+b;
138 double xi = - (
a1-
a2)/(b1-b2);
139 double yi =
a1+b1*xi;
161 float p2_x,
float p2_y,
float p3_x,
float p3_y,
float *i_x,
float *i_y)
163 float s1_x, s1_y, s2_x, s2_y;
164 s1_x = p1_x - p0_x; s1_y = p1_y - p0_y;
165 s2_x = p3_x - p2_x; s2_y = p3_y - p2_y;
168 s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
169 t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);
171 if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
187 glBegin(GL_TRIANGLES);
190 glVertex3f(x, y, z-
height);
195 glVertex3f(x, y, z-
height);
200 glVertex3f(x, y, z-
height);
205 glVertex3f(x, y, z-
height);
216 -1, 1, 1, 0, 0, 1, 1, 1, 0,
217 -1,-1, 1, 0, 0, 1, 1, 0, 0,
218 1,-1, 1, 0, 0, 1, 1, 0, 1,
220 1, 1, 1, 1, 0, 0, 1, 1, 1,
221 1,-1, 1, 1, 0, 0, 1, 0, 1,
222 1,-1,-1, 1, 0, 0, 0, 0, 1,
223 1, 1,-1, 1, 0, 0, 0, 1, 1,
225 1, 1, 1, 0, 1, 0, 1, 1, 1,
226 1, 1,-1, 0, 1, 0, 0, 1, 1,
227 -1, 1,-1, 0, 1, 0, 0, 1, 0,
228 -1, 1, 1, 0, 1, 0, 1, 1, 0,
230 -1, 1, 1, -1, 0, 0, 1, 1, 0,
231 -1, 1,-1, -1, 0, 0, 0, 1, 0,
232 -1,-1,-1, -1, 0, 0, 0, 0, 0,
233 -1,-1, 1, -1, 0, 0, 1, 0, 0,
235 -1,-1,-1, 0,-1, 0, 0, 0, 0,
236 1,-1,-1, 0,-1, 0, 0, 0, 1,
237 1,-1, 1, 0,-1, 0, 1, 0, 1,
238 -1,-1, 1, 0,-1, 0, 1, 0, 0,
240 1,-1,-1, 0, 0,-1, 0, 0, 1,
241 -1,-1,-1, 0, 0,-1, 0, 0, 0,
242 -1, 1,-1, 0, 0,-1, 0, 1, 0,
243 1, 1,-1, 0, 0,-1, 0, 1, 1 };
250 20,21,22, 22,23,20 };
255 {-1, -1, -1, -1, -1, 1,
256 -1, -1, -1, -1, 1, -1,
257 -1, -1, -1, 1, -1, -1,
270 glEnableClientState(GL_VERTEX_ARRAY);
271 glVertexPointer(3, GL_FLOAT, 0, vertices);
274 glTranslatef(xx, yy, zz);
275 glScalef(rad, rad, rad);
276 glDrawArrays(GL_LINES, 0, 24);
281 glDisableClientState(GL_VERTEX_ARRAY);
285 void DrawBox(GLfloat xx, GLfloat yy, GLfloat zz, GLfloat rad)
287 glEnable(GL_NORMALIZE);
289 glEnableClientState(GL_NORMAL_ARRAY);
291 glEnableClientState(GL_VERTEX_ARRAY);
292 glNormalPointer(GL_FLOAT, 9 *
sizeof(GLfloat),
vertices3 + 3);
294 glVertexPointer(3, GL_FLOAT, 9 *
sizeof(GLfloat),
vertices3);
297 glTranslatef(xx, yy, zz);
298 glScalef(rad, rad, rad);
299 glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE,
indices);
303 glDisableClientState(GL_VERTEX_ARRAY);
305 glDisableClientState(GL_NORMAL_ARRAY);
306 glDisable(GL_NORMALIZE);
309 void DrawCylinder(GLfloat xx, GLfloat yy, GLfloat zz, GLfloat innerRad, GLfloat outerRad, GLfloat
height)
311 const double resolution = 30;
312 const double step =
TWOPI/resolution;
313 glBegin(GL_TRIANGLE_STRIP);
315 for (
double t = 0; t <
TWOPI; t += step)
317 glVertex3f(xx+innerRad*cos(t), yy-
height/2, zz+innerRad*sin(t));
318 glVertex3f(xx+outerRad*cos(t), yy-
height/2, zz+outerRad*sin(t));
320 glVertex3f(xx+innerRad*cos(0), yy-
height/2, zz+innerRad*sin(0));
321 glVertex3f(xx+outerRad*cos(0), yy-
height/2, zz+outerRad*sin(0));
324 glBegin(GL_TRIANGLE_STRIP);
325 glNormal3f(0, -1, 0);
326 for (
double t = 0; t <
TWOPI; t += step)
328 glVertex3f(xx+innerRad*cos(t), yy+
height/2, zz+innerRad*sin(t));
329 glVertex3f(xx+outerRad*cos(t), yy+
height/2, zz+outerRad*sin(t));
331 glVertex3f(xx+innerRad*cos(0), yy+
height/2, zz+innerRad*sin(0));
332 glVertex3f(xx+outerRad*cos(0), yy+
height/2, zz+outerRad*sin(0));
335 glBegin(GL_TRIANGLE_STRIP);
336 for (
double t = 0; t <
TWOPI; t += step)
338 glNormal3f(-cos(t), 0, -sin(t));
339 glVertex3f(xx+outerRad*cos(t), yy+
height/2, zz+outerRad*sin(t));
340 glVertex3f(xx+outerRad*cos(t), yy-
height/2, zz+outerRad*sin(t));
342 glVertex3f(xx+outerRad*cos(0), yy+
height/2, zz+outerRad*sin(0));
343 glVertex3f(xx+outerRad*cos(0), yy-
height/2, zz+outerRad*sin(0));
384 void OutlineRect(GLdouble left, GLdouble top, GLdouble right, GLdouble bottom,
double zz)
386 glDisable(GL_LIGHTING);
387 glBegin(GL_LINE_LOOP);
388 glVertex3f(left, top, zz);
389 glVertex3f(right, top, zz);
390 glVertex3f(right, bottom, zz);
391 glVertex3f(left, bottom, zz);
396 void DrawSquare(GLdouble xx, GLdouble yy, GLdouble zz, GLdouble rad)
398 glBegin(GL_TRIANGLE_STRIP);
399 glVertex3f(xx-rad, yy-rad, zz);
400 glVertex3f(xx+rad, yy-rad, zz);
401 glVertex3f(xx-rad, yy+rad, zz);
402 glVertex3f(xx+rad, yy+rad, zz);
407 void FrameCircle(GLdouble _x, GLdouble _y, GLdouble tRadius, GLdouble lineWidth,
int segments,
float rotation)
409 double resolution =
TWOPI/segments;
410 glBegin(GL_TRIANGLE_STRIP);
411 for (
int x = 0; x <= segments; x++)
415 glVertex2f(_x+s*(tRadius-lineWidth/2), _y+c*(tRadius-lineWidth/2));
416 glVertex2f(_x+s*(tRadius+lineWidth/2), _y+c*(tRadius+lineWidth/2));
423 double resolution =
TWOPI/segments;
424 glBegin(GL_TRIANGLE_FAN);
426 for (
int x = 0; x <= segments; x++)
433 void DrawSphere(GLdouble _x, GLdouble _y, GLdouble _z, GLdouble tRadius)
435 static std::vector<double> px_cache;
436 static std::vector<double> py_cache;
437 static std::vector<double> pz_cache;
438 glEnable(GL_LIGHTING);
440 glTranslatef(_x, _y, _z);
444 double theta1,theta2,theta3;
447 if (tRadius < 0) tRadius = -tRadius;
449 if (n < 4 || tRadius <= 0)
455 else if (px_cache.size() == 0)
457 for (j=n/4;j<n/2;j++)
462 glBegin(GL_QUAD_STRIP);
468 theta3 = i *
TWOPI / n;
470 e.x = cos(theta2) * cos(theta3);
471 e.y = cos(theta2) * sin(theta3);
474 px_cache.push_back(e.x);
475 py_cache.push_back(e.y);
476 pz_cache.push_back(e.z);
480 p.z = - tRadius * e.z;
482 glNormal3f(-e.x,-e.y,e.z);
484 glVertex3f(p.x,p.y,p.z);
486 e.x = cos(theta1) * cos(theta3);
487 e.y = cos(theta1) * sin(theta3);
490 px_cache.push_back(e.x);
491 py_cache.push_back(e.y);
492 pz_cache.push_back(e.z);
496 p.z = - tRadius * e.z;
498 glNormal3f(-e.x,-e.y,e.z);
500 glVertex3f(p.x,p.y,p.z);
507 for (j=n/4;j<n/2;j++)
509 glBegin(GL_QUAD_STRIP);
512 glNormal3d(-px_cache[which], -py_cache[which], pz_cache[which]);
513 glVertex3d(tRadius*px_cache[which], tRadius*py_cache[which], -tRadius*pz_cache[which]);
515 glNormal3d(-px_cache[which], -py_cache[which], pz_cache[which]);
516 glVertex3d(tRadius*px_cache[which], tRadius*py_cache[which], -tRadius*pz_cache[which]);
522 glTranslatef(-_x, -_y, -_z);
523 glDisable(GL_LIGHTING);
526 void DrawText(
double x,
double y,
double z,
double scale,
const char *str)
530 glTranslatef(x, y, z);
531 glScalef(scale/300, scale/300.0, 1);
532 glRotatef(180, 0.0, 0.0, 1.0);
533 glRotatef(180, 0.0, 1.0, 0.0);
534 glDisable(GL_LIGHTING);
550 glTranslatef(x, y, z);
551 glScalef(scale/300, scale/300.0, 1);
552 glRotatef(180, 0.0, 0.0, 1.0);
553 glRotatef(180, 0.0, 1.0, 0.0);
554 glDisable(GL_LIGHTING);
555 glTranslatef(-
width/2, -4.0/scale, 0);
565 void SetLighting(GLfloat ambientf, GLfloat diffusef, GLfloat specularf)
574 GLfloat position[4] = {-5.0,5.0,30.0,0.0};
575 GLfloat ambient[4] = {1.0, 1.0, 1.0, 1.0};
576 GLfloat diffuse[4] = {1.0, 1.0, 1.0, 1.0};
577 GLfloat specular[4] = {1.0, 1.0, 1.0, 1.0};
578 for (
int x = 0; x < 3; x++)
580 ambient[x] *= ambientf;
581 diffuse[x] *= diffusef;
582 specular[x] *= specularf;
588 glEnable(GL_COLOR_MATERIAL);
589 glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
611 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
612 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE);
615 glLightfv(GL_LIGHT0,GL_POSITION,position);
616 glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);
617 glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse);
618 glLightfv(GL_LIGHT0,GL_SPECULAR,specular);