alternative to assert
[gtkD.git] / gtkD / demos / gl / SimpleGL.d
blobdf0c54b79fc4e2cd7deb480421c847393969bb8f
1 /*
2 * This file is part of dui.
3 *
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.
8 *
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;
38 /**
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
50 GLfloat width;
51 GLfloat height;
53 /** need to include the mixin to add GL capabilities to this widget */
54 mixin GLCapability;
56 /**
57 * Construct a simple DrawingArea and sets the GLCapabilities
59 this()
61 super(300, 300);
62 setGLCapability(); // set the GL capabilities for this widget
65 /**
66 * put any gl initializations here
67 * returns true to consume the event
69 bit initGL()
71 resizeGL(null);
72 return true;
75 /**
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);
85 glLoadIdentity ();
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);
97 glEnd();
99 return true;
103 * This method is called when the window is resized
104 * returns true to consume the event
106 bit resizeGL(GdkEventConfigure* event = null)
108 GLfloat w;
109 GLfloat h;
111 if ( event == null )
113 w = getWidth();
114 h = getHeight();
116 else
118 w = event.width;
119 h = event.height;
122 width = w;
123 height = h;
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);
135 glLoadIdentity ();
136 gluPerspective(20, w/h, 0.1, 10);
137 glMatrixMode (GL_MODELVIEW);
139 return true;
143 private import glgdk.GLdInit;
145 void main(char[][] args)
147 GtkD.init(args);
149 GLdInit.init(null, null);
151 SimpleGL simpleGL = new SimpleGL();
152 MainWindow window = new MainWindow("Simplest OpenGL Example");
153 window.add(simpleGL);
154 window.showAll();
156 GtkD.main();