Added openGL to automake build. Fixed perspective display.
[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
52         /* Current page width */
53         GLfloat width = 0;
54         /* Current page height */
55         GLfloat height = 0;
56         /* Aspect ratio */
57         GLfloat aspect = 0;
58
59         gtk_widget_get_allocation (widget, &allocation);
60         w = allocation.width;
61         h = allocation.height;
62
63         aspect = w / h;
64         /*** OpenGL BEGIN ***/
65         if (!gtk_widget_begin_gl (widget))
66                 return FALSE;
67
68         glViewport (0, 0, w, h);
69
70         glMatrixMode (GL_PROJECTION);
71         glLoadIdentity ();
72         gluPerspective (60.0, aspect, 1.0, 60.0);
73         glMatrixMode(GL_MODELVIEW);
74
75         gtk_widget_end_gl (widget, FALSE);
76         /*** OpenGL END ***/
77
78         return TRUE;
79 }
80
81 /**
82  * The "realize" signal handler. All the OpenGL initialization
83  * should be performed here, such as default background colour,
84  * certain states etc.
85  */
86 void drawing_area_realize (GtkWidget *widget, gpointer data)
87 {
88         /*** OpenGL BEGIN ***/
89         if (!gtk_widget_begin_gl (widget)) {
90                 return;
91         }
92
93         glClearColor (0.0, 0.0, 0.0, 0.0);
94         glShadeModel (GL_FLAT);
95
96         gtk_widget_end_gl (widget, FALSE);
97         /*** OpenGL END ***/
98
99         return;
100 }
101
102 /**
103  * The "unrealize" signal handler. Any processing required when
104  * the OpenGL-capable window is unrealized should be done here.
105  */
106 void drawing_area_unrealize (GtkWidget *widget, gpointer data)
107 {
108         /* Code */
109 }