HOG2
GLUtil.cpp
Go to the documentation of this file.
1 /*
2  * $Id: glUtil.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 6/8/05.
6  * Modified by Nathan Sturtevant on 02/29/20.
7  *
8  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9  *
10  */
11 
12 #include "GLUtil.h"
13 #include "FPUtil.h"
14 #include <math.h>
15 #include <assert.h>
16 #include <vector>
17 #include <cstring>
18 
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);
21 
22 bool operator==(const recVec &l1, const recVec &l2)
23 {
24  return (fequal(l1.x, l2.x) && fequal(l1.y, l2.y));
25 }
26 
27 std::ostream& operator <<(std::ostream &out, const recVec &loc)
28 {
29  out << "(" << loc.x << ", " << loc.y << ", " << loc.z <<")";
30  return out;
31 }
32 
33 
40 {
41  double length = this->length();
42 
43  if (length != 0)
44  {
45  x /= length;
46  y /= length;
47  z /= length;
48  }
49  else {
50  x = 0;
51  y = 0;
52  z = 0;
53  }
54 }
55 
56 double recVec::length() const
57 {
58  return sqrt(x * x + y * y + z * z);
59 }
60 
61 bool line2d::crosses(line2d which) const
62 {
63  if ((which.start == start) || (which.end == end) ||
64  (which.start == end) || (which.end == start))
65  return false;
66 
67  // //input x1,y1 input x2,y2
68  //input u1,v1 input u2,v2
69  line2d here(start, end);
70  double maxx1, maxx2, maxy1, maxy2;
71  double minx1, minx2, miny1, miny2;
72  if (here.start.x > here.end.x)
73  { maxx1 = here.start.x; minx1 = here.end.x; }
74  else
75  { maxx1 = here.end.x; minx1 = here.start.x; }
76 
77  if (here.start.y > here.end.y)
78  { maxy1 = here.start.y; miny1 = here.end.y; }
79  else
80  { maxy1 = here.end.y; miny1 = here.start.y; }
81 
82  if (which.start.x > which.end.x)
83  { maxx2 = which.start.x; minx2 = which.end.x; }
84  else
85  { maxx2 = which.end.x; minx2 = which.start.x; }
86 
87  if (which.start.y > which.end.y)
88  { maxy2 = which.start.y; miny2 = which.end.y; }
89  else
90  { maxy2 = which.end.y; miny2 = which.start.y; }
91 
92  if (fless(maxx1,minx2) || fless(maxx2, minx1) || fless(maxy1, miny2) || fless(maxy2, miny1))
93  return false;
94 
95  return fastCrossTest(start.x, start.y, end.x, end.y,
96  which.start.x, which.start.y, which.end.x, which.end.y,
97  0, 0);
98  //
99  if (fequal(maxx1, minx1)) // this is "here"
100  {
101  // already know that they share bounding boxes
102  // here, they must cross
103  if ((maxy2 < maxy1) && (miny2 > miny1))
104  return true;
105 
106  // y = mx + b
107  double m = (which.end.y-which.start.y)/(which.end.x-which.start.x);
108  double b = which.start.y - m*which.start.x;
109  double y = m*here.start.x+b;
110  if (fless(y, maxy1) && fgreater(y, miny1)) // on the line
111  return true;
112  return false;
113  }
114  if (fequal(maxx2, minx2)) // this is "which"
115  {
116  // already know that they share bounding boxes
117  // here, they must cross
118  if ((maxy1 < maxy2) && (miny1 > miny2))
119  return true;
120 
121  // y = mx + b
122  double m = (here.end.y-here.start.y)/(here.end.x-here.start.x);
123  double b = here.start.y - m*here.start.x;
124  double y = m*which.start.x+b;
125  if (fless(y, maxy2) && fgreater(y, miny2)) // on the line
126  return true;
127  return false;
128  }
129 
130  double b1 = (which.end.y-which.start.y)/(which.end.x-which.start.x);// (A)
131  double b2 = (here.end.y-here.start.y)/(here.end.x-here.start.x);// (B)
132 
133  double a1 = which.start.y-b1*which.start.x;
134  double a2 = here.start.y-b2*here.start.x;
135 
136  if (fequal(b1, b2))
137  return false;
138  double xi = - (a1-a2)/(b1-b2); //(C)
139  double yi = a1+b1*xi;
140  // these are actual >= but we exempt points
141  if ((!fless((which.start.x-xi)*(xi-which.end.x), 0)) &&
142  (!fless((here.start.x-xi)*(xi-here.end.x), 0)) &&
143  (!fless((which.start.y-yi)*(yi-which.end.y), 0)) &&
144  (!fless((here.start.y-yi)*(yi-here.end.y), 0)))
145  {
146  //printf("lines cross at (%f, %f)\n",xi,yi);
147  return true;
148  }
149  else {
150  return false;
151  //print "lines do not cross";
152  }
153  assert(false);
154 }
155 
156 // Returns 1 if the lines intersect, otherwise 0. In addition, if the lines
157 // intersect the intersection point may be stored in the floats i_x and i_y.
158 // taken from: http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect
159 //
160 bool fastCrossTest(float p0_x, float p0_y, float p1_x, float p1_y,
161  float p2_x, float p2_y, float p3_x, float p3_y, float *i_x, float *i_y)
162 {
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;
166 
167  float s, t;
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);
170 
171  if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
172  {
173  // Collision detected
174 // if (i_x != NULL)
175 // *i_x = p0_x + (t * s1_x);
176 // if (i_y != NULL)
177 // *i_y = p0_y + (t * s1_y);
178  return 1;
179  }
180 
181  return 0; // No collision
182 }
183 
184 
185 void DrawPyramid(GLfloat x, GLfloat y, GLfloat z, GLfloat height, GLfloat width)
186 {
187  glBegin(GL_TRIANGLES);
188  // glNormal3f(ROOT2D2, -ROOT2D2, 0);
189  glNormal3f(-ROOT2D2, ROOT2D2, 0);
190  glVertex3f(x, y, z-height);
191  glVertex3f(x-width, y-width, z);
192  glVertex3f(x-width, y+width, z);
193 
194  glNormal3f(0, ROOT2D2, ROOT2D2);
195  glVertex3f(x, y, z-height);
196  glVertex3f(x-width, y+width, z);
197  glVertex3f(x+width, y+width, z);
198 
199  glNormal3f(ROOT2D2, ROOT2D2, 0);
200  glVertex3f(x, y, z-height);
201  glVertex3f(x+width, y+width, z);
202  glVertex3f(x+width, y-width, z);
203 
204  glNormal3f(0, ROOT2D2, -ROOT2D2);
205  glVertex3f(x, y, z-height);
206  glVertex3f(x+width, y-width, z);
207  glVertex3f(x-width, y-width, z);
208  glEnd();
209 }
210 
211 // interleaved vertex array for glDrawElements() & glDrawRangeElements() ======
212 // All vertex attributes (position, normal, color) are packed together as a
213 // struct or set, for example, ((V,N,C), (V,N,C), (V,N,C),...).
214 // It is called an array of struct, and provides better memory locality.
215 GLfloat vertices3[] = { 1, 1, 1, 0, 0, 1, 1, 1, 1, // v0 (front)
216  -1, 1, 1, 0, 0, 1, 1, 1, 0, // v1
217  -1,-1, 1, 0, 0, 1, 1, 0, 0, // v2
218  1,-1, 1, 0, 0, 1, 1, 0, 1, // v3
219 
220  1, 1, 1, 1, 0, 0, 1, 1, 1, // v0 (right)
221  1,-1, 1, 1, 0, 0, 1, 0, 1, // v3
222  1,-1,-1, 1, 0, 0, 0, 0, 1, // v4
223  1, 1,-1, 1, 0, 0, 0, 1, 1, // v5
224 
225  1, 1, 1, 0, 1, 0, 1, 1, 1, // v0 (top)
226  1, 1,-1, 0, 1, 0, 0, 1, 1, // v5
227  -1, 1,-1, 0, 1, 0, 0, 1, 0, // v6
228  -1, 1, 1, 0, 1, 0, 1, 1, 0, // v1
229 
230  -1, 1, 1, -1, 0, 0, 1, 1, 0, // v1 (left)
231  -1, 1,-1, -1, 0, 0, 0, 1, 0, // v6
232  -1,-1,-1, -1, 0, 0, 0, 0, 0, // v7
233  -1,-1, 1, -1, 0, 0, 1, 0, 0, // v2
234 
235  -1,-1,-1, 0,-1, 0, 0, 0, 0, // v7 (bottom)
236  1,-1,-1, 0,-1, 0, 0, 0, 1, // v4
237  1,-1, 1, 0,-1, 0, 1, 0, 1, // v3
238  -1,-1, 1, 0,-1, 0, 1, 0, 0, // v2
239 
240  1,-1,-1, 0, 0,-1, 0, 0, 1, // v4 (back)
241  -1,-1,-1, 0, 0,-1, 0, 0, 0, // v7
242  -1, 1,-1, 0, 0,-1, 0, 1, 0, // v6
243  1, 1,-1, 0, 0,-1, 0, 1, 1 }; // v5
244 
245 GLubyte indices[] = { 0, 1, 2, 2, 3, 0, // front
246  4, 5, 6, 6, 7, 4, // right
247  8, 9,10, 10,11, 8, // top
248  12,13,14, 14,15,12, // left
249  16,17,18, 18,19,16, // bottom
250  20,21,22, 22,23,20 }; // back
251 
252 void DrawBoxFrame(GLfloat xx, GLfloat yy, GLfloat zz, GLfloat rad)
253 {
254  GLfloat vertices[] =
255  {-1, -1, -1, -1, -1, 1,
256  -1, -1, -1, -1, 1, -1,
257  -1, -1, -1, 1, -1, -1,
258  1, 1, 1, 1, 1, -1,
259  1, 1, 1, 1, -1, 1,
260  1, 1, 1, -1, 1, 1,
261  1, 1, -1, -1, 1, -1,
262  1, 1, -1, 1, -1, -1,
263  -1, 1, 1, -1, -1, 1,
264  -1, 1, 1, -1, 1, -1,
265  1, -1, -1, 1, -1, 1,
266  -1, -1, 1, 1, -1, 1
267  }; // 36 of vertex coords
268 
269  // activate and specify pointer to vertex array
270  glEnableClientState(GL_VERTEX_ARRAY);
271  glVertexPointer(3, GL_FLOAT, 0, vertices);
272 
273  glPushMatrix();
274  glTranslatef(xx, yy, zz); // move to bottom-left
275  glScalef(rad, rad, rad);
276  glDrawArrays(GL_LINES, 0, 24);
277 
278  glPopMatrix();
279 
280  // deactivate vertex arrays after drawing
281  glDisableClientState(GL_VERTEX_ARRAY);
282 
283 }
284 
285 void DrawBox(GLfloat xx, GLfloat yy, GLfloat zz, GLfloat rad)
286 {
287  glEnable(GL_NORMALIZE);
288  // enable and specify pointers to vertex arrays
289  glEnableClientState(GL_NORMAL_ARRAY);
290  //glEnableClientState(GL_COLOR_ARRAY);
291  glEnableClientState(GL_VERTEX_ARRAY);
292  glNormalPointer(GL_FLOAT, 9 * sizeof(GLfloat), vertices3 + 3);
293  //glColorPointer(3, GL_FLOAT, 9 * sizeof(GLfloat), vertices3 + 6);
294  glVertexPointer(3, GL_FLOAT, 9 * sizeof(GLfloat), vertices3);
295 
296  glPushMatrix();
297  glTranslatef(xx, yy, zz); // move to bottom-left
298  glScalef(rad, rad, rad);
299  glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
300 
301  glPopMatrix();
302 
303  glDisableClientState(GL_VERTEX_ARRAY); // disable vertex arrays
304  //glDisableClientState(GL_COLOR_ARRAY);
305  glDisableClientState(GL_NORMAL_ARRAY);
306  glDisable(GL_NORMALIZE);
307 }
308 
309 void DrawCylinder(GLfloat xx, GLfloat yy, GLfloat zz, GLfloat innerRad, GLfloat outerRad, GLfloat height)
310 {
311  const double resolution = 30;
312  const double step = TWOPI/resolution;
313  glBegin(GL_TRIANGLE_STRIP);
314  glNormal3f(0, 1, 0);
315  for (double t = 0; t < TWOPI; t += step)
316  {
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));
319  }
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));
322  glEnd();
323 
324  glBegin(GL_TRIANGLE_STRIP);
325  glNormal3f(0, -1, 0);
326  for (double t = 0; t < TWOPI; t += step)
327  {
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));
330  }
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));
333  glEnd();
334 
335  glBegin(GL_TRIANGLE_STRIP);
336  for (double t = 0; t < TWOPI; t += step)
337  {
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));
341  }
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));
344  glEnd();
345 
346 }
347 
348 //
349 //void DrawBox(GLfloat xx, GLfloat yy, GLfloat zz, GLfloat rad)
350 //{
351 // glBegin(GL_QUAD_STRIP);
352 // glVertex3f(xx-rad, yy-rad, zz-rad);
353 // glVertex3f(xx-rad, yy+rad, zz-rad);
354 //
355 // glVertex3f(xx+rad, yy-rad, zz-rad);
356 // glVertex3f(xx+rad, yy+rad, zz-rad);
357 //
358 // glVertex3f(xx+rad, yy-rad, zz+rad);
359 // glVertex3f(xx+rad, yy+rad, zz+rad);
360 //
361 // glVertex3f(xx-rad, yy-rad, zz+rad);
362 // glVertex3f(xx-rad, yy+rad, zz+rad);
363 //
364 // glVertex3f(xx-rad, yy-rad, zz-rad);
365 // glVertex3f(xx-rad, yy+rad, zz-rad);
366 //
367 // glEnd();
368 //
369 // glBegin(GL_QUADS);
370 // glVertex3f(xx-rad, yy+rad, zz-rad);
371 // glVertex3f(xx+rad, yy+rad, zz-rad);
372 // glVertex3f(xx+rad, yy+rad, zz+rad);
373 // glVertex3f(xx-rad, yy+rad, zz+rad);
374 // glEnd();
375 //
376 // glBegin(GL_QUADS);
377 // glVertex3f(xx-rad, yy-rad, zz-rad);
378 // glVertex3f(xx+rad, yy-rad, zz-rad);
379 // glVertex3f(xx+rad, yy-rad, zz+rad);
380 // glVertex3f(xx-rad, yy-rad, zz+rad);
381 // glEnd();
382 //}
383 
384 void OutlineRect(GLdouble left, GLdouble top, GLdouble right, GLdouble bottom, double zz)
385 {
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);
392  glEnd();
393 
394 }
395 
396 void DrawSquare(GLdouble xx, GLdouble yy, GLdouble zz, GLdouble rad)
397 {
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);
403  glEnd();
404 
405 }
406 
407 void FrameCircle(GLdouble _x, GLdouble _y, GLdouble tRadius, GLdouble lineWidth, int segments, float rotation)
408 {
409  double resolution = TWOPI/segments;
410  glBegin(GL_TRIANGLE_STRIP);
411  for (int x = 0; x <= segments; x++)
412  {
413  GLdouble s = sin(resolution*x+rotation*TWOPI/360.0);
414  GLdouble c = cos(resolution*x+rotation*TWOPI/360.0);
415  glVertex2f(_x+s*(tRadius-lineWidth/2), _y+c*(tRadius-lineWidth/2));
416  glVertex2f(_x+s*(tRadius+lineWidth/2), _y+c*(tRadius+lineWidth/2));
417  }
418  glEnd();
419 }
420 
421 void DrawCircle(GLdouble _x, GLdouble _y, GLdouble tRadius, int segments, float rotation)
422 {
423  double resolution = TWOPI/segments;
424  glBegin(GL_TRIANGLE_FAN);
425  glVertex2f(_x, _y);
426  for (int x = 0; x <= segments; x++)
427  {
428  glVertex2f(_x+sin(resolution*x+rotation*TWOPI/360.0)*tRadius, _y+cos(resolution*x+rotation*TWOPI/360.0)*tRadius);
429  }
430  glEnd();
431 }
432 
433 void DrawSphere(GLdouble _x, GLdouble _y, GLdouble _z, GLdouble tRadius)
434 {
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);
439 
440  glTranslatef(_x, _y, _z);
441 
442  int i,j;
443  int n = 16; // precision
444  double theta1,theta2,theta3;
445  point3d e,p;//,c(0, 0, 0);
446 
447  if (tRadius < 0) tRadius = -tRadius;
448  if (n < 0) n = -n;
449  if (n < 4 || tRadius <= 0)
450  {
451  glBegin(GL_POINTS);
452  glVertex3f(0, 0, 0);
453  glEnd();
454  }
455  else if (px_cache.size() == 0)
456  {
457  for (j=n/4;j<n/2;j++)
458  {
459  theta1 = j * TWOPI / n - PID2;
460  theta2 = (j + 1) * TWOPI / n - PID2;
461 
462  glBegin(GL_QUAD_STRIP);
463  //glBegin(GL_POINTS);
464  //glBegin(GL_TRIANGLE_STRIP);
465  //glBegin(GL_LINE_STRIP);
466  for (i=0;i<=n;i++)
467  {
468  theta3 = i * TWOPI / n;
469 
470  e.x = cos(theta2) * cos(theta3);
471  e.y = cos(theta2) * sin(theta3);
472  e.z = sin(theta2);
473 
474  px_cache.push_back(e.x);
475  py_cache.push_back(e.y);
476  pz_cache.push_back(e.z);
477 
478  p.x = tRadius * e.x;
479  p.y = tRadius * e.y;
480  p.z = - tRadius * e.z;
481 
482  glNormal3f(-e.x,-e.y,e.z);
483  //glTexCoord2f(i/(double)n,2*(j+1)/(double)n);
484  glVertex3f(p.x,p.y,p.z);
485 
486  e.x = cos(theta1) * cos(theta3);
487  e.y = cos(theta1) * sin(theta3);
488  e.z = sin(theta1);
489 
490  px_cache.push_back(e.x);
491  py_cache.push_back(e.y);
492  pz_cache.push_back(e.z);
493 
494  p.x = tRadius * e.x;
495  p.y = tRadius * e.y;
496  p.z = - tRadius * e.z;
497 
498  glNormal3f(-e.x,-e.y,e.z);
499  //glTexCoord2f(i/(double)n,2*j/(double)n);
500  glVertex3f(p.x,p.y,p.z);
501  }
502  glEnd();
503  }
504  }
505  else {
506  int which = 0;
507  for (j=n/4;j<n/2;j++)
508  {
509  glBegin(GL_QUAD_STRIP);
510  for (i=0;i<=n;i++)
511  {
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]);
514  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]);
517  which++;
518  }
519  glEnd();
520  }
521  }
522  glTranslatef(-_x, -_y, -_z);
523  glDisable(GL_LIGHTING);
524 }
525 
526 void DrawText(double x, double y, double z, double scale, const char *str)
527 {
528  glPushMatrix();
529  //glEnable(GL_LINE_SMOOTH);
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);
535  // for (size_t which = 0; which < strlen(str); which++)
536  // glutStrokeCharacter(GLUT_STROKE_ROMAN, str[which]);
537 // glEnable(GL_LIGHTING);
538  //glTranslatef(-x/width+0.5, -y/height+0.5, 0);
539  glPopMatrix();
540 }
541 
542 void DrawTextCentered(double x, double y, double z, double scale, const char *str)
543 {
544  glPushMatrix();
545 
546  int width = 0;
547  // for (size_t which = 0; which < strlen(str); which++)
548  // width += glutStrokeWidth(GLUT_STROKE_ROMAN, str[which]);
549 
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);
556 
557  // for (size_t which = 0; which < strlen(str); which++)
558  // glutStrokeCharacter(GLUT_STROKE_ROMAN, str[which]);
559 
560 // glEnable(GL_LIGHTING);
561  //glTranslatef(-x/width+0.5, -y/height+0.5, 0);
562  glPopMatrix();
563 }
564 
565 void SetLighting(GLfloat ambientf, GLfloat diffusef, GLfloat specularf)
566 {
567  // GLfloat mat_specular[] = {0.2, 0.2, 0.2, 1.0};
568  // GLfloat mat_shininess[] = {50.0};
569 
570  // GLfloat position[4] = {7.0,-7.0,12.0,0.0};
571  // GLfloat position[4] = {-1.0,-3.0,5.0,0.0};
572  // GLfloat position[4] = {-1.0,5.0,5.0,0.0};
573  // GLfloat position[4] = {-0.0,1.0,3.0,0.0};
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++)
579  {
580  ambient[x] *= ambientf;
581  diffuse[x] *= diffusef;
582  specular[x] *= specularf;
583  }
584 
585  // glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
586  // glMaterialfv (GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
587 
588  glEnable(GL_COLOR_MATERIAL);
589  glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
590 
591  // switch (mode) {
592  // case 0:
593  // break;
594  // case 1:
595  // glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_FALSE);
596  // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_FALSE);
597  // break;
598  // case 2:
599  // glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_FALSE);
600  // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE);
601  // break;
602  // case 3:
603  // glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
604  // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_FALSE);
605  // break;
606  // case 4:
607  // glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
608  // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE);
609  // break;
610  // }
611  glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
612  glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE);
613 
614 
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);
619  glEnable(GL_LIGHT0);
620 }
621 
loc::x
int x
Definition: MapGenerators.cpp:296
OutlineRect
void OutlineRect(GLdouble left, GLdouble top, GLdouble right, GLdouble bottom, double zz)
Definition: GLUtil.cpp:384
DrawSquare
void DrawSquare(GLdouble xx, GLdouble yy, GLdouble zz, GLdouble rad)
Definition: GLUtil.cpp:396
line2d::start
recVec start
Definition: GLUtil.h:160
DrawCircle
void DrawCircle(GLdouble _x, GLdouble _y, GLdouble tRadius, int segments, float rotation)
Definition: GLUtil.cpp:421
operator<<
std::ostream & operator<<(std::ostream &out, const recVec &loc)
Definition: GLUtil.cpp:27
recVec
A generic vector (essentially the same as a point, but offers normalization)
Definition: GLUtil.h:78
recVec::z
GLdouble z
Definition: GLUtil.h:98
loc::y
int y
Definition: MapGenerators.cpp:296
SetLighting
void SetLighting(GLfloat ambientf, GLfloat diffusef, GLfloat specularf)
Definition: GLUtil.cpp:565
DrawCylinder
void DrawCylinder(GLfloat xx, GLfloat yy, GLfloat zz, GLfloat innerRad, GLfloat outerRad, GLfloat height)
Definition: GLUtil.cpp:309
line2d::end
recVec end
Definition: GLUtil.h:161
FPUtil.h
DrawSphere
void DrawSphere(GLdouble _x, GLdouble _y, GLdouble _z, GLdouble tRadius)
Definition: GLUtil.cpp:433
width
int width
Definition: SFML_HOG.cpp:54
rotation
float rotation[3][3]
Definition: RC.cpp:21
vertices3
GLfloat vertices3[]
Definition: GLUtil.cpp:215
recVec::length
double length() const
Definition: GLUtil.cpp:56
DrawBoxFrame
void DrawBoxFrame(GLfloat xx, GLfloat yy, GLfloat zz, GLfloat rad)
Definition: GLUtil.cpp:252
loc
Definition: MapGenerators.cpp:296
DrawTextCentered
void DrawTextCentered(double x, double y, double z, double scale, const char *str)
Definition: GLUtil.cpp:542
point3d
#define point3d
Definition: GLUtil.h:123
fastCrossTest
bool fastCrossTest(float p0_x, float p0_y, float p1_x, float p1_y, float p2_x, float p2_y, float p3_x, float p3_y, float *i_x, float *i_y)
Definition: GLUtil.cpp:160
fless
bool fless(double a, double b)
Definition: FPUtil.h:28
line2d
‍**
Definition: GLUtil.h:155
indices
GLubyte indices[]
Definition: GLUtil.cpp:245
DrawText
void DrawText(double x, double y, double z, double scale, const char *str)
Definition: GLUtil.cpp:526
PID2
static const double PID2
Definition: GLUtil.h:68
a1
TemplateAStar< xyLoc, tDirection, MapEnvironment > a1
Definition: Sample.cpp:34
height
int height
Definition: SFML_HOG.cpp:54
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
line2d::crosses
bool crosses(line2d which) const
Definition: GLUtil.cpp:61
GLUtil.h
FrameCircle
void FrameCircle(GLdouble _x, GLdouble _y, GLdouble tRadius, GLdouble lineWidth, int segments, float rotation)
Definition: GLUtil.cpp:407
operator==
bool operator==(const recVec &l1, const recVec &l2)
Definition: GLUtil.cpp:22
DrawBox
void DrawBox(GLfloat xx, GLfloat yy, GLfloat zz, GLfloat rad)
Definition: GLUtil.cpp:285
TWOPI
static const double TWOPI
Definition: GLUtil.h:65
ROOT2D2
static const double ROOT2D2
Definition: GLUtil.h:73
a2
TemplateAStar< xyLoc, tDirection, MapEnvironment > a2
Definition: Sample.cpp:35
recVec::y
GLdouble y
Definition: GLUtil.h:98
fequal
bool fequal(double a, double b, double tolerance=TOLERANCE)
Definition: FPUtil.h:32
recVec::x
GLdouble x
Definition: GLUtil.h:98
recVec::normalise
void normalise()
Normalize a vector.
Definition: GLUtil.cpp:39
DrawPyramid
void DrawPyramid(GLfloat x, GLfloat y, GLfloat z, GLfloat height, GLfloat width)
Draw a pyramid with the tip at the given location, given height, and width from center to edge as wid...
Definition: GLUtil.cpp:185