Mercurial > projects > dwt2
annotate org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet195.d @ 124:6f2199435268
fixed snippet 217 and OpenGL snippets
author | Denis Shelomovskij <verylonglogin.reg@gmail.com> |
---|---|
date | Thu, 11 Aug 2011 19:28:27 +0300 |
parents | 536e43f63c81 |
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 * IBM Corporation - initial API and implementation | |
10 * Port to the D Programming Language: | |
11 * John Reimer <terminal.node@gmail.com> | |
12 *******************************************************************************/ | |
13 | |
14 module org.eclipse.swt.snippets.Snippet195; | |
15 | |
16 /* | |
17 * SWT OpenGL snippet: based on snippet195.java | |
18 * | |
19 * For a list of all SWT example snippets see | |
20 * http://www.eclipse.org/swt/snippets/ | |
21 * | |
22 * @since 3.2 | |
23 */ | |
24 | |
25 import org.eclipse.swt.SWT; | |
26 | |
27 import org.eclipse.swt.layout.FillLayout; | |
28 import org.eclipse.swt.widgets.Shell; | |
29 import org.eclipse.swt.widgets.Display; | |
30 import org.eclipse.swt.widgets.Event; | |
31 import org.eclipse.swt.widgets.Composite; | |
32 import org.eclipse.swt.widgets.Listener; | |
33 import org.eclipse.swt.graphics.Rectangle; | |
34 import org.eclipse.swt.opengl.GLCanvas; | |
35 import org.eclipse.swt.opengl.GLData; | |
36 | |
124
6f2199435268
fixed snippet 217 and OpenGL snippets
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
120
diff
changeset
|
37 import java.lang.all; |
6f2199435268
fixed snippet 217 and OpenGL snippets
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
120
diff
changeset
|
38 |
28 | 39 import derelict.opengl.gl; |
40 import derelict.opengl.glu; | |
41 | |
112 | 42 version(Tango){ |
43 import Math = tango.math.Math; | |
44 } else { // Phobos | |
45 import Math = std.math; | |
46 } | |
28 | 47 |
48 void drawTorus(float r, float R, int nsides, int rings) | |
49 { | |
50 float ringDelta = 2.0f * cast(float) Math.PI / rings; | |
51 float sideDelta = 2.0f * cast(float) Math.PI / nsides; | |
52 float theta = 0.0f, cosTheta = 1.0f, sinTheta = 0.0f; | |
53 for (int i = rings - 1; i >= 0; i--) { | |
54 float theta1 = theta + ringDelta; | |
55 float cosTheta1 = cast(float) Math.cos(theta1); | |
56 float sinTheta1 = cast(float) Math.sin(theta1); | |
57 glBegin(GL_QUAD_STRIP); | |
58 float phi = 0.0f; | |
59 for (int j = nsides; j >= 0; j--) { | |
60 phi += sideDelta; | |
61 float cosPhi = cast(float) Math.cos(phi); | |
62 float sinPhi = cast(float) Math.sin(phi); | |
63 float dist = R + r * cosPhi; | |
64 glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi); | |
65 glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi); | |
66 glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi); | |
67 glVertex3f(cosTheta * dist, -sinTheta * dist, r * sinPhi); | |
68 } | |
69 glEnd(); | |
70 theta = theta1; | |
71 cosTheta = cosTheta1; | |
72 sinTheta = sinTheta1; | |
73 } | |
74 } | |
75 | |
76 void main() | |
77 { | |
78 DerelictGL.load(); | |
79 DerelictGLU.load(); | |
80 | |
81 Display display = new Display(); | |
82 Shell shell = new Shell(display); | |
83 shell.setLayout(new FillLayout()); | |
84 Composite comp = new Composite(shell, SWT.NONE); | |
85 comp.setLayout(new FillLayout()); | |
86 GLData data = new GLData (); | |
87 data.doubleBuffer = true; | |
88 GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data); | |
89 | |
90 canvas.setCurrent(); | |
91 | |
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
|
92 canvas.addListener(SWT.Resize, new class Listener { |
28 | 93 public void handleEvent(Event event) { |
94 Rectangle bounds = canvas.getBounds(); | |
95 float fAspect = cast(float) bounds.width / cast(float) bounds.height; | |
96 | |
97 glViewport(0, 0, bounds.width, bounds.height); | |
98 glMatrixMode(GL_PROJECTION); | |
99 glLoadIdentity(); | |
100 gluPerspective(45.0f, fAspect, 0.5f, 400.0f); | |
101 glMatrixMode(GL_MODELVIEW); | |
102 glLoadIdentity(); | |
103 } | |
104 }); | |
105 | |
106 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); | |
107 glColor3f(1.0f, 0.0f, 0.0f); | |
108 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); | |
109 glClearDepth(1.0); | |
110 glLineWidth(2); | |
111 glEnable(GL_DEPTH_TEST); | |
112 | |
113 shell.setText("SWT/DerelictGL Example"); | |
114 shell.setSize(640, 480); | |
115 shell.open(); | |
116 | |
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
|
117 display.asyncExec(new class Runnable { |
28 | 118 int rot = 0; |
119 public void run() { | |
120 if (!canvas.isDisposed()) { | |
121 canvas.setCurrent(); | |
122 | |
123 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
124 glClearColor(.3f, .5f, .8f, 1.0f); | |
125 glLoadIdentity(); | |
126 glTranslatef(0.0f, 0.0f, -10.0f); | |
127 float frot = rot; | |
128 glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f); | |
129 glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f); | |
130 rot++; | |
131 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); | |
132 glColor3f(0.9f, 0.9f, 0.9f); | |
133 drawTorus(1, 1.9f + (cast(float) Math.sin((0.004f * frot))), 15, 15); | |
134 canvas.swapBuffers(); | |
135 display.asyncExec(this); | |
136 } | |
137 } | |
138 }); | |
139 | |
140 while (!shell.isDisposed()) { | |
141 if (!display.readAndDispatch()) | |
142 display.sleep(); | |
143 } | |
144 display.dispose(); | |
145 } | |
146 |