2 // "$Id: cube.cxx 8033 2010-12-15 12:11:16Z AlbrechtS $"
4 // Another forms test program for the Fast Light Tool Kit (FLTK).
6 // Modified to have 2 cubes to test multiple OpenGL contexts
8 // Copyright 1998-2010 by Bill Spitzak and others.
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.
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.
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
25 // Please report all bugs and problems on the following page:
27 // http://www.fltk.org/str.php
32 #include <FL/Fl_Window.H>
33 #include <FL/Fl_Box.H>
34 #include <FL/Fl_Button.H>
35 #include <FL/Fl_Radio_Light_Button.H>
36 #include <FL/Fl_Slider.H>
40 class cube_box
: public Fl_Box
{
46 cube_box(int x
,int y
,int w
,int h
,const char *l
=0)
47 :Fl_Box(FL_DOWN_BOX
,x
,y
,w
,h
,l
){
48 label("This demo does\nnot work without GL");
52 #include <FL/Fl_Gl_Window.H>
55 class cube_box
: public Fl_Gl_Window
{
63 cube_box(int x
,int y
,int w
,int h
,const char *l
=0)
64 : Fl_Gl_Window(x
,y
,w
,h
,l
) {lasttime
= 0.0;}
67 /* The cube definition */
68 float v0
[3] = {0.0, 0.0, 0.0};
69 float v1
[3] = {1.0, 0.0, 0.0};
70 float v2
[3] = {1.0, 1.0, 0.0};
71 float v3
[3] = {0.0, 1.0, 0.0};
72 float v4
[3] = {0.0, 0.0, 1.0};
73 float v5
[3] = {1.0, 0.0, 1.0};
74 float v6
[3] = {1.0, 1.0, 1.0};
75 float v7
[3] = {0.0, 1.0, 1.0};
77 #define v3f(x) glVertex3fv(x)
79 void drawcube(int wire
) {
80 /* Draw a colored cube */
81 glBegin(wire
? GL_LINE_LOOP
: GL_POLYGON
);
83 v3f(v0
); v3f(v1
); v3f(v2
); v3f(v3
);
85 glBegin(wire
? GL_LINE_LOOP
: GL_POLYGON
);
86 glColor3ub(0,255,255); v3f(v4
); v3f(v5
); v3f(v6
); v3f(v7
);
88 glBegin(wire
? GL_LINE_LOOP
: GL_POLYGON
);
89 glColor3ub(255,0,255); v3f(v0
); v3f(v1
); v3f(v5
); v3f(v4
);
91 glBegin(wire
? GL_LINE_LOOP
: GL_POLYGON
);
92 glColor3ub(255,255,0); v3f(v2
); v3f(v3
); v3f(v7
); v3f(v6
);
94 glBegin(wire
? GL_LINE_LOOP
: GL_POLYGON
);
95 glColor3ub(0,255,0); v3f(v0
); v3f(v4
); v3f(v7
); v3f(v3
);
97 glBegin(wire
? GL_LINE_LOOP
: GL_POLYGON
);
98 glColor3ub(255,0,0); v3f(v1
); v3f(v2
); v3f(v6
); v3f(v5
);
102 void cube_box::draw() {
103 lasttime
= lasttime
+speed
;
106 glViewport(0,0,w(),h());
107 glEnable(GL_DEPTH_TEST
);
108 glFrustum(-1,1,-1,1,2,10000);
109 glTranslatef(0,0,-10);
110 gl_font(FL_HELVETICA_BOLD
, 16 );
112 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
114 glRotatef(float(lasttime
*1.6),0,0,1);
115 glRotatef(float(lasttime
*4.2),1,0,0);
116 glRotatef(float(lasttime
*2.3),0,1,0);
117 glTranslatef(-1.0, 1.2f
, -1.5);
118 glScalef(float(size
),float(size
),float(size
));
122 glDisable(GL_DEPTH_TEST
);
123 gl_draw(wire
? "Cube: wire" : "Cube: flat", -4.5f
, -4.5f
);
124 glEnable(GL_DEPTH_TEST
);
127 int cube_box::handle(int e
) {
129 case FL_ENTER
: cursor(FL_CURSOR_CROSS
); break;
130 case FL_LEAVE
: cursor(FL_CURSOR_DEFAULT
); break;
132 return Fl_Gl_Window::handle(e
);
138 Fl_Slider
*speed
, *size
;
139 Fl_Button
*button
, *wire
, *flat
;
140 cube_box
*cube
, *cube2
;
142 void makeform(const char *name
) {
143 form
= new Fl_Window(510+390,390,name
);
144 new Fl_Box(FL_DOWN_FRAME
,20,20,350,350,"");
145 new Fl_Box(FL_DOWN_FRAME
,510,20,350,350,"");
146 speed
= new Fl_Slider(FL_VERT_SLIDER
,390,90,40,220,"Speed");
147 size
= new Fl_Slider(FL_VERT_SLIDER
,450,90,40,220,"Size");
148 wire
= new Fl_Radio_Light_Button(390,20,100,30,"Wire");
149 flat
= new Fl_Radio_Light_Button(390,50,100,30,"Flat");
150 button
= new Fl_Button(390,340,100,30,"Exit");
151 cube
= new cube_box(23,23,344,344, 0);
152 cube2
= new cube_box(513,23,344,344, 0);
153 Fl_Box
*b
= new Fl_Box(FL_NO_BOX
,cube
->x(),size
->y(),
154 cube
->w(),size
->h(),0);
160 // added to demo printing
161 #include <FL/Fl_Sys_Menu_Bar.H>
162 #include <FL/Fl_Printer.H>
164 void print_cb(Fl_Widget
*w
, void *data
)
167 Fl_Window
*win
= Fl::first_window();
169 if( printer
.start_job(1) ) return;
170 if( printer
.start_page() ) return;
171 printer
.scale(0.5,0.5);
172 printer
.print_widget( win
);
176 // end of printing demo
178 int main(int argc
, char **argv
) {
180 // added to demo printing
182 static Fl_Menu_Item items
[] = {
183 { "Print", 0, 0, 0, FL_SUBMENU
},
184 { "Print window", 0, print_cb
, 0, 0 },
188 Fl_Sys_Menu_Bar
*menubar_
;
189 menubar_
= new Fl_Sys_Menu_Bar(0, 0, 60, 20);
190 menubar_
->box(FL_FLAT_BOX
);
191 menubar_
->menu(items
);
193 // end of printing demo
195 speed
->value(cube
->speed
= cube2
->speed
= 1.0);
196 size
->bounds(4,0.01);
197 size
->value(cube
->size
= cube2
->size
= 1.0);
198 flat
->value(1); cube
->wire
= 0; cube2
->wire
= 1;
200 form
->show(argc
,argv
);
204 // This demonstrates how to manipulate OpenGL contexts.
205 // In this case the same context is used by multiple windows (I'm not
206 // sure if this is allowed on Win32, can somebody check?).
207 // This fixes a bug on the XFree86 3.0 OpenGL where only one context
208 // per program seems to work, but there are probably better uses for
210 cube
->make_current(); // causes context to be created
211 cube2
->context(cube
->context()); // share the contexts
214 if (form
->visible() && speed
->value())
215 {if (!Fl::check()) break;} // returns immediately
217 {if (!Fl::wait()) break;} // waits until something happens
218 cube
->wire
= wire
->value();
219 cube2
->wire
= !wire
->value();
220 cube
->size
= cube2
->size
= size
->value();
221 cube
->speed
= cube2
->speed
= speed
->value();
224 if (Fl::readqueue() == button
) break;
230 // End of "$Id: cube.cxx 8033 2010-12-15 12:11:16Z AlbrechtS $".