2 * This file is part of dui.
4 * dui is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * dui 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 Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with dui; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 module simpleGL
.SimpleGL
;
21 private import gtk
.DrawingArea
;
22 private import glgtk
.GLCapability
;
23 private import glgdk
.GLDrawable
;
24 private import glgdk
.GLConfig
;
25 private import glgdk
.GLContext
;
26 private import gtkglc
.glgdktypes
;
28 private import gtkglc
.gl
;
29 private import gtkglc
.glu
;
31 private import gdk
.Event
;
33 private import gtk
.Widget
;
35 private import gtk
.GtkD
;
36 private import gtk
.MainWindow
;
39 * This is a Simple class extending the DrawingArea widget.
40 * A really simple Demo illustrating OpenGL with DUI
41 * It uses the new GLCapability mixin to add the GL capabilities to the widget.
42 * This example is provided under the terms of the GPL License.
43 * Note the initialization of the GLCapabilities on the constructor.
45 * @author pac@tuxfamily.org
47 class SimpleGL
: DrawingArea
53 /** need to include the mixin to add GL capabilities to this widget */
57 * Construct a simple DrawingArea and sets the GLCapabilities
62 setGLCapability(); // set the GL capabilities for this widget
66 * put any gl initializations here
67 * returns true to consume the event
76 * This method is called every time the window must be paint or repaint
77 * This is where you put the OpenGL call to draw something.
78 * This method call be called directly by the application without an event object
79 * to force redrawing of the scene.
80 * returns true to consume the event
82 bit
drawGL(GdkEventExpose
* event
= null)
84 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
);
87 gluLookAt(0, 0, 10, 0, 0, 0, 0, 1,0); //Set the camera position
89 //Just Draw a tri-colored triangle
90 glBegin(GL_TRIANGLES
);
91 glColor3f(1.0f,0.0f,0.0f);
92 glVertex3f( 0.0f, 1.0f, 0.0f);
93 glColor3f(0.0f,1.0f,0.0f);
94 glVertex3f(-1.0f,-1.0f, 0.0f);
95 glColor3f(0.0f,0.0f,1.0f);
96 glVertex3f( 1.0f,-1.0f, 0.0f);
103 * This method is called when the window is resized
104 * returns true to consume the event
106 bit
resizeGL(GdkEventConfigure
* event
= null)
126 //writefln("SimpleGL.resizeGL %s %s", w, h);
128 glViewport (0, 0, cast(int)w
, cast(int)h
); //Adjust the viewport according to new window dimensions
131 * Update the projection Matrix accoding to the new dimension
132 * and reset the OpenGL state to MODELVIEW
134 glMatrixMode (GL_PROJECTION
);
136 gluPerspective(20, w
/h
, 0.1, 10);
137 glMatrixMode (GL_MODELVIEW
);
143 private import glgdk
.GLdInit
;
145 void main(char[][] args
)
149 GLdInit
.init(null, null);
151 SimpleGL simpleGL
= new SimpleGL();
152 MainWindow window
= new MainWindow("Simplest OpenGL Example");
153 window
.add(simpleGL
);