1 /*******************************************************************************
2  * Copyright (c) 2011 LWJGL Project 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, and under the terms of the
7  * BSD license, see http://lwjgl.org/license.php for details.
8  *
9  * Contributors:
10  *    Jens von Pilgrim - initial implementation
11  ******************************************************************************/
12 
13 package org.lwjgl.info;
14 
15 import org.eclipse.core.runtime.IBundleGroup;
16 import org.eclipse.core.runtime.IBundleGroupProvider;
17 import org.eclipse.core.runtime.Platform;
18 import org.eclipse.jface.action.IStatusLineManager;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Rectangle;
21 import org.eclipse.swt.opengl.GLCanvas;
22 import org.eclipse.swt.opengl.GLData;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.Event;
26 import org.eclipse.swt.widgets.Listener;
27 import org.eclipse.ui.part.ViewPart;
28 import org.lwjgl.LWJGLException;
29 import org.lwjgl.opengl.GL11;
30 import org.lwjgl.opengl.GLContext;
31 import org.lwjgl.util.glu.GLU;
32 
33 /**
34  * Simple view for testing whether LWJGL is installed correctly on your
35  * system.
36  * The example is based on
37  * <a href="http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet195.java?view=markup&content-type=text%2Fvnd.viewcvs-markup&revision=HEAD">snippet 195</a>.
38  *
39  * @author 	Jens von Pilgrim
40  * @version	$Revision$
41  * @since 	16.07.2007
42  * @headurl $HeadURL$
43  */
44 public class LWJGLTestView extends ViewPart {
45 
46 	GLCanvas canvas;
47 
48 	FpsStatusLineItem fpsstatuslineitem;
49 
getFeatureVersion(String myFeatureId)50 	static String getFeatureVersion(String myFeatureId) {
51 		IBundleGroupProvider[] providers = Platform.getBundleGroupProviders();
52 		if (providers != null) {
53 			for (int i = 0; i < providers.length; ++i) {
54 				IBundleGroup[] bundleGroups = providers[i].getBundleGroups();
55 				for (IBundleGroup bg : bundleGroups) {
56 					if (bg.getIdentifier().equals(myFeatureId)) {
57 						return bg.getVersion();
58 					}
59 				}
60 			}
61 		}
62 		return "Feature not found";
63 	}
64 
65 	/**
66 	 * {@inheritDoc}
67 	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
68 	 */
69 	@Override
createPartControl(Composite parent)70 	public void createPartControl(Composite parent) {
71 
72 		String strVersion = getFeatureVersion("org.lwjgl");
73 		this.setPartName("org.lwjgl " + strVersion);
74 
75 		IStatusLineManager statusLine = this.getViewSite().getActionBars()
76 				.getStatusLineManager();
77 
78 		fpsstatuslineitem = new FpsStatusLineItem();
79 		statusLine.add(fpsstatuslineitem);
80 
81 		GLData data = new GLData();
82 		data.doubleBuffer = true;
83 		canvas = new GLCanvas(parent, SWT.NONE, data);
84 
85 		canvas.setCurrent();
86 		try {
87 			GLContext.useContext(canvas);
88 		} catch (LWJGLException e) {
89 			e.printStackTrace();
90 		}
91 
92 		canvas.addListener(SWT.Resize, new Listener() {
93 			public void handleEvent(Event event) {
94 				Rectangle bounds = canvas.getBounds();
95 				float fAspect = (float) bounds.width / (float) bounds.height;
96 				canvas.setCurrent();
97 				try {
98 					GLContext.useContext(canvas);
99 				} catch (LWJGLException e) {
100 					e.printStackTrace();
101 				}
102 				GL11.glViewport(0, 0, bounds.width, bounds.height);
103 				GL11.glMatrixMode(GL11.GL_PROJECTION);
104 				GL11.glLoadIdentity();
105 				GLU.gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
106 				GL11.glMatrixMode(GL11.GL_MODELVIEW);
107 				GL11.glLoadIdentity();
108 			}
109 		});
110 
111 		GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
112 		GL11.glColor3f(1.0f, 0.0f, 0.0f);
113 		GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
114 		GL11.glClearDepth(1.0);
115 		GL11.glLineWidth(2);
116 		GL11.glEnable(GL11.GL_DEPTH_TEST);
117 
118 		Display.getCurrent().asyncExec(initRunnable());
119 
120 	}
121 
122 	/**
123 	 * {@inheritDoc}
124 	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
125 	 */
126 	@Override
setFocus()127 	public void setFocus() {
128 		canvas.setFocus();
129 	}
130 
drawTorus(float r, float R, int nsides, int rings)131 	static void drawTorus(float r, float R, int nsides, int rings) {
132 		float ringDelta = 2.0f * (float) Math.PI / rings;
133 		float sideDelta = 2.0f * (float) Math.PI / nsides;
134 		float theta = 0.0f, cosTheta = 1.0f, sinTheta = 0.0f;
135 		for (int i = rings - 1; i >= 0; i--) {
136 			float theta1 = theta + ringDelta;
137 			float cosTheta1 = (float) Math.cos(theta1);
138 			float sinTheta1 = (float) Math.sin(theta1);
139 			GL11.glBegin(GL11.GL_QUAD_STRIP);
140 			float phi = 0.0f;
141 			for (int j = nsides; j >= 0; j--) {
142 				phi += sideDelta;
143 				float cosPhi = (float) Math.cos(phi);
144 				float sinPhi = (float) Math.sin(phi);
145 				float dist = R + r * cosPhi;
146 				GL11
147 						.glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi,
148 								sinPhi);
149 				GL11
150 						.glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, r
151 								* sinPhi);
152 				GL11.glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
153 				GL11.glVertex3f(cosTheta * dist, -sinTheta * dist, r * sinPhi);
154 			}
155 			GL11.glEnd();
156 			theta = theta1;
157 			cosTheta = cosTheta1;
158 			sinTheta = sinTheta1;
159 		}
160 	}
161 
initRunnable()162 	Runnable initRunnable() {
163 		return new Runnable() {
164 			int rot = 0;
165 
166 			public void run() {
167 				if (!canvas.isDisposed() && canvas.isVisible()) {
168 
169 					fpsstatuslineitem.renderPassStarted();
170 
171 					canvas.setCurrent();
172 					try {
173 						GLContext.useContext(canvas);
174 					} catch (LWJGLException e) {
175 						e.printStackTrace();
176 					}
177 					GL11.glClear(GL11.GL_COLOR_BUFFER_BIT
178 							| GL11.GL_DEPTH_BUFFER_BIT);
179 					GL11.glClearColor(.2f, .7f, .2f, 1.0f);
180 					GL11.glLoadIdentity();
181 					GL11.glTranslatef(0.0f, 0.0f, -10.0f);
182 					float frot = rot;
183 					GL11
184 							.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot,
185 									1.0f);
186 					GL11.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f);
187 					rot++;
188 					GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
189 					GL11.glColor3f(0.9f, 0.9f, 0.9f);
190 					drawTorus(1, 1.9f + ((float) Math.sin((0.004f * frot))),
191 							15, 15);
192 
193 					canvas.swapBuffers();
194 
195 					fpsstatuslineitem.renderPassFinished();
196 					Display.getCurrent().asyncExec(this);
197 
198 				}
199 			}
200 		};
201 	}
202 
203 }
204