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 gdk.Event; 22 private import gtk.DrawingArea; 23 private import gtk.Main; 24 private import gtk.MainWindow; 25 private import gtk.Widget; 26 private import glgdk.GLConfig; 27 private import glgdk.GLContext; 28 private import glgdk.GLdInit; 29 private import glgdk.GLWindow; 30 private import glgtk.GLCapability; 31 private import gtkglc.glgdktypes; 32 33 private import gtkglc.gl; 34 private import gtkglc.glu; 35 36 /** 37 * This is a Simple class extending the DrawingArea widget. 38 * A really simple Demo illustrating OpenGL with GtkD 39 * It uses the GLCapability mixin to add the GL capabilities to the widget. 40 * This example is provided under the terms of the GPL License. 41 * Note the initialization of the GLCapabilities on the constructor. 42 * 43 * @author pac@tuxfamily.org 44 */ 45 class SimpleGL : DrawingArea 46 { 47 48 GLfloat width; 49 GLfloat height; 50 51 /** need to include the mixin to add GL capabilities to this widget */ 52 mixin GLCapability; 53 54 /** 55 * Construct a simple DrawingArea and sets the GLCapabilities 56 */ 57 this() 58 { 59 super(300, 300); 60 setGlCapability(); // set the GL capabilities for this widget 61 } 62 63 /** 64 * put any gl initializations here 65 * returns true to consume the event 66 */ 67 void initGL() 68 { 69 resizeGL(null); 70 } 71 72 /** 73 * This method is called every time the window must be paint or repaint 74 * This is where you put the OpenGL call to draw something. 75 * This method call be called directly by the application without an event object 76 * to force redrawing of the scene. 77 * returns true to consume the event 78 */ 79 bool drawGL() 80 { 81 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 82 glLoadIdentity (); 83 84 gluLookAt(0, 0, 10, 0, 0, 0, 0, 1,0); //Set the camera position 85 86 //Just Draw a tri-colored triangle 87 glBegin(GL_TRIANGLES); 88 glColor3f(1.0f,0.0f,0.0f); 89 glVertex3f( 0.0f, 1.0f, 0.0f); 90 glColor3f(0.0f,1.0f,0.0f); 91 glVertex3f(-1.0f,-1.0f, 0.0f); 92 glColor3f(0.0f,0.0f,1.0f); 93 glVertex3f( 1.0f,-1.0f, 0.0f); 94 glEnd(); 95 96 return true; 97 } 98 99 /** 100 * This method is called when the window is resized 101 * returns true to consume the event 102 */ 103 bool resizeGL(Event event = null) 104 { 105 GLfloat w; 106 GLfloat h; 107 108 if ( event is null || event.type != GdkEventType.CONFIGURE ) 109 { 110 w = getWidth(); 111 h = getHeight(); 112 } 113 else 114 { 115 w = event.configure.width; 116 h = event.configure.height; 117 } 118 119 width = w; 120 height = h; 121 122 glViewport (0, 0, cast(int)w, cast(int)h); //Adjust the viewport according to new window dimensions 123 124 /* 125 * Update the projection Matrix accoding to the new dimension 126 * and reset the OpenGL state to MODELVIEW 127 */ 128 glMatrixMode (GL_PROJECTION); 129 glLoadIdentity (); 130 gluPerspective(20, w/h, 0.1, 10); 131 glMatrixMode (GL_MODELVIEW); 132 133 return true; 134 } 135 } 136 137 void main(string[] args) 138 { 139 Main.init(args); 140 141 GLdInit.init(args); 142 143 SimpleGL simpleGL = new SimpleGL(); 144 MainWindow window = new MainWindow("Simplest OpenGL Example"); 145 window.add(simpleGL); 146 window.showAll(); 147 148 Main.run(); 149 }