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