1 /*
2  *  This file is part of XForms.
3  *
4  *  XForms is free software; you can redistribute it and/or modify it
5  *  under the terms of the GNU Lesser General Public License as
6  *  published by the Free Software Foundation; either version 2.1, or
7  *  (at your option) any later version.
8  *
9  *  XForms is distributed in the hope that it will be useful, but
10  *  WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public
15  *  License along with XForms; see the file COPYING.  If not, write to
16  *  the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
17  *  MA 02111-1307, USA.
18  */
19 
20 
21 /*
22  * OpenGL rendering in its own window
23  *
24  *  T.C. Zhao and M. Overmars
25  */
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include <stdlib.h>
32 #include <GL/glx.h>
33 #include <GL/gl.h>
34 
35 #include "include/forms.h"
36 #include "gl/glcanvas.h"
37 
38 extern Window fl_glwincreate( int *,
39                               GLXContext *,
40                               int, int );
41 
42 /* Forms and Objects */
43 
44 typedef struct {
45     FL_FORM   * glcontrol;
46     void      * vdata;
47     char      * cdata;
48     long        ldata;
49     FL_OBJECT * polybutt;
50 } FD_glcontrol;
51 
52 extern FD_glcontrol *create_form_glcontrol( void );
53 
54 extern int handle_expose( XEvent *,
55                           void * );
56 
57 int prim = GL_POLYGON;     /* GL primitive to draw */
58 
59 Window glwin;
60 
61 
62 /***************************************
63  ***************************************/
64 
65 int
main(int argc,char * argv[])66 main( int    argc,
67       char * argv[ ] )
68 {
69     FD_glcontrol *fd_glcontrol;
70     GLXContext context;
71     int config[ ] = { GLX_RGBA, GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None };
72 
73     fl_initialize( &argc, argv, "FormDemo", 0, 0 );
74     fd_glcontrol = create_form_glcontrol( );
75 
76     if ( ! ( glwin = fl_glwincreate( config, &context, 250, 250 ) ) )
77     {
78         fprintf( stderr, "GLWin: Can't create OpenGL window\n" );
79         exit( 0 );
80     }
81 
82     fl_add_event_callback( glwin, Expose, handle_expose, 0 );
83 
84     fl_winshow( glwin );
85 
86     /* show the first form */
87 
88     fl_set_button( fd_glcontrol->polybutt, 1 );
89     fl_show_form( fd_glcontrol->glcontrol, FL_PLACE_CENTER,
90                   FL_FULLBORDER, "glcontrol" );
91 
92     fl_do_forms( );
93 
94     return 0;
95 }
96 
97 
98 /***************************************
99  ***************************************/
100 
101 static void
draw_cube(void)102 draw_cube( void )
103 {
104     glColor3f( 1.0, 0.0, 0.0 );
105     glBegin( prim );
106     glVertex3f( 1.0, 1.0, 1.0 );
107     glVertex3f( 1.0, -1.0, 1.0 );
108     glVertex3f( 1.0, -1.0, -1.0 );
109     glVertex3f( 1.0, 1.0, -1.0 );
110     glEnd( );
111 
112     glBegin( prim );
113     glVertex3f( -1.0, 1.0, 1.0 );
114     glVertex3f( -1.0, 1.0, -1.0 );
115     glVertex3f( -1.0, -1.0, -1.0 );
116     glVertex3f( -1.0, -1.0, 1.0 );
117     glEnd( );
118 
119     /* Y faces */
120 
121     glColor3f( 0.0, 1.0, 0.0 );
122     glBegin( prim );
123     glVertex3f(  1.0, 1.0,  1.0 );
124     glVertex3f(  1.0, 1.0, -1.0 );
125     glVertex3f( -1.0, 1.0, -1.0 );
126     glVertex3f( -1.0, 1.0,  1.0 );
127     glEnd( );
128 
129     glBegin( prim );
130     glVertex3f(  1.0, -1.0,  1.0 );
131     glVertex3f( -1.0, -1.0,  1.0 );
132     glVertex3f( -1.0, -1.0, -1.0 );
133     glVertex3f(  1.0, -1.0, -1.0 );
134     glEnd( );
135 
136    /* Z faces */
137 
138     glColor3f( 0.0, 0.0, 1.0 );
139     glBegin( prim );
140     glVertex3f(  1.0,  1.0,  1.0 );
141     glVertex3f( -1.0,  1.0,  1.0 );
142     glVertex3f( -1.0, -1.0,  1.0 );
143     glVertex3f(  1.0, -1.0,  1.0 );
144     glEnd( );
145 
146     glBegin( prim );
147     glVertex3f(  1.0, 1.0, -1.0 );
148     glVertex3f(  1.0,-1.0, -1.0 );
149     glVertex3f( -1.0,-1.0, -1.0 );
150     glVertex3f( -1.0, 1.0, -1.0 );
151     glEnd( );
152 }
153 
154 
155 /***************************************
156  ***************************************/
157 
158 int
animate(XEvent * ev,void * data FL_UNUSED_ARG)159 animate( XEvent * ev,
160          void   * data  FL_UNUSED_ARG )
161 {
162     static GLfloat xrot,
163                    yrot,
164                    zrot;
165 
166     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
167     glPushMatrix( );
168     glRotatef( xrot, 1.0, 0.0, 0.0 );
169     glRotatef( yrot, 0.0, 1.0, 0.0 );
170     glRotatef( zrot, 0.0, 0.0, 1.0 );
171     glTranslatef( -1.0,1.2,-0.5 );
172     draw_cube( );
173     glPopMatrix( );
174     glFinish( );
175 
176     if ( ev->type != Expose )
177     {
178         xrot += 10.0;
179         yrot += 7.0;
180         zrot -= 3.0;
181     }
182 
183     glXSwapBuffers( fl_display, glwin );
184     return 0;
185 }
186 
187 
188 /***************************************
189  * Initialization and expose handler
190  ***************************************/
191 
192 int
handle_expose(XEvent * xev,void * data FL_UNUSED_ARG)193 handle_expose( XEvent * xev,
194                void   * data  FL_UNUSED_ARG )
195 {
196     FL_Coord w ,h ;
197 
198     fl_get_winsize( glwin, &w, &h );
199     glViewport( 0,0, w, h );
200 
201     glClearColor( 0.0,0.0,0.0,0.0 );
202 
203     glMatrixMode( GL_PROJECTION );
204     glLoadIdentity( );
205     glFrustum( -1.0, 1.0, -1.0, 1.0, 1.0, 10.0 );
206     glTranslatef( 0.0, 0.0, -5.0 );
207 
208     glMatrixMode( GL_MODELVIEW );
209     glLoadIdentity( );
210     glCullFace( GL_BACK );
211     glEnable( GL_CULL_FACE );
212     glShadeModel( GL_FLAT );
213     animate( xev, 0 );
214     return 0;
215 }
216 
217 
218 /* Form definition generated by fdesign */
219 
220 /* callback functions */
221 
222 /***************************************
223  ***************************************/
224 
225 void
change_primitive(FL_OBJECT * ob FL_UNUSED_ARG,long data)226 change_primitive( FL_OBJECT * ob    FL_UNUSED_ARG,
227                   long        data )
228 {
229     prim = data;
230 }
231 
232 
233 /***************************************
234  ***************************************/
235 
236 void
animate_callback(FL_OBJECT * ob,long data FL_UNUSED_ARG)237 animate_callback( FL_OBJECT * ob,
238                   long        data  FL_UNUSED_ARG )
239 {
240     fl_set_idle_callback( fl_get_button( ob ) ? animate:0, 0 );
241 }
242 
243 
244 /***************************************
245  ***************************************/
246 
247 FD_glcontrol *
create_form_glcontrol(void)248 create_form_glcontrol( void )
249 {
250     FL_OBJECT *obj;
251     FD_glcontrol *fdui = fl_calloc( 1, sizeof *fdui );
252 
253     fdui->glcontrol = fl_bgn_form( FL_NO_BOX, 241, 121 );
254 
255     fl_add_box( FL_UP_BOX, 0, 0, 241, 121, "" );
256 
257     fl_add_frame( FL_ENGRAVED_FRAME, 20, 26, 90, 70, "" );
258 
259     fdui->polybutt = obj = fl_add_checkbutton( FL_RADIO_BUTTON,
260                                                30, 61, 70, 30, "Polygon" );
261     fl_set_object_callback( obj, change_primitive, GL_POLYGON );
262 
263     obj = fl_add_checkbutton( FL_RADIO_BUTTON, 30, 31, 70, 30, "Line" );
264     fl_set_object_callback( obj, change_primitive, GL_LINE_LOOP );
265 
266     obj = fl_add_button( FL_NORMAL_BUTTON, 150, 21, 80, 30, "Done" );
267     fl_set_button_shortcut( obj, "Dd", 1 );
268 
269     obj = fl_add_roundbutton( FL_PUSH_BUTTON, 150, 71, 80, 30, "Animate" );
270     fl_set_object_callback( obj, animate_callback, 0 );
271 
272     fl_end_form( );
273 
274     return fdui;
275 }
276 
277 
278 /*
279  * Local variables:
280  * tab-width: 4
281  * indent-tabs-mode: nil
282  * End:
283  */
284