1 /*
2  * Copyright (C) 1998 Janne Löf <jlof@mail.student.oulu.fi>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 
20 #include <glib.h>		/* For G_OS_WIN32 */
21 
22 #include <math.h>
23 #include <gtk/gtk.h>
24 #ifdef G_OS_WIN32
25 #include <windows.h>
26 #endif
27 #include <GL/gl.h>
28 #include <GL/glu.h>
29 
30 #include <gdk/gdk.h>
31 #include <gtkgl/gdkgl.h>
32 
33 
34 int visual_attributes[] = { GDK_GL_RGBA,
35 			    GDK_GL_RED_SIZE,1,
36 			    GDK_GL_GREEN_SIZE,1,
37 			    GDK_GL_BLUE_SIZE,1,
38 			    GDK_GL_NONE };
39 
40 
main(int argc,char ** argv)41 int main(int argc, char **argv)
42 {
43   GtkWidget *window,*pixmapwidget;
44   GdkVisual *visual;
45   GdkPixmap *pixmap;
46   GdkGLPixmap *glpixmap;
47   GdkGLContext *context;
48 
49   gtk_init(&argc, &argv);
50 
51   /* check opengl */
52   if (gdk_gl_query() == FALSE) {
53     g_print("OpenGL not supported\n");
54     return 0;
55   }
56 
57   /* select and use visual as default so all widgets are OpenGL renderable */
58   visual = gdk_gl_choose_visual(visual_attributes);
59   if (visual == NULL) {
60     g_print("Can't get visual\n");
61     return 0;
62   }
63   gtk_widget_set_default_colormap(gdk_colormap_new(visual, TRUE));
64   gtk_widget_set_default_visual(visual);
65 
66   /* top level window. */
67   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
68   gtk_window_set_title(GTK_WINDOW(window), "glpixmap");
69   gtk_container_set_border_width(GTK_CONTAINER(window), 10);
70   gtk_signal_connect(GTK_OBJECT(window), "delete_event",
71 		     GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
72   gtk_widget_set_usize(window, 100,100);
73   gtk_widget_show(window);
74 
75   /* pixmap */
76   context  = gdk_gl_context_new(visual);
77   pixmap = gdk_pixmap_new(NULL, 80,80, visual->depth);
78   glpixmap = gdk_gl_pixmap_new(visual, pixmap);
79   if (gdk_gl_pixmap_make_current(glpixmap, context)) {
80     glMatrixMode(GL_PROJECTION);
81     glLoadIdentity();
82     gluOrtho2D(0,100,100,0);
83     glMatrixMode(GL_MODELVIEW);
84     glLoadIdentity();
85 
86     glClearColor(0,0,0,1);
87     glClear(GL_COLOR_BUFFER_BIT);
88     glColor3f(1,1,1);
89     glBegin(GL_TRIANGLES);
90     glVertex2f(10,10);
91     glVertex2f(10,90);
92     glVertex2f(90,90);
93     glEnd();
94   }
95   gdk_gl_context_unref(context);
96   gdk_gl_pixmap_unref(glpixmap);
97 
98 
99   /* pixmapwidget */
100   pixmapwidget = gtk_pixmap_new( pixmap, NULL );
101   gdk_pixmap_unref(pixmap);
102 
103   gtk_container_add(GTK_CONTAINER(window), pixmapwidget);
104   gtk_widget_show(pixmapwidget);
105 
106   gtk_main();
107 
108   return 0;
109 }
110