1 |
/**
|
2 |
* Navit, a modular navigation system.
|
3 |
* Copyright (C) 2005-2008 Navit Team
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* version 2 as published by the Free Software Foundation.
|
8 |
*
|
9 |
* This program is distributed in the hope that it will be useful,
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
* GNU General Public License for more details.
|
13 |
*
|
14 |
* You should have received a copy of the GNU General Public License
|
15 |
* along with this program; if not, write to the
|
16 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
17 |
* Boston, MA 02110-1301, USA.
|
18 |
*/
|
19 |
|
20 |
#include <stdlib.h>
|
21 |
#include <gtk/gtk.h>
|
22 |
#include <gtk/gtkgl.h>
|
23 |
#include <GL/gl.h>
|
24 |
#include <GL/glu.h>
|
25 |
#include "point.h"
|
26 |
#include "graphics.h"
|
27 |
#include "container.h"
|
28 |
|
29 |
|
30 |
struct graphics_gra {
|
31 |
GtkWidget *widget;
|
32 |
int width;
|
33 |
int height;
|
34 |
int library_init;
|
35 |
int visible;
|
36 |
int buffer;
|
37 |
};
|
38 |
|
39 |
struct graphics_font {
|
40 |
};
|
41 |
|
42 |
struct graphics_gc {
|
43 |
double fr,fg,fb;
|
44 |
double br,bg,bb;
|
45 |
double width;
|
46 |
struct graphics_gra *gra;
|
47 |
};
|
48 |
|
49 |
static struct graphics_font *font_new(struct graphics *gr, int size)
|
50 |
{
|
51 |
struct graphics_font *font=g_new(struct graphics_font, 1);
|
52 |
return font;
|
53 |
}
|
54 |
|
55 |
static struct graphics_gc *gc_new(struct graphics *gr)
|
56 |
{
|
57 |
struct graphics_gc *gc=g_new(struct graphics_gc, 1);
|
58 |
|
59 |
gc->fr=1;
|
60 |
gc->fg=1;
|
61 |
gc->fb=1;
|
62 |
gc->br=0;
|
63 |
gc->bg=0;
|
64 |
gc->bb=0;
|
65 |
gc->width=1;
|
66 |
gc->gra=gr->gra;
|
67 |
return gc;
|
68 |
}
|
69 |
|
70 |
static void
|
71 |
gc_set_linewidth(struct graphics_gc *gc, int w)
|
72 |
{
|
73 |
gc->width=w;
|
74 |
}
|
75 |
|
76 |
static void
|
77 |
gc_set_foreground(struct graphics_gc *gc, int r, int g, int b)
|
78 |
{
|
79 |
gc->fr=r/65535.0;
|
80 |
gc->fg=g/65535.0;
|
81 |
gc->fb=b/65535.0;
|
82 |
}
|
83 |
|
84 |
static void
|
85 |
gc_set_background(struct graphics_gc *gc, int r, int g, int b)
|
86 |
{
|
87 |
gc->br=r/65535.0;
|
88 |
gc->bg=g/65535.0;
|
89 |
gc->bb=b/65535.0;
|
90 |
}
|
91 |
|
92 |
static void
|
93 |
vertex(struct point *p)
|
94 |
{
|
95 |
double x,y;
|
96 |
x=p->x;
|
97 |
y=p->y;
|
98 |
x/=792;
|
99 |
y/=469;
|
100 |
x-=0.5;
|
101 |
y=0.5-y;
|
102 |
glVertex3f(x,y,0);
|
103 |
}
|
104 |
|
105 |
static void
|
106 |
draw_lines(struct graphics *gr, struct graphics_gc *gc, struct point *p, int count)
|
107 |
{
|
108 |
int i;
|
109 |
|
110 |
glLineWidth(gc->width);
|
111 |
glColor3f(gc->fr, gc->fg, gc->fb);
|
112 |
glBegin(GL_LINE_STRIP);
|
113 |
for (i=0 ; i < count ; i++)
|
114 |
vertex(p++);
|
115 |
glEnd();
|
116 |
}
|
117 |
|
118 |
static void
|
119 |
draw_polygon(struct graphics *gr, struct graphics_gc *gc, struct point *p, int count)
|
120 |
{
|
121 |
int i;
|
122 |
double x,y;
|
123 |
glColor3f(gc->fr, gc->fg, gc->fb);
|
124 |
glBegin(GL_POLYGON);
|
125 |
for (i=0 ; i < count ; i++)
|
126 |
vertex(p++);
|
127 |
glEnd();
|
128 |
}
|
129 |
|
130 |
|
131 |
static void
|
132 |
draw_circle(struct graphics *gr, struct graphics_gc *gc, struct point *p, int r)
|
133 |
{
|
134 |
|
135 |
}
|
136 |
|
137 |
static void
|
138 |
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)
|
139 |
{
|
140 |
}
|
141 |
|
142 |
static void
|
143 |
draw_begin(struct graphics *gr)
|
144 |
{
|
145 |
printf("draw_begin\n");
|
146 |
glClearColor(gr->gc[0]->br, gr->gc[0]->bg, gr->gc[0]->bb, 0);
|
147 |
glNewList(1, GL_COMPILE);
|
148 |
gr->gra->buffer=1;
|
149 |
}
|
150 |
|
151 |
static void
|
152 |
draw_end(struct graphics *gr)
|
153 |
{
|
154 |
printf("draw_end\n");
|
155 |
glEndList();
|
156 |
gr->gra->buffer=0;
|
157 |
}
|
158 |
|
159 |
static void realize(GtkWidget * widget, gpointer data)
|
160 |
{
|
161 |
GdkGLContext *glcontext = gtk_widget_get_gl_context(widget);
|
162 |
GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(widget);
|
163 |
|
164 |
GLUquadricObj *qobj;
|
165 |
static GLfloat light_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };
|
166 |
static GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
|
167 |
|
168 |
/*** OpenGL BEGIN ***/
|
169 |
if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
|
170 |
return;
|
171 |
|
172 |
qobj = gluNewQuadric();
|
173 |
gluQuadricDrawStyle(qobj, GLU_FILL);
|
174 |
#if 0
|
175 |
glNewList(1, GL_COMPILE);
|
176 |
gluSphere(qobj, 1.0, 20, 20);
|
177 |
glBegin(GL_LINE_STRIP);
|
178 |
glVertex3f(0.0,0.1,0.0);
|
179 |
glVertex3f(0.1,0.1,0.0);
|
180 |
glVertex3f(0.1,0.2,0.0);
|
181 |
glVertex3f(0.2,0.2,0.0);
|
182 |
glEnd();
|
183 |
glEndList();
|
184 |
#endif
|
185 |
|
186 |
#if 0
|
187 |
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
|
188 |
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
|
189 |
glEnable(GL_LIGHTING);
|
190 |
glEnable(GL_LIGHT0);
|
191 |
glEnable(GL_DEPTH_TEST);
|
192 |
#endif
|
193 |
|
194 |
glClearColor(1.0, 1.0, 1.0, 1.0);
|
195 |
glClearDepth(1.0);
|
196 |
|
197 |
glViewport(0, 0,
|
198 |
widget->allocation.width, widget->allocation.height);
|
199 |
|
200 |
glMatrixMode(GL_PROJECTION);
|
201 |
glLoadIdentity();
|
202 |
gluPerspective(19.0, 1.0, 1.0, 10.0);
|
203 |
|
204 |
glMatrixMode(GL_MODELVIEW);
|
205 |
glLoadIdentity();
|
206 |
gluLookAt(0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
|
207 |
glTranslatef(0.0, 0.0, 0.0);
|
208 |
|
209 |
gdk_gl_drawable_gl_end(gldrawable);
|
210 |
/*** OpenGL END ***/
|
211 |
}
|
212 |
|
213 |
static gboolean
|
214 |
configure(GtkWidget * widget, GdkEventConfigure * event, gpointer user_data)
|
215 |
{
|
216 |
GdkGLContext *glcontext = gtk_widget_get_gl_context(widget);
|
217 |
GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(widget);
|
218 |
struct container *co=user_data;
|
219 |
struct graphics_gra *gra=co->gra->gra;
|
220 |
|
221 |
|
222 |
printf("configure %d %d\n",gra->width, gra->height);
|
223 |
gra->width=widget->allocation.width;
|
224 |
gra->height=widget->allocation.height;
|
225 |
|
226 |
/*** OpenGL BEGIN ***/
|
227 |
if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
|
228 |
return FALSE;
|
229 |
|
230 |
glViewport(0, 0,
|
231 |
widget->allocation.width, widget->allocation.height);
|
232 |
|
233 |
gdk_gl_drawable_gl_end(gldrawable);
|
234 |
/*** OpenGL END ***/
|
235 |
if (gra->visible)
|
236 |
graphics_resize(co, gra->width, gra->height);
|
237 |
|
238 |
return TRUE;
|
239 |
}
|
240 |
|
241 |
static gboolean
|
242 |
expose(GtkWidget * widget, GdkEventExpose * event, gpointer user_data)
|
243 |
{
|
244 |
GdkGLContext *glcontext = gtk_widget_get_gl_context(widget);
|
245 |
GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(widget);
|
246 |
struct container *co=user_data;
|
247 |
struct graphics_gra *gra=co->gra->gra;
|
248 |
|
249 |
printf("expose\n");
|
250 |
if (! gra->visible) {
|
251 |
gra->visible=1;
|
252 |
configure(widget, NULL, user_data);
|
253 |
}
|
254 |
/*** OpenGL BEGIN ***/
|
255 |
if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
|
256 |
return FALSE;
|
257 |
|
258 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
259 |
|
260 |
glCallList(1);
|
261 |
|
262 |
if (gdk_gl_drawable_is_double_buffered(gldrawable))
|
263 |
gdk_gl_drawable_swap_buffers(gldrawable);
|
264 |
else
|
265 |
glFlush();
|
266 |
|
267 |
gdk_gl_drawable_gl_end(gldrawable);
|
268 |
/*** OpenGL END ***/
|
269 |
|
270 |
return TRUE;
|
271 |
}
|
272 |
|
273 |
|
274 |
struct graphics *
|
275 |
graphics_gtk_gl_area_new(struct container *co, GtkWidget **widget)
|
276 |
{
|
277 |
GdkGLConfig *glconfig;
|
278 |
gint major, minor;
|
279 |
GtkWidget *drawing_area;
|
280 |
|
281 |
struct graphics *this=g_new0(struct graphics,1);
|
282 |
this->draw_lines=draw_lines;
|
283 |
this->draw_polygon=draw_polygon;
|
284 |
this->draw_circle=draw_circle;
|
285 |
this->draw_text=draw_text;
|
286 |
#if 0
|
287 |
this->draw_begin=draw_begin;
|
288 |
this->draw_end=draw_end;
|
289 |
#endif
|
290 |
this->gc_new=gc_new;
|
291 |
this->gc_set_linewidth=gc_set_linewidth;
|
292 |
this->gc_set_foreground=gc_set_foreground;
|
293 |
this->gc_set_background=gc_set_background;
|
294 |
this->font_new=font_new;
|
295 |
this->gra=g_new0(struct graphics_gra, 1);
|
296 |
|
297 |
/*
|
298 |
* Init GtkGLExt.
|
299 |
*/
|
300 |
|
301 |
gtk_gl_init(NULL, NULL);
|
302 |
|
303 |
/*
|
304 |
* Query OpenGL extension version.
|
305 |
*/
|
306 |
|
307 |
gdk_gl_query_version(&major, &minor);
|
308 |
g_print("OpenGL extension version - %d.%d\n", major, minor);
|
309 |
|
310 |
/*
|
311 |
* Configure OpenGL-capable visual.
|
312 |
*/
|
313 |
|
314 |
/* Try double-buffered visual */
|
315 |
glconfig = gdk_gl_config_new_by_mode(GDK_GL_MODE_RGB |
|
316 |
GDK_GL_MODE_DEPTH |
|
317 |
GDK_GL_MODE_DOUBLE);
|
318 |
if (glconfig == NULL) {
|
319 |
g_print("*** Cannot find the double-buffered visual.\n");
|
320 |
g_print("*** Trying single-buffered visual.\n");
|
321 |
|
322 |
/* Try single-buffered visual */
|
323 |
glconfig = gdk_gl_config_new_by_mode(GDK_GL_MODE_RGB |
|
324 |
GDK_GL_MODE_DEPTH);
|
325 |
if (glconfig == NULL) {
|
326 |
g_print
|
327 |
("*** No appropriate OpenGL-capable visual found.\n");
|
328 |
exit(1);
|
329 |
}
|
330 |
}
|
331 |
|
332 |
|
333 |
drawing_area = gtk_drawing_area_new();
|
334 |
|
335 |
/* Set OpenGL-capability to the widget. */
|
336 |
gtk_widget_set_gl_capability(drawing_area,
|
337 |
glconfig,
|
338 |
NULL, TRUE, GDK_GL_RGBA_TYPE);
|
339 |
|
340 |
g_signal_connect_after(G_OBJECT(drawing_area), "realize",
|
341 |
G_CALLBACK(realize), NULL);
|
342 |
g_signal_connect(G_OBJECT(drawing_area), "configure_event",
|
343 |
G_CALLBACK(configure), co);
|
344 |
g_signal_connect(G_OBJECT(drawing_area), "expose_event",
|
345 |
G_CALLBACK(expose), co);
|
346 |
|
347 |
*widget=drawing_area;
|
348 |
this->gra->widget=drawing_area;
|
349 |
return this;
|
350 |
}
|
351 |
|
352 |
|