1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet174"
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  *     Sebastian Davids - initial implementation
45  *     IBM Corporation
46  * Port to the D programming language:
47  *     Bill Baxter <bill@billbaxter.com>
48  *******************************************************************************/
49 module org.eclipse.swt.snippets.Snippet174;
50 
51 /*
52  * SWT OpenGL snippet: draw a square
53  * 
54  * This snippet requires the experimental org.eclipse.swt.opengl plugin, which
55  * is not included in SWT by default and should only be used with versions of
56  * SWT prior to 3.2.  For information on using OpenGL in SWT see
57  * http://www.eclipse.org/swt/opengl/ .
58  * 
59  * For a list of all SWT example snippets see
60  * http://www.eclipse.org/swt/snippets/
61  * 
62  * @since 3.2
63  */
64 import org.eclipse.swt.SWT;
65 import org.eclipse.swt.widgets.Display;
66 import org.eclipse.swt.widgets.Shell;
67 import org.eclipse.swt.graphics.Rectangle;
68 import org.eclipse.swt.events.ControlEvent;
69 import org.eclipse.swt.events.ControlAdapter;
70 import org.eclipse.swt.layout.FillLayout;
71 import org.eclipse.swt.opengl.GLCanvas;
72 import org.eclipse.swt.opengl.GLData;
73 
74 import java.lang.all;
75 
76 import derelict.opengl.gl;
77 import derelict.opengl.glu;
78 
79 version(Tango){
80     import Math = tango.math.Math;
81 } else { // Phobos
82     import Math = std.algorithm;
83 }
84 
85 void main() 
86 {
87     DerelictGL.load();
88     DerelictGLU.load();
89 
90     Display display = new Display();
91     Shell shell = new Shell(display);
92     shell.setText("OpenGL in SWT");
93     shell.setLayout(new FillLayout());
94     GLData data = new GLData();
95     data.doubleBuffer = true;
96     GLCanvas canvas = new GLCanvas(shell, SWT.NO_BACKGROUND, data);
97     canvas.addControlListener(new class ControlAdapter {
98         public void controlResized(ControlEvent e) {
99             resize(canvas);
100         }
101     });
102     init(canvas);
103     (new class Runnable {
104         public void run() {
105             if (canvas.isDisposed()) return;
106             render();
107             canvas.swapBuffers();
108             canvas.getDisplay().timerExec(15, this);
109         }
110     }).run();
111     shell.open();
112     while (!shell.isDisposed()) {
113         if (!display.readAndDispatch()) display.sleep();
114     }
115     display.dispose();
116 }
117 
118 void init(GLCanvas canvas) {
119     canvas.setCurrent();
120     resize(canvas);
121     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
122     glColor3f(0.0f, 0.0f, 0.0f);
123     glClearDepth(1.0f);
124     glEnable(GL_DEPTH_TEST);
125     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
126 }
127 
128 void render() {
129     static int rot = 0;
130     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
131     glLoadIdentity();
132     glTranslatef(0.0f, 0.0f, -6.0f);
133     glRotatef(rot++, 0,0,1);
134     rot %= 360;
135     glBegin(GL_QUADS);
136     glVertex3f(-1.0f, 1.0f, 0.0f);
137     glVertex3f(1.0f, 1.0f, 0.0f);
138     glVertex3f(1.0f, -1.0f, 0.0f);
139     glVertex3f(-1.0f, -1.0f, 0.0f);
140     glEnd();
141 }
142 
143 void resize(GLCanvas canvas) {
144     canvas.setCurrent();
145     Rectangle rect = canvas.getClientArea();
146     int width = rect.width;
147     int height = Math.max(rect.height, 1);
148     glViewport(0, 0, width, height);
149     glMatrixMode(GL_PROJECTION);
150     glLoadIdentity();
151     float aspect = cast(float) width / cast(float) height;
152     gluPerspective(45.0f, aspect, 0.5f, 400.0f);
153     glMatrixMode(GL_MODELVIEW);
154     glLoadIdentity();
155 }
156