DUI version 00.07 |
DUI OpenGL support (Linux only) is implemented with a binding on GtkGlExt
OpenGL support is still experimental but due to the simplicity of the GtkGlExt API few changes should be needed to the final version. DUI OpenGL is tested on:
Here is a picture generate through DUI
and the code
/* * button.c: * Simple toggle button example. * * written by Naofumi Yasufuku <naofumi@users.sourceforge.net> * adapted by Antonio Monteiro to the DUI toolkit <duitoolkit@yahoo.ca> * this example is released under GPL license */ /* * This file is part of dui. * * dui is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * dui is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with dui; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ module testGL.TestGL; import dui.All; import ggl.All; private import std.math; /** * A GL toggle button */ class GLToggleButton : ToggleButton , RealizeListener, MapListener, ExposeListener, ConfigureListener, VisibilityListener, IdleListener, ButtonClickedListener { bit animate = true; GLfloat angle = 0.0; GLfloat pos_y = 0.0; int idleID; DrawingArea da; this(GLConfig glconfig) { VBox vbox = new VBox(false,0); vbox.setBorderWidth(10); da = new DrawingArea(); da.setSizeRequest(200,200); GLWidget.setGLCapability(da,glconfig,null,true,GLRenderType.RGBA_TYPE); vbox.packStart(da,true,true,0); /* * Label. */ Label label = new Label("Toggle Animation"); vbox.packStart(label,false,false,10); /* Add VBox. */ add(vbox); Dispatcher dispatcher = Dispatcher.getDispatcher(); dispatcher.addRealizeListener(this,da); dispatcher.addExposeListener(this,da); dispatcher.addConfigureListener(this,da); dispatcher.addMapListener(this,da); dispatcher.addVisibilityListener(this,da); dispatcher.addButtonClickedListener(this,this,"ToggleAnimation"); } bit idleCallback() { angle += 1.0; if (angle >= 360.0) { angle -= 360.0; } pos_y = std.math.sin(angle * std.math.PI / 180.0); da.draw(); return true; } bit exposeCallback(Widget widget) { GLContext context = GLWidget.getGLContext(widget); GLDrawable drawable = GLWidget.getGLDrawable(widget); /* brass */ static GLfloat ambient[4] = [ 0.329412, 0.223529, 0.027451, 1.0 ]; static GLfloat diffuse[4] = [ 0.780392, 0.568627, 0.113725, 1.0 ]; static GLfloat specular[4] = [ 0.992157, 0.941176, 0.807843, 1.0 ]; static GLfloat shininess = 0.21794872 * 128.0; /*** OpenGL BEGIN ***/ if ( !drawable.begin(context) ) { return false; } glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity (); glTranslatef (0.0, 0.0, -10.0); glPushMatrix (); glTranslatef (0.0, pos_y, 0.0); glRotatef (angle, 0.0, 1.0, 0.0); glMaterialfv (GL_FRONT, GL_AMBIENT, ambient); glMaterialfv (GL_FRONT, GL_DIFFUSE, diffuse); glMaterialfv (GL_FRONT, GL_SPECULAR, specular); glMaterialf (GL_FRONT, GL_SHININESS, shininess); //GLDraw.torus(true, 0.3, 0.6, 30, 30); GLDraw.teapot(true, 1.1); glPopMatrix (); if ( drawable.isDoubleBuffered() ) drawable.swapBuffers(); else glFlush (); drawable.end(); /*** OpenGL END ***/ return true; } bit noExposeCallback(Widget widget){return false;} bit configureCallback(Widget widget, EventConfigure e) { GLDrawable drawable = GLWidget.getGLDrawable(widget); GLContext context = GLWidget.getGLContext(widget); GLfloat w = widget.getWidth(); GLfloat h = widget.getHeight(); GLfloat aspect; /*** OpenGL BEGIN ***/ if ( !drawable.begin(context) ) { return false; } glViewport (0, 0, w, h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); if (w > h) { aspect = w / h; glFrustum (-aspect, aspect, -1.0, 1.0, 5.0, 60.0); } else { aspect = h / w; glFrustum (-1.0, 1.0, -aspect, aspect, 5.0, 60.0); } glMatrixMode (GL_MODELVIEW); drawable.end(); /*** OpenGL END ***/ return true; } void addIdle() { if ( idleID == 0 ) { idleID = Dispatcher.getDispatcher().addIdleListener(this); } } void removeIdle() { if ( idleID != 0 ) { Dispatcher.getDispatcher().removeIdleListener(idleID); idleID = 0; } } bit mapCallback(Drawable drawable) { if (animate) { addIdle(); } return true; } bit unmapCallback(Drawable drawable) { removeIdle(); return true; } void buttonClickedCallback(Button button, char[] action) { animate = !animate; if (animate) { addIdle(); } else { removeIdle(); draw(); } } bit realizeCallback(Widget widget) { printf("realizeCallback\n"); GLContext context = GLWidget.getGLContext(widget); GLDrawable drawable = GLWidget.getGLDrawable(widget); static GLfloat ambient[] = [ 0.0, 0.0, 0.0, 1.0 ]; static GLfloat diffuse[] = [ 1.0, 1.0, 1.0, 1.0 ]; static GLfloat position[] = [ 1.0, 1.0, 1.0, 0.0 ]; static GLfloat lmodel_ambient[] = [0.2, 0.2, 0.2, 1.0]; static GLfloat local_view[] = [0.0]; /*** OpenGL BEGIN ***/ if (!drawable.begin(context) ) { return false; } glLightfv (GL_LIGHT0, GL_AMBIENT, ambient); glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse); glLightfv (GL_LIGHT0, GL_POSITION, position); glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); glLightModelfv (GL_LIGHT_MODEL_LOCAL_VIEWER, local_view); glEnable (GL_LIGHTING); glEnable (GL_LIGHT0); glEnable (GL_DEPTH_TEST); glClearColor (1.0, 1.0, 1.0, 1.0); glClearDepth (1.0); drawable.end(); return false; } bit unrealizeCallback(Widget widget){return false;} bit visibilityCallback(Widget widget, EventVisibility e) { printf("visibilityCallback\n"); if (animate) { if (e.getState() == VisibilityState.FULLY_OBSCURED) { removeIdle(); } else { addIdle(); } } return true; } } public: class TestGL : MainWindow { this(DUI dui) { super("DUI test GL"); setReallocateRedraws(true); setBorderWidth(10); show(); } } void main(char [][]args) { DUI dui; int argc = args.length; for ( int i=0 ; i<args.length ; i++ ) { printf("arg %d = %.*s\n\0",i,args[i]); } dui = DUI.dui(args); int argc1 = 0; GL.init(&argc1, null); /* * Configure OpenGL-capable visual. */ /* Try double-buffered visual */ GLConfig configgl = new GLConfig(GLConfigMode.RGB | GLConfigMode.DEPTH | GLConfigMode.DOUBLE); if (configgl.ggl() === null) { printf ("*** Cannot find the double-buffered visual.\n"); printf ("*** Trying single-buffered visual.\n"); /* Try single-buffered visual */ configgl = new GLConfig(GLConfigMode.RGB | GLConfigMode.DEPTH); if (configgl.ggl() === null ) { printf ("*** No appropriate OpenGL-capable visual found.\n"); } } if ( configgl.ggl() !== null ) { Dispatcher.getDispatcher(); TestGL testGL = new TestGL(dui); testGL.add(new GLToggleButton(configgl)); //dui.thread.wait(); dui.go(); } }
|
|