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.

232 lines
6.5KB

  1. //
  2. // "$Id: cube.cxx 8033 2010-12-15 12:11:16Z AlbrechtS $"
  3. //
  4. // Another forms test program for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Modified to have 2 cubes to test multiple OpenGL contexts
  7. //
  8. // Copyright 1998-2010 by Bill Spitzak and others.
  9. //
  10. // This library is free software; you can redistribute it and/or
  11. // modify it under the terms of the GNU Library General Public
  12. // License as published by the Free Software Foundation; either
  13. // version 2 of the License, or (at your option) any later version.
  14. //
  15. // This library is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. // Library General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Library General Public
  21. // License along with this library; if not, write to the Free Software
  22. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  23. // USA.
  24. //
  25. // Please report all bugs and problems on the following page:
  26. //
  27. // http://www.fltk.org/str.php
  28. //
  29. #include <config.h>
  30. #include <FL/Fl.H>
  31. #include <FL/Fl_Window.H>
  32. #include <FL/Fl_Box.H>
  33. #include <FL/Fl_Button.H>
  34. #include <FL/Fl_Radio_Light_Button.H>
  35. #include <FL/Fl_Slider.H>
  36. #include <stdlib.h>
  37. #if !HAVE_GL
  38. class cube_box : public Fl_Box {
  39. public:
  40. double lasttime;
  41. int wire;
  42. double size;
  43. double speed;
  44. cube_box(int x,int y,int w,int h,const char *l=0)
  45. :Fl_Box(FL_DOWN_BOX,x,y,w,h,l){
  46. label("This demo does\nnot work without GL");
  47. }
  48. };
  49. #else
  50. #include <FL/Fl_Gl_Window.H>
  51. #include <FL/gl.h>
  52. class cube_box : public Fl_Gl_Window {
  53. void draw();
  54. int handle(int);
  55. public:
  56. double lasttime;
  57. int wire;
  58. double size;
  59. double speed;
  60. cube_box(int x,int y,int w,int h,const char *l=0)
  61. : Fl_Gl_Window(x,y,w,h,l) {lasttime = 0.0;}
  62. };
  63. /* The cube definition */
  64. float v0[3] = {0.0, 0.0, 0.0};
  65. float v1[3] = {1.0, 0.0, 0.0};
  66. float v2[3] = {1.0, 1.0, 0.0};
  67. float v3[3] = {0.0, 1.0, 0.0};
  68. float v4[3] = {0.0, 0.0, 1.0};
  69. float v5[3] = {1.0, 0.0, 1.0};
  70. float v6[3] = {1.0, 1.0, 1.0};
  71. float v7[3] = {0.0, 1.0, 1.0};
  72. #define v3f(x) glVertex3fv(x)
  73. void drawcube(int wire) {
  74. /* Draw a colored cube */
  75. glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
  76. glColor3ub(0,0,255);
  77. v3f(v0); v3f(v1); v3f(v2); v3f(v3);
  78. glEnd();
  79. glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
  80. glColor3ub(0,255,255); v3f(v4); v3f(v5); v3f(v6); v3f(v7);
  81. glEnd();
  82. glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
  83. glColor3ub(255,0,255); v3f(v0); v3f(v1); v3f(v5); v3f(v4);
  84. glEnd();
  85. glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
  86. glColor3ub(255,255,0); v3f(v2); v3f(v3); v3f(v7); v3f(v6);
  87. glEnd();
  88. glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
  89. glColor3ub(0,255,0); v3f(v0); v3f(v4); v3f(v7); v3f(v3);
  90. glEnd();
  91. glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
  92. glColor3ub(255,0,0); v3f(v1); v3f(v2); v3f(v6); v3f(v5);
  93. glEnd();
  94. }
  95. void cube_box::draw() {
  96. lasttime = lasttime+speed;
  97. if (!valid()) {
  98. glLoadIdentity();
  99. glViewport(0,0,w(),h());
  100. glEnable(GL_DEPTH_TEST);
  101. glFrustum(-1,1,-1,1,2,10000);
  102. glTranslatef(0,0,-10);
  103. gl_font(FL_HELVETICA_BOLD, 16 );
  104. }
  105. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  106. glPushMatrix();
  107. glRotatef(float(lasttime*1.6),0,0,1);
  108. glRotatef(float(lasttime*4.2),1,0,0);
  109. glRotatef(float(lasttime*2.3),0,1,0);
  110. glTranslatef(-1.0, 1.2f, -1.5);
  111. glScalef(float(size),float(size),float(size));
  112. drawcube(wire);
  113. glPopMatrix();
  114. gl_color(FL_GRAY);
  115. glDisable(GL_DEPTH_TEST);
  116. gl_draw(wire ? "Cube: wire" : "Cube: flat", -4.5f, -4.5f );
  117. glEnable(GL_DEPTH_TEST);
  118. }
  119. int cube_box::handle(int e) {
  120. switch (e) {
  121. case FL_ENTER: cursor(FL_CURSOR_CROSS); break;
  122. case FL_LEAVE: cursor(FL_CURSOR_DEFAULT); break;
  123. }
  124. return Fl_Gl_Window::handle(e);
  125. }
  126. #endif
  127. Fl_Window *form;
  128. Fl_Slider *speed, *size;
  129. Fl_Button *button, *wire, *flat;
  130. cube_box *cube, *cube2;
  131. void makeform(const char *name) {
  132. form = new Fl_Window(510+390,390,name);
  133. new Fl_Box(FL_DOWN_FRAME,20,20,350,350,"");
  134. new Fl_Box(FL_DOWN_FRAME,510,20,350,350,"");
  135. speed = new Fl_Slider(FL_VERT_SLIDER,390,90,40,220,"Speed");
  136. size = new Fl_Slider(FL_VERT_SLIDER,450,90,40,220,"Size");
  137. wire = new Fl_Radio_Light_Button(390,20,100,30,"Wire");
  138. flat = new Fl_Radio_Light_Button(390,50,100,30,"Flat");
  139. button = new Fl_Button(390,340,100,30,"Exit");
  140. cube = new cube_box(23,23,344,344, 0);
  141. cube2 = new cube_box(513,23,344,344, 0);
  142. Fl_Box *b = new Fl_Box(FL_NO_BOX,cube->x(),size->y(),
  143. cube->w(),size->h(),0);
  144. form->resizable(b);
  145. b->hide();
  146. form->end();
  147. }
  148. // added to demo printing
  149. #include <FL/Fl_Sys_Menu_Bar.H>
  150. #include <FL/Fl_Printer.H>
  151. void print_cb(Fl_Widget *w, void *data)
  152. {
  153. Fl_Printer printer;
  154. Fl_Window *win = Fl::first_window();
  155. if(!win) return;
  156. if( printer.start_job(1) ) return;
  157. if( printer.start_page() ) return;
  158. printer.scale(0.5,0.5);
  159. printer.print_widget( win );
  160. printer.end_page();
  161. printer.end_job();
  162. }
  163. // end of printing demo
  164. int main(int argc, char **argv) {
  165. makeform(argv[0]);
  166. // added to demo printing
  167. form->begin();
  168. static Fl_Menu_Item items[] = {
  169. { "Print", 0, 0, 0, FL_SUBMENU },
  170. { "Print window", 0, print_cb, 0, 0 },
  171. { 0 },
  172. { 0 }
  173. };
  174. Fl_Sys_Menu_Bar *menubar_;
  175. menubar_ = new Fl_Sys_Menu_Bar(0, 0, 60, 20);
  176. menubar_->box(FL_FLAT_BOX);
  177. menubar_->menu(items);
  178. form->end();
  179. // end of printing demo
  180. speed->bounds(4,0);
  181. speed->value(cube->speed = cube2->speed = 1.0);
  182. size->bounds(4,0.01);
  183. size->value(cube->size = cube2->size = 1.0);
  184. flat->value(1); cube->wire = 0; cube2->wire = 1;
  185. form->label("cube");
  186. form->show(argc,argv);
  187. cube->show();
  188. cube2->show();
  189. #if 0
  190. // This demonstrates how to manipulate OpenGL contexts.
  191. // In this case the same context is used by multiple windows (I'm not
  192. // sure if this is allowed on Win32, can somebody check?).
  193. // This fixes a bug on the XFree86 3.0 OpenGL where only one context
  194. // per program seems to work, but there are probably better uses for
  195. // this!
  196. cube->make_current(); // causes context to be created
  197. cube2->context(cube->context()); // share the contexts
  198. #endif
  199. for (;;) {
  200. if (form->visible() && speed->value())
  201. {if (!Fl::check()) break;} // returns immediately
  202. else
  203. {if (!Fl::wait()) break;} // waits until something happens
  204. cube->wire = wire->value();
  205. cube2->wire = !wire->value();
  206. cube->size = cube2->size = size->value();
  207. cube->speed = cube2->speed = speed->value();
  208. cube->redraw();
  209. cube2->redraw();
  210. if (Fl::readqueue() == button) break;
  211. }
  212. return 0;
  213. }
  214. //
  215. // End of "$Id: cube.cxx 8033 2010-12-15 12:11:16Z AlbrechtS $".
  216. //