libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moP5.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moP5.cpp
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright(C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28  Andrés Colubri
29 
30 *******************************************************************************/
31 
32 #include "moP5.h"
33 #include "moMath.h"
34 
36 {
38  float defCoeff = 1 / 255.0;
39  for (int i = 0; i < 4; i++)
40  {
41  colorRGBCoeff[i] = defCoeff;
42  colorHSBCoeff[i] = defCoeff;
43  }
44 
45  strokeColor[0] = 0.7;
46  strokeColor[1] = 0.7;
47  strokeColor[2] = 0.7;
48  strokeColor[3] = 1.0;
49 
50  fillColor[0] = 0.7;
51  fillColor[1] = 0.7;
52  fillColor[2] = 0.7;
53  fillColor[3] = 1.0;
54 }
55 
56 void moP5::triangle(float x1, float y1, float x2, float y2, float x3, float y3)
57 {
58  // Propper handling of stroke, fill color and line weight
59 #ifndef OPENGLESV2
60  glBegin(GL_TRIANGLES);
61  glVertex2f(x1, y1);
62  glVertex2f(x2, y2);
63  glVertex2f(x3, y3);
64  glEnd();
65 #else
66 
67 #endif
68 }
69 
70 void moP5::line(float x1, float y1, float x2, float y2)
71 {
72  glEnable( GL_TEXTURE_2D );
73  glBindTexture(GL_TEXTURE_2D,0);
74  glColor4f(strokeColor[0], strokeColor[1], strokeColor[2], strokeColor[3]);
75 #ifndef OPENGLESV2
76  glBegin(GL_LINES);
77  glVertex2f(x1, y1);
78  glVertex2f(x2, y2);
79  glEnd();
80 #else
81 
82 #endif
83 }
84 
85 void moP5::line(float x1, float y1, float z1, float x2, float y2, float z2)
86 {
87  glEnable( GL_TEXTURE_2D );
88  glBindTexture(GL_TEXTURE_2D,0);
89  glColor4f(strokeColor[0], strokeColor[1], strokeColor[2], strokeColor[3]);
90 #ifndef OPENGLESV2
91  glBegin(GL_LINES);
92  glVertex3f(x1, y1, z1);
93  glVertex3f(x2, y2, z2);
94  glEnd();
95 #else
96 
97 #endif
98 }
99 
100 void moP5::arc(float x, float y, float width, float height, float start, float stop, int slices, float band )
101 {
102  // Implement display list with precomputed circle coordinates...
103  glEnable( GL_TEXTURE_2D );
104  glBindTexture(GL_TEXTURE_2D,0);
105 
106  float N;
107  float Nstep;
108 
109  N = (float)slices;
110  Nstep = MO_P5_TWO_PI / N;
111 
112  switch(fillMode) {
113 
114  case MO_P5_NOFILL:
115 #ifndef OPENGLESV2
116  glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
117  glColor4f(strokeColor[0], strokeColor[1], strokeColor[2], strokeColor[3]);
118  glBegin(GL_LINE_LOOP);
119 #else
120 
121 #endif
122  break;
123 
124  case MO_P5_FILL:
125 #ifndef OPENGLESV2
126  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
127  glColor4f(fillColor[0], fillColor[1], fillColor[2], fillColor[3]);
128 
129  if ( band>0.0 ) {
130  glBegin(GL_TRIANGLE_STRIP);
131  } else glBegin(GL_TRIANGLE_FAN);
132 #else
133 
134 #endif
135  break;
136 
137  default:
138  return;
139  break;
140 
141  }
142 
143 #ifndef OPENGLESV2
144  if ( band==0.0 ) {
146  glVertex2f( (float)x, (float)y);
147  }
148 
149  for(float t = start; t <= stop; t += Nstep ) {
150  if ( band>0.0 ) {
152  glVertex2f( (width-band)* cos(t) + (float)x, (height-band) * sin(t) + (float)y);
153  }
154 
155  glVertex2f(width* cos(t) + (float)x, height * sin(t) + (float)y);
156 
157  }
158  glEnd();
159 #else
160 
161 #endif
162 
163 }
164 
165 void moP5::point(float x, float y)
166 {
167 #ifndef OPENGLESV2
168  glBegin(GL_POINTS);
169  glVertex2f(x, y);
170  glEnd();
171 #else
172 
173 #endif
174 }
175 
176 
177 void moP5::point(float x, float y, float z)
178 {
179 #ifndef OPENGLESV2
180  glBegin(GL_POINTS);
181  glVertex3f(x, y, z);
182  glEnd();
183 #else
184 
185 #endif
186 }
187 
188 void moP5::quad(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
189 {
190 #ifndef OPENGLESV2
191  glBegin(GL_QUADS);
192  glVertex2f(x1, y1);
193  glVertex2f(x2, y2);
194  glVertex2f(x3, y3);
195  glVertex2f(x4, y4);
196  glEnd();
197 #else
198 
199 #endif
200 }
201 
202 void moP5::ellipse(float x, float y, float width, float height, int slices )
203 {
204  // Implement display list with precomputed circle coordinates...
205  // and scale it to the right size.
206  glEnable( GL_TEXTURE_2D );
207  glBindTexture(GL_TEXTURE_2D,0);
208 #ifndef OPENGLESV2
209  float N;
210 
211  N = (float)slices;
212 
213  switch(fillMode) {
214 
215  case MO_P5_NOFILL:
216  glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
217  glColor4f(strokeColor[0], strokeColor[1], strokeColor[2], strokeColor[3]);
218  glBegin(GL_LINE_LOOP);
219  for(float t = 0; t <= MO_P5_TWO_PI; t += MO_P5_TWO_PI/N)
220  glVertex2f(width* cos(t) + (float)x, height * sin(t) + (float)y);
221  glEnd();
222  break;
223 
224  case MO_P5_FILL:
225  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
226  glColor4f(fillColor[0], fillColor[1], fillColor[2], fillColor[3]);
227  glBegin(GL_POLYGON);
228  for(float t = 0; t <= MO_P5_TWO_PI; t += MO_P5_TWO_PI/N)
229  glVertex2f(width * cos(t) + (float)x, height * sin(t) + (float)y);
230  glEnd();
231  break;
232  }
233 #else
234 
235 #endif
236 }
237 
238 void moP5::rect(float x, float y, float width, float height)
239 {
240 
241  int N;
242 
243  N = 12;
244 #ifndef OPENGLESV2
245  switch(fillMode) {
246 
247  case MO_P5_NOFILL:
248  glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
249  glBindTexture(GL_TEXTURE_2D,0);
250  glColor4f(strokeColor[0], strokeColor[1], strokeColor[2], strokeColor[3]);
251  break;
252 
253  case MO_P5_FILL:
254  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
255  glBindTexture(GL_TEXTURE_2D,0);
256  glColor4f(fillColor[0], fillColor[1], fillColor[2], fillColor[3]);
257  break;
258  }
259 
260  glRectf(x, y, x + width, y + height);
261 #else
262 
263 #endif
264 }
265 
266 void moP5::strokeWeight(float width)
267 {
268  glLineWidth(width);
269 }
270 
271 /*
272 void moP5::smooth()
273 {
274 }
275 
276 void moP5::strokeJoin(moP5StrokeJoinMode MODE)
277 {
278 }
279 
280 void moP5::noSmooth()
281 {
282 }
283 
284 void moP5::ellipseMode(moP5ShapeMode MODE)
285 {
286 }
287 
288 void moP5::rectMode(moP5ShapeMode MODE)
289 {
290 }
291 
292 void moP5::strokeCap(moP5StrokeCapMode MODE)
293 {
294 }
295 */
296 
297 void moP5::background(float gray)
298 {
299  generateTmpColor(gray, gray, gray);
300  glClearColor(tmpColor[0], tmpColor[1], tmpColor[2], 1.0);
301  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
302 }
303 
304 void moP5::background(float gray, float alpha)
305 {
306  generateTmpColor(gray, gray, gray, alpha);
307  glClearColor(tmpColor[0], tmpColor[1], tmpColor[2], tmpColor[3]);
308  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
309 }
310 
311 void moP5::background(float value1, float value2, float value3)
312 {
313  generateTmpColor(value1, value2, value3);
314  glClearColor(tmpColor[0], tmpColor[1], tmpColor[2], 1.0);
315  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
316 }
317 
318 void moP5::background(float value1, float value2, float value3, float alpha)
319 {
320  generateTmpColor(value1, value2, value3, alpha);
321  glClearColor(tmpColor[0], tmpColor[1], tmpColor[2], tmpColor[3]);
322  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
323 }
324 
325 void moP5::colorMode(int mode)
326 {
327  currColorMode = mode;
328 }
329 
330 void moP5::colorMode(int mode, float range)
331 {
332  currColorMode = mode;
333 
334  float coeff = 1 / 255.0;
335  if ((0 < range) && (range <= 255.0)) coeff = 1 / range;
336 
337  if (currColorMode == MO_P5_RGB)
338  {
339  for (int i = 0; i < 4; i++) colorRGBCoeff[i] = coeff;
340  }
341  else
342  {
343  for (int i = 0; i < 4; i++) colorHSBCoeff[i] = coeff;
344  }
345 }
346 
347 void moP5::colorMode(int mode, float range1, float range2, float range3)
348 {
349  currColorMode = mode;
350 
351  float coeff1 = 1 / 255.0;
352  float coeff2 = 1 / 255.0;
353  float coeff3 = 1 / 255.0;
354  if ((0 < range1) && (range1 <= 255.0)) coeff1 = 1 / range1;
355  if ((0 < range2) && (range2 <= 255.0)) coeff2 = 1 / range2;
356  if ((0 < range3) && (range3 <= 255.0)) coeff3 = 1 / range3;
357 
358  if (currColorMode == MO_P5_RGB)
359  {
360  colorRGBCoeff[0] = coeff1;
361  colorRGBCoeff[1] = coeff2;
362  colorRGBCoeff[2] = coeff3;
363  }
364  else
365  {
366  colorHSBCoeff[0] = coeff1;
367  colorHSBCoeff[1] = coeff2;
368  colorHSBCoeff[2] = coeff3;
369  }
370 }
371 
372 void moP5::colorMode(int mode, float range1, float range2, float range3, float range4)
373 {
374  currColorMode = mode;
375 
376  float coeff1 = 1 / 255.0;
377  float coeff2 = 1 / 255.0;
378  float coeff3 = 1 / 255.0;
379  float coeff4 = 1 / 255.0;
380  if ((0 < range1) && (range1 <= 255.0)) coeff1 = 1 / range1;
381  if ((0 < range2) && (range2 <= 255.0)) coeff2 = 1 / range2;
382  if ((0 < range3) && (range3 <= 255.0)) coeff3 = 1 / range3;
383  if ((0 < range4) && (range4 <= 255.0)) coeff4 = 1 / range4;
384 
385  if (currColorMode == MO_P5_RGB)
386  {
387  colorRGBCoeff[0] = coeff1;
388  colorRGBCoeff[1] = coeff2;
389  colorRGBCoeff[2] = coeff3;
390  colorRGBCoeff[3] = coeff4;
391  }
392  else
393  {
394  colorHSBCoeff[0] = coeff1;
395  colorHSBCoeff[1] = coeff2;
396  colorHSBCoeff[2] = coeff3;
397  colorHSBCoeff[3] = coeff4;
398  }
399 }
400 
401 void moP5::stroke(float gray)
402 {
403  strokeColor[0] = gray;
404  strokeColor[1] = gray;
405  strokeColor[2] = gray;
406  strokeColor[3] = 1.0;
407 }
408 
409 void moP5::stroke(float gray, float alpha)
410 {
411  stroke(gray);
412  strokeColor[3] = alpha;
413 }
414 
415 void moP5::stroke(float value1, float value2, float value3)
416 {
417  strokeColor[0] = value1;
418  strokeColor[1] = value2;
419  strokeColor[2] = value3;
420  strokeColor[3] = 1.0;
421 }
422 
423 void moP5::stroke(float value1, float value2, float value3, float alpha)
424 {
425  strokeColor[0] = value1;
426  strokeColor[1] = value2;
427  strokeColor[2] = value3;
428  strokeColor[3] = alpha;
429 }
430 
432 {
434 #ifndef OPENGLESV2
435  glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
436 #else
437 
438 #endif
439 }
440 
442 {
443  strokeColor[0] = 0.0;
444  strokeColor[1] = 0.0;
445  strokeColor[2] = 0.0;
446  strokeColor[3] = 0.0;
447 }
448 
449 void moP5::fill(float gray)
450 {
451  fill( gray, gray, gray , 1.0 );
452 }
453 
454 void moP5::fill(float gray, float alpha)
455 {
456  fill( gray, gray, gray , alpha );
457 }
458 
459 void moP5::fill(float value1, float value2, float value3)
460 {
461  fill( value1, value2, value3 , 1.0 );
462 }
463 
464 void moP5::fill(float value1, float value2, float value3, float alpha)
465 {
467 #ifndef OPENGLESV2
468  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
469 #else
470 
471 #endif
472  fillColor[0] = value1;
473  fillColor[1] = value2;
474  fillColor[2] = value3;
475  fillColor[3] = alpha;
476 }
477 
479 {
480 #ifndef OPENGLESV2
481  glMatrixMode(GL_MODELVIEW);
482  glPushMatrix();
483 #else
484 
485 #endif
486 }
487 
489 {
490 #ifndef OPENGLESV2
491  glMatrixMode(GL_MODELVIEW);
492  glPopMatrix();
493 #else
494 
495 #endif
496 }
497 
499 {
500 #ifndef OPENGLESV2
501  glMatrixMode(GL_MODELVIEW);
502  glLoadIdentity();
503 #else
504 
505 #endif
506 }
507 
508 void moP5::scale(float size)
509 {
510 #ifndef OPENGLESV2
511  glScalef(size, size, size);
512 #else
513 
514 #endif
515 }
516 
517 void moP5::scale(float x, float y, float z)
518 {
519 #ifndef OPENGLESV2
520  glScalef(x, y, z);
521 #else
522 
523 #endif
524 }
525 
526 void moP5::translate(float x, float y, float z)
527 {
528 #ifndef OPENGLESV2
529  glTranslatef(x, y, z);
530 #else
531 
532 #endif
533 }
534 
535 void moP5::rotate(float angle, float x, float y, float z)
536 {
537 #ifndef OPENGLESV2
538  glRotatef(angle * moMathf::RAD_TO_DEG, x, y, z);
539 #else
540 
541 #endif
542 }
543 
544 void moP5::generateTmpColor(float comp1, float comp2, float comp3)
545 {
546  if (currColorMode == MO_P5_RGB) generateTmpColor(comp1, comp2, comp3, 1 / colorRGBCoeff[3]);
547  else generateTmpColor(comp1, comp2, comp3, 1 / colorHSBCoeff[3]);
548 }
549 
550 void moP5::generateTmpColor(float comp1, float comp2, float comp3, float comp4)
551 {
552  if (currColorMode == MO_P5_RGB)
553  {
554  // Direct translation to RGBA.
555  tmpColor[0] = comp1 * colorRGBCoeff[0];
556  tmpColor[1] = comp2 * colorRGBCoeff[1];
557  tmpColor[2] = comp3 * colorRGBCoeff[2];
558  tmpColor[3] = comp4 * colorRGBCoeff[3];
559  }
560  else
561  {
562  // Converting HSB to RGB.
563 
564  // Normalizing HSB components first.
565  float h = comp1 * colorHSBCoeff[0];
566  float s = comp2 * colorHSBCoeff[1];
567  float l = comp3 * colorHSBCoeff[2];
568  float r, g, b;
569 
570  convertHSLtoRGB(h, s, l, r, g, b);
571 
572  // convertRGBtoHSB returns normalized RGB components.
573  tmpColor[0] = r;
574  tmpColor[1] = g;
575  tmpColor[2] = b;
576  tmpColor[3] = comp4 * colorHSBCoeff[3];
577  }
578 }
579 
580 void moP5::convertHSLtoRGB(float h, float s, float l, float& r, float& g, float& b)
581 {
582  float m1, m2;
583 
584  if (s == 0.0)
585  {
586  r = l;
587  g = l;
588  b = l;
589  }
590  else
591  {
592  if (l <= 0.5) m2 = l * (1.0 + s);
593  else m2 = l + s - (l * s);
594  m1 = 2.0 * l - m2;
595  r = HuetoRGB(m1, m2, h + 1.0 / 3.0);
596  g = HuetoRGB(m1, m2, h);
597  b = HuetoRGB(m1, m2, h - 1.0 / 3.0);
598  }
599 }
600 
601 float moP5::HuetoRGB(float m1, float m2, float h)
602 {
603  if (h < 0) h = h + 1.0;
604  if (h > 1) h = h - 1.0;
605  if (6.0 * h < 1) return (m1 + (m2 - m1) * h * 6.0);
606  else
607  {
608  if (2.0 * h < 1) return m2;
609  else
610  if (3.0 * h < 2.0) return (m1 + (m2 - m1) * ((2.0 / 3.0) - h) * 6.0);
611  else return m1;
612  }
613 }
614 
615 const float moP5::MO_P5_HALF_PI = 1.57079632679489661923;
616 const float moP5::MO_P5_TWO_PI = 6.28318530717958647693;
617 const float moP5::MO_P5_PI = 3.14159265358979323846;
618 
619 
620 /*
621 Code to create display list to show ellipses:
622 (display list tutorial here:
623 http://www.lighthouse3d.com/opengl/displaylists/)
624 Thread about VBO vs DL:
625 http://www.gamedev.net/community/forums/topic.asp?topic_id=378417
626 
627 1) Construct Lookup table (LUT) (it will be used also for arcs.
628 
629 float sinLUT[];
630 float cosLUT[];
631 float SINCOS_PRECISION = 0.1f;
632 int SINCOS_LENGTH = (int) (360f / SINCOS_PRECISION);
633 
634  // Initializing sine and cosine lookup tables.
635  sinLUT = new float[SINCOS_LENGTH];
636  cosLUT = new float[SINCOS_LENGTH];
637  for (int i = 0; i < SINCOS_LENGTH; i++)
638  {
639  sinLUT[i] = (float) Math.sin(i * DEG_TO_RAD * SINCOS_PRECISION);
640  cosLUT[i] = (float) Math.cos(i * DEG_TO_RAD * SINCOS_PRECISION);
641  }
642 
643 2) Create Display lis
644 
645 GLuint unityEllipse;
646 unityEllipse = glGenLists(1);
647 
648 glNewList(unityEllipse, GL_COMPILE);
649  drawUnityEllipse();
650 glEndList();
651 
652 glScalef(x, y, z);
653 glTranslate(x, y, z);
654 glCallList(unityEllipse);
655 
656 3) drawUnityEllipse() implementation:
657 
658  int increment = 1;
659  for (int i = startLUT; i < stopLUT; i += increment)
660  {
661  int ii = i % SINCOS_LENGTH;
662  vertex(cosLUT[ii] * circleRadius0,
663  sinLUT[ii] * circleRadius0);
664  vertex(cosLUT[ii] * circleRadius1,
665  sinLUT[ii] * circleRadius1);
666  }
667 
668  */
669 
670 
static const float MO_P5_PI
Definition: moP5.h:356
void fill(float gray)
Definition: moP5.cpp:449
void scale(float size)
Definition: moP5.cpp:508
void arc(float x, float y, float width, float height, float start, float stop, int slices=24, float band=0.0)
Definition: moP5.cpp:100
float colorHSBCoeff[4]
Definition: moP5.h:363
void translate(float x, float y, float z=0.0f)
Definition: moP5.cpp:526
const MOlong RAD_TO_DEG
Definition: moMath.cpp:50
void triangle(float x1, float y1, float x2, float y2, float x3, float y3)
Definition: moP5.cpp:56
static const float MO_P5_HALF_PI
Definition: moP5.h:354
void popMatrix()
Definition: moP5.cpp:488
static const float MO_P5_TWO_PI
Definition: moP5.h:355
void quad(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
Definition: moP5.cpp:188
void generateTmpColor(float comp1, float comp2, float comp3)
Definition: moP5.cpp:544
float fillColor[4]
Definition: moP5.h:368
moP5()
Definition: moP5.cpp:35
float colorRGBCoeff[4]
Definition: moP5.h:362
void background(float gray)
Definition: moP5.cpp:297
void ellipse(float x, float y, float width, float height, int slices=24)
Definition: moP5.cpp:202
void rotate(float angle, float x=0.0f, float y=0.0f, float z=1.0f)
Definition: moP5.cpp:535
void stroke(float gray)
Definition: moP5.cpp:401
float strokeColor[4]
Definition: moP5.h:367
void noStroke()
Definition: moP5.cpp:441
void strokeWeight(float width)
Definition: moP5.cpp:266
void noFill()
Definition: moP5.cpp:431
void colorMode(int mode)
Definition: moP5.cpp:325
void rect(float x, float y, float width, float height)
Definition: moP5.cpp:238
moP5FillMode fillMode
Definition: moP5.h:365
void point(float x, float y)
Definition: moP5.cpp:165
float HuetoRGB(float m1, float m2, float h)
Definition: moP5.cpp:601
void pushMatrix()
Definition: moP5.cpp:478
void resetMatrix()
Definition: moP5.cpp:498
void line(float x1, float y1, float x2, float y2)
Definition: moP5.cpp:70
float tmpColor[4]
Definition: moP5.h:366
void convertHSLtoRGB(float h, float s, float l, float &r, float &g, float &b)
Definition: moP5.cpp:580
int currColorMode
Definition: moP5.h:361