1 #!/usr/bin/env dub 2 /+ 3 dub.sdl: 4 name "snippet195" 5 dependency "dwt" path="../../../../../../" 6 libs \ 7 "atk-1.0" \ 8 "cairo" \ 9 "dl" \ 10 "fontconfig" \ 11 "gdk-x11-2.0" \ 12 "gdk_pixbuf-2.0" \ 13 "glib-2.0" \ 14 "gmodule-2.0" \ 15 "gnomeui-2" \ 16 "gnomevfs-2" \ 17 "gobject-2.0" \ 18 "gthread-2.0" \ 19 "gtk-x11-2.0" \ 20 "pango-1.0" \ 21 "pangocairo-1.0" \ 22 "X11" \ 23 "Xcomposite" \ 24 "Xcursor" \ 25 "Xdamage" \ 26 "Xext" \ 27 "Xfixes" \ 28 "Xi" \ 29 "Xinerama" \ 30 "Xrandr" \ 31 "Xrender" \ 32 "Xtst" \ 33 platform="linux" 34 +/ 35 36 /******************************************************************************* 37 * Copyright (c) 2000, 2005 IBM Corporation and others. 38 * All rights reserved. This program and the accompanying materials 39 * are made available under the terms of the Eclipse Public License v1.0 40 * which accompanies this distribution, and is available at 41 * http://www.eclipse.org/legal/epl-v10.html 42 * 43 * Contributors: 44 * IBM Corporation - initial API and implementation 45 * Port to the D Programming Language: 46 * John Reimer <terminal.node@gmail.com> 47 *******************************************************************************/ 48 49 module org.eclipse.swt.snippets.Snippet195; 50 51 /* 52 * SWT OpenGL snippet: based on snippet195.java 53 * 54 * For a list of all SWT example snippets see 55 * http://www.eclipse.org/swt/snippets/ 56 * 57 * @since 3.2 58 */ 59 60 import org.eclipse.swt.SWT; 61 62 import org.eclipse.swt.layout.FillLayout; 63 import org.eclipse.swt.widgets.Shell; 64 import org.eclipse.swt.widgets.Display; 65 import org.eclipse.swt.widgets.Event; 66 import org.eclipse.swt.widgets.Composite; 67 import org.eclipse.swt.widgets.Listener; 68 import org.eclipse.swt.graphics.Rectangle; 69 import org.eclipse.swt.opengl.GLCanvas; 70 import org.eclipse.swt.opengl.GLData; 71 72 import java.lang.all; 73 74 import derelict.opengl.gl; 75 import derelict.opengl.glu; 76 77 version(Tango){ 78 import Math = tango.math.Math; 79 } else { // Phobos 80 import Math = std.math; 81 } 82 83 void drawTorus(float r, float R, int nsides, int rings) 84 { 85 float ringDelta = 2.0f * cast(float) Math.PI / rings; 86 float sideDelta = 2.0f * cast(float) Math.PI / nsides; 87 float theta = 0.0f, cosTheta = 1.0f, sinTheta = 0.0f; 88 for (int i = rings - 1; i >= 0; i--) { 89 float theta1 = theta + ringDelta; 90 float cosTheta1 = cast(float) Math.cos(theta1); 91 float sinTheta1 = cast(float) Math.sin(theta1); 92 glBegin(GL_QUAD_STRIP); 93 float phi = 0.0f; 94 for (int j = nsides; j >= 0; j--) { 95 phi += sideDelta; 96 float cosPhi = cast(float) Math.cos(phi); 97 float sinPhi = cast(float) Math.sin(phi); 98 float dist = R + r * cosPhi; 99 glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi); 100 glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi); 101 glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi); 102 glVertex3f(cosTheta * dist, -sinTheta * dist, r * sinPhi); 103 } 104 glEnd(); 105 theta = theta1; 106 cosTheta = cosTheta1; 107 sinTheta = sinTheta1; 108 } 109 } 110 111 void main() 112 { 113 DerelictGL.load(); 114 DerelictGLU.load(); 115 116 Display display = new Display(); 117 Shell shell = new Shell(display); 118 shell.setLayout(new FillLayout()); 119 Composite comp = new Composite(shell, SWT.NONE); 120 comp.setLayout(new FillLayout()); 121 GLData data = new GLData (); 122 data.doubleBuffer = true; 123 GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data); 124 125 canvas.setCurrent(); 126 127 canvas.addListener(SWT.Resize, new class Listener { 128 public void handleEvent(Event event) { 129 Rectangle bounds = canvas.getBounds(); 130 float fAspect = cast(float) bounds.width / cast(float) bounds.height; 131 132 glViewport(0, 0, bounds.width, bounds.height); 133 glMatrixMode(GL_PROJECTION); 134 glLoadIdentity(); 135 gluPerspective(45.0f, fAspect, 0.5f, 400.0f); 136 glMatrixMode(GL_MODELVIEW); 137 glLoadIdentity(); 138 } 139 }); 140 141 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 142 glColor3f(1.0f, 0.0f, 0.0f); 143 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 144 glClearDepth(1.0); 145 glLineWidth(2); 146 glEnable(GL_DEPTH_TEST); 147 148 shell.setText("SWT/DerelictGL Example"); 149 shell.setSize(640, 480); 150 shell.open(); 151 152 display.asyncExec(new class Runnable { 153 int rot = 0; 154 public void run() { 155 if (!canvas.isDisposed()) { 156 canvas.setCurrent(); 157 158 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 159 glClearColor(.3f, .5f, .8f, 1.0f); 160 glLoadIdentity(); 161 glTranslatef(0.0f, 0.0f, -10.0f); 162 float frot = rot; 163 glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f); 164 glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f); 165 rot++; 166 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 167 glColor3f(0.9f, 0.9f, 0.9f); 168 drawTorus(1, 1.9f + (cast(float) Math.sin((0.004f * frot))), 15, 15); 169 canvas.swapBuffers(); 170 display.asyncExec(this); 171 } 172 } 173 }); 174 175 while (!shell.isDisposed()) { 176 if (!display.readAndDispatch()) 177 display.sleep(); 178 } 179 display.dispose(); 180 } 181