28
|
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 org.eclipse.swt.snippets.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 org.eclipse.swt.SWT;
|
|
30 import org.eclipse.swt.widgets.Display;
|
|
31 import org.eclipse.swt.widgets.Shell;
|
|
32 import org.eclipse.swt.graphics.Rectangle;
|
|
33 import org.eclipse.swt.events.ControlEvent;
|
|
34 import org.eclipse.swt.events.ControlAdapter;
|
|
35 import org.eclipse.swt.layout.FillLayout;
|
|
36 import org.eclipse.swt.opengl.GLCanvas;
|
|
37 import org.eclipse.swt.opengl.GLData;
|
|
38
|
|
39 import derelict.opengl.gl;
|
|
40 import derelict.opengl.glu;
|
|
41
|
|
42 import Math = tango.math.Math;
|
|
43
|
|
44 public static void main()
|
|
45 {
|
|
46 DerelictGL.load();
|
|
47 DerelictGLU.load();
|
|
48
|
|
49 Display display = new Display();
|
|
50 Shell shell = new Shell(display);
|
|
51 shell.setText("OpenGL in SWT");
|
|
52 shell.setLayout(new FillLayout());
|
|
53 GLData data = new GLData();
|
|
54 data.doubleBuffer = true;
|
|
55 final GLCanvas canvas = new GLCanvas(shell, SWT.NO_BACKGROUND, data);
|
|
56 canvas.addControlListener(new class ControlAdapter {
|
|
57 public void controlResized(ControlEvent e) {
|
|
58 resize(canvas);
|
|
59 }
|
|
60 });
|
|
61 init(canvas);
|
|
62 (new class Runnable {
|
|
63 public void run() {
|
|
64 if (canvas.isDisposed()) return;
|
|
65 render();
|
|
66 canvas.swapBuffers();
|
|
67 canvas.getDisplay().timerExec(15, this);
|
|
68 }
|
|
69 }).run();
|
|
70 shell.open();
|
|
71 while (!shell.isDisposed()) {
|
|
72 if (!display.readAndDispatch()) display.sleep();
|
|
73 }
|
|
74 display.dispose();
|
|
75 }
|
|
76
|
|
77 static void init(GLCanvas canvas) {
|
|
78 canvas.setCurrent();
|
|
79 resize(canvas);
|
|
80 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
|
81 glColor3f(0.0f, 0.0f, 0.0f);
|
|
82 glClearDepth(1.0f);
|
|
83 glEnable(GL_DEPTH_TEST);
|
|
84 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
|
85 }
|
|
86
|
|
87 static void render() {
|
|
88 static int rot = 0;
|
|
89 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
90 glLoadIdentity();
|
|
91 glTranslatef(0.0f, 0.0f, -6.0f);
|
|
92 glRotatef(rot++, 0,0,1);
|
|
93 rot %= 360;
|
|
94 glBegin(GL_QUADS);
|
|
95 glVertex3f(-1.0f, 1.0f, 0.0f);
|
|
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 glEnd();
|
|
100 }
|
|
101
|
|
102 static void resize(GLCanvas canvas) {
|
|
103 canvas.setCurrent();
|
|
104 Rectangle rect = canvas.getClientArea();
|
|
105 int width = rect.width;
|
|
106 int height = Math.max(rect.height, 1);
|
|
107 glViewport(0, 0, width, height);
|
|
108 glMatrixMode(GL_PROJECTION);
|
|
109 glLoadIdentity();
|
|
110 float aspect = cast(float) width / cast(float) height;
|
|
111 gluPerspective(45.0f, aspect, 0.5f, 400.0f);
|
|
112 glMatrixMode(GL_MODELVIEW);
|
|
113 glLoadIdentity();
|
|
114 }
|
|
115
|