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>
24 guint tex, texl, texr;
26 gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer _)
28 if (event->keyval == GDK_q)
33 gboolean on_expose(GtkWidget *drawing, GdkEventExpose *event, gpointer _)
35 glClearColor(0.5, 0.5, 1.0, 1.0);
36 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
38 glMatrixMode(GL_PROJECTION);
40 glOrtho(-1,1, -1,1, 10,-10);
41 glMatrixMode(GL_MODELVIEW);
43 glTranslatef(0, 0, -5);
45 glEnable(GL_COLOR_MATERIAL);
46 glDisable(GL_TEXTURE_2D);
47 glColor3f(1.0, 1.0, 1.0);
49 glVertex3f(-0.25, -0.75, 0.0);
50 glVertex3f(-0.25, 0.75, 0.0);
51 glVertex3f( 0.25, 0.75, 0.0);
52 glVertex3f( 0.25, -0.75, 0.0);
56 glDisable(GL_COLOR_MATERIAL);
57 glEnable(GL_TEXTURE_2D);
63 glBlendFunc(GL_ONE, GL_ZERO);
64 glBindTexture(GL_TEXTURE_2D, texl);
66 glTexCoord2f(0.0, y); glVertex3f(-0.75, 0.0, 0.0);
67 glTexCoord2f(0.0, 1.0); glVertex3f(-0.75, 0.5, 0.0);
68 glTexCoord2f(2.0, 1.0); glVertex3f( 0.75, 0.5, 0.0);
69 glTexCoord2f(2.0, y); glVertex3f( 0.75, 0.0, 0.0);
73 glBlendFunc(GL_ONE, GL_ONE);
74 glBindTexture(GL_TEXTURE_2D, texr);
76 glTexCoord2f(-1.0, y); glVertex3f(-0.75, 0.0, 0.0);
77 glTexCoord2f(-1.0, 1.0); glVertex3f(-0.75, 0.5, 0.0);
78 glTexCoord2f( 1.0, 1.0); glVertex3f( 0.75, 0.5, 0.0);
79 glTexCoord2f( 1.0, y); glVertex3f( 0.75, 0.0, 0.0);
83 glBlendFunc(GL_ONE, GL_ZERO);
84 glBindTexture(GL_TEXTURE_2D, tex);
86 glTexCoord2f(0.0, 0.0); glVertex3f(-0.75, -0.5, 0.0);
87 glTexCoord2f(0.0, 1.0-y); glVertex3f(-0.75, -0.0, 0.0);
88 glTexCoord2f(1.0, 1.0-y); glVertex3f( 0.75, -0.0, 0.0);
89 glTexCoord2f(1.0, 0.0); glVertex3f( 0.75, -0.5, 0.0);
94 GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
95 if (gdk_gl_drawable_is_double_buffered(gldrawable))
96 gdk_gl_drawable_swap_buffers(gldrawable);
101 gboolean on_configure(GtkWidget *drawing, GdkEventConfigure *event, gpointer _)
104 drawing->allocation.width,
105 drawing->allocation.height);
109 guint load_tex(gchar *filename)
111 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
112 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
113 int width = gdk_pixbuf_get_width(pixbuf);
114 int height = gdk_pixbuf_get_height(pixbuf);
115 int alpha = gdk_pixbuf_get_has_alpha(pixbuf);
117 glGenTextures(1, &tex);
118 glBindTexture(GL_TEXTURE_2D, tex);
119 glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
120 (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
121 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
122 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
126 g_object_unref(pixbuf);
130 int main(int argc, char **argv)
132 gtk_init(&argc, &argv);
134 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
135 GtkWidget *drawing = gtk_drawing_area_new();
136 GdkGLConfig *glconfig = gdk_gl_config_new_by_mode((GdkGLConfigMode)(
137 GDK_GL_MODE_RGBA | GDK_GL_MODE_DEPTH |
138 GDK_GL_MODE_DOUBLE | GDK_GL_MODE_ALPHA));
139 g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
140 g_signal_connect(window, "key-press-event", G_CALLBACK(on_key_press), NULL);
141 g_signal_connect(drawing, "expose-event", G_CALLBACK(on_expose), NULL);
142 g_signal_connect(drawing, "configure-event", G_CALLBACK(on_configure), NULL);
143 gtk_widget_set_gl_capability(drawing, glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE);
144 gtk_container_add(GTK_CONTAINER(window), drawing);
145 gtk_widget_show_all(window);
148 GdkGLContext *glcontext = gtk_widget_get_gl_context(GTK_WIDGET(drawing));
149 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(GTK_WIDGET(drawing));
150 gdk_gl_drawable_gl_begin(gldrawable, glcontext);
153 texl = load_tex("texls.png");
154 texr = load_tex("texrs.png");
155 tex = load_tex("tex.png");
159 gdk_gl_drawable_gl_end(gldrawable);