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