annotate dwtsnippets/opengl/Snippet195.d @ 29:2e9b106ed4b1

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