1 /* 2 * This file is part of gtkD. 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 3 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. 13 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 17 */ 18 19 module simpleGL.SimpleGL; 20 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; 27 28 private import gtkglc.gl; 29 private import gtkglc.glu; 30 31 private import gdk.Event; 32 33 private import gtk.Widget; 34 35 private import gtk.Main; 36 private import gtk.MainWindow; 37 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. 44 * 45 * @author pac@tuxfamily.org 46 */ 47 class SimpleGL : DrawingArea 48 { 49 50 GLfloat width; 51 GLfloat height; 52 53 /** need to include the mixin to add GL capabilities to this widget */ 54 mixin GLCapability; 55 56 /** 57 * Construct a simple DrawingArea and sets the GLCapabilities 58 */ 59 this() 60 { 61 super(300, 300); 62 setGLCapability(); // set the GL capabilities for this widget 63 } 64 65 /** 66 * put any gl initializations here 67 * returns true to consume the event 68 */ 69 bool initGL() 70 { 71 resizeGL(null); 72 return true; 73 } 74 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 81 */ 82 bool drawGL(GdkEventExpose* event = null) 83 { 84 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 85 glLoadIdentity (); 86 87 gluLookAt(0, 0, 10, 0, 0, 0, 0, 1,0); //Set the camera position 88 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(); 98 99 return true; 100 } 101 102 /** 103 * This method is called when the window is resized 104 * returns true to consume the event 105 */ 106 bool resizeGL(GdkEventConfigure* event = null) 107 { 108 GLfloat w; 109 GLfloat h; 110 111 if ( event == null ) 112 { 113 w = getWidth(); 114 h = getHeight(); 115 } 116 else 117 { 118 w = event.width; 119 h = event.height; 120 } 121 122 width = w; 123 height = h; 124 125 126 //writefln("SimpleGL.resizeGL %s %s", w, h); 127 128 glViewport (0, 0, cast(int)w, cast(int)h); //Adjust the viewport according to new window dimensions 129 130 /* 131 * Update the projection Matrix accoding to the new dimension 132 * and reset the OpenGL state to MODELVIEW 133 */ 134 glMatrixMode (GL_PROJECTION); 135 glLoadIdentity (); 136 gluPerspective(20, w/h, 0.1, 10); 137 glMatrixMode (GL_MODELVIEW); 138 139 return true; 140 } 141 } 142 143 private import glgdk.GLdInit; 144 145 void main(string[] args) 146 { 147 Main.init(args); 148 149 GLdInit.init(args); 150 151 SimpleGL simpleGL = new SimpleGL(); 152 MainWindow window = new MainWindow("Simplest OpenGL Example"); 153 window.add(simpleGL); 154 window.showAll(); 155 156 Main.run(); 157 158 }