You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.3KB

  1. //
  2. // "$Id: CubeView.cxx 7903 2010-11-28 21:06:39Z matt $"
  3. //
  4. // CubeView class implementation for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2010 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. #include "CubeView.h"
  28. #include <math.h>
  29. #if HAVE_GL
  30. CubeView::CubeView(int x,int y,int w,int h,const char *l)
  31. : Fl_Gl_Window(x,y,w,h,l)
  32. #else
  33. CubeView::CubeView(int x,int y,int w,int h,const char *l)
  34. : Fl_Box(x,y,w,h,l)
  35. #endif /* HAVE_GL */
  36. {
  37. vAng = 0.0;
  38. hAng=0.0;
  39. size=10.0;
  40. xshift=0.0;
  41. yshift=0.0;
  42. /* The cube definition. These are the vertices of a unit cube
  43. * centered on the origin.*/
  44. boxv0[0] = -0.5; boxv0[1] = -0.5; boxv0[2] = -0.5;
  45. boxv1[0] = 0.5; boxv1[1] = -0.5; boxv1[2] = -0.5;
  46. boxv2[0] = 0.5; boxv2[1] = 0.5; boxv2[2] = -0.5;
  47. boxv3[0] = -0.5; boxv3[1] = 0.5; boxv3[2] = -0.5;
  48. boxv4[0] = -0.5; boxv4[1] = -0.5; boxv4[2] = 0.5;
  49. boxv5[0] = 0.5; boxv5[1] = -0.5; boxv5[2] = 0.5;
  50. boxv6[0] = 0.5; boxv6[1] = 0.5; boxv6[2] = 0.5;
  51. boxv7[0] = -0.5; boxv7[1] = 0.5; boxv7[2] = 0.5;
  52. #if !HAVE_GL
  53. label("OpenGL is required for this demo to operate.");
  54. align(FL_ALIGN_WRAP | FL_ALIGN_INSIDE);
  55. #endif /* !HAVE_GL */
  56. }
  57. #if HAVE_GL
  58. void CubeView::drawCube() {
  59. /* Draw a colored cube */
  60. #define ALPHA 0.5
  61. glShadeModel(GL_FLAT);
  62. glBegin(GL_QUADS);
  63. glColor4f(0.0, 0.0, 1.0, ALPHA);
  64. glVertex3fv(boxv0);
  65. glVertex3fv(boxv1);
  66. glVertex3fv(boxv2);
  67. glVertex3fv(boxv3);
  68. glColor4f(1.0, 1.0, 0.0, ALPHA);
  69. glVertex3fv(boxv0);
  70. glVertex3fv(boxv4);
  71. glVertex3fv(boxv5);
  72. glVertex3fv(boxv1);
  73. glColor4f(0.0, 1.0, 1.0, ALPHA);
  74. glVertex3fv(boxv2);
  75. glVertex3fv(boxv6);
  76. glVertex3fv(boxv7);
  77. glVertex3fv(boxv3);
  78. glColor4f(1.0, 0.0, 0.0, ALPHA);
  79. glVertex3fv(boxv4);
  80. glVertex3fv(boxv5);
  81. glVertex3fv(boxv6);
  82. glVertex3fv(boxv7);
  83. glColor4f(1.0, 0.0, 1.0, ALPHA);
  84. glVertex3fv(boxv0);
  85. glVertex3fv(boxv3);
  86. glVertex3fv(boxv7);
  87. glVertex3fv(boxv4);
  88. glColor4f(0.0, 1.0, 0.0, ALPHA);
  89. glVertex3fv(boxv1);
  90. glVertex3fv(boxv5);
  91. glVertex3fv(boxv6);
  92. glVertex3fv(boxv2);
  93. glEnd();
  94. glColor3f(1.0, 1.0, 1.0);
  95. glBegin(GL_LINES);
  96. glVertex3fv(boxv0);
  97. glVertex3fv(boxv1);
  98. glVertex3fv(boxv1);
  99. glVertex3fv(boxv2);
  100. glVertex3fv(boxv2);
  101. glVertex3fv(boxv3);
  102. glVertex3fv(boxv3);
  103. glVertex3fv(boxv0);
  104. glVertex3fv(boxv4);
  105. glVertex3fv(boxv5);
  106. glVertex3fv(boxv5);
  107. glVertex3fv(boxv6);
  108. glVertex3fv(boxv6);
  109. glVertex3fv(boxv7);
  110. glVertex3fv(boxv7);
  111. glVertex3fv(boxv4);
  112. glVertex3fv(boxv0);
  113. glVertex3fv(boxv4);
  114. glVertex3fv(boxv1);
  115. glVertex3fv(boxv5);
  116. glVertex3fv(boxv2);
  117. glVertex3fv(boxv6);
  118. glVertex3fv(boxv3);
  119. glVertex3fv(boxv7);
  120. glEnd();
  121. }//drawCube
  122. void CubeView::draw() {
  123. if (!valid()) {
  124. glLoadIdentity();
  125. glViewport(0,0,w(),h());
  126. glOrtho(-10,10,-10,10,-20050,10000);
  127. glEnable(GL_BLEND);
  128. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  129. }
  130. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  131. glPushMatrix();
  132. glTranslatef(xshift, yshift, 0);
  133. glRotatef(hAng,0,1,0); glRotatef(vAng,1,0,0);
  134. glScalef(float(size),float(size),float(size));
  135. drawCube();
  136. glPopMatrix();
  137. }
  138. #endif /* HAVE_GL */
  139. //
  140. // End of "$Id: CubeView.cxx 7903 2010-11-28 21:06:39Z matt $".
  141. //