Mercurial > projects > dwt2
annotate org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet174.d @ 125:c43718956f21 default tip
Updated the snippets status.
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Thu, 11 Aug 2011 19:55:14 +0200 |
parents | 6f2199435268 |
children |
rev | line source |
---|---|
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 | |
124
6f2199435268
fixed snippet 217 and OpenGL snippets
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
120
diff
changeset
|
39 import java.lang.all; |
6f2199435268
fixed snippet 217 and OpenGL snippets
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
120
diff
changeset
|
40 |
28 | 41 import derelict.opengl.gl; |
42 import derelict.opengl.glu; | |
43 | |
112 | 44 version(Tango){ |
45 import Math = tango.math.Math; | |
46 } else { // Phobos | |
47 import Math = std.algorithm; | |
48 } | |
28 | 49 |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
112
diff
changeset
|
50 void main() |
28 | 51 { |
52 DerelictGL.load(); | |
53 DerelictGLU.load(); | |
54 | |
55 Display display = new Display(); | |
56 Shell shell = new Shell(display); | |
57 shell.setText("OpenGL in SWT"); | |
58 shell.setLayout(new FillLayout()); | |
59 GLData data = new GLData(); | |
60 data.doubleBuffer = true; | |
112 | 61 GLCanvas canvas = new GLCanvas(shell, SWT.NO_BACKGROUND, data); |
28 | 62 canvas.addControlListener(new class ControlAdapter { |
63 public void controlResized(ControlEvent e) { | |
64 resize(canvas); | |
65 } | |
66 }); | |
67 init(canvas); | |
68 (new class Runnable { | |
69 public void run() { | |
70 if (canvas.isDisposed()) return; | |
71 render(); | |
72 canvas.swapBuffers(); | |
73 canvas.getDisplay().timerExec(15, this); | |
74 } | |
75 }).run(); | |
76 shell.open(); | |
77 while (!shell.isDisposed()) { | |
78 if (!display.readAndDispatch()) display.sleep(); | |
79 } | |
80 display.dispose(); | |
81 } | |
82 | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
112
diff
changeset
|
83 void init(GLCanvas canvas) { |
28 | 84 canvas.setCurrent(); |
85 resize(canvas); | |
86 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); | |
87 glColor3f(0.0f, 0.0f, 0.0f); | |
88 glClearDepth(1.0f); | |
89 glEnable(GL_DEPTH_TEST); | |
90 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); | |
91 } | |
92 | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
112
diff
changeset
|
93 void render() { |
28 | 94 static int rot = 0; |
95 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
96 glLoadIdentity(); | |
97 glTranslatef(0.0f, 0.0f, -6.0f); | |
98 glRotatef(rot++, 0,0,1); | |
99 rot %= 360; | |
100 glBegin(GL_QUADS); | |
101 glVertex3f(-1.0f, 1.0f, 0.0f); | |
102 glVertex3f(1.0f, 1.0f, 0.0f); | |
103 glVertex3f(1.0f, -1.0f, 0.0f); | |
104 glVertex3f(-1.0f, -1.0f, 0.0f); | |
105 glEnd(); | |
106 } | |
107 | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
112
diff
changeset
|
108 void resize(GLCanvas canvas) { |
28 | 109 canvas.setCurrent(); |
110 Rectangle rect = canvas.getClientArea(); | |
111 int width = rect.width; | |
112 int height = Math.max(rect.height, 1); | |
113 glViewport(0, 0, width, height); | |
114 glMatrixMode(GL_PROJECTION); | |
115 glLoadIdentity(); | |
116 float aspect = cast(float) width / cast(float) height; | |
117 gluPerspective(45.0f, aspect, 0.5f, 400.0f); | |
118 glMatrixMode(GL_MODELVIEW); | |
119 glLoadIdentity(); | |
120 } | |
121 |