2 * Copyright (C) 2009-2010 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>
25 static gboolean on_expose(GtkWidget *drawing, GdkEventExpose *event, gpointer _)
27 //glClearColor(0.5, 0.5, 1.0, 1.0);
28 glClearColor(0.0, 0.0, 0.0, 0.0);
29 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
31 /* No sorting, just add */
33 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
34 //glBlendFunc(GL_ONE, GL_ONE);
37 glMatrixMode(GL_MODELVIEW);
39 float light_ambient[] = {0.3f, 0.3f, 0.1f, 1.0f};
40 float light_diffuse[] = {0.9f, 0.9f, 0.9f, 1.0f};
41 float light_position[] = {-30.0f, 50.0f, 40.0f, 1.0f};
42 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
43 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
44 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
45 glEnable(GL_COLOR_MATERIAL);
46 glEnable(GL_LIGHTING);
50 glMatrixMode(GL_PROJECTION);
52 glFrustum(-1,1, -1,1, 2,10);
54 glMatrixMode(GL_MODELVIEW);
56 gluLookAt(0,2,-3, 0,0,0, 0,1,0);
59 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
61 glColor4f(1, 0, 0, 0.5);
62 glBegin(GL_TRIANGLE_STRIP);
64 glVertex3i(-1, -1, 1);
65 glVertex3i( 1, 1, -1);
66 glVertex3i( 1, -1, -1);
69 glColor4f(0, 1, 0, 0.5);
70 glBegin(GL_TRIANGLE_STRIP);
72 glVertex3i( 1, -1, 1);
73 glVertex3i(-1, 1, -1);
74 glVertex3i(-1, -1, -1);
78 GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
79 if (gdk_gl_drawable_is_double_buffered(gldrawable))
80 gdk_gl_drawable_swap_buffers(gldrawable);
86 static gboolean on_configure(GtkWidget *drawing, GdkEventConfigure *event, gpointer _)
89 drawing->allocation.width,
90 drawing->allocation.height);
94 static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer _)
96 if (event->keyval == GDK_q)
101 int main(int argc, char **argv)
103 gtk_init(&argc, &argv);
105 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
106 GtkWidget *drawing = gtk_drawing_area_new();
107 GdkGLConfig *glconfig = gdk_gl_config_new_by_mode((GdkGLConfigMode)(
108 GDK_GL_MODE_RGBA | GDK_GL_MODE_DEPTH |
109 GDK_GL_MODE_DOUBLE | GDK_GL_MODE_ALPHA));
110 g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
111 g_signal_connect(window, "key-press-event", G_CALLBACK(on_key_press), NULL);
112 g_signal_connect(drawing, "expose-event", G_CALLBACK(on_expose), NULL);
113 g_signal_connect(drawing, "configure-event", G_CALLBACK(on_configure), NULL);
114 gtk_widget_set_gl_capability(drawing, glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE);
115 gtk_container_add(GTK_CONTAINER(window), drawing);
116 gtk_widget_show_all(window);
119 GdkGLContext *glcontext = gtk_widget_get_gl_context(GTK_WIDGET(drawing));
120 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(GTK_WIDGET(drawing));
121 gdk_gl_drawable_gl_begin(gldrawable, glcontext);
125 gdk_gl_drawable_gl_end(gldrawable);