Created example openGL triangle in window.
[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
8 /**
9  * The "draw" signal handler. All the OpenGL re-drawing should
10  * be done here. This is repeatedly called as the painting routine
11  * every time the 'draw' event is signalled.
12  */
13 gboolean drawing_area_draw(GtkWidget *widget, cairo_t *cr, gpointer user_data)
14 {
15         /*** OpenGL BEGIN ***/
16         if (!gtk_widget_begin_gl (widget)) {
17                 return FALSE;
18         }
19
20 //      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
21
22         glPushMatrix ();
23                 glLoadIdentity ();
24                 glColor3f (1.0, 1.0, 1.0);
25                 glBegin(GL_TRIANGLES);
26                         glVertex3f( 0.0f, 1.0f, 0.0f);    // Top
27                         glVertex3f(-1.0f,-1.0f, 0.0f);    // Bottom Left
28                         glVertex3f( 1.0f,-1.0f, 0.0f);    // Bottom Right
29                 glEnd();
30         glPopMatrix ();
31
32         gtk_widget_end_gl (widget, TRUE);
33         /*** OpenGL END ***/
34
35         return TRUE;
36 }
37
38 /**
39  * The "configure_event" signal handler. Any processing required when
40  * the OpenGL-capable drawing area is re-configured should be done here.
41  * Almost always it will be used to resize the OpenGL viewport when
42  * the window is resized.
43  */
44 gboolean drawing_area_configure_event (GtkWidget *widget,
45                                         GdkEventConfigure *event, gpointer data)
46 {
47         GtkAllocation allocation;
48
49         /* OpenGL widget width */
50         GLfloat w;
51         /* OpenGL widget height */
52         GLfloat h;
53
54         /* Current page width */
55         GLfloat width = 0;
56         /* Current page height */
57         GLfloat height = 0;
58         /* Aspect ratio */
59         GLfloat aspect = 0;
60
61         gtk_widget_get_allocation (widget, &allocation);
62         w = allocation.width;
63         h = allocation.height;
64
65         /*** OpenGL BEGIN ***/
66         if (!gtk_widget_begin_gl (widget))
67                 return FALSE;
68
69         glViewport (0, 0, w, h);
70
71         glMatrixMode (GL_PROJECTION);
72         glLoadIdentity ();
73
74 //      if (w > h) {
75                 aspect = w / h;
76 //              glFrustum (-aspect, aspect, -1.0, 1.0, 5.0, 60.0);
77 //      }
78 //      else {
79 //              aspect = h / w;
80                 glFrustum (-1.0, 1.0, -aspect, aspect, 5.0, 60.0);
81 //      }
82
83         glMatrixMode (GL_MODELVIEW);
84
85         gtk_widget_end_gl (widget, FALSE);
86         /*** OpenGL END ***/
87
88         return TRUE;
89 }
90
91 /**
92  * The "realize" signal handler. All the OpenGL initialization
93  * should be performed here, such as default background colour,
94  * certain states etc.
95  */
96 void drawing_area_realize (GtkWidget *widget, gpointer data)
97 {
98         /*** OpenGL BEGIN ***/
99         if (!gtk_widget_begin_gl (widget)) {
100                 return;
101         }
102
103         glClearColor (0.0, 0.0, 0.0, 0.0);
104         glShadeModel (GL_FLAT);
105
106         gtk_widget_end_gl (widget, FALSE);
107         /*** OpenGL END ***/
108
109         return;
110 }
111
112 /**
113  * The "unrealize" signal handler. Any processing required when
114  * the OpenGL-capable window is unrealized should be done here.
115  */
116 void drawing_area_unrealize (GtkWidget *widget, gpointer data)
117 {
118         /* Code */
119 }