2 * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
19 #include <gtk/gtkgl.h>
20 #include <gdk/gdkkeysyms.h>
31 static gboolean on_expose(GritsTester *tester, GdkEventExpose *event, gpointer _)
33 g_debug("GritsTester: on_expose");
35 glClearColor(0.0, 0.0, 0.0, 0.0);
36 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
40 glEnable(GL_DEPTH_TEST);
41 glDepthFunc(GL_LEQUAL);
42 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
44 //glDisable(GL_DEPTH_TEST);
45 //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
48 glMatrixMode(GL_MODELVIEW);
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);
62 glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
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);
79 GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
80 gdk_gl_drawable_swap_buffers(gldrawable);
84 static gboolean on_key_press(GritsTester *tester, GdkEventKey *event, gpointer _)
86 g_debug("GritsTester: on_key_press");
87 guint kv = event->keyval;
89 glGetDoublev(GL_MODELVIEW_MATRIX, model);
91 glMatrixMode(GL_MODELVIEW);
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);
109 gtk_widget_queue_draw(GTK_WIDGET(tester));
113 static gboolean on_configure(GritsTester *tester, GdkEventConfigure *event, gpointer _)
115 g_debug("GritsTester: on_configure");
117 double width = GTK_WIDGET(tester)->allocation.width;
118 double height = GTK_WIDGET(tester)->allocation.height;
120 /* Setup OpenGL Window */
121 glViewport(0, 0, width, height);
122 glMatrixMode(GL_PROJECTION);
124 double ang = atan(height/FOV_DIST);
125 gluPerspective(rad2deg(ang)*2, width/height, 0.001, 100);
130 static void on_realize(GritsTester *tester, gpointer _)
132 g_debug("GritsTester: on_realize");
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();
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);
147 /* Re-queue resize incase configure was triggered before realize */
148 gtk_widget_queue_resize(GTK_WIDGET(tester));
150 /* Setup model view matrix */
151 glMatrixMode(GL_MODELVIEW);
153 glTranslated(0, 0, -2);
159 gpointer grits_tester_add(GritsTester *tester, GritsObject *object)
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);
167 GritsObject *grits_tester_remove(GritsTester *tester, gpointer _ref)
169 g_debug("GritsTester: remove");
171 GritsObject *object = ref->data;
172 tester->objects = g_list_remove_link(tester->objects, ref);
176 GritsTester *grits_tester_new()
178 g_debug("GritsTester: new");
179 GritsTester *tester = g_object_new(GRITS_TYPE_TESTER, NULL);
186 G_DEFINE_TYPE(GritsTester, grits_tester, GTK_TYPE_DRAWING_AREA);
187 static void grits_tester_init(GritsTester *tester)
189 g_debug("GritsTester: init");
190 tester->objects = NULL;
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);
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);
203 /* Finish OpenGL init after it's realized */
204 g_signal_connect(tester, "realize", G_CALLBACK(on_realize), NULL);
206 static void grits_tester_class_init(GritsTesterClass *klass)
208 g_debug("GritsTester: class_init");