summaryrefslogtreecommitdiff
path: root/navit/graphics/gtk_gl_ext/graphics_gtk_gl_ext.c
blob: c91e94c6397d801e09714abb242d85bb74a377f2 (plain)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <stdlib.h>
#include <gtk/gtk.h>
#include <gtk/gtkgl.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "point.h"
#include "graphics.h"
#include "container.h"


struct graphics_gra {
	GtkWidget *widget;
	int width;
	int height;
	int library_init;
	int visible;
	int buffer;
};

struct graphics_font {
};

struct graphics_gc {
	double fr,fg,fb;
	double br,bg,bb;
	double width;
	struct graphics_gra *gra;
};

static struct graphics_font *font_new(struct graphics *gr, int size)
{
	struct graphics_font *font=g_new(struct graphics_font, 1);
	return font;
}

static struct graphics_gc *gc_new(struct graphics *gr)
{
	struct graphics_gc *gc=g_new(struct graphics_gc, 1);

	gc->fr=1;
	gc->fg=1;
	gc->fb=1;
	gc->br=0;
	gc->bg=0;
	gc->bb=0;
	gc->width=1;
	gc->gra=gr->gra;
	return gc;
}

static void
gc_set_linewidth(struct graphics_gc *gc, int w)
{
	gc->width=w;	
}

static void
gc_set_foreground(struct graphics_gc *gc, int r, int g, int b)
{
	gc->fr=r/65535.0;
	gc->fg=g/65535.0;
	gc->fb=b/65535.0;
}

static void
gc_set_background(struct graphics_gc *gc, int r, int g, int b)
{
	gc->br=r/65535.0;
	gc->bg=g/65535.0;
	gc->bb=b/65535.0;
}

static void
vertex(struct point *p)
{
	double x,y;
	x=p->x;
	y=p->y;
	x/=792;	
	y/=469;	
	x-=0.5;
	y=0.5-y;
	glVertex3f(x,y,0);
}

static void
draw_lines(struct graphics *gr, struct graphics_gc *gc, struct point *p, int count)
{
	int i;

	glLineWidth(gc->width);
	glColor3f(gc->fr, gc->fg, gc->fb);
	glBegin(GL_LINE_STRIP);
	for (i=0 ; i < count ; i++) 
		vertex(p++);
	glEnd();
}

static void
draw_polygon(struct graphics *gr, struct graphics_gc *gc, struct point *p, int count)
{
	int i;
	double x,y;
	glColor3f(gc->fr, gc->fg, gc->fb);
	glBegin(GL_POLYGON);
	for (i=0 ; i < count ; i++) 
		vertex(p++);
	glEnd();
}


static void
draw_circle(struct graphics *gr, struct graphics_gc *gc, struct point *p, int r)
{
	
}

static void
draw_text(struct graphics *gr, struct graphics_gc *fg, struct graphics_gc *bg, struct graphics_font *font, unsigned char *text, int x, int y, int dx, int dy)
{
}

static void
draw_begin(struct graphics *gr)
{
	printf("draw_begin\n");
	glClearColor(gr->gc[0]->br, gr->gc[0]->bg, gr->gc[0]->bb, 0);
	glNewList(1, GL_COMPILE);
	gr->gra->buffer=1;
}

static void
draw_end(struct graphics *gr)
{
	printf("draw_end\n");
	glEndList();
	gr->gra->buffer=0;
}

static void realize(GtkWidget * widget, gpointer data)
{
	GdkGLContext *glcontext = gtk_widget_get_gl_context(widget);
	GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(widget);

	GLUquadricObj *qobj;
	static GLfloat light_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };
	static GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };

  /*** OpenGL BEGIN ***/
	if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
		return;

	qobj = gluNewQuadric();
	gluQuadricDrawStyle(qobj, GLU_FILL);
#if 0
	glNewList(1, GL_COMPILE);
	gluSphere(qobj, 1.0, 20, 20);
	glBegin(GL_LINE_STRIP);
	glVertex3f(0.0,0.1,0.0);	
	glVertex3f(0.1,0.1,0.0);	
	glVertex3f(0.1,0.2,0.0);	
	glVertex3f(0.2,0.2,0.0);	
	glEnd();
	glEndList();
#endif

#if 0
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_DEPTH_TEST);
#endif

	glClearColor(1.0, 1.0, 1.0, 1.0);
	glClearDepth(1.0);

	glViewport(0, 0,
		   widget->allocation.width, widget->allocation.height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(19.0, 1.0, 1.0, 10.0);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
	glTranslatef(0.0, 0.0, 0.0);

	gdk_gl_drawable_gl_end(gldrawable);
  /*** OpenGL END ***/
}

static gboolean
configure(GtkWidget * widget, GdkEventConfigure * event, gpointer user_data)
{
	GdkGLContext *glcontext = gtk_widget_get_gl_context(widget);
	GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(widget);
	struct container *co=user_data;
	struct graphics_gra *gra=co->gra->gra;


	printf("configure %d %d\n",gra->width, gra->height);
	gra->width=widget->allocation.width;
        gra->height=widget->allocation.height;

  /*** OpenGL BEGIN ***/
	if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
		return FALSE;

	glViewport(0, 0,
		   widget->allocation.width, widget->allocation.height);

	gdk_gl_drawable_gl_end(gldrawable);
  /*** OpenGL END ***/
	if (gra->visible)
   	     graphics_resize(co, gra->width, gra->height);

	return TRUE;
}

static gboolean
expose(GtkWidget * widget, GdkEventExpose * event, gpointer user_data)
{
	GdkGLContext *glcontext = gtk_widget_get_gl_context(widget);
	GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(widget);
	struct container *co=user_data;
	struct graphics_gra *gra=co->gra->gra;

	printf("expose\n");
	if (! gra->visible) {
		gra->visible=1;
		configure(widget, NULL, user_data);
	}
  /*** OpenGL BEGIN ***/
	if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
		return FALSE;

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glCallList(1);

	if (gdk_gl_drawable_is_double_buffered(gldrawable))
		gdk_gl_drawable_swap_buffers(gldrawable);
	else
		glFlush();

	gdk_gl_drawable_gl_end(gldrawable);
  /*** OpenGL END ***/

	return TRUE;
}


struct graphics *
graphics_gtk_gl_area_new(struct container *co, GtkWidget **widget)
{
	GdkGLConfig *glconfig;
	gint major, minor;
	GtkWidget *drawing_area;

	struct graphics *this=g_new0(struct graphics,1);
	this->draw_lines=draw_lines;
	this->draw_polygon=draw_polygon;
	this->draw_circle=draw_circle;
	this->draw_text=draw_text;
#if 0
	this->draw_begin=draw_begin;
	this->draw_end=draw_end;
#endif
	this->gc_new=gc_new;
	this->gc_set_linewidth=gc_set_linewidth;
	this->gc_set_foreground=gc_set_foreground;
	this->gc_set_background=gc_set_background;
	this->font_new=font_new;
	this->gra=g_new0(struct graphics_gra, 1);

	/*
	 * Init GtkGLExt.
	 */

	gtk_gl_init(NULL, NULL);

	/*
	 * Query OpenGL extension version.
	 */

	gdk_gl_query_version(&major, &minor);
	g_print("OpenGL extension version - %d.%d\n", major, minor);

	/*
	 * Configure OpenGL-capable visual.
	 */

	/* Try double-buffered visual */
	glconfig = gdk_gl_config_new_by_mode(GDK_GL_MODE_RGB |
					     GDK_GL_MODE_DEPTH |
					     GDK_GL_MODE_DOUBLE);
	if (glconfig == NULL) {
		g_print("*** Cannot find the double-buffered visual.\n");
		g_print("*** Trying single-buffered visual.\n");

		/* Try single-buffered visual */
		glconfig = gdk_gl_config_new_by_mode(GDK_GL_MODE_RGB |
						     GDK_GL_MODE_DEPTH);
		if (glconfig == NULL) {
			g_print
			    ("*** No appropriate OpenGL-capable visual found.\n");
			exit(1);
		}
	}


	drawing_area = gtk_drawing_area_new();

	/* Set OpenGL-capability to the widget. */
	gtk_widget_set_gl_capability(drawing_area,
				     glconfig,
				     NULL, TRUE, GDK_GL_RGBA_TYPE);

	g_signal_connect_after(G_OBJECT(drawing_area), "realize",
			       G_CALLBACK(realize), NULL);
	g_signal_connect(G_OBJECT(drawing_area), "configure_event",
			 G_CALLBACK(configure), co);
	g_signal_connect(G_OBJECT(drawing_area), "expose_event",
			 G_CALLBACK(expose), co);

	*widget=drawing_area;
	this->gra->widget=drawing_area;
	return this;
}