File: glpixmap.c

package info (click to toggle)
gtkgl2 2.1.0-1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 448 kB
  • sloc: ansic: 3,004; makefile: 67; sh: 5
file content (108 lines) | stat: -rw-r--r-- 2,969 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (C) 1998 Janne Löf <jlof@mail.student.oulu.fi>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include <glib.h>		/* For G_OS_WIN32 */

#include <math.h>
#include <gtk/gtk.h>
#ifdef G_OS_WIN32
#include <windows.h>
#endif
#include <GL/gl.h>

#include <gdk/gdk.h>
#include <gtkgl/gdkgl.h>


int visual_attributes[] = { GDK_GL_RGBA,
			    GDK_GL_RED_SIZE,1,
			    GDK_GL_GREEN_SIZE,1,
			    GDK_GL_BLUE_SIZE,1,
			    GDK_GL_NONE };


int main(int argc, char **argv)
{
  GtkWidget *window,*pixmapwidget;
  GdkVisual *visual;
  GdkPixmap *pixmap;
  GdkGLPixmap *glpixmap;
  GdkGLContext *context;

  gtk_init(&argc, &argv);

  /* check opengl */
  if (gdk_gl_query() == FALSE) {
    g_print("OpenGL not supported\n");
    return 0;
  }

  /* select and use visual as default so all widgets are OpenGL renderable */
  visual = gdk_gl_choose_visual(visual_attributes);
  if (visual == NULL) {
    g_print("Can't get visual\n");
    return 0;
  }
  gtk_widget_set_default_colormap(gdk_colormap_new(visual, TRUE));
  gtk_widget_set_default_visual(visual);

  /* top level window. */
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "glpixmap");
  gtk_container_set_border_width(GTK_CONTAINER(window), 10);
  gtk_signal_connect(GTK_OBJECT(window), "delete_event",
		     GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
  gtk_widget_set_usize(window, 100,100);
  gtk_widget_show(window);

  /* pixmap */
  context  = gdk_gl_context_new(visual);
  pixmap = gdk_pixmap_new(NULL, 80,80, gdk_visual_get_depth (visual));
  glpixmap = gdk_gl_pixmap_new(visual, pixmap);
  if (gdk_gl_pixmap_make_current(glpixmap, context)) {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,100,100,0,-1,1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glClearColor(0,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1,1,1);
    glBegin(GL_TRIANGLES);
    glVertex2f(10,10);
    glVertex2f(10,90);
    glVertex2f(90,90);
    glEnd();
  }
  gdk_gl_context_unref(context);
  gdk_gl_pixmap_unref(glpixmap);


  /* pixmapwidget */
  pixmapwidget = gtk_pixmap_new( pixmap, NULL );
  gdk_pixmap_unref(pixmap);

  gtk_container_add(GTK_CONTAINER(window), pixmapwidget);
  gtk_widget_show(pixmapwidget);

  gtk_main();

  return 0;
}