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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
78
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
1 /*******************************************************************************
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2000, 2005 IBM Corporation and others.
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
3 * All rights reserved. This program and the accompanying materials
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
4 * are made available under the terms of the Eclipse Public License v1.0
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
5 * which accompanies this distribution, and is available at
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
6 * http://www.eclipse.org/legal/epl-v10.html
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
7 *
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
8 * Contributors:
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
9 * IBM Corporation - initial API and implementation
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
10 * Port to the D Programming Language:
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
11 * John Reimer <terminal.node@gmail.com>
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
12 *******************************************************************************/
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
13
120
1f0a7a472680 fix rm snippets from module statements.
Frank Benoit <benoit@tionex.de>
parents: 78
diff changeset
14 module opengl.Snippet195;
78
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
15
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
16 /*
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
17 * SWT OpenGL snippet: based on snippet195.java
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
18 *
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
19 * For a list of all SWT example snippets see
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
20 * http://www.eclipse.org/swt/snippets/
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
21 *
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
22 * @since 3.2
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
23 */
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
24
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
25 import dwt.DWT;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
26 import dwt.dwthelper.Runnable;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
27
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
28 import dwt.layout.FillLayout;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
29 import dwt.widgets.Shell;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
30 import dwt.widgets.Display;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
31 import dwt.widgets.Event;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
32 import dwt.widgets.Composite;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
33 import dwt.widgets.Listener;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
34 import dwt.graphics.Rectangle;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
35 import dwt.opengl.GLCanvas;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
36 import dwt.opengl.GLData;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
37
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
38 import derelict.opengl.gl;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
39 import derelict.opengl.glu;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
40
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
41 import Math = tango.math.Math;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
42
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
43 void drawTorus(float r, float R, int nsides, int rings)
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
44 {
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
45 float ringDelta = 2.0f * cast(float) Math.PI / rings;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
46 float sideDelta = 2.0f * cast(float) Math.PI / nsides;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
47 float theta = 0.0f, cosTheta = 1.0f, sinTheta = 0.0f;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
48 for (int i = rings - 1; i >= 0; i--) {
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
49 float theta1 = theta + ringDelta;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
50 float cosTheta1 = cast(float) Math.cos(theta1);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
51 float sinTheta1 = cast(float) Math.sin(theta1);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
52 glBegin(GL_QUAD_STRIP);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
53 float phi = 0.0f;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
54 for (int j = nsides; j >= 0; j--) {
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
55 phi += sideDelta;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
56 float cosPhi = cast(float) Math.cos(phi);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
57 float sinPhi = cast(float) Math.sin(phi);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
58 float dist = R + r * cosPhi;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
59 glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
60 glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
61 glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
62 glVertex3f(cosTheta * dist, -sinTheta * dist, r * sinPhi);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
63 }
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
64 glEnd();
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
65 theta = theta1;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
66 cosTheta = cosTheta1;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
67 sinTheta = sinTheta1;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
68 }
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
69 }
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
70
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
71 void main()
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
72 {
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
73 DerelictGL.load();
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
74 DerelictGLU.load();
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
75
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
76 Display display = new Display();
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
77 Shell shell = new Shell(display);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
78 shell.setLayout(new FillLayout());
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
79 Composite comp = new Composite(shell, DWT.NONE);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
80 comp.setLayout(new FillLayout());
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
81 GLData data = new GLData ();
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
82 data.doubleBuffer = true;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
83 GLCanvas canvas = new GLCanvas(comp, DWT.NONE, data);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
84
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
85 canvas.setCurrent();
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
86
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
87 canvas.addListener(DWT.Resize, new class() Listener {
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
88 public void handleEvent(Event event) {
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
89 Rectangle bounds = canvas.getBounds();
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
90 float fAspect = cast(float) bounds.width / cast(float) bounds.height;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
91
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
92 glViewport(0, 0, bounds.width, bounds.height);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
93 glMatrixMode(GL_PROJECTION);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
94 glLoadIdentity();
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
95 gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
96 glMatrixMode(GL_MODELVIEW);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
97 glLoadIdentity();
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
98 }
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
99 });
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
100
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
101 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
102 glColor3f(1.0f, 0.0f, 0.0f);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
103 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
104 glClearDepth(1.0);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
105 glLineWidth(2);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
106 glEnable(GL_DEPTH_TEST);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
107
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
108 shell.setText("DWT/DerelictGL Example");
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
109 shell.setSize(640, 480);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
110 shell.open();
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
111
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
112 display.asyncExec(new class() Runnable {
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
113 int rot = 0;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
114 public void run() {
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
115 if (!canvas.isDisposed()) {
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
116 canvas.setCurrent();
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
117
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
118 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
119 glClearColor(.3f, .5f, .8f, 1.0f);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
120 glLoadIdentity();
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
121 glTranslatef(0.0f, 0.0f, -10.0f);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
122 float frot = rot;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
123 glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
124 glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
125 rot++;
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
126 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
127 glColor3f(0.9f, 0.9f, 0.9f);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
128 drawTorus(1, 1.9f + (cast(float) Math.sin((0.004f * frot))), 15, 15);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
129 canvas.swapBuffers();
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
130 display.asyncExec(this);
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
131 }
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
132 }
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
133 });
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
134
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
135 while (!shell.isDisposed()) {
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
136 if (!display.readAndDispatch())
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
137 display.sleep();
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
138 }
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
139 display.dispose();
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
140 }
4a04b6759f98 Clean up directory names
John Reimer <terminal.node@gmail.com>
parents:
diff changeset
141