File: glwin.c

package info (click to toggle)
libforms 1.0.93sp1-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 11,548 kB
  • sloc: ansic: 97,227; sh: 9,236; makefile: 858
file content (274 lines) | stat: -rw-r--r-- 6,142 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 *  This file is part of XForms.
 *
 *  XForms is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1, or
 *  (at your option) any later version.
 *
 *  XForms is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with XForms; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
 *  MA 02111-1307, USA.
 */


/*
 * OpenGL rendering in its own window
 *
 *  T.C. Zhao and M. Overmars
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "GL/glx.h"
#include "GL/gl.h"
#include "include/forms.h"
#include "gl/glcanvas.h"
#include <stdlib.h>

extern Window fl_glwincreate( int *,
							  GLXContext *,
							  int, int );

/* Forms and Objects */

typedef struct {
	FL_FORM   * glcontrol;
	void      * vdata;
	char      * cdata;
	long        ldata;
	FL_OBJECT * polybutt;
} FD_glcontrol;

extern FD_glcontrol *create_form_glcontrol( void );

extern int handle_expose( XEvent *,
						  void * );

int prim = GL_POLYGON;     /* GL primitive to draw */

Window glwin;


/***************************************
 ***************************************/

int
main( int    argc,
	  char * argv[ ] )
{
	FD_glcontrol *fd_glcontrol;
	GLXContext context;
	int config[ ] = { GLX_RGBA, GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None };

	fl_initialize( &argc, argv, "FormDemo", 0, 0 );
	fd_glcontrol = create_form_glcontrol( );

	if ( ! ( glwin = fl_glwincreate( config, &context, 250, 250 ) ) )
	{
		fprintf( stderr, "GLWin: Can't create OpenGL window\n" );
		exit( 0 );
	}

	fl_add_event_callback( glwin, Expose, handle_expose, 0 );

	fl_winshow( glwin );

	/* show the first form */

	fl_set_button( fd_glcontrol->polybutt, 1 );
	fl_show_form( fd_glcontrol->glcontrol, FL_PLACE_CENTER,
				  FL_FULLBORDER, "glcontrol" );

	fl_do_forms( );

	return 0;
}


/***************************************
 ***************************************/

static void
draw_cube( void )
{
	glColor3f( 1.0, 0.0, 0.0 );
	glBegin( prim );
	glVertex3f( 1.0, 1.0, 1.0 );
	glVertex3f( 1.0, -1.0, 1.0 );
	glVertex3f( 1.0, -1.0, -1.0 );
	glVertex3f( 1.0, 1.0, -1.0 );
	glEnd( );

	glBegin( prim );
	glVertex3f( -1.0, 1.0, 1.0 );
	glVertex3f( -1.0, 1.0, -1.0 );
	glVertex3f( -1.0, -1.0, -1.0 );
	glVertex3f( -1.0, -1.0, 1.0 );
	glEnd( );

	/* Y faces */

	glColor3f( 0.0, 1.0, 0.0 );
	glBegin( prim );
	glVertex3f(  1.0, 1.0,  1.0 );
	glVertex3f(  1.0, 1.0, -1.0 );
	glVertex3f( -1.0, 1.0, -1.0 );
	glVertex3f( -1.0, 1.0,  1.0 );
	glEnd( );

	glBegin( prim );
	glVertex3f(  1.0, -1.0,  1.0 );
	glVertex3f( -1.0, -1.0,  1.0 );
	glVertex3f( -1.0, -1.0, -1.0 );
	glVertex3f(  1.0, -1.0, -1.0 );
	glEnd( );

   /* Z faces */

	glColor3f( 0.0, 0.0, 1.0 );
	glBegin( prim );
	glVertex3f(  1.0,  1.0,  1.0 );
	glVertex3f( -1.0,  1.0,  1.0 );
	glVertex3f( -1.0, -1.0,  1.0 );
	glVertex3f(  1.0, -1.0,  1.0 );
	glEnd( );

	glBegin( prim );
	glVertex3f(  1.0, 1.0, -1.0 );
	glVertex3f(  1.0,-1.0, -1.0 );
	glVertex3f( -1.0,-1.0, -1.0 );
	glVertex3f( -1.0, 1.0, -1.0 );
	glEnd( );
}


/***************************************
 ***************************************/

int
animate( XEvent * ev,
		 void   * data  FL_UNUSED_ARG )
{
    static GLfloat xrot,
		           yrot,
		           zrot;

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix( );
    glRotatef( xrot, 1.0, 0.0, 0.0 );
    glRotatef( yrot, 0.0, 1.0, 0.0 );
    glRotatef( zrot, 0.0, 0.0, 1.0 );
    glTranslatef( -1.0,1.2,-0.5 );
    draw_cube( );
    glPopMatrix( );
    glFinish( );

    if ( ev->type != Expose )
    {
		xrot += 10.0;
		yrot += 7.0;
		zrot -= 3.0;
    }

    glXSwapBuffers( fl_display, glwin );
    return 0;
}


/***************************************
 * Initialization and expose handler
 ***************************************/

int
handle_expose( XEvent * xev,
			   void   * data  FL_UNUSED_ARG )
{
    FL_Coord w ,h ;

    fl_get_winsize( glwin, &w, &h );
    glViewport( 0,0, w, h );

    glClearColor( 0.0,0.0,0.0,0.0 );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity( );
    glFrustum( -1.0, 1.0, -1.0, 1.0, 1.0, 10.0 );
    glTranslatef( 0.0, 0.0, -5.0 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity( );
    glCullFace( GL_BACK );
    glEnable( GL_CULL_FACE );
    glShadeModel( GL_FLAT );
    animate( xev, 0 );
    return 0;
}


/* Form definition generated by fdesign */

/* callback functions */

/***************************************
 ***************************************/

void
change_primitive( FL_OBJECT * ob    FL_UNUSED_ARG,
				  long        data )
{
    prim = data;
}


/***************************************
 ***************************************/

void
animate_callback( FL_OBJECT * ob,
				  long        data  FL_UNUSED_ARG )
{
    fl_set_idle_callback( fl_get_button( ob ) ? animate:0, 0 );
}


/***************************************
 ***************************************/

FD_glcontrol *
create_form_glcontrol( void )
{
	FL_OBJECT *obj;
	FD_glcontrol *fdui = fl_calloc( 1, sizeof *fdui );

	fdui->glcontrol = fl_bgn_form( FL_NO_BOX, 241, 121 );

	fl_add_box( FL_UP_BOX, 0, 0, 241, 121, "" );

	fl_add_frame( FL_ENGRAVED_FRAME, 20, 26, 90, 70, "" );

	fdui->polybutt = obj = fl_add_checkbutton( FL_RADIO_BUTTON,
											   30, 61, 70, 30, "Polygon" );
    fl_set_object_callback( obj, change_primitive, GL_POLYGON );

	obj = fl_add_checkbutton( FL_RADIO_BUTTON, 30, 31, 70, 30, "Line" );
    fl_set_object_callback( obj, change_primitive, GL_LINE_LOOP );

	obj = fl_add_button( FL_NORMAL_BUTTON, 150, 21, 80, 30, "Done" );
    fl_set_button_shortcut( obj, "Dd", 1 );

	obj = fl_add_roundbutton( FL_PUSH_BUTTON, 150, 71, 80, 30, "Animate" );
    fl_set_object_callback( obj, animate_callback, 0 );

	fl_end_form( );

	return fdui;
}