]> Pileus Git - grits/blob - examples/tester.c
e567056cc4eb8b36c71a99e274819cd69de1b964
[grits] / examples / tester.c
1 /*
2  * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <gtk/gtk.h>
19 #include <gtk/gtkgl.h>
20 #include <gdk/gdkkeysyms.h>
21 #include <GL/gl.h>
22 #include <GL/glu.h>
23 #include <stdlib.h>
24 #include <math.h>
25
26 #include "tester.h"
27
28 /*************
29  * Callbacks *
30  *************/
31 static gboolean on_expose(GritsTester *tester, GdkEventExpose *event, gpointer _)
32 {
33         g_debug("GritsTester: on_expose");
34
35         glClearColor(0.0, 0.0, 0.0, 0.0);
36         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
37         glClearDepth(1.0);
38         glEnable(GL_BLEND);
39
40         glEnable(GL_DEPTH_TEST);
41         glDepthFunc(GL_LEQUAL);
42         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
43
44         //glDisable(GL_DEPTH_TEST);
45         //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
46
47         /* Lighting */
48         glMatrixMode(GL_MODELVIEW);
49         glPushMatrix();
50         glLoadIdentity();
51         float light_ambient[]  = {0.1f, 0.1f, 0.1f, 1.0f};
52         float light_diffuse[]  = {0.6f, 0.6f, 0.8f, 1.0f};
53         float light_position[] = {-40.0f, 80.0f, 40.0f, 1.0f};
54         glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
55         glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
56         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
57         glEnable(GL_COLOR_MATERIAL);
58         glEnable(GL_LIGHTING);
59         glEnable(GL_LIGHT0);
60         glPopMatrix();
61
62         glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
63
64         /* Draw */
65         gdk_gl_draw_cube(FALSE, 2);
66         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
67         for (GList *cur = tester->objects; cur; cur = cur->next)
68                 grits_object_draw(cur->data, NULL);
69         if (tester->wireframe) {
70                 glDisable(GL_LIGHTING);
71                 glEnable(GL_POLYGON_OFFSET_LINE);
72                 glPolygonOffset(-1,0);
73                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
74                 for (GList *cur = tester->objects; cur; cur = cur->next)
75                         grits_object_draw(cur->data, NULL);
76         }
77
78         /* Flush */
79         GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
80         gdk_gl_drawable_swap_buffers(gldrawable);
81         return FALSE;
82 }
83
84 static gboolean on_key_press(GritsTester *tester, GdkEventKey *event, gpointer _)
85 {
86         g_debug("GritsTester: on_key_press");
87         guint kv = event->keyval;
88         gdouble model[16];
89         glGetDoublev(GL_MODELVIEW_MATRIX, model);
90
91         glMatrixMode(GL_MODELVIEW);
92         glLoadIdentity();
93              if (kv == GDK_q) gtk_main_quit();
94         else if (kv == GDK_w) tester->wireframe = !tester->wireframe;
95         else if (kv == GDK_h) glTranslated( 0.1,  0,  0);
96         else if (kv == GDK_l) glTranslated(-0.1,  0,  0);
97         else if (kv == GDK_j) glTranslated( 0,  0.1,  0);
98         else if (kv == GDK_k) glTranslated( 0, -0.1,  0);
99         else if (kv == GDK_i) glTranslated( 0,  0,  0.1);
100         else if (kv == GDK_o) glTranslated( 0,  0, -0.1);
101         else if (kv == GDK_H) glRotated(-5, 0,1,0);
102         else if (kv == GDK_L) glRotated( 5, 0,1,0);
103         else if (kv == GDK_J) glRotated( 5, 1,0,0);
104         else if (kv == GDK_K) glRotated(-5, 1,0,0);
105         else if (kv == GDK_I) glRotated(-5, 0,0,1);
106         else if (kv == GDK_O) glRotated( 5, 0,0,1);
107         glMultMatrixd(model);
108
109         gtk_widget_queue_draw(GTK_WIDGET(tester));
110         return FALSE;
111 }
112
113 static gboolean on_configure(GritsTester *tester, GdkEventConfigure *event, gpointer _)
114 {
115         g_debug("GritsTester: on_configure");
116
117         double width  = GTK_WIDGET(tester)->allocation.width;
118         double height = GTK_WIDGET(tester)->allocation.height;
119
120         /* Setup OpenGL Window */
121         glViewport(0, 0, width, height);
122         glMatrixMode(GL_PROJECTION);
123         glLoadIdentity();
124         double ang = atan(height/FOV_DIST);
125         gluPerspective(rad2deg(ang)*2, width/height, 0.001, 100);
126
127         return FALSE;
128 }
129
130 static void on_realize(GritsTester *tester, gpointer _)
131 {
132         g_debug("GritsTester: on_realize");
133
134         /* Start OpenGL */
135         GdkGLContext   *glcontext  = gtk_widget_get_gl_context(GTK_WIDGET(tester));
136         GdkGLDrawable  *gldrawable = gtk_widget_get_gl_drawable(GTK_WIDGET(tester));
137         if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
138                 g_assert_not_reached();
139
140         /* Connect signals and idle functions now that opengl is fully initialized */
141         g_object_set(tester, "can-focus", TRUE, NULL);
142         gtk_widget_add_events(GTK_WIDGET(tester), GDK_KEY_PRESS_MASK);
143         g_signal_connect(tester, "configure-event", G_CALLBACK(on_configure), NULL);
144         g_signal_connect(tester, "expose-event",    G_CALLBACK(on_expose),    NULL);
145         g_signal_connect(tester, "key-press-event", G_CALLBACK(on_key_press), NULL);
146
147         /* Re-queue resize incase configure was triggered before realize */
148         gtk_widget_queue_resize(GTK_WIDGET(tester));
149
150         /* Setup model view matrix */
151         glMatrixMode(GL_MODELVIEW);
152         glLoadIdentity();
153         glTranslated(0, 0, -2);
154 }
155
156 /***********
157  * Methods *
158  ***********/
159 gpointer grits_tester_add(GritsTester *tester, GritsObject *object)
160 {
161         g_debug("GritsTester: add");
162         gtk_widget_queue_draw(GTK_WIDGET(tester));
163         object->viewer = (void*)tester;
164         return tester->objects = g_list_prepend(tester->objects, object);
165 }
166
167 GritsObject *grits_tester_remove(GritsTester *tester, gpointer _ref)
168 {
169         g_debug("GritsTester: remove");
170         GList *ref = _ref;
171         GritsObject *object = ref->data;
172         tester->objects = g_list_remove_link(tester->objects, ref);
173         return object;
174 }
175
176 GritsTester *grits_tester_new()
177 {
178         g_debug("GritsTester: new");
179         GritsTester *tester = g_object_new(GRITS_TYPE_TESTER, NULL);
180         return tester;
181 }
182
183 /****************
184  * GObject code *
185  ****************/
186 G_DEFINE_TYPE(GritsTester, grits_tester, GTK_TYPE_DRAWING_AREA);
187 static void grits_tester_init(GritsTester *tester)
188 {
189         g_debug("GritsTester: init");
190         tester->objects = NULL;
191
192         /* Set OpenGL before "realize" */
193         GdkGLConfig *glconfig = gdk_gl_config_new_by_mode(
194                         GDK_GL_MODE_RGBA   | GDK_GL_MODE_DEPTH |
195                         GDK_GL_MODE_DOUBLE | GDK_GL_MODE_ALPHA);
196         if (!glconfig)
197                 g_error("Failed to create glconfig");
198         if (!gtk_widget_set_gl_capability(GTK_WIDGET(tester),
199                                 glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE))
200                 g_error("GL lacks required capabilities");
201         g_object_unref(glconfig);
202
203         /* Finish OpenGL init after it's realized */
204         g_signal_connect(tester, "realize", G_CALLBACK(on_realize), NULL);
205 }
206 static void grits_tester_class_init(GritsTesterClass *klass)
207 {
208         g_debug("GritsTester: class_init");
209 }