78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2005 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * Sebastian Davids - initial implementation
|
|
10 * IBM Corporation
|
|
11 * Port to the D programming language:
|
|
12 * Bill Baxter <bill@billbaxter.com>
|
|
13 *******************************************************************************/
|
|
14 module snippets.opengl.Snippet174;
|
|
15
|
|
16 /*
|
|
17 * SWT OpenGL snippet: draw a square
|
|
18 *
|
|
19 * This snippet requires the experimental org.eclipse.swt.opengl plugin, which
|
|
20 * is not included in SWT by default and should only be used with versions of
|
|
21 * SWT prior to 3.2. For information on using OpenGL in SWT see
|
|
22 * http://www.eclipse.org/swt/opengl/ .
|
|
23 *
|
|
24 * For a list of all SWT example snippets see
|
|
25 * http://www.eclipse.org/swt/snippets/
|
|
26 *
|
|
27 * @since 3.2
|
|
28 */
|
|
29 import dwt.DWT;
|
|
30 import dwt.dwthelper.Runnable;
|
|
31 import dwt.widgets.Display;
|
|
32 import dwt.widgets.Shell;
|
|
33 import dwt.graphics.Rectangle;
|
|
34 import dwt.events.ControlEvent;
|
|
35 import dwt.events.ControlAdapter;
|
|
36 import dwt.layout.FillLayout;
|
|
37 import dwt.opengl.GLCanvas;
|
|
38 import dwt.opengl.GLData;
|
|
39
|
|
40 import derelict.opengl.gl;
|
|
41 import derelict.opengl.glu;
|
|
42
|
|
43 import Math = tango.math.Math;
|
|
44
|
|
45 public static void main()
|
|
46 {
|
|
47 DerelictGL.load();
|
|
48 DerelictGLU.load();
|
|
49
|
|
50 Display display = new Display();
|
|
51 Shell shell = new Shell(display);
|
|
52 shell.setText("OpenGL in DWT");
|
|
53 shell.setLayout(new FillLayout());
|
|
54 GLData data = new GLData();
|
|
55 data.doubleBuffer = true;
|
|
56 final GLCanvas canvas = new GLCanvas(shell, DWT.NO_BACKGROUND, data);
|
|
57 canvas.addControlListener(new class ControlAdapter {
|
|
58 public void controlResized(ControlEvent e) {
|
|
59 resize(canvas);
|
|
60 }
|
|
61 });
|
|
62 init(canvas);
|
|
63 (new class Runnable {
|
|
64 public void run() {
|
|
65 if (canvas.isDisposed()) return;
|
|
66 render();
|
|
67 canvas.swapBuffers();
|
|
68 canvas.getDisplay().timerExec(15, this);
|
|
69 }
|
|
70 }).run();
|
|
71 shell.open();
|
|
72 while (!shell.isDisposed()) {
|
|
73 if (!display.readAndDispatch()) display.sleep();
|
|
74 }
|
|
75 display.dispose();
|
|
76 }
|
|
77
|
|
78 static void init(GLCanvas canvas) {
|
|
79 canvas.setCurrent();
|
|
80 resize(canvas);
|
|
81 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
|
82 glColor3f(0.0f, 0.0f, 0.0f);
|
|
83 glClearDepth(1.0f);
|
|
84 glEnable(GL_DEPTH_TEST);
|
|
85 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
|
86 }
|
|
87
|
|
88 static void render() {
|
|
89 static int rot = 0;
|
|
90 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
91 glLoadIdentity();
|
|
92 glTranslatef(0.0f, 0.0f, -6.0f);
|
|
93 glRotatef(rot++, 0,0,1);
|
|
94 rot %= 360;
|
|
95 glBegin(GL_QUADS);
|
|
96 glVertex3f(-1.0f, 1.0f, 0.0f);
|
|
97 glVertex3f(1.0f, 1.0f, 0.0f);
|
|
98 glVertex3f(1.0f, -1.0f, 0.0f);
|
|
99 glVertex3f(-1.0f, -1.0f, 0.0f);
|
|
100 glEnd();
|
|
101 }
|
|
102
|
|
103 static void resize(GLCanvas canvas) {
|
|
104 canvas.setCurrent();
|
|
105 Rectangle rect = canvas.getClientArea();
|
|
106 int width = rect.width;
|
|
107 int height = Math.max(rect.height, 1);
|
|
108 glViewport(0, 0, width, height);
|
|
109 glMatrixMode(GL_PROJECTION);
|
|
110 glLoadIdentity();
|
|
111 float aspect = cast(float) width / cast(float) height;
|
|
112 gluPerspective(45.0f, aspect, 0.5f, 400.0f);
|
|
113 glMatrixMode(GL_MODELVIEW);
|
|
114 glLoadIdentity();
|
|
115 }
|
|
116
|