9cb96b0136136f54b517875ee4bec9b9fe04d872
[familia.git] / src / viewport / drawing_area.c
1 #include <gtk/gtk.h>
2 #include <gtk/gtkgl.h>
3 #include <GL/gl.h>
4
5 #include "../debug.h"
6 #include "drawing_area.h"
7 #include "../graphics/individual.h"
8
9 /**
10  * The "draw" signal handler. All the OpenGL re-drawing should
11  * be done here. This is repeatedly called as the painting routine
12  * every time the 'draw' event is signalled.
13  */
14 gboolean drawing_area_draw(GtkWidget *widget, cairo_t *cr, gpointer user_data)
15 {
16         /*** OpenGL BEGIN ***/
17         if (!gtk_widget_begin_gl (widget)) {
18                 return FALSE;
19         }
20
21         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
22
23         glPushMatrix ();
24                 glLoadIdentity ();
25                 glTranslatef(0, 0, -10);
26                 glColor3f (1.0, 1.0, 1.0);
27                 graphics_render_individual();
28         glPopMatrix ();
29
30         gtk_widget_end_gl (widget, TRUE);
31         /*** OpenGL END ***/
32
33         return TRUE;
34 }
35
36 /**
37  * The "configure_event" signal handler. Any processing required when
38  * the OpenGL-capable drawing area is re-configured should be done here.
39  * Almost always it will be used to resize the OpenGL viewport when
40  * the window is resized.
41  */
42 gboolean drawing_area_configure_event (GtkWidget *widget,
43                                         GdkEventConfigure *event, gpointer data)
44 {
45         GtkAllocation allocation;
46
47         /* OpenGL widget width */
48         GLfloat w;
49         /* OpenGL widget height */
50         GLfloat h;
51         /* Aspect ratio */
52         GLfloat aspect = 0;
53
54         gtk_widget_get_allocation (widget, &allocation);
55         w = allocation.width;
56         h = allocation.height;
57
58         aspect = w / h;
59         /*** OpenGL BEGIN ***/
60         if (!gtk_widget_begin_gl (widget))
61                 return FALSE;
62
63         glViewport (0, 0, w, h);
64
65         glMatrixMode (GL_PROJECTION);
66         glLoadIdentity ();
67         gluPerspective (60.0, aspect, 1.0, 60.0);
68         glMatrixMode(GL_MODELVIEW);
69
70         gtk_widget_end_gl (widget, FALSE);
71         /*** OpenGL END ***/
72
73         return TRUE;
74 }
75
76 /**
77  * The "realize" signal handler. All the OpenGL initialization
78  * should be performed here, such as default background colour,
79  * certain states etc.
80  */
81 void drawing_area_realize (GtkWidget *widget, gpointer data)
82 {
83         /*** OpenGL BEGIN ***/
84         if (!gtk_widget_begin_gl (widget)) {
85                 return;
86         }
87
88         glClearColor (0.0, 0.0, 0.0, 0.0);
89         glShadeModel (GL_FLAT);
90
91         gtk_widget_end_gl (widget, FALSE);
92         /*** OpenGL END ***/
93
94         return;
95 }
96
97 /**
98  * The "unrealize" signal handler. Any processing required when
99  * the OpenGL-capable window is unrealized should be done here.
100  */
101 void drawing_area_unrealize (GtkWidget *widget, gpointer data)
102 {
103         /* Code */
104 }