]> Pileus Git - grits/blob - examples/sorting/tes.c
Update copyright and email address
[grits] / examples / sorting / tes.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 #include <stdlib.h>
24
25 static gboolean on_expose(GtkWidget *drawing, GdkEventExpose *event, gpointer _)
26 {
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);
30
31         /* No sorting, just add */
32         glEnable(GL_BLEND);
33         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
34         //glBlendFunc(GL_ONE, GL_ONE);
35
36         /* Lighting */
37         glMatrixMode(GL_MODELVIEW);
38         glLoadIdentity();
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);
47         glEnable(GL_LIGHT0);
48
49         /* Projection */
50         glMatrixMode(GL_PROJECTION);
51         glLoadIdentity();
52         glFrustum(-1,1, -1,1, 2,10);
53
54         glMatrixMode(GL_MODELVIEW);
55         glLoadIdentity();
56         gluLookAt(0,2,-3, 0,0,0, 0,1,0);
57
58         /* Draw polys */
59         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
60
61         glColor4f(1, 0, 0, 0.5);
62         glBegin(GL_TRIANGLE_STRIP);
63         glVertex3i(-1,  1,  1);
64         glVertex3i(-1, -1,  1);
65         glVertex3i( 1,  1, -1);
66         glVertex3i( 1, -1, -1);
67         glEnd();
68
69         glColor4f(0, 1, 0, 0.5);
70         glBegin(GL_TRIANGLE_STRIP);
71         glVertex3i( 1,  1,  1);
72         glVertex3i( 1, -1,  1);
73         glVertex3i(-1,  1, -1);
74         glVertex3i(-1, -1, -1);
75         glEnd();
76
77         /* Flush */
78         GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
79         if (gdk_gl_drawable_is_double_buffered(gldrawable))
80                 gdk_gl_drawable_swap_buffers(gldrawable);
81         else
82                 glFlush();
83         return FALSE;
84 }
85
86 static gboolean on_configure(GtkWidget *drawing, GdkEventConfigure *event, gpointer _)
87 {
88         glViewport(0, 0,
89                 drawing->allocation.width,
90                 drawing->allocation.height);
91         return FALSE;
92 }
93
94 static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer _)
95 {
96         if (event->keyval == GDK_q)
97                 gtk_main_quit();
98         return FALSE;
99 }
100
101 int main(int argc, char **argv)
102 {
103         gtk_init(&argc, &argv);
104
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);
117
118         /* OpenGL setup */
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);
122
123         gtk_main();
124
125         gdk_gl_drawable_gl_end(gldrawable);
126 }