]> Pileus Git - grits/blob - examples/tex/tex.c
Update copyright and email address
[grits] / examples / tex / tex.c
1 /*
2  * Copyright (C) 2009-2010 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
24 guint tex, texl, texr;
25
26 gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer _)
27 {
28         if (event->keyval == GDK_q)
29                 gtk_main_quit();
30         return FALSE;
31 }
32
33 gboolean on_expose(GtkWidget *drawing, GdkEventExpose *event, gpointer _)
34 {
35         glClearColor(0.5, 0.5, 1.0, 1.0);
36         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
37
38         glMatrixMode(GL_PROJECTION);
39         glLoadIdentity();
40         glOrtho(-1,1, -1,1, 10,-10);
41         glMatrixMode(GL_MODELVIEW);
42         glLoadIdentity();
43         glTranslatef(0, 0, -5);
44
45         glEnable(GL_COLOR_MATERIAL);
46         glDisable(GL_TEXTURE_2D);
47         glColor3f(1.0, 1.0, 1.0);
48         glBegin(GL_QUADS);
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);
53         glEnd();
54
55         /* Textures */
56         glDisable(GL_COLOR_MATERIAL);
57         glEnable(GL_TEXTURE_2D);
58         glEnable(GL_BLEND);
59
60         gdouble y = 0.875;
61
62         /* Left */
63         glBlendFunc(GL_ONE, GL_ZERO);
64         glBindTexture(GL_TEXTURE_2D, texl);
65         glBegin(GL_QUADS);
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);
70         glEnd();
71
72         /* Right */
73         glBlendFunc(GL_ONE, GL_ONE);
74         glBindTexture(GL_TEXTURE_2D, texr);
75         glBegin(GL_QUADS);
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);
80         glEnd();
81
82         /* Bottom */
83         glBlendFunc(GL_ONE, GL_ZERO);
84         glBindTexture(GL_TEXTURE_2D, tex);
85         glBegin(GL_QUADS);
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);
90         glEnd();
91
92
93         /* Flush */
94         GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
95         if (gdk_gl_drawable_is_double_buffered(gldrawable))
96                 gdk_gl_drawable_swap_buffers(gldrawable);
97         else
98                 glFlush();
99         return FALSE;
100 }
101 gboolean on_configure(GtkWidget *drawing, GdkEventConfigure *event, gpointer _)
102 {
103         glViewport(0, 0,
104                 drawing->allocation.width,
105                 drawing->allocation.height);
106         return FALSE;
107 }
108
109 guint load_tex(gchar *filename)
110 {
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);
116         guint      tex;
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);
123
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);
127         return tex;
128 }
129
130 int main(int argc, char **argv)
131 {
132         gtk_init(&argc, &argv);
133
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);
146
147         /* OpenGL setup */
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);
151
152         /* Load texture */
153         texl = load_tex("texls.png");
154         texr = load_tex("texrs.png");
155         tex  = load_tex("tex.png");
156
157         gtk_main();
158
159         gdk_gl_drawable_gl_end(gldrawable);
160 }