]> Pileus Git - grits/blob - examples/tess/convex.c
Switch to GDK_KEY_*
[grits] / examples / tess / convex.c
1 /*
2  * Copyright (C) 2011 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 <math.h>
19 #include <gtk/gtk.h>
20 #include <gtk/gtkgl.h>
21 #include <gdk/gdkkeysyms.h>
22 #include <GL/gl.h>
23 #include <GL/glu.h>
24
25 guint tex, texl, texr;
26
27 gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer _)
28 {
29         if (event->keyval == GDK_KEY_q)
30                 gtk_main_quit();
31         return FALSE;
32 }
33
34 gboolean on_expose(GtkWidget *drawing, GdkEventExpose *event, gpointer _)
35 {
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
49         /* Create vertexes */
50         double verts[20][3];
51         for (int i = 0; i < G_N_ELEMENTS(verts); i++) {
52                 float ang = 2*G_PI * i / G_N_ELEMENTS(verts);
53                 verts[i][0] = sin(ang) * (i%2+0.5) * 0.6;
54                 verts[i][1] = cos(ang) * (i%2+0.5) * 0.6;
55                 verts[i][2] = 0;
56         }
57
58         /* Draw raw polygon */
59         glColor4f(0.5, 0.0, 0.0, 1.0);
60         GLUtesselator *tess = gluNewTess();
61         gluTessCallback(tess, GLU_TESS_BEGIN,  glBegin);
62         gluTessCallback(tess, GLU_TESS_VERTEX, glVertex3dv);
63         gluTessCallback(tess, GLU_TESS_END,    glEnd);
64         gluTessBeginPolygon(tess, NULL);
65         gluTessBeginContour(tess);
66         for (int i = 0; i < G_N_ELEMENTS(verts); i++)
67                 gluTessVertex(tess, verts[i], verts[i]);
68         gluTessEndContour(tess);
69         gluTessEndPolygon(tess);
70         gluDeleteTess(tess);
71
72         /* Draw tesselated polygon */
73         //glColor4f(0.0, 0.0, 0.5, 1.0);
74         //glBegin(GL_POLYGON);
75         //for (int i = 0; i < G_N_ELEMENTS(verts); i++)
76         //      glVertex3dv(verts[i]);
77         //glEnd();
78
79         /* Draw outline */
80         glColor4f(0.8, 0.8, 0.8, 1.0);
81         glBegin(GL_LINE_LOOP);
82         for (int i = 0; i < G_N_ELEMENTS(verts); i++)
83                 glVertex3dv(verts[i]);
84         glEnd();
85
86         /* Flush */
87         GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
88         if (gdk_gl_drawable_is_double_buffered(gldrawable))
89                 gdk_gl_drawable_swap_buffers(gldrawable);
90         else
91                 glFlush();
92         return FALSE;
93 }
94 gboolean on_configure(GtkWidget *drawing, GdkEventConfigure *event, gpointer _)
95 {
96         glViewport(0, 0,
97                 drawing->allocation.width,
98                 drawing->allocation.height);
99         return FALSE;
100 }
101
102 int main(int argc, char **argv)
103 {
104         gtk_init(&argc, &argv);
105
106         GtkWidget   *window   = gtk_window_new(GTK_WINDOW_TOPLEVEL);
107         GtkWidget   *drawing  = gtk_drawing_area_new();
108         GdkGLConfig *glconfig = gdk_gl_config_new_by_mode((GdkGLConfigMode)(
109                         GDK_GL_MODE_RGBA   | GDK_GL_MODE_DEPTH |
110                         GDK_GL_MODE_DOUBLE | GDK_GL_MODE_ALPHA));
111         g_signal_connect(window,  "destroy",         G_CALLBACK(gtk_main_quit), NULL);
112         g_signal_connect(window,  "key-press-event", G_CALLBACK(on_key_press),  NULL);
113         g_signal_connect(drawing, "expose-event",    G_CALLBACK(on_expose),     NULL);
114         g_signal_connect(drawing, "configure-event", G_CALLBACK(on_configure),  NULL);
115         gtk_widget_set_gl_capability(drawing, glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE);
116         gtk_container_add(GTK_CONTAINER(window), drawing);
117         gtk_widget_show_all(window);
118
119         /* OpenGL setup */
120         GdkGLContext  *glcontext  = gtk_widget_get_gl_context(GTK_WIDGET(drawing));
121         GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(GTK_WIDGET(drawing));
122         gdk_gl_drawable_gl_begin(gldrawable, glcontext);
123
124         /* Go */
125         gtk_main();
126         gdk_gl_drawable_gl_end(gldrawable);
127 }