using GLib using Gtk using GL using GLU public class Sample GLib Objec

  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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using GLib;
using Gtk;
using GL;
using GLU;
public class Sample : GLib.Object
{
static GLfloat[] light_diffuse;
static GLfloat[] light_position;
private static void realize (Widget widget)
{
Gdk.GL.Context glcontext = Gtk.GL.GtkWidgetExtension.get_gl_context (widget);
Gdk.GL.Drawable gldrawable = Gtk.GL.GtkWidgetExtension.get_gl_drawable (widget);
Quadric qobj;
if (!(bool)gldrawable.gl_begin (glcontext))
return;
qobj = new Quadric ();
qobj.QuadricDrawStyle (GLU.Consts.FILL);
GL.NewList (1, GL.Consts.COMPILE);
qobj.Sphere (1.0f, 20, 30);
GL.EndList ();
GL.Lightfv (GL.Consts.LIGHT0, GL.Consts.DIFFUSE, light_diffuse);
GL.Lightfv (GL.Consts.LIGHT0, GL.Consts.POSITION, light_position);
GL.Enable (GL.Consts.LIGHTING);
GL.Enable (GL.Consts.LIGHT0);
GL.Enable (GL.Consts.DEPTH_TEST);
GL.ClearColor (1.0f, 1.0f, 1.0f, 1.0f);
GL.ClearDepth (1.0f);
GL.Viewport (0, 0, (GLsizei)widget.allocation.width, (GLsizei)widget.allocation.height);
GL.MatrixMode (GL.Consts.PROJECTION);
GL.LoadIdentity ();
GLU.Perspective (40.0f, 1.0f, 1.0f, 10.0f);
GL.MatrixMode (GL.Consts.MODELVIEW);
GL.LoadIdentity ();
GLU.LookAt (0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
GL.Translatef (0.0f, 0.0f, -3.0f);
gldrawable.gl_end ();
}
private static bool configure_event (Widget widget, Gdk.Event event)
{
Gdk.GL.Context glcontext = Gtk.GL.GtkWidgetExtension.get_gl_context (widget);
Gdk.GL.Drawable gldrawable = Gtk.GL.GtkWidgetExtension.get_gl_drawable (widget);
if (!(bool)gldrawable.gl_begin (glcontext))
return false;
GL.Viewport (0, 0, (GLsizei)widget.allocation.width, (GLsizei)widget.allocation.height);
gldrawable.gl_end ();
return true;
}
private static bool expose_event (Widget widget, Gdk.Event event)
{
Gdk.GL.Context glcontext = Gtk.GL.GtkWidgetExtension.get_gl_context (widget);
Gdk.GL.Drawable gldrawable = Gtk.GL.GtkWidgetExtension.get_gl_drawable (widget);
if (!(bool)gldrawable.gl_begin (glcontext))
return false;
GL.Clear (GL.Consts.COLOR_BUFFER_BIT | GL.Consts.DEPTH_BUFFER_BIT);
GL.CallList (1);
if (gldrawable.is_double_buffered ())
gldrawable.swap_buffers ();
else
GL.Flush ();
gldrawable.gl_end ();
return true;
}
public static void main (string[] args)
{
Gdk.GL.Config glconfig;
int major, minor;
Window window;
VBox vbox;
DrawingArea drawing_area;
Button button;
Gtk.init (ref args);
Gtk.GL.init (ref args);
Gdk.GL.query_version (out major, out minor);
print ("\nOpenGL extension version - %d.%d\n", major, minor);
glconfig = new Gdk.GL.Config.by_mode (Gdk.GL.ConfigMode.RGB | Gdk.GL.ConfigMode.DEPTH | Gdk.GL.ConfigMode.DOUBLE);
if (glconfig == null)
{
print ("*** Cannot find the double-buffered visual.\n");
print ("*** Trying single-buffered visual.\n");
glconfig = new Gdk.GL.Config.by_mode (Gdk.GL.ConfigMode.RGB | Gdk.GL.ConfigMode.DEPTH);
if (glconfig == null)
{
print ("*** No appropriate OpenGL-capable visual found.\n");
return;
}
}
window = new Window (Gtk.WindowType.TOPLEVEL);
window.title = "simple";
window.set_reallocate_redraws (true);
window.destroy += Gtk.main_quit;
vbox = new VBox (false, 0);
window.add (vbox);
vbox.show ();
light_diffuse = new GLfloat[] { 1.0f, 0.0f, 0.0f, 1.0f };
light_position = new GLfloat[] { 1.0f, 1.0f, 1.0f, 0.0f };
drawing_area = new DrawingArea ();
drawing_area.set_size_request (200, 200);
Gtk.GL.GtkWidgetExtension.set_gl_capability (drawing_area, glconfig, null, true, Gdk.GL.RenderType.RGBA_TYPE);
drawing_area.realize += realize;
drawing_area.configure_event += configure_event;
drawing_area.expose_event += expose_event;
vbox.pack_start (drawing_area, true, true, 0);
drawing_area.show ();
button = new Button.with_label ("Quit");
button.clicked += Gtk.main_quit;
vbox.pack_start (button, false, false, 0);
button.show ();
window.show ();
Gtk.main ();
}
}