diff options
author | Grant Gayed | 2003-07-10 20:41:12 +0000 |
---|---|---|
committer | Grant Gayed | 2003-07-10 20:41:12 +0000 |
commit | 2bffa3862a37be63a266d84772815ad0cccee5d1 (patch) | |
tree | 9a9d9850e027e6ba1bddb5cde89079cef035ed0a /examples/org.eclipse.swt.opengl.examples | |
parent | 949a243710db76895ae62e7e3836ee5ccd5921de (diff) | |
download | eclipse.platform.swt-2bffa3862a37be63a266d84772815ad0cccee5d1.tar.gz eclipse.platform.swt-2bffa3862a37be63a266d84772815ad0cccee5d1.tar.xz eclipse.platform.swt-2bffa3862a37be63a266d84772815ad0cccee5d1.zip |
initial
Diffstat (limited to 'examples/org.eclipse.swt.opengl.examples')
26 files changed, 4548 insertions, 0 deletions
diff --git a/examples/org.eclipse.swt.opengl.examples/.classpath b/examples/org.eclipse.swt.opengl.examples/.classpath new file mode 100644 index 0000000000..8437e3bb55 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/.classpath @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="src" path="/org.eclipse.swt.opengl"/> + <classpathentry kind="src" path="/org.eclipse.swt"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/examples/org.eclipse.swt.opengl.examples/.project b/examples/org.eclipse.swt.opengl.examples/.project new file mode 100644 index 0000000000..9112e8e738 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/.project @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.eclipse.swt.opengl.examples</name> + <comment></comment> + <projects> + <project>org.eclipse.swt</project> + <project>org.eclipse.swt.opengl</project> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/AntialiasingTab.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/AntialiasingTab.java new file mode 100644 index 0000000000..78a1fbb4a9 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/AntialiasingTab.java @@ -0,0 +1,152 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.opengl.*; +import org.eclipse.swt.widgets.*; + +class AntialiasingTab extends OpenGLTab { + private Button antiAliasButton; + private float xPos = 0.0f, yPos = 0.0f, zPos = -6.0f; + + /** + * @see OpenGLTab#createControls(Composite) + */ + void createControls(Composite composite) { + Group movementGroup = new Group(composite, SWT.NONE); + movementGroup.setText("Translation"); + movementGroup.setLayout(new GridLayout(2, false)); + + new Label(movementGroup, SWT.NONE).setText("X:"); + final Slider xMove = new Slider(movementGroup, SWT.NONE); + xMove.setIncrement(1); + xMove.setMaximum(12); + xMove.setMinimum(0); + xMove.setThumb(2); + xMove.setPageIncrement(2); + xMove.setSelection(5); + xMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + xPos = xMove.getSelection() - 5; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Y:"); + final Slider yMove = new Slider(movementGroup, SWT.NONE); + yMove.setIncrement(1); + yMove.setMaximum(12); + yMove.setMinimum(0); + yMove.setThumb(2); + yMove.setPageIncrement(2); + yMove.setSelection(5); + yMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + yPos = yMove.getSelection() - 5; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Z:"); + final Slider zMove = new Slider(movementGroup, SWT.NONE); + zMove.setIncrement(1); + zMove.setMaximum(12); + zMove.setMinimum(0); + zMove.setThumb(2); + zMove.setPageIncrement(2); + zMove.setSelection(5); + zMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + zPos = zMove.getSelection() - 11; + } + }); + + antiAliasButton = new Button(composite, SWT.CHECK); + antiAliasButton.setText("Anti-Aliasing"); + antiAliasButton.setSelection(true); + antiAliasButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + if (antiAliasButton.getSelection()) { + GL.glEnable(GL.GL_LINE_SMOOTH); + } else { + GL.glDisable(GL.GL_LINE_SMOOTH); + } + } + }); + + ColorSelectionGroup colorGroup = + new ColorSelectionGroup(composite, SWT.NONE); + colorGroup.setText("Foreground color"); + colorGroup.addColorSelectionListener(new IColorSelectionListener() { + public void handleColorSelection(RGB rgb) { + GL.glColor3ub((byte) rgb.red, (byte) rgb.green, (byte) rgb.blue); + } + }); + } + + /** + * @see OpenGLTab#dispose() + */ + void dispose() { + super.dispose(); + GL.glDeleteLists(1, 1); + } + + /** + * @see OpenGLTab#getTabText() + */ + String getTabText() { + return "Anti-aliasing"; + } + + /** + * @see OpenGLTab#init() + */ + void init() { + GL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + GL.glColor3f(1.0f, 0.0f, 0.0f); + final float[] BEZIER_POINTS = { + -1.5f, -1.5f, 4.0f, -0.5f, -1.5f, 2.0f, 0.5f, -1.5f, + -1.0f, 1.5f, -1.5f, 2.0f, -1.5f, -0.5f, 1.0f, -0.5f, + -0.5f, 3.0f, 0.5f, -0.5f, 0.0f, 1.5f, -0.5f, -1.0f, + -1.5f, 0.5f, 4.0f, -0.5f, 0.5f, 0.0f, 0.5f, 0.5f, + 3.0f, 1.5f, 0.5f, 4.0f, -1.5f, 1.5f, -2.0f, -0.5f, + 1.5f, -2.0f, 0.5f, 1.5f, 0.0f, 1.5f, 1.5f, -1.0f, + }; + GL.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_NICEST); + GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); + GL.glMap2f(GL.GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, BEZIER_POINTS); + GL.glMapGrid2f(30, 0.0f, 1.0f, 30, 0.0f, 1.0f); + + GL.glEnable(GL.GL_AUTO_NORMAL); + GL.glEnable(GL.GL_LINE_SMOOTH); + GL.glEnable(GL.GL_LINE_STIPPLE); + GL.glEnable(GL.GL_BLEND); + GL.glEnable(GL.GL_MAP2_VERTEX_3); + + // create display lists + GL.glNewList(1, GL.GL_COMPILE); + GL.glEvalMesh2(GL.GL_LINE, 0, 30, 0, 30); + GL.glEndList(); + } + + /** + * @see OpenGLTab#renderScene() + */ + void renderScene() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT); + GL.glLoadIdentity(); + GL.glTranslatef(xPos, yPos, zPos); + GL.glCallList(1); // draw the beizer surface + } +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/AreaTab.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/AreaTab.java new file mode 100644 index 0000000000..5c6ffab751 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/AreaTab.java @@ -0,0 +1,434 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.opengl.*; +import org.eclipse.swt.widgets.*; + +class AreaTab extends OpenGLTab { + abstract class Shape { + abstract void draw(); + } + class State { + private String name; + private int index; + /** + * Constructor. + * + * @param name the display name of this state + * @param index the display list index corresponding to this state + */ + State (String name, int index) { + super(); + this.index = index; + this.name = name; + } + void display() { + GL.glCallList(index); + } + void dispose() { + GL.glDeleteLists(index, 1); + } + String getName() { + return name; + } + } + + private State[] states; + private State currentState; + private float xPos = 0.0f, yPos = 0.0f, zPos = -30.0f; + private float xRot = 90.0f, yRot = 0.0f, zRot = 0.0f; + private int quadratic, disk; + + /** + * @see OpenGLTab#createControls(Composite) + */ + void createControls(Composite composite) { + Group movementGroup = new Group(composite, SWT.NONE); + movementGroup.setText("Translation"); + movementGroup.setLayout(new GridLayout(2, false)); + + new Label(movementGroup, SWT.NONE).setText("X:"); + final Slider xMove = new Slider(movementGroup, SWT.NONE); + xMove.setIncrement(1); + xMove.setMaximum(22); + xMove.setMinimum(0); + xMove.setThumb(2); + xMove.setPageIncrement(2); + xMove.setSelection(10); + xMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + xPos = xMove.getSelection() - 10; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Y:"); + final Slider yMove = new Slider(movementGroup, SWT.NONE); + yMove.setIncrement(1); + yMove.setMaximum(22); + yMove.setMinimum(0); + yMove.setThumb(2); + yMove.setPageIncrement(2); + yMove.setSelection(10); + yMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + yPos = yMove.getSelection() - 10; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Z:"); + final Slider zMove = new Slider(movementGroup, SWT.NONE); + zMove.setIncrement(1); + zMove.setMaximum(22); + zMove.setMinimum(0); + zMove.setThumb(2); + zMove.setPageIncrement(2); + zMove.setSelection(10); + zMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + zPos = zMove.getSelection() - 40; + } + }); + + Group rotationGroup = new Group(composite, SWT.NONE); + rotationGroup.setText("Rotation"); + rotationGroup.setLayout(new GridLayout(2, false)); + + new Label(rotationGroup, SWT.NONE).setText("X:"); + final Slider xRotation = new Slider(rotationGroup, SWT.NONE); + xRotation.setIncrement(10); + xRotation.setMaximum(362); + xRotation.setMinimum(0); + xRotation.setThumb(2); + xRotation.setPageIncrement(20); + xRotation.setSelection(90); + xRotation.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + xRot = xRotation.getSelection(); + } + }); + + new Label(rotationGroup, SWT.NONE).setText("Y:"); + final Slider yRotation = new Slider(rotationGroup, SWT.NONE); + yRotation.setIncrement(10); + yRotation.setMaximum(362); + yRotation.setMinimum(0); + yRotation.setThumb(2); + yRotation.setPageIncrement(20); + yRotation.setSelection(0); + yRotation.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + yRot = yRotation.getSelection(); + } + }); + + new Label(rotationGroup, SWT.NONE).setText("Z:"); + final Slider zRotation = new Slider(rotationGroup, SWT.NONE); + zRotation.setIncrement(10); + zRotation.setMaximum(362); + zRotation.setMinimum(0); + zRotation.setThumb(2); + zRotation.setPageIncrement(20); + zRotation.setSelection(0); + zRotation.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + zRot = zRotation.getSelection(); + } + }); + + Composite optionsGroup = new Composite(composite, SWT.NONE); + GridLayout layout = new GridLayout(2, false); + layout.marginWidth = 0; + optionsGroup.setLayout(layout); + + new Label(optionsGroup, SWT.NONE).setText("Shape:"); + + final Combo statesCombo = new Combo(optionsGroup, SWT.READ_ONLY); + for (int i = 0; i < states.length; i++) { + statesCombo.add(states[i].getName()); + } + statesCombo.select(0); + statesCombo.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + currentState = states[statesCombo.getSelectionIndex()]; + } + }); + + final Button lightsButton = new Button(composite, SWT.CHECK); + lightsButton.setText("Lights"); + lightsButton.setSelection(true); + lightsButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + if (lightsButton.getSelection()) { + GL.glEnable(GL.GL_LIGHTING); + } else { + GL.glDisable(GL.GL_LIGHTING); + } + } + }); + } + + /** + * @see OpenGLTab#dispose() + */ + void dispose() { + super.dispose(); + GLU.gluDeleteQuadric(quadratic); + GLU.gluDeleteQuadric(disk); + if (states != null) { + for (int i = 0; i < states.length; i++) { + states [i].dispose(); + } + } + } + + /** + * Draws the logical AND of two shapes. + * + * @param a shape A + * @param b shape B + */ + void drawAandB(Shape a, Shape b) { + // draw parts of B that are inside A + drawAinsideB(a, b, GL.GL_BACK, GL.GL_NOTEQUAL); + // we do not want the following to show up + GL.glColorMask(false, false, false, false); + // turn on depth testing + GL.glEnable(GL.GL_DEPTH_TEST); + GL.glDepthFunc(GL.GL_ALWAYS); + // render the front face of B + b.draw(); + // reset the depth function + GL.glDepthFunc(GL.GL_LESS); + // draw parts of A that are inside B + drawAinsideB(b, a, GL.GL_BACK, GL.GL_NOTEQUAL); + } + + /** + * Draws the contents of one shape that appear within another. + * + * @param a the shape to draw + * @param b the constraining shape + * @param face + * @param test + */ + void drawAinsideB(Shape a, Shape b, int face, int test) { + // turn off the color buffer + GL.glColorMask(false, false, false, false); + // clear the stencil buffer + GL.glClearStencil(0); + GL.glEnable(GL.GL_DEPTH_TEST); + // set to proper Culling + GL.glCullFace(face); + // render shape A + a.draw(); + // set depth mask + GL.glDepthMask(false); + // enable stencil test + GL.glEnable(GL.GL_STENCIL_TEST); + GL.glStencilFunc(GL.GL_ALWAYS, 0, 0); + // set the stencil buffer to increment if the depth test passes + GL.glStencilOp(GL.GL_KEEP, GL.GL_KEEP, GL.GL_INCR); + // turn on back face culling + GL.glCullFace(GL.GL_BACK); + // render B + b.draw(); + // set the stencil buffer to decrement if the depth test passes + GL.glStencilOp(GL.GL_KEEP, GL.GL_KEEP, GL.GL_DECR); + // cull the front face + GL.glCullFace(GL.GL_FRONT); + // render B again + b.draw(); + // set depth mask + GL.glDepthMask(true); + GL.glColorMask(true, true, true, true); + // set the stencil buffer + GL.glStencilFunc(test, 0, 1); + // turn off depth testing + GL.glDisable(GL.GL_DEPTH_TEST); + // set to proper culling + GL.glCullFace(face); + // render A + a.draw(); + // disable stencil test + GL.glDisable(GL.GL_STENCIL_TEST); + } + + /** + * Draws the logical OR of two shapes. + * + * @param a shape A + * @param b shape B + */ + void drawAorB(Shape a, Shape b) { + GL.glEnable(GL.GL_DEPTH_TEST); + a.draw(); + b.draw(); + GL.glDisable(GL.GL_DEPTH_TEST); + } + + /** + * Draws one shape subtracted from another. + * + * @param a the base shape + * @param b the shape to subtract + */ + void drawAsubB(Shape a, Shape b) { + // draw back parts of B inside A + drawAinsideB(b, a, GL.GL_FRONT, GL.GL_NOTEQUAL); + // we do not want the following to show up + GL.glColorMask(false, false, false, false); + GL.glEnable(GL.GL_DEPTH_TEST); + // change the depth test to GL_ALWAYS + GL.glDepthFunc(GL.GL_ALWAYS); + // render the front face of B + a.draw(); + // reset the depth function + GL.glDepthFunc(GL.GL_LESS); + // draw front parts of A outside B + drawAinsideB(a, b, GL.GL_BACK, GL.GL_EQUAL); + } + + /** + * Draws the specifed shape. + * + * @param shape the shape to draw + */ + void drawShape(Shape shape) { + GL.glEnable(GL.GL_DEPTH_TEST); + shape.draw(); + GL.glDisable(GL.GL_DEPTH_TEST); + } + + /** + * @see OpenGLTab#getTabText() + */ + String getTabText() { + return "Area"; + } + + /** + * @see OpenGLTab#init() + */ + void init() { + if (!hasStencilSupport()) return; + + GL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + float[] lightPos = { 0.0f, 5.0f, -10.0f, 1.0f }; + GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightPos); + quadratic = GLU.gluNewQuadric(); + disk = GLU.gluNewQuadric(); + + GL.glEnable(GL.GL_CULL_FACE); + GL.glEnable(GL.GL_LIST_MODE); + GL.glEnable(GL.GL_LIGHT0); + GL.glEnable(GL.GL_LIGHTING); + GL.glLightModeli(GL.GL_LIGHT_MODEL_TWO_SIDE, GL.GL_TRUE); + + final float[] sphereMaterial = { 0.0f, 1.0f, 0.0f, 1.0f }; + final float[] cylinderMaterial = { 1.0f, 0.0f, 0.0f, 1.0f }; + + Shape sphere = new Shape() { + public void draw() { + GL.glMaterialfv( + GL.GL_FRONT_AND_BACK, + GL.GL_AMBIENT_AND_DIFFUSE, + sphereMaterial); + GL.glColor3f(0.0f, 1.0f, 0.0f); + GLU.gluSphere(quadratic, 3, 32, 32); + } + }; + Shape cylinder = new Shape() { + public void draw() { + GL.glMaterialfv( + GL.GL_FRONT_AND_BACK, + GL.GL_AMBIENT_AND_DIFFUSE, + cylinderMaterial); + GL.glColor3f(1.0f, 0.0f, 0.0f); + GLU.gluQuadricOrientation(disk, GLU.GLU_INSIDE); + GL.glPushMatrix(); + GL.glTranslatef(1.0f, 1.0f, 0.0f); + GLU.gluDisk(disk, 0, 3, 32, 32); + GLU.gluCylinder(quadratic, 3, 3, 6, 32, 32); + GL.glPushMatrix(); + GL.glTranslatef(0.0f, 0.0f, 6.0f); + GLU.gluQuadricOrientation(disk, GLU.GLU_OUTSIDE); + GLU.gluDisk(disk, 0, 3, 32, 32); + GL.glPopMatrix(); + GL.glPopMatrix(); + } + }; + + // create the display lists and states + states = new State[6]; + int index = 1; + + GL.glNewList(index, GL.GL_COMPILE); + drawShape(cylinder); + GL.glEndList(); + states[0] = new State("Cylinder",index++); + + GL.glNewList(index, GL.GL_COMPILE); + drawShape(sphere); + GL.glEndList(); + states[1] = new State("Sphere",index++); + + GL.glNewList(index, GL.GL_COMPILE); + drawAorB(cylinder, sphere); + GL.glEndList(); + states[2] = new State("Cylinder OR Sphere",index++); + + GL.glNewList(index, GL.GL_COMPILE); + drawAandB(cylinder, sphere); + GL.glEndList(); + states[3] = new State("Cylinder AND Sphere",index++); + + GL.glNewList(index, GL.GL_COMPILE); + drawAsubB(cylinder, sphere); + GL.glEndList(); + states[4] = new State("Cylinder SUB Sphere",index++); + + GL.glNewList(index, GL.GL_COMPILE); + drawAsubB(sphere, cylinder); + GL.glEndList(); + states[5] = new State("Sphere SUB Cylinder",index++); + + currentState = states[0]; + } + + /** + * @see OpenGLTab#isStencilSupportNeeded + */ + boolean isStencilSupportNeeded() { + return true; + } + + /** + * @see OpenGLTab#renderScene() + */ + void renderScene() { + GL.glClear( + GL.GL_COLOR_BUFFER_BIT + | GL.GL_DEPTH_BUFFER_BIT + | GL.GL_STENCIL_BUFFER_BIT); + + GL.glLoadIdentity(); + GL.glTranslatef(xPos, yPos, zPos); + GL.glRotatef(xRot, 1.0f, 0.0f, 0.0f); + GL.glRotatef(yRot, 0.0f, 1.0f, 0.0f); + GL.glRotatef(zRot, 0.0f, 0.0f, 1.0f); + + currentState.display(); + } +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/BezierTab.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/BezierTab.java new file mode 100644 index 0000000000..e66b8414c8 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/BezierTab.java @@ -0,0 +1,211 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.opengl.*; +import org.eclipse.swt.widgets.*; + +class BezierTab extends SelectionTab { + private boolean showCtrlPoints = true; + private Point offset; + private int currentPoint = -1; + private int lineDivisions = 30; + private double[][] ctrlPts = { + { 1.5, 0.5, 0.0, 0.6, 0.9, 0.0, 0.85, 0.12, + 0.0, 1.1, 1.0, 0.0, 0.53, 1.4, 0.0 + }, + { 0.53, 1.4, 0.0, 1.03, 1.87, 0.0, 1.52, 0.26, + 0.0, 1.86, 0.43, 0.0, 1.5, 0.5, 0.0 + } + }; + private static final int LENGTH = 5; + private static final int[][] PICK_NAMES = { + { 1, 2, 3, 4, 5 }, + { 6, 7, 8, 9, 10 } + }; + + /** + * @see OpenGLTab#createControls(Composite) + */ + void createControls(Composite composite) { + new Label(composite, SWT.NONE).setText("Click and drag points to adjust shape."); + new Label(composite, SWT.NONE).setText("Line division count:"); + final Slider divisions = new Slider(composite, SWT.NONE); + divisions.setIncrement(1); + divisions.setMaximum(52); + divisions.setMinimum(1); + divisions.setThumb(2); + divisions.setPageIncrement(2); + divisions.setSelection(30); + divisions.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + lineDivisions = divisions.getSelection(); + } + }); + + final Button showPointsButton = new Button(composite, SWT.CHECK); + showPointsButton.setText("Show Points"); + showPointsButton.setSelection(true); + showPointsButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + showCtrlPoints = showPointsButton.getSelection(); + } + }); + + final Button blendButton = new Button(composite, SWT.CHECK); + blendButton.setText("Blend"); + blendButton.setSelection(true); + blendButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + if (blendButton.getSelection()) { + GL.glEnable(GL.GL_BLEND); + } else { + GL.glDisable(GL.GL_BLEND); + } + } + }); + + final Button smoothLineButton = new Button(composite, SWT.CHECK); + smoothLineButton.setText("Smooth Line"); + smoothLineButton.setSelection(true); + smoothLineButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + if (smoothLineButton.getSelection()) { + GL.glEnable(GL.GL_LINE_SMOOTH); + } else { + GL.glDisable(GL.GL_LINE_SMOOTH); + } + } + }); + + final Canvas glCanvas = getGlCanvas(); + glCanvas.addMouseListener(new MouseAdapter() { + public void mouseUp(MouseEvent e) { + offset = null; + } + + }); + + glCanvas.addListener(SWT.MouseDown, new Listener() { + public void handleEvent(Event e) { + e.y = glCanvas.getClientArea().height - e.y; + if (e.button == 1) { + if (processSelection(e.x, e.y, 10) > 0) { + offset = new Point(e.x, e.y); + } else { + currentPoint = 0; + } + } + } + }); + + glCanvas.addListener(SWT.MouseMove, new Listener() { + public void handleEvent(Event e) { + if (offset == null) return; + int currentSegment = (currentPoint - 1) / LENGTH; + int current = (currentPoint - 1) * 3; + if (currentPoint > LENGTH) { + current = (currentPoint - 6) * 3; + } + Rectangle rect = glCanvas.getClientArea(); + e.y = rect.height - e.y; + if (0 < e.x && e.x < rect.width && 0 < e.y && e.y < rect.height && currentPoint > 0) { + ctrlPts[currentSegment][current] = (float) e.x / (float) 200; + ctrlPts[currentSegment][current + 1] = (float) e.y / (float) 200; + switch (currentPoint) { + case 10 : + ctrlPts[0][0] = (float) e.x / (float) 200; + ctrlPts[0][1] = (float) e.y / (float) 200; + break; + case 6 : + ctrlPts[0][12] = (float) e.x / (float) 200; + ctrlPts[0][13] = (float) e.y / (float) 200; + break; + } + } + } + }); + } + + /** + * @see OpenGLTab#getTabText() + */ + String getTabText() { + return "Bezier"; + } + + /** + * @see OpenGLTab#init() + */ + void init() { + GL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + GL.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_NICEST); + GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); + GL.glPointSize(7.0f); + GL.glLineWidth(4.0f); + + GL.glEnable(GL.GL_AUTO_NORMAL); + GL.glEnable(GL.GL_MAP1_VERTEX_3); + GL.glEnable(GL.GL_LINE_SMOOTH); + GL.glEnable(GL.GL_BLEND); + } + + /** + * @see SelectionTab.processPick (int[], int) + */ + void processPick(int[] pSelectBuff, int hits) { + int counter = 0; + currentPoint = 0; + for (int i = 0; i < hits; i++) { + int count = pSelectBuff[counter]; + counter += 3; + for (int j = 0; j < count; j++) { + currentPoint = pSelectBuff[counter]; + counter++; + } + } + } + + /** + * @see OpenGLTab#renderScene() + */ + void renderScene() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + GL.glLoadIdentity(); + GL.glTranslatef(-1.0f, -1.0f, -2.45f); + GL.glColor3f(0.0f, 0.0f, 1.0f); + for (int i = 0; i < ctrlPts.length; i++) { + GL.glMapGrid1d(lineDivisions, 0.0f, 1f); + GL.glMap1d(GL.GL_MAP1_VERTEX_3, 0, 1, 3, LENGTH, ctrlPts[i]); + GL.glEvalMesh1(GL.GL_LINE, 0, lineDivisions); + } + if (showCtrlPoints) { + GL.glPushName(0); + for (int j = 0; j < ctrlPts.length; j++) { + for (int i = 0; i < LENGTH * 3; i += 3) { + GL.glLoadName(PICK_NAMES[j][i / 3]); + GL.glBegin(GL.GL_POINTS); + if (PICK_NAMES[j][i / 3] == currentPoint) { + GL.glColor3f(1.0f, 0.0f, 0.0f); + } else { + GL.glColor3f(0.0f, 0.0f, 0.0f); + } + GL.glVertex3d(ctrlPts[j][i], ctrlPts[j][i + 1], ctrlPts[j][i + 2]); + GL.glEnd(); + } + } + } + } +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/BitmapTextTab.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/BitmapTextTab.java new file mode 100644 index 0000000000..084f45a213 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/BitmapTextTab.java @@ -0,0 +1,203 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.opengl.*; +import org.eclipse.swt.widgets.*; + +class BitmapTextTab extends OpenGLTab { + private Text messageText; + private FontData fontData; + private float[] textColor = { 1.0f, 0.0f, 0.0f }; + private float xPos = -130.0f, yPos = 0.0f; + private int listIndexBase; + private final static int LIST_INDEX_SIZE = 256; + private final static int DEFAULT_FONT_SIZE = 24; + private final static String DEFAULT_FONT_NAME = "Courier"; + + /** + * @see OpenGLTab#createControls(Composite) + */ + void createControls(Composite composite) { + Group movementGroup = new Group(composite, SWT.NONE); + movementGroup.setText("Translation"); + movementGroup.setLayout(new GridLayout(2, false)); + + new Label(movementGroup, SWT.NONE).setText("X:"); + final Slider xMove = new Slider(movementGroup, SWT.NONE); + xMove.setIncrement(1); + xMove.setMaximum(42); + xMove.setMinimum(0); + xMove.setThumb(2); + xMove.setPageIncrement(2); + xMove.setSelection(7); + xMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + xPos = (xMove.getSelection() * 10) - 200; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Y:"); + final Slider yMove = new Slider(movementGroup, SWT.NONE); + yMove.setIncrement(1); + yMove.setMaximum(42); + yMove.setMinimum(0); + yMove.setThumb(2); + yMove.setPageIncrement(2); + yMove.setSelection(20); + yMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + yPos = (yMove.getSelection() * 10) - 200; + } + }); + + Composite textGroup = new Composite(composite,SWT.NONE); + GridLayout layout = new GridLayout(2,false); + layout.marginHeight = 0; + layout.marginWidth = 0; + textGroup.setLayout(layout); + textGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); + + new Label(textGroup, SWT.NONE).setText("Text:"); + messageText = new Text(textGroup, SWT.BORDER); + GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); + data.grabExcessHorizontalSpace = true; + messageText.setLayoutData(data); + messageText.setText("OpenGL - SWT"); + + final Button fontSelectButton = new Button(composite, SWT.NONE); + fontSelectButton.setText("Set Font"); + + final ColorSelectionGroup colorGroup = + new ColorSelectionGroup(composite, SWT.NONE); + colorGroup.setText("Text color"); + colorGroup.addColorSelectionListener(new IColorSelectionListener() { + public void handleColorSelection(RGB rgb) { + GL.glColor3ub((byte) rgb.red, (byte) rgb.green, (byte) rgb.blue); + } + }); + + fontSelectButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + FontDialog fontDialog = new FontDialog(fontSelectButton.getShell()); + fontDialog.setText("Choose Font Options"); + double[] currentColor = new double[4]; + GL.glGetDoublev(GL.GL_CURRENT_COLOR, currentColor); + fontDialog.setRGB( + new RGB( + (int) currentColor[0] * 255, + (int) currentColor[1] * 255, + (int) currentColor[2] * 255)); + fontDialog.setFontData(fontData); + FontData result = fontDialog.open(); + if (result != null) { + fontData = result; + RGB rgb = fontDialog.getRGB(); + GL.glColor3ub((byte) rgb.red, (byte) rgb.green, (byte) rgb.blue); + colorGroup.setRGB(rgb); + getContext().loadBitmapFont(fontData, null, listIndexBase, 32, 96); + } + } + }); + } + + /** + * @see OpenGLTab#dispose() + */ + void dispose() { + super.dispose(); + GL.glDeleteLists(listIndexBase, LIST_INDEX_SIZE); + } + + /** + * Draws the text to the screen + * + * @param string the text to draw + */ + void drawText(String string) { + char[] stringChars = string.toCharArray(); + int[] text = new int[stringChars.length]; + for (int i = 0; i < text.length; i++) { + text[i] = (int) stringChars[i]; + } + // pushes the display list bits + GL.glPushAttrib(GL.GL_LIST_BIT); + // sets the base character to 32 + GL.glListBase(listIndexBase - 32); + GL.glCallLists(text.length, GL.GL_UNSIGNED_INT, text); + // pops the display list bits + GL.glPopAttrib(); + } + + /** + * @see OpenGLTab#getTabText() + */ + String getTabText() { + return "Bitmap Text"; + } + + /** + * @see OpenGLTab#init() + */ + void init() { + GL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + GL.glColor3fv(textColor); + GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); + GL.glEnable(GL.GL_DEPTH_TEST); + GL.glEnable(GL.GL_BLEND); + // build the initial font + listIndexBase = GL.glGenLists(LIST_INDEX_SIZE); + fontData = new FontData(); + fontData.setHeight(DEFAULT_FONT_SIZE); + fontData.setName(DEFAULT_FONT_NAME); + getContext().loadBitmapFont(fontData, null, listIndexBase, 32, LIST_INDEX_SIZE); + } + + /** + * @see OpenGLTab#renderScene() + */ + void renderScene() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + GL.glLoadIdentity(); + GL.glRasterPos2f(xPos, yPos); + drawText(messageText.getText()); + } + + /** + * @see OpenGLTab#setupViewingArea() + */ + void setupViewingArea() { + // use ortho view since this tab does not need any depth + Rectangle rect = getGlCanvas().getClientArea(); + int width = rect.width; + int height = rect.height; + width = Math.max(width, 1); + GL.glViewport(0, 0, width, height); + float nRange = 200.0f; + GL.glMatrixMode(GL.GL_PROJECTION); + GL.glLoadIdentity(); + if (width <= height) { + GL.glOrtho( + -nRange, nRange, -nRange * height / width, + nRange * height / width, -nRange, nRange); + } else { + GL.glOrtho( + -nRange * height / width, nRange * height / width, -nRange, + nRange, -nRange, nRange); + } + GL.glMatrixMode(GL.GL_MODELVIEW); + GL.glLoadIdentity(); + } +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/ColorSelectionGroup.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/ColorSelectionGroup.java new file mode 100644 index 0000000000..f0c3cdde9d --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/ColorSelectionGroup.java @@ -0,0 +1,166 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import java.util.Vector; +import org.eclipse.swt.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.opengl.GL; +import org.eclipse.swt.widgets.*; + +class ColorSelectionGroup implements Listener { + private Color color; + private Label label; + private Image image; + private Button button; + private Vector listeners = new Vector(); + + /** + * Constructor. + * + * @param parent the parent composite + * @param style style bits to be applied to the color group + */ + ColorSelectionGroup(Composite parent, int style) { + super(); + initColor(parent.getDisplay()); + + Composite colorGroup = new Composite(parent, style); + GridLayout layout = new GridLayout(2, false); + layout.marginWidth = 0; + layout.marginHeight = 0; + colorGroup.setLayout(layout); +// GridData data = new GridData(GridData.FILL_HORIZONTAL); +// data.grabExcessHorizontalSpace = true; +// colorGroup.setLayoutData(data); + + button = new Button(colorGroup, SWT.NONE); + button.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END)); + image = new Image(button.getDisplay(), 12, 12); + drawButtonImage(); + button.setImage(image); + button.addListener(SWT.Selection, this); + button.addDisposeListener(new DisposeListener() { + public void widgetDisposed(DisposeEvent event) { + if (image != null) image.dispose(); + if (color != null) color.dispose(); + } + }); + + label = new Label(colorGroup, SWT.NONE); + label.setText("Color"); +// data = new GridData(GridData.FILL_HORIZONTAL); +// data.grabExcessHorizontalSpace = true; +// label.setLayoutData(data); + } + + /** + * Adds the argument to this group's collection of + * color selection listeners. + * + * @param listener + */ + void addColorSelectionListener(IColorSelectionListener listener) { + listeners.addElement(listener); + } + + /** + * Draws the colored square on the selection button. + */ + void drawButtonImage() { + GC gc = new GC(image); + gc.setBackground(color); + Rectangle bounds = image.getBounds(); + gc.fillRectangle(0, 0, bounds.width, bounds.height); + gc.drawRectangle(0, 0, bounds.width - 1, bounds.height - 1); + gc.dispose(); + } + + /** + * @see org.eclipse.swt.widgets.Listener#handleEvent(Event) + */ + public void handleEvent(Event e) { + Shell shell = button.getShell(); + ColorDialog colorDialog = new ColorDialog(shell); + colorDialog.setRGB( + new RGB(color.getRed(), color.getGreen(), color.getBlue())); + RGB rgb = colorDialog.open(); + if (rgb == null) return; + setRGB(rgb); + notifyListeners(rgb); + } + + /** + * Initializes the color by querying for the current color. + * + * @param display + */ + void initColor(Display display) { + double[] currentColor = new double[4]; + GL.glGetDoublev(GL.GL_CURRENT_COLOR, currentColor); + RGB rgb = + new RGB( + (int) currentColor[0] * 255, + (int) currentColor[1] * 255, + (int) currentColor[2] * 255); + color = new Color(display, rgb); + } + + /** + * Notifies all registered color selection listeners. + * + * @param value + */ + void notifyListeners(RGB rgb) { + IColorSelectionListener[] listenersArr = + new IColorSelectionListener[listeners.size()]; + listeners.copyInto(listenersArr); + for (int i = 0; i < listenersArr.length; i++) { + listenersArr [i].handleColorSelection(rgb); + } + } + + /** + * Removes the argument from this group's collection of color selection + * listeners. If the argument is not a registered listener then does + * nothing. + * + * @param listener + */ + void removeColorSelectionListener(IColorSelectionListener listener) { + listeners.removeElement(listener); + } + + /** + * Sets the text for the group's label. + * + * @param text the new label text + */ + void setText(String text) { + label.setText(text); + } + + /** + * Sets the current color. + * + * @param rgb the rgb of the new color + */ + void setRGB(RGB rgb) { + Color oldColor = color; + color = new Color(button.getDisplay(), rgb); + drawButtonImage(); + button.setImage(image); + if (oldColor != null) oldColor.dispose(); + } +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/FogTab.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/FogTab.java new file mode 100644 index 0000000000..da017ed1db --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/FogTab.java @@ -0,0 +1,254 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.opengl.*; +import org.eclipse.swt.widgets.*; + +class FogTab extends OpenGLTab { + + private float rotY = 0.0f; + private float yPos = 0.0f, xPos = 0.0f, zPos = -15.0f; + private int cubeListIndexBase; + private final static int[] FOG_TYPES = { GL.GL_LINEAR, GL.GL_EXP, GL.GL_EXP2 }; + private final static String[] FOG_NAMES = { "LINEAR", "GL_EXP", "GL_EXP2" }; + private final static int SLEEP_LENGTH = 0; + + /** + * @see OpenGLTab#createControls(Composite) + */ + void createControls(Composite composite) { + Group movementGroup = new Group(composite, SWT.NONE); + movementGroup.setText("Translation"); + movementGroup.setLayout(new GridLayout(2, false)); + + new Label(movementGroup, SWT.NONE).setText("X:"); + final Slider xMove = new Slider(movementGroup, SWT.NONE); + xMove.setIncrement(1); + xMove.setMaximum(12); + xMove.setMinimum(0); + xMove.setThumb(2); + xMove.setPageIncrement(2); + xMove.setSelection(5); + xMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + xPos = xMove.getSelection() - 5; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Y:"); + final Slider yMove = new Slider(movementGroup, SWT.NONE); + yMove.setIncrement(1); + yMove.setMaximum(12); + yMove.setMinimum(0); + yMove.setThumb(2); + yMove.setPageIncrement(2); + yMove.setSelection(5); + yMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + yPos = yMove.getSelection() - 5; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Z:"); + final Slider zMove = new Slider(movementGroup, SWT.NONE); + zMove.setIncrement(1); + zMove.setMaximum(24); + zMove.setMinimum(0); + zMove.setThumb(4); + zMove.setPageIncrement(2); + zMove.setSelection(10); + zMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + zPos = zMove.getSelection() - 25; + } + }); + + Composite fogTypesGroup = new Composite(composite,SWT.NONE); + GridLayout layout = new GridLayout(2,false); + layout.marginHeight = 0; + layout.marginWidth = 0; + fogTypesGroup.setLayout(layout); + fogTypesGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); + + new Label(fogTypesGroup, SWT.NONE).setText("Fog Types:"); + final Combo fogTypeCombo = new Combo(fogTypesGroup, SWT.READ_ONLY); + GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); + data.grabExcessHorizontalSpace = true; + fogTypeCombo.setLayoutData(data); + fogTypeCombo.setItems(FOG_NAMES); + fogTypeCombo.select(0); + + new Label(composite, SWT.NONE).setText("Fog Density:"); + final Slider fogDensitySlider = new Slider(composite, SWT.NONE); + fogDensitySlider.setIncrement(1); + fogDensitySlider.setMaximum(32); + fogDensitySlider.setMinimum(0); + fogDensitySlider.setThumb(2); + fogDensitySlider.setPageIncrement(5); + fogDensitySlider.setSelection(0); + fogDensitySlider.setEnabled(false); + fogDensitySlider.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + float fogDensity = ((float)fogDensitySlider.getSelection()) / 100; + GL.glFogf(GL.GL_FOG_DENSITY, fogDensity); + } + }); + fogTypeCombo.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + int currentSelection = fogTypeCombo.getSelectionIndex(); + // fog type GL.GL_LINEAR does not utilize fogDensity, but the other fog types do + fogDensitySlider.setEnabled(currentSelection != 0); + GL.glFogf(GL.GL_FOG_MODE, FOG_TYPES[currentSelection]); + } + }); + } + + /** + * Creates a cube at 0,0 in the viewport. + */ + void createCube() { + GL.glNewList(cubeListIndexBase, GL.GL_COMPILE); + GL.glBegin(GL.GL_QUADS); + // front + GL.glColor3f(0.0f, 1.0f, 0.0f); + GL.glVertex3f(-0.5f, -0.5f, 0.5f); // bottom left + GL.glColor3f(0.0f, 0.0f, 1.0f); + GL.glVertex3f(0.5f, -0.5f, 0.5f); // bottom right + GL.glColor3f(1.0f, 1.0f, 0.0f); + GL.glVertex3f(0.5f, 0.5f, 0.5f); // top right + GL.glColor3f(1.0f, 0.0f, 0.0f); + GL.glVertex3f(-0.5f, 0.5f, 0.5f); // top left + // back + GL.glColor3f(0.0f, 0.0f, 1.0f); + GL.glVertex3f(-0.5f, -0.5f, -0.5f); // bottom left + GL.glColor3f(0.0f, 1.0f, 0.0f); + GL.glVertex3f(0.5f, -0.5f, -0.5f); // bottom right + GL.glColor3f(1.0f, 0.0f, 0.0f); + GL.glVertex3f(0.5f, 0.5f, -0.5f); // top right + GL.glColor3f(1.0f, 1.0f, 0.0f); + GL.glVertex3f(-0.5f, 0.5f, -0.5f); // top left + // left + GL.glColor3f(0.0f, 0.0f, 1.0f); + GL.glVertex3f(-0.5f, -0.5f, -0.5f); // bottom left + GL.glColor3f(0.0f, 1.0f, 0.0f); + GL.glVertex3f(-0.5f, -0.5f, 0.5f); // bottom right + GL.glColor3f(1.0f, 0.0f, 0.0f); + GL.glVertex3f(-0.5f, 0.5f, 0.5f); // top right + GL.glColor3f(1.0f, 1.0f, 0.0f); + GL.glVertex3f(-0.5f, 0.5f, -0.5f); // top left + // right + GL.glColor3f(0.0f, 0.0f, 1.0f); + GL.glVertex3f(0.5f, -0.5f, 0.5f); // bottom left + GL.glColor3f(0.0f, 1.0f, 0.0f); + GL.glVertex3f(0.5f, -0.5f, -0.5f); // bottom right + GL.glColor3f(1.0f, 0.0f, 0.0f); + GL.glVertex3f(0.5f, 0.5f, -0.5f); // top right + GL.glColor3f(1.0f, 1.0f, 0.0f); + GL.glVertex3f(0.5f, 0.5f, 0.5f); // top left + // top + GL.glColor3f(1.0f, 0.0f, 0.0f); + GL.glVertex3f(0.5f, 0.5f, -0.5f); + GL.glColor3f(1.0f, 1.0f, 0.0f); + GL.glVertex3f(-0.5f, 0.5f, -0.5f); + GL.glColor3f(1.0f, 0.0f, 0.0f); + GL.glVertex3f(-0.5f, 0.5f, 0.5f); + GL.glColor3f(1.0f, 1.0f, 0.0f); + GL.glVertex3f(0.5f, 0.5f, 0.5f); + // bottom + GL.glColor3f(0.0f, 0.0f, 1.0f); + GL.glVertex3f(0.5f, -0.5f, 0.5f); + GL.glColor3f(0.0f, 1.0f, 0.0f); + GL.glVertex3f(-0.5f, -0.5f, 0.5f); + GL.glColor3f(0.0f, 0.0f, 1.0f); + GL.glVertex3f(-0.5f, -0.5f, -0.5f); + GL.glColor3f(0.0f, 1.0f, 0.0f); + GL.glVertex3f(0.5f, -0.5f, -0.5f); + GL.glEnd(); + GL.glEndList(); + } + + /** + * @see OpenGLTab#dispose() + */ + void dispose() { + super.dispose(); + GL.glDeleteLists(cubeListIndexBase, 1); + } + + /** + * @see OpenGLTab#getSleepLength() + */ + int getSleepLength() { + return SLEEP_LENGTH; + } + /** + * @see OpenGLTab#getTabText() + */ + String getTabText() { + return "Fog"; + } + + /** + * @see OpenGLTab#init() + */ + void init() { + GL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + // fog color should be the same as the clear color + // to look appropriate + float[] fogColor = { 1.0f, 1.0f, 1.0f, 1.0f }; + GL.glFogfv(GL.GL_FOG_COLOR, fogColor); + GL.glHint(GL.GL_FOG_HINT, GL.GL_DONT_CARE); + GL.glFogf(GL.GL_FOG_START, 0); + GL.glFogf(GL.GL_FOG_DENSITY, 0.0f); + // set the end of the start distance; anything > 15 + // units from the camera will be covered in fog + GL.glFogf(GL.GL_FOG_END, 15); + GL.glFogf(GL.GL_FOG_MODE, FOG_TYPES[0]); + GL.glEnable(GL.GL_FOG); + GL.glEnable(GL.GL_DEPTH_TEST); + + cubeListIndexBase = GL.glGenLists(1); + createCube(); + } + + /** + * @see OpenGLTab#renderScene() + */ + void renderScene() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + GL.glLoadIdentity(); + GL.glTranslatef(xPos, yPos, zPos); + GL.glRotatef(rotY, 0.0f, 1.0f, 0.0f); + + GL.glCallList(cubeListIndexBase); + + GL.glPushMatrix(); + GL.glTranslatef(3, 0, -3); + GL.glCallList(cubeListIndexBase); + GL.glPopMatrix(); + + GL.glPushMatrix(); + GL.glTranslatef(-3, 0, -3); + GL.glCallList(cubeListIndexBase); + GL.glPopMatrix(); + + GL.glPushMatrix(); + GL.glTranslatef(0, 0, 4); + GL.glCallList(cubeListIndexBase); + GL.glPopMatrix(); + + rotY += 0.6f; + } +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/GradientTab.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/GradientTab.java new file mode 100644 index 0000000000..61e93097d5 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/GradientTab.java @@ -0,0 +1,233 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.opengl.*; +import org.eclipse.swt.widgets.*; + +class GradientTab extends OpenGLTab { + private float xPos = 0.0f, yPos = 0.0f, zPos = -7.0f; + private float xRot = 180.0f, yRot = 180.0f, zRot = 180.0f; + private int currentSelection = 1; + private final static float[] BEZIER_COLORS = { + 0.0f, 1.0f, 0.0f, 0.0f, 0.3f, 0.6f, 0.1f, 0.0f, + 0.8f, 0.2f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, 0.8f, 0.8f, 0.8f, 0.0f + }; + private final static float[] BEZIER_POINTS = { + -1.5f, -1.5f, 4.0f, -0.5f, -1.5f, 2.0f, 0.5f, -1.5f, + -1.0f, 1.5f, -1.5f, 2.0f, -1.5f, -0.5f, 1.0f, -0.5f, + -0.5f, 3.0f, 0.5f, -0.5f, 0.0f, 1.5f, -0.5f, -1.0f, + -1.5f, 0.5f, 4.0f, -0.5f, 0.5f, 0.0f, 0.5f, 0.5f, + 3.0f, 1.5f, 0.5f, 4.0f, -1.5f, 1.5f, -2.0f, -0.5f, + 1.5f, -2.0f, 0.5f, 1.5f, 0.0f, 1.5f, 1.5f, -1.0f, + }; + private final static String[] OBJECT_NAMES = { "Bezier", "Square" }; + + /** + * @see OpenGLTab#createControls(Composite) + */ + void createControls(Composite composite) { + Group movementGroup = new Group(composite, SWT.NONE); + movementGroup.setText("Translation"); + movementGroup.setLayout(new GridLayout(2, false)); + + new Label(movementGroup, SWT.NONE).setText("X:"); + final Slider xMove = new Slider(movementGroup, SWT.NONE); + xMove.setIncrement(1); + xMove.setMaximum(12); + xMove.setMinimum(0); + xMove.setThumb(2); + xMove.setPageIncrement(2); + xMove.setSelection(5); + xMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + xPos = xMove.getSelection() - 5; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Y:"); + final Slider yMove = new Slider(movementGroup, SWT.NONE); + yMove.setIncrement(1); + yMove.setMaximum(12); + yMove.setMinimum(0); + yMove.setThumb(2); + yMove.setPageIncrement(2); + yMove.setSelection(5); + yMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + yPos = yMove.getSelection() - 5; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Z:"); + final Slider zMove = new Slider(movementGroup, SWT.NONE); + zMove.setIncrement(1); + zMove.setMaximum(12); + zMove.setMinimum(0); + zMove.setThumb(2); + zMove.setPageIncrement(2); + zMove.setSelection(5); + zMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + zPos = zMove.getSelection() - 12; + } + }); + + Group rotationGroup = new Group(composite, SWT.NONE); + rotationGroup.setText("Rotation"); + rotationGroup.setLayout(new GridLayout(2, false)); + + new Label(rotationGroup, SWT.NONE).setText("X:"); + final Slider xRotation = new Slider(rotationGroup, SWT.NONE); + xRotation.setIncrement(10); + xRotation.setMaximum(362); + xRotation.setMinimum(0); + xRotation.setThumb(2); + xRotation.setPageIncrement(20); + xRotation.setSelection(180); + xRotation.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + xRot = xRotation.getSelection(); + } + }); + + new Label(rotationGroup, SWT.NONE).setText("Y:"); + final Slider yRotation = new Slider(rotationGroup, SWT.NONE); + yRotation.setIncrement(10); + yRotation.setMaximum(362); + yRotation.setMinimum(0); + yRotation.setThumb(2); + yRotation.setPageIncrement(20); + yRotation.setSelection(180); + yRotation.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + yRot = yRotation.getSelection(); + } + }); + + new Label(rotationGroup, SWT.NONE).setText("Z:"); + final Slider zRotation = new Slider(rotationGroup, SWT.NONE); + zRotation.setIncrement(10); + zRotation.setMaximum(362); + zRotation.setMinimum(0); + zRotation.setThumb(2); + zRotation.setPageIncrement(20); + zRotation.setSelection(180); + zRotation.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + zRot = zRotation.getSelection(); + } + }); + + Composite objectGroup = new Composite(composite,SWT.NONE); + GridLayout layout = new GridLayout(2,false); + layout.marginHeight = 0; + layout.marginWidth = 0; + objectGroup.setLayout(layout); + objectGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); + + new Label(objectGroup, SWT.NONE).setText("Object:"); + final Combo objectCombo = new Combo(objectGroup, SWT.READ_ONLY); + GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); + data.grabExcessHorizontalSpace = true; + objectCombo.setLayoutData(data); + objectCombo.setItems(OBJECT_NAMES); + objectCombo.select(0); + objectCombo.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + currentSelection = objectCombo.getSelectionIndex() + 1; + } + }); + } + + /** + * @see OpenGLTab#dispose() + */ + void dispose() { + super.dispose(); + GL.glDeleteLists(1, 2); + } + + /** + * @see OpenGLTab#getTabText() + */ + String getTabText() { + return "Gradients"; + } + + /** + * @see OpenGLTab#init() + */ + void init() { + GL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + GL.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_NICEST); + GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); + GL.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL); + GL.glMap2f(GL.GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, BEZIER_POINTS); + GL.glMap2f(GL.GL_MAP2_COLOR_4, 0, 1, 4, 1, 0, 1, 4, 6, BEZIER_COLORS); + GL.glMapGrid2f(20, 0.0f, 1.0f, 20, 0.0f, 1.0f); + GL.glShadeModel(GL.GL_SMOOTH); + GL.glEnable(GL.GL_LINE_SMOOTH); + GL.glEnable(GL.GL_LINE_STIPPLE); + GL.glEnable(GL.GL_BLEND); + GL.glEnable(GL.GL_MAP2_COLOR_4); + GL.glEnable(GL.GL_MAP2_VERTEX_3); + GL.glEnable(GL.GL_DEPTH_TEST); + + // create display lists + GL.glNewList(1, GL.GL_COMPILE); + GL.glEvalMesh2(GL.GL_FILL, 0, 20, 0, 20); + GL.glEndList(); + GL.glNewList(2, GL.GL_COMPILE); + GL.glBegin(GL.GL_TRIANGLE_FAN); + GL.glColor3f(0.0f, 1.0f, 0.0f); + GL.glVertex3f(0.0f, 0.0f, 0.0f); + GL.glColor3f(1.0f, 0.0f, 0.0f); + GL.glVertex3f(0.0f, 2.0f, 0.0f); + GL.glColor3f(0.0f, 1.0f, 0.0f); + GL.glVertex3f(-2.0f, 2.0f, 0.0f); + GL.glColor3f(0.0f, 0.0f, 1.0f); + GL.glVertex3f(-2.0f, 0.0f, 0.0f); + GL.glColor3f(0.0f, 1.0f, 0.0f); + GL.glVertex3f(-2.0f, -2.0f, 0.0f); + GL.glColor3f(1.0f, 0.0f, 0.0f); + GL.glVertex3f(0.0f, -2.0f, 0.0f); + GL.glColor3f(0.0f, 1.0f, 0.0f); + GL.glVertex3f(2.0f, -2.0f, 0.0f); + GL.glColor3f(0.0f, 0.0f, 1.0f); + GL.glVertex3f(2.0f, 0.0f, 0.0f); + GL.glColor3f(0.0f, 1.0f, 0.0f); + GL.glVertex3f(2.0f, 2.0f, 0.0f); + GL.glColor3f(1.0f, 0.0f, 0.0f); + GL.glVertex3f(0.0f, 2.0f, 0.0f); + GL.glEnd(); + GL.glEndList(); + } + + /** + * @see OpenGLTab#renderScene() + */ + void renderScene() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT); + GL.glLoadIdentity(); + GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + GL.glTranslatef(xPos, yPos, zPos); + GL.glRotatef(xRot, 1.0f, 0.0f, 0.0f); + GL.glRotatef(yRot, 0.0f, 1.0f, 0.0f); + GL.glRotatef(zRot, 0.0f, 0.0f, 1.0f); + GL.glColor3f(1.0f, 0.0f, 0.0f); + GL.glCallList(currentSelection); + } +}
\ No newline at end of file diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/IColorSelectionListener.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/IColorSelectionListener.java new file mode 100644 index 0000000000..346b5639dc --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/IColorSelectionListener.java @@ -0,0 +1,18 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + +import org.eclipse.swt.graphics.RGB; + + +public interface IColorSelectionListener { + public void handleColorSelection(RGB rgb); +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/LightTab.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/LightTab.java new file mode 100644 index 0000000000..701eb9cefa --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/LightTab.java @@ -0,0 +1,354 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.opengl.*; +import org.eclipse.swt.widgets.*; + +class LightTab extends OpenGLTab { + private float[] materialShininess = { 25.0f }; + private float[] lightPosition = { 0.0f, 0.0f, 0.0f, 1.0f }; + private float[][] colorValues = { + { 1.0f, 1.0f, 1.0f, 1.0f }, // light diffuse + { 1.0f, 1.0f, 1.0f, 1.0f }, // light ambient + { 1.0f, 1.0f, 1.0f, 1.0f }, // light specular + { 0.5f, 0.5f, 0.5f, 1.0f }, // light main ambient + { 0.0f, 0.0f, 1.0f, 1.0f }, // material diffuse + { 0.3f, 0.3f, 0.3f, 1.0f }, // material ambient + { 1.0f, 1.0f, 1.0f, 1.0f } // material specular + }; + private float yPos = 0.0f, xPos = 0.0f, zPos = -20.0f; + private int quadratic, sphere; + // sourceType values: 0 = light, 1 = material + private int sourceType; + // lightType values: 0 = diffuse, 1 = ambient, 2 = specular, 3 = main ambient + private int lightType = 0; + + /** + * @see OpenGLTab#createControls(Composite) + */ + void createControls(Composite composite) { + Group movementGroup = new Group(composite, SWT.NONE); + movementGroup.setText("Translation"); + movementGroup.setLayout(new GridLayout(2, false)); + + new Label(movementGroup, SWT.NONE).setText("X:"); + final Slider xMove = new Slider(movementGroup, SWT.NONE); + xMove.setIncrement(1); + xMove.setMaximum(22); + xMove.setMinimum(0); + xMove.setThumb(2); + xMove.setPageIncrement(2); + xMove.setSelection(10); + xMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + xPos = xMove.getSelection() - 10; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Y:"); + final Slider yMove = new Slider(movementGroup, SWT.NONE); + yMove.setIncrement(1); + yMove.setMaximum(22); + yMove.setMinimum(0); + yMove.setThumb(2); + yMove.setPageIncrement(2); + yMove.setSelection(10); + yMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + yPos = yMove.getSelection() - 10; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Z:"); + final Slider zMove = new Slider(movementGroup, SWT.NONE); + zMove.setIncrement(1); + zMove.setMaximum(22); + zMove.setMinimum(0); + zMove.setThumb(2); + zMove.setPageIncrement(2); + zMove.setSelection(10); + zMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + zPos = zMove.getSelection() - 30; + } + }); + + Group positionGroup = new Group(composite, SWT.NONE); + positionGroup.setText("Light Position"); + positionGroup.setLayout(new GridLayout(2, false)); + + new Label(positionGroup, SWT.NONE).setText("X:"); + final Slider lightX = new Slider(positionGroup, SWT.NONE); + lightX.setIncrement(1); + lightX.setMaximum(22); + lightX.setMinimum(0); + lightX.setThumb(2); + lightX.setPageIncrement(2); + lightX.setSelection(10); + lightX.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + lightPosition[0] = ((float) lightX.getSelection() * 10) - 100; + } + }); + + new Label(positionGroup, SWT.NONE).setText("Y:"); + final Slider lightY = new Slider(positionGroup, SWT.NONE); + lightY.setIncrement(1); + lightY.setMaximum(22); + lightY.setMinimum(0); + lightY.setThumb(2); + lightY.setPageIncrement(2); + lightY.setSelection(10); + lightY.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + lightPosition[1] = ((float) lightY.getSelection() * 10) - 100; + } + }); + + new Label(positionGroup, SWT.NONE).setText("Z:"); + final Slider lightZ = new Slider(positionGroup, SWT.NONE); + lightZ.setIncrement(1); + lightZ.setMaximum(22); + lightZ.setMinimum(0); + lightZ.setThumb(2); + lightZ.setPageIncrement(2); + lightZ.setSelection(10); + lightZ.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + lightPosition[2] = ((float) lightZ.getSelection() * 10) - 20; + } + }); + + Group lightColorsGroup = new Group(composite, SWT.NONE); + lightColorsGroup.setText("Light Colors"); + lightColorsGroup.setLayout(new GridLayout()); + GridData data = new GridData(GridData.FILL_BOTH); + lightColorsGroup.setLayoutData(data); + + ColorSelectionGroup lightDiffuseButton = + new ColorSelectionGroup(lightColorsGroup, SWT.NONE); + lightDiffuseButton.setText("Diffuse"); + lightDiffuseButton.setRGB( + new RGB( + (int) (colorValues[0][0] * 255), + (int) (colorValues[0][1] * 255), + (int) (colorValues[0][2] * 255))); + lightDiffuseButton + .addColorSelectionListener(new IColorSelectionListener() { + public void handleColorSelection(RGB rgb) { + colorValues[0][0] = ((float) rgb.red) / 255; + colorValues[0][1] = ((float) rgb.green) / 255; + colorValues[0][2] = ((float) rgb.blue) / 255; + setColorValues(); + } + }); + + ColorSelectionGroup lightAmbientButton = + new ColorSelectionGroup(lightColorsGroup, SWT.NONE); + lightAmbientButton.setText("Ambient"); + lightAmbientButton.setRGB( + new RGB( + (int) (colorValues[1][0] * 255), + (int) (colorValues[1][1] * 255), + (int) (colorValues[1][2] * 255))); + lightAmbientButton + .addColorSelectionListener(new IColorSelectionListener() { + public void handleColorSelection(RGB rgb) { + colorValues[1][0] = ((float) rgb.red) / 255; + colorValues[1][1] = ((float) rgb.green) / 255; + colorValues[1][2] = ((float) rgb.blue) / 255; + setColorValues(); + } + }); + + ColorSelectionGroup lightSpecularButton = + new ColorSelectionGroup(lightColorsGroup, SWT.NONE); + lightSpecularButton.setText("Specular"); + lightSpecularButton.setRGB( + new RGB( + (int) (colorValues[2][0] * 255), + (int) (colorValues[2][1] * 255), + (int) (colorValues[2][2] * 255))); + lightSpecularButton + .addColorSelectionListener(new IColorSelectionListener() { + public void handleColorSelection(RGB rgb) { + colorValues[2][0] = ((float) rgb.red) / 255; + colorValues[2][1] = ((float) rgb.green) / 255; + colorValues[2][2] = ((float) rgb.blue) / 255; + setColorValues(); + } + }); + + ColorSelectionGroup lightMainAmbientButton = + new ColorSelectionGroup(lightColorsGroup, SWT.NONE); + lightMainAmbientButton.setText("Main Ambient"); + lightMainAmbientButton.setRGB( + new RGB( + (int) (colorValues[3][0] * 255), + (int) (colorValues[3][1] * 255), + (int) (colorValues[3][2] * 255))); + lightMainAmbientButton + .addColorSelectionListener(new IColorSelectionListener() { + public void handleColorSelection(RGB rgb) { + colorValues[3][0] = ((float) rgb.red) / 255; + colorValues[3][1] = ((float) rgb.green) / 255; + colorValues[3][2] = ((float) rgb.blue) / 255; + setColorValues(); + } + }); + + Group materialColorsGroup = new Group(composite, SWT.NONE); + materialColorsGroup.setText("Material Colors"); + materialColorsGroup.setLayout(new GridLayout()); + data = new GridData(GridData.FILL_BOTH); + materialColorsGroup.setLayoutData(data); + + ColorSelectionGroup materialDiffuseButton = + new ColorSelectionGroup(materialColorsGroup, SWT.NONE); + materialDiffuseButton.setText("Diffuse"); + materialDiffuseButton.setRGB( + new RGB( + (int) (colorValues[4][0] * 255), + (int) (colorValues[4][1] * 255), + (int) (colorValues[4][2] * 255))); + materialDiffuseButton + .addColorSelectionListener(new IColorSelectionListener() { + public void handleColorSelection(RGB rgb) { + colorValues[4][0] = ((float) rgb.red) / 255; + colorValues[4][1] = ((float) rgb.green) / 255; + colorValues[4][2] = ((float) rgb.blue) / 255; + setColorValues(); + } + }); + + ColorSelectionGroup materialAmbientButton = + new ColorSelectionGroup(materialColorsGroup, SWT.NONE); + materialAmbientButton.setText("Ambient"); + materialAmbientButton.setRGB( + new RGB( + (int) (colorValues[5][0] * 255), + (int) (colorValues[5][1] * 255), + (int) (colorValues[5][2] * 255))); + materialAmbientButton + .addColorSelectionListener(new IColorSelectionListener() { + public void handleColorSelection(RGB rgb) { + colorValues[5][0] = ((float) rgb.red) / 255; + colorValues[5][1] = ((float) rgb.green) / 255; + colorValues[5][2] = ((float) rgb.blue) / 255; + setColorValues(); + } + }); + + ColorSelectionGroup materialSpecularButton = + new ColorSelectionGroup(materialColorsGroup, SWT.NONE); + materialSpecularButton.setText("Specular"); + materialSpecularButton.setRGB( + new RGB( + (int) (colorValues[6][0] * 255), + (int) (colorValues[6][1] * 255), + (int) (colorValues[6][2] * 255))); + materialSpecularButton + .addColorSelectionListener(new IColorSelectionListener() { + public void handleColorSelection(RGB rgb) { + colorValues[6][0] = ((float) rgb.red) / 255; + colorValues[6][1] = ((float) rgb.green) / 255; + colorValues[6][2] = ((float) rgb.blue) / 255; + setColorValues(); + } + }); + + new Label(composite, SWT.NONE).setText("Material Shininess:"); + final Slider shineSlider = new Slider(composite, SWT.NONE); + shineSlider.setIncrement(5); + shineSlider.setMaximum(102); + shineSlider.setMinimum(0); + shineSlider.setThumb(2); + shineSlider.setPageIncrement(10); + shineSlider.setSelection(25); + shineSlider.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + materialShininess[0] = shineSlider.getSelection(); + GL.glMaterialfv(GL.GL_FRONT_AND_BACK, + GL.GL_SHININESS, + materialShininess); + } + }); + } + + /** + * @see OpenGLTab#dispose() + */ + void dispose() { + super.dispose(); + GLU.gluDeleteQuadric(quadratic); + GL.glDeleteLists(sphere, 1); + } + + /** + * @see OpenGLTab#getTabText() + */ + String getTabText() { + return "Light"; + } + + /** + * @see OpenGLTab#init() + */ + void init() { + GL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + GL.glClearDepth(1.0); + setColorValues(); + GL.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, materialShininess); + quadratic = GLU.gluNewQuadric(); + GLU.gluQuadricNormals(quadratic, GLU.GLU_SMOOTH); + GL.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_NICEST); + GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); + GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); + GL.glPointSize(2.0f); + GL.glEnable(GL.GL_LIGHTING); + GL.glEnable(GL.GL_LIGHT0); + GL.glEnable(GL.GL_DEPTH_TEST); + sphere = GL.glGenLists(1); + GL.glNewList(sphere, GL.GL_COMPILE); + GLU.gluSphere(quadratic, 3, 32, 32); + GL.glEndList(); + } + + /** + * @see OpenGLTab#renderScene() + */ + void renderScene() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + GL.glLoadIdentity(); + GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightPosition); + GL.glTranslatef(xPos, yPos, zPos); + GL.glCallList(sphere); + } + + /** + * Sets the current color values into the rendered display. + */ + void setColorValues() { + GL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, colorValues[0]); + GL.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE, colorValues[4]); + GL.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT, colorValues[5]); + GL.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, colorValues[1]); + GL.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, colorValues[2]); + GL.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, colorValues[6]); + GL.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT, colorValues[3]); + GL.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, materialShininess); + } +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/NurbTab.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/NurbTab.java new file mode 100644 index 0000000000..c25e8c6647 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/NurbTab.java @@ -0,0 +1,294 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.opengl.*; +import org.eclipse.swt.widgets.*; + +class NurbTab extends OpenGLTab { + private float xPos = -5.0f, yPos = -5.0f, zPos = -25.0f; + private float xRot = 330.0f, yRot = 0.0f, zRot = 0.0f; + private int nurb; + + /** + * Returns a 3 dimensional array to store vertex points for the nurb + * surface. + * + * @return the array + */ + float[][][] buildNurb() { + float[][][] ctrl = new float[4][4][3]; + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + // set the x value + ctrl[i][j][0] = 2 * (i + (float) Math.cos(i + j)); + // set the y value + ctrl[i][j][1] = 2 * (j + (float) Math.cos(i + j)); + // set the z value + if ((i == 1 && j == 1) || (i == 2 && j == 2)) { + // this makes the hill + ctrl[i][j][2] = 6.0f; + } else { + ctrl[i][j][2] = 0.0f; + } + } + } + // sets this particular z value to a large number to make a high + // hill + ctrl[2][2][2] = 8.0f; + + return ctrl; + } + + /** + * Returns a 1 dimensional array representing the 3 dimensional + * argument. This result can be passed to the nurb renderer + * + * @param ctrl source 3D array + * @return 1D array + */ + float[] convert(float[][][] ctrl) { + float[] pts = + new float[ctrl.length * ctrl[0].length * ctrl[0][0].length]; + int row = 0; + int col = 0; + // set rowIncr to number of columns * depth + int rowIncr = ctrl[0].length * ctrl[0][0].length; + // set colIncr to depth + int colIncr = ctrl[0][0].length; + // loop through rows + for (int i = 0; i < ctrl.length; i++) { + col = 0; + // loop through columns + for (int j = 0; j < ctrl[0].length; j++) { + // loop through to get the depth values + for (int k = 0; k < ctrl[0][0].length; k++) { + pts[row + col + k] = ctrl[i][j][k]; + } + col += colIncr; + } + row += rowIncr; + } + return pts; + } + + /** + * @see OpenGLTab#createControls(Composite) + */ + void createControls(final Composite composite) { + Group movementGroup = new Group(composite, SWT.NONE); + movementGroup.setText("Translation"); + movementGroup.setLayout(new GridLayout(2, false)); + + new Label(movementGroup, SWT.NONE).setText("X:"); + final Slider xMove = new Slider(movementGroup, SWT.NONE); + xMove.setIncrement(1); + xMove.setMaximum(12); + xMove.setMinimum(0); + xMove.setThumb(2); + xMove.setPageIncrement(2); + xMove.setSelection(5); + xMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + xPos = xMove.getSelection() - 10; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Y:"); + final Slider yMove = new Slider(movementGroup, SWT.NONE); + yMove.setIncrement(1); + yMove.setMaximum(12); + yMove.setMinimum(0); + yMove.setThumb(2); + yMove.setPageIncrement(2); + yMove.setSelection(5); + yMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + yPos = yMove.getSelection() - 10; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Z:"); + final Slider zMove = new Slider(movementGroup, SWT.NONE); + zMove.setIncrement(1); + zMove.setMaximum(12); + zMove.setMinimum(0); + zMove.setThumb(2); + zMove.setPageIncrement(2); + zMove.setSelection(5); + zMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + zPos = zMove.getSelection() - 30; + } + }); + + Group rotationGroup = new Group(composite, SWT.NONE); + rotationGroup.setText("Rotation"); + rotationGroup.setLayout(new GridLayout(2, false)); + + new Label(rotationGroup, SWT.NONE).setText("X:"); + final Slider xRotation = new Slider(rotationGroup, SWT.NONE); + xRotation.setIncrement(10); + xRotation.setMaximum(362); + xRotation.setMinimum(0); + xRotation.setThumb(2); + xRotation.setPageIncrement(20); + xRotation.setSelection(330); + xRotation.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + xRot = xRotation.getSelection(); + } + }); + + new Label(rotationGroup, SWT.NONE).setText("Y:"); + final Slider yRotation = new Slider(rotationGroup, SWT.NONE); + yRotation.setIncrement(10); + yRotation.setMaximum(362); + yRotation.setMinimum(0); + yRotation.setThumb(2); + yRotation.setPageIncrement(20); + yRotation.setSelection(0); + yRotation.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + yRot = yRotation.getSelection(); + } + }); + + new Label(rotationGroup, SWT.NONE).setText("Z:"); + final Slider zRotation = new Slider(rotationGroup, SWT.NONE); + zRotation.setIncrement(10); + zRotation.setMaximum(362); + zRotation.setMinimum(0); + zRotation.setThumb(2); + zRotation.setPageIncrement(20); + zRotation.setSelection(0); + zRotation.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + zRot = zRotation.getSelection(); + } + }); + + new Label(composite, SWT.NONE).setText("Sampling Tolerance:"); + final Slider sSlider = new Slider(composite, SWT.NONE); + sSlider.setIncrement(1); + sSlider.setMaximum(101); + sSlider.setMinimum(0); + sSlider.setThumb(2); + sSlider.setPageIncrement(2); + sSlider.setSelection(75); + sSlider.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + GLU.gluNurbsProperty( + nurb, + GLU.GLU_SAMPLING_TOLERANCE, + sSlider.getSelection() + 1); + } + }); + + final Button fillButton = new Button(composite, SWT.CHECK); + fillButton.setText("Fill"); + fillButton.setSelection(true); + fillButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + if (fillButton.getSelection()) { + GLU.gluNurbsProperty( + nurb, + GLU.GLU_DISPLAY_MODE, + GLU.GLU_FILL); + } else { + GLU.gluNurbsProperty( + nurb, + GLU.GLU_DISPLAY_MODE, + GLU.GLU_OUTLINE_POLYGON); + } + } + }); + + ColorSelectionGroup colorGroup = + new ColorSelectionGroup(composite, SWT.NONE); + colorGroup.setText("Foreground color"); + colorGroup.addColorSelectionListener(new IColorSelectionListener() { + public void handleColorSelection(RGB rgb) { + GL.glColor3ub((byte) rgb.red, (byte) rgb.green, (byte) rgb.blue); + } + }); + } + + /** + * @see OpenGLTab#dispose() + */ + void dispose() { + super.dispose(); + GLU.gluDeleteNurbsRenderer(nurb); + } + + /** + * @see OpenGLTab#getTabText() + */ + String getTabText() { + return "Nurb"; + } + + /** + * @see OpenGLTab#init() + */ + void init() { + GL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + GL.glColor3f(1.0f, 0.0f, 0.0f); + // set up material color which helps show curves + float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; + float mat_shininess[] = { 100.0f }; + GL.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, mat_specular); + GL.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, mat_shininess); + GL.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE); + + GL.glEnable(GL.GL_LIGHTING); + GL.glEnable(GL.GL_LIGHT0); + GL.glEnable(GL.GL_COLOR_MATERIAL); + GL.glEnable(GL.GL_DEPTH_TEST); + GL.glEnable(GL.GL_AUTO_NORMAL); + GL.glEnable(GL.GL_NORMALIZE); + + nurb = GLU.gluNewNurbsRenderer(); + + GLU.gluNurbsProperty( + nurb, + GLU.GLU_SAMPLING_METHOD, + GLU.GLU_PATH_LENGTH); + // sampling tolerance is specific for GLU.GLU_PATH_LENGTH + GLU.gluNurbsProperty(nurb, GLU.GLU_SAMPLING_TOLERANCE, 75.0f); + GLU.gluNurbsProperty(nurb, GLU.GLU_DISPLAY_MODE, GLU.GLU_FILL); + } + + /** + * @see OpenGLTab#renderScene() + */ + void renderScene() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + GL.glLoadIdentity(); + // do translation and rotation + GL.glTranslatef(xPos, yPos, zPos); + GL.glRotatef(xRot, 1.0f, 0.0f, 0.0f); + GL.glRotatef(yRot, 0.0f, 1.0f, 0.0f); + GL.glRotatef(zRot, 0.0f, 0.0f, 1.0f); + // define nurbs surface + float[] knots = { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f }; + GLU.gluBeginSurface(nurb); + GLU.gluNurbsSurface( + nurb, 8, knots, 8, knots, 12, 3, + convert(buildNurb()), 4, 4, GL.GL_MAP2_VERTEX_3); + GLU.gluEndSurface(nurb); + } +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/ObjectsTab.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/ObjectsTab.java new file mode 100644 index 0000000000..3ef288e451 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/ObjectsTab.java @@ -0,0 +1,376 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.opengl.*; +import org.eclipse.swt.widgets.*; + +class ObjectsTab extends OpenGLTab { + private class Shape { + private String name; + private int index; + /** + * Constructor. + * + * @param name the display name of this shape + * @param index the display list index corresponding to this shape + */ + Shape(String name, int index) { + super(); + this.index = index; + this.name = name; + } + void dispose() { + GL.glDeleteLists(index, 1); + } + void draw() { + GL.glCallList(index); + } + String getName() { + return name; + } + } + + private Shape[] shapes; + private Shape currentShape; + private float xRot = 0.0f, yRot = 0.0f; + private boolean fill = true; + private int quadratic; + private final static int SLEEP_LENGTH = 50; + + /** + * @see OpenGLTab#createControls(Composite) + */ + void createControls(final Composite composite) { + GridLayout layout = new GridLayout(2, false); + layout.marginWidth = 0; + Composite controls = new Composite(composite, SWT.NONE); + controls.setLayout(layout); + + new Label(controls, SWT.NONE).setText("Object:"); + final Combo objectsCombo = new Combo(controls, SWT.READ_ONLY); + for (int i = 0; i < shapes.length; i++) { + objectsCombo.add(shapes[i].getName()); + } + objectsCombo.select(0); + objectsCombo.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + currentShape = shapes[objectsCombo.getSelectionIndex()]; + } + }); + + final Button fillToggleButton = new Button(controls, SWT.CHECK); + fillToggleButton.setText("Fill"); + fillToggleButton.setSelection(true); + fillToggleButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + fill = fillToggleButton.getSelection(); + } + }); + + ColorSelectionGroup colorGroup = + new ColorSelectionGroup(composite, SWT.NONE); + colorGroup.setText("Foreground color"); + colorGroup.addColorSelectionListener(new IColorSelectionListener() { + public void handleColorSelection(RGB rgb) { + GL.glColor3ub((byte) rgb.red, (byte) rgb.green, (byte) rgb.blue); + } + }); + } + + /** + * @see OpenGLTab#dispose() + */ + void dispose() { + super.dispose(); + GLU.gluDeleteQuadric(quadratic); + for (int i = 0; i < shapes.length; i++) { + shapes [i].dispose(); + } + } + + /** + * Creates a circle centered at 0,0 in the viewport. + * + * @param radius + * @param xShrink + * @param yShrink + */ + void drawCircle(float radius, float xShrink, float yShrink) { + GL.glBegin(GL.GL_POLYGON); + float dia = (float) 2.0 * (float) Math.PI; + for (float angle = 0.0f; angle <= dia; angle += 0.1f) { + GL.glVertex2d(radius * Math.cos(angle), radius * Math.sin(angle)); + } + GL.glEnd(); + } + + /** + * Creates a cube centered at 0,0 in the viewport. + * + * @param width + * @param height + * @param depth + */ + void drawCube(float width, float height, float depth) { + GL.glBegin(GL.GL_QUADS); + // front + GL.glVertex3f(-width, -height, depth); // bottom left + GL.glVertex3f(width, -height, depth); // bottom right + GL.glVertex3f(width, height, depth); // top right + GL.glVertex3f(-width, height, depth); // top left + // back + GL.glVertex3f(-width, -height, -depth); // bottom left + GL.glVertex3f(width, -height, -depth); // bottom right + GL.glVertex3f(width, height, -depth); // top right + GL.glVertex3f(-width, height, -depth); // top left + // left + GL.glVertex3f(-width, -height, -depth); // bottom left + GL.glVertex3f(-width, -height, depth); // bottom right + GL.glVertex3f(-width, height, depth); // top right + GL.glVertex3f(-width, height, -depth); // top left + // right + GL.glVertex3f(width, -height, depth); // bottom left + GL.glVertex3f(width, -height, -depth); // bottom right + GL.glVertex3f(width, height, -depth); // top right + GL.glVertex3f(width, height, depth); // top left + // top + GL.glVertex3f(width, height, -depth); // back right + GL.glVertex3f(-width, height, -depth); // back left + GL.glVertex3f(-width, height, depth); // front left + GL.glVertex3f(width, height, depth); // front right + // bottom + GL.glVertex3f(width, -height, depth); // front right + GL.glVertex3f(-width, -height, depth); // front left + GL.glVertex3f(-width, -height, -depth); // back left + GL.glVertex3f(width, -height, -depth); // back right + GL.glEnd(); + } + + /** + * Creates a pyramid centered at 0,0 in the viewport. + * + * @param width + * @param height + * @param depth + */ + void drawPyramid(float width, float height, float depth) { + GL.glBegin(GL.GL_TRIANGLES); + // front + GL.glVertex3f(0.0f, height, 0.0f); // top + GL.glVertex3f(-width, -height, depth); // left + GL.glVertex3f(width, -height, depth); // right + // left + GL.glVertex3f(0.0f, height, 0.0f); // top + GL.glVertex3f(-width, -height, -depth); // left + GL.glVertex3f(-width, -height, depth); // right + // front + GL.glVertex3f(-width, -height, -depth); // left + GL.glVertex3f(0.0f, height, 0.0f); // top + GL.glVertex3f(width, -height, -depth); // right + // right + GL.glVertex3f(0.0f, height, 0.0f); // top + GL.glVertex3f(width, -height, depth); // left + GL.glVertex3f(width, -height, -depth); // right + GL.glEnd(); + GL.glBegin(GL.GL_QUADS); + // bottom + GL.glVertex3f(-width, -height, depth); // front left + GL.glVertex3f(-width, -height, -depth); // back left + GL.glVertex3f(width, -height, -depth); // back right + GL.glVertex3f(width, -height, depth); // front right + GL.glEnd(); + } + + /** + * Creates a square centered at 0,0 in the viewport. + * + * @param width + * @param height + */ + void drawSquare(float width, float height) { + GL.glBegin(GL.GL_QUADS); + GL.glVertex3f(-width, -height, 0.0f); // bottom left + GL.glVertex3f(width, -height, 0.0f); // bottom right + GL.glVertex3f(width, height, 0.0f); // top right + GL.glVertex3f(-width, height, 0.0f); // top left + GL.glEnd(); + } + + /** + * This method is from glut_shapes.c . + * + * @param r + * @param R + * @param nsides + * @param rings + */ + void drawTorus(float r, float R, int nsides, int rings) { + float ringDelta = 2.0f * (float) Math.PI / rings; + float sideDelta = 2.0f * (float) Math.PI / nsides; + float theta = 0.0f; + float cosTheta = 1.0f; + float sinTheta = 0.0f; + + for (int i = rings - 1; i >= 0; i--) { + float theta1 = theta + ringDelta; + float cosTheta1 = (float) Math.cos(theta1); + float sinTheta1 = (float) Math.sin(theta1); + GL.glBegin(GL.GL_QUAD_STRIP); + float phi = 0.0f; + + for (int j = nsides; j >= 0; j--) { + phi += sideDelta; + float cosPhi = (float) Math.cos(phi); + float sinPhi = (float) Math.sin(phi); + float dist = R + r * cosPhi; + GL.glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi); + GL.glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi); + GL.glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi); + GL.glVertex3f(cosTheta * dist, -sinTheta * dist, r * sinPhi); + } + + GL.glEnd(); + theta = theta1; + cosTheta = cosTheta1; + sinTheta = sinTheta1; + } + } + + /** + * Creates a triangle centered at 0,0 in the viewport. + * + * @param width + * @param height + */ + void drawTriangle(float width, float height) { + GL.glBegin(GL.GL_TRIANGLES); + GL.glVertex3f(0.0f, height, 0.0f); // middle + GL.glVertex3f(-width, -height, 0.0f); // left + GL.glVertex3f(width, -height, 0.0f); // right + GL.glEnd(); + } + + /** + * @see OpenGLTab#getSleepLength() + */ + int getSleepLength() { + return SLEEP_LENGTH; + } + + /** + * @see OpenGLTab#getTabText() + */ + String getTabText() { + return "Objects"; + } + + /** + * @see OpenGLTab#init() + */ + void init() { + GL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + GL.glColor3f(1.0f, 0.0f, 0.0f); + GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); + GL.glClearDepth(1.0); + quadratic = GLU.gluNewQuadric(); + GLU.gluQuadricNormals(quadratic, GLU.GLU_SMOOTH); + GL.glLineWidth(2); + + GL.glEnable(GL.GL_DEPTH_TEST); + + // create the display lists and shapes + shapes = new Shape[11]; + int index = 1; + + GL.glNewList(index, GL.GL_COMPILE); + drawTriangle(2.0f, 2.0f); + GL.glEndList(); + shapes[0] = new Shape("Triangle", index++); + + GL.glNewList(index, GL.GL_COMPILE); + drawPyramid(2.0f, 2.0f, 2.0f); + GL.glEndList(); + shapes[1] = new Shape("Pyramid", index++); + + GL.glNewList(index, GL.GL_COMPILE); + drawSquare(2.0f, 2.0f); + GL.glEndList(); + shapes[2] = new Shape("Square", index++); + + GL.glNewList(index, GL.GL_COMPILE); + drawCube(2.0f, 2.0f, 2.0f); + GL.glEndList(); + shapes[3] = new Shape("Cube", index++); + + GL.glNewList(index, GL.GL_COMPILE); + drawCircle(2.0f, 0.0f, 0.0f); + GL.glEndList(); + shapes[4] = new Shape("Circle", index++); + + GL.glNewList(index, GL.GL_COMPILE); + GLU.gluPartialDisk(quadratic, 0.5, 2.0, 18, 18, 90, 125); + GL.glEndList(); + shapes[5] = new Shape("Partial Disk", index++); + + GL.glNewList(index, GL.GL_COMPILE); + GLU.gluDisk(quadratic, 0.5, 2.0, 18, 18); + GL.glEndList(); + shapes[6] = new Shape("Disk", index++); + + GL.glNewList(index, GL.GL_COMPILE); + drawTorus(1, 2, 18, 18); + GL.glEndList(); + shapes[7] = new Shape("Torus", index++); + + GL.glNewList(index, GL.GL_COMPILE); + GLU.gluSphere(quadratic, 2.0, 18, 18); + GL.glEndList(); + shapes[8] = new Shape("Sphere", index++); + + GL.glNewList(index, GL.GL_COMPILE); + GLU.gluCylinder(quadratic, 2.0, 2.0, 4, 18, 18); + GL.glEndList(); + shapes[9] = new Shape("Cylinder", index++); + + GL.glNewList(index, GL.GL_COMPILE); + GLU.gluCylinder(quadratic, 0, 2.0, 4, 18, 18); + GL.glEndList(); + shapes[10] = new Shape("Cone", index++); + + currentShape = shapes[0]; + } + + /** + * @see OpenGLTab#renderScene() + */ + void renderScene() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + GL.glLoadIdentity(); + GL.glTranslatef(0.0f, 0.0f, -14.0f); + // rotate around X and Y axis + GL.glRotatef(yRot, 0.0f, 1.0f, 0.0f); + GL.glRotatef(xRot, 1.0f, 0.0f, 0.0f); + // sets polygon fill mode + if (fill) { + GL.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL); + } else { + GL.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE); + } + currentShape.draw(); + yRot += 1.5; + xRot += 1.5; + } +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/OpenGLExample.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/OpenGLExample.java new file mode 100644 index 0000000000..9e7e10d53f --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/OpenGLExample.java @@ -0,0 +1,116 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class OpenGLExample { + private TabFolder tabFolder; + private OpenGLTab[] tabs; + private int sleep; + + /** + * Renders the scene of the current tab. + */ + void display() { + int index = tabFolder.getSelectionIndex(); + tabs[index].render(); + tabs[index].swap(); + } + + /** + * Disposes of all contained tabs. + */ + void dispose() { + tabFolder = null; + for (int i = 0; i < tabs.length; i++) { + tabs[i].dispose(); + } + } + + /** + * Runs the OpenGL example + */ + void run() { + final Display display = new Display(); + final Shell shell = + new Shell(display, SWT.SHELL_TRIM | SWT.NO_BACKGROUND); + shell.setLayout(new FillLayout()); + + tabFolder = new TabFolder(shell, SWT.NONE); + tabs = + new OpenGLTab[] { + new AntialiasingTab(), + new AreaTab(), + new BezierTab(), + new BitmapTextTab(), + new FogTab(), + new GradientTab(), + new LightTab(), + new NurbTab(), + new ObjectsTab(), + // outline tab is windows specific + new OutlineTextTab (), + new ReflectionTab(), + new StencilTab(), + new TextureTab(), + new TransparencyTab()}; + + for (int i = 0; i < tabs.length; i++) { + TabItem item = new TabItem(tabFolder, SWT.NONE); + item.setText(tabs[i].getTabText()); + item.setControl(tabs[i].createTabFolderPage(tabFolder)); + } + + tabFolder.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + OpenGLTab tab = tabs[tabFolder.getSelectionIndex()]; + tab.setCurrent(); + sleep = tab.getSleepLength(); + } + }); + if (tabs.length > 0) { + tabs[0].setCurrent(); + sleep = tabs[0].getSleepLength(); + } + + shell.setText("OpenGL Example"); + Runnable timer = new Runnable() { + public void run() { + if (shell.isDisposed()) return; + display(); + display.timerExec(sleep, this); + } + }; + timer.run(); + shell.addListener(SWT.Dispose, new Listener() { + public void handleEvent(Event e) { + dispose(); + } + }); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) { + display.sleep(); + } + } + } + + /** + * Invokes as a standalone program. + */ + public static void main(String[] args) { + new OpenGLExample().run(); + } +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/OpenGLTab.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/OpenGLTab.java new file mode 100644 index 0000000000..d8523b9e0c --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/OpenGLTab.java @@ -0,0 +1,243 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.opengl.*; + +/** + * <code>OpenGLTab</code> is the abstract superclass of every page + * in the example's tab folder. Each page in the tab folder + * displays a different example. + * + * An OpenGLTab itself is not a control but instead provides a hierarchy + * with which to share code that is common to every page in the folder. + */ +abstract class OpenGLTab { + private Canvas glCanvas; + private Composite tabFolderPage; + private GLContext context; + private boolean stencilSupport; + private final static int DEFAULT_SLEEP_LENGTH = 100; + + /** + * Creates this tab's controls. Subclasses must override. + * + * @param composite the parent composite + */ + abstract void createControls(Composite composite); + + /** + * Creates the OpenGL canvas on which all drawing is done. + */ + void createOpenGLContext() { + context = new GLContext(glCanvas); + setCurrent(); + setupViewingArea(); + } + + /** + * Creates the tab folder page. + * + * @param tabFolder the parent tab folder + * @return the new page + */ + Composite createTabFolderPage(TabFolder tabFolder) { + tabFolderPage = new Composite(tabFolder, SWT.NONE); + tabFolderPage.setLayout(new GridLayout(2, false)); + + GridData gridData = new GridData(); + gridData.heightHint = 400; + gridData.widthHint = 400; + gridData.verticalAlignment = GridData.BEGINNING; + glCanvas = new Canvas(tabFolderPage, SWT.NONE); + glCanvas.setLayout(new GridLayout()); + glCanvas.setLayoutData(gridData); + glCanvas.setSize(400, 400); // needed for windows + + gridData = new GridData(); + gridData.verticalAlignment = GridData.BEGINNING; + Composite controlComposite = new Composite(tabFolderPage, SWT.NONE); + controlComposite.setLayout(new GridLayout()); + controlComposite.setLayoutData(gridData); + + // create the OpenGL Screen and controls + createOpenGLContext(); + + // determine if native stencil support is available + int[] param = new int[1]; + GL.glGetIntegerv(GL.GL_STENCIL_BITS, param); + stencilSupport = param[0] != 0; + + init(); + + if (!isStencilSupportNeeded() || hasStencilSupport()) { + createControls(controlComposite); + } else { + Label label = new Label(controlComposite, SWT.NONE); + label.setText("This tab requires native stencil support."); + } + + return tabFolderPage; + } + + /** + * Disposes all resources allocated by this tab. + */ + void dispose() { + context.dispose(); + } + + /** + * Returns the context for this tab. + * + * @return GLContext + */ + GLContext getContext() { + return context; + } + + /** + * Returns the glCanvas for this tab. + * + * @return Canvas + */ + Canvas getGlCanvas() { + return glCanvas; + } + + /** + * Returns the length of time in milliseconds that the example + * should sleep between animation redraws. As this length + * increases, user responsiveness increases and the frequency of + * animation redraws decreases. Subclasses with moving animations + * may wish to override this default implementation to return a + * smaller value if their animations do not occur frequently enough. + * + * @return the length of time in milliseconds to sleep between redraws + */ + int getSleepLength() { + return DEFAULT_SLEEP_LENGTH; + } + + /** + * Returns the text for this tab. Subclasses must override. + * + * @return the text for the tab item + */ + abstract String getTabText(); + + /** + * Returns whether this machine has native stencils support. + * + * @return boolean + */ + boolean hasStencilSupport() { + return stencilSupport; + } + + /** + * Initialize OpenGL resources for this tab. Subclasses must override. + */ + abstract void init(); + + /** + * Loads a texture. + * + * @param context + * @param fileName + * @param index + * @param texture[] + */ + static void loadTexture(GLContext context, String fileName, int index, int[] texture) { + GL.glBindTexture(GL.GL_TEXTURE_2D, texture[index]); + ImageData source = + new ImageData(OpenGLTab.class.getResourceAsStream(fileName)); + Image image = new Image(Display.getCurrent(), source); + Image newImage = new Image(Display.getCurrent(), 256, 256); + GC gc = new GC(newImage); + gc.drawImage(image, 0, 0, source.width, source.height, 0, 0, 256, 256); + source = newImage.getImageData(); + gc.dispose(); + source = context.convertImageData(source); + newImage.dispose(); + image.dispose(); + GL.glTexImage2D( + GL.GL_TEXTURE_2D, 0, 3, + source.width, source.height, 0, + GL.GL_RGB, GL.GL_UNSIGNED_BYTE, source.data); + GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR); + GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); + } + + /** + * Renders this tab. + */ + void render() { + if (!isStencilSupportNeeded() || hasStencilSupport()) { + renderScene(); + } else { + GL.glClear(GL.GL_COLOR_BUFFER_BIT); + } + } + + /** + * Renders the scene for this tab. Subclasses must override. + */ + abstract void renderScene(); + + /** + * Returns whether this tab requires stencil support in order to display + * properly. Subclasses may wish to override this method. + * + * @return boolean + */ + boolean isStencilSupportNeeded() { + return false; + } + + /** + * Sets this rendering context to be current. + */ + void setCurrent() { + context.setCurrent(); + } + + /** + * Sets up the viewing area for the OpenGL screen. The default + * behavior is to use a perspective view, but there also exist frustrum + * and ortho views. Subclasses may wish to override this method. + */ + void setupViewingArea() { + Rectangle rect = glCanvas.getClientArea(); + int width = rect.width; + int height = rect.height; + height = Math.max(height, 1); + GL.glViewport(0, 0, width, height); + GL.glMatrixMode(GL.GL_PROJECTION); // select the projection matrix + GL.glLoadIdentity(); // reset the projection matrix + float fAspect = (float) width / (float) height; + GLU.gluPerspective(45.0f, fAspect, 0.5f, 400.0f); + GL.glMatrixMode(GL.GL_MODELVIEW); // select the modelview matrix + GL.glLoadIdentity(); + } + + /** + * Swaps the buffers. + */ + void swap() { + context.swapBuffers(); + } +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/OutlineTextTab.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/OutlineTextTab.java new file mode 100644 index 0000000000..0742f0056d --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/OutlineTextTab.java @@ -0,0 +1,251 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.opengl.*; +import org.eclipse.swt.widgets.*; + +class OutlineTextTab extends OpenGLTab { + private Text messageText; + private FontData fontData; + private float[] textColor = { 1.0f, 0.0f, 0.0f }; + private boolean fill = true; + private float extrude = 0.0f; + private float xPos = 0.0f, yPos = 0.0f, zPos = -10.0f; + private float xRot = 0.0f; + private int listIndexBase; + private final static int LIST_INDEX_SIZE = 256; + private final static int DEFAULT_FONT_SIZE = 24; + private final static String DEFAULT_FONT_NAME = "Arial"; + private final static int SLEEP_LENGTH = 50; + + /** + * @see OpenGLTab#createControls(Composite) + */ + void createControls(final Composite composite) { + if (!SWT.getPlatform().startsWith("win32")) { + new Label(composite, SWT.NONE).setText("This tab requires win32."); + return; + } + + Group movementGroup = new Group(composite, SWT.NONE); + movementGroup.setText("Translation"); + movementGroup.setLayout(new GridLayout(2, false)); + + new Label(movementGroup, SWT.NONE).setText("X:"); + final Slider xMove = new Slider(movementGroup, SWT.NONE); + xMove.setIncrement(1); + xMove.setMaximum(22); + xMove.setMinimum(0); + xMove.setThumb(2); + xMove.setPageIncrement(2); + xMove.setSelection(10); + xMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + xPos = xMove.getSelection() - 10; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Y:"); + final Slider yMove = new Slider(movementGroup, SWT.NONE); + yMove.setIncrement(1); + yMove.setMaximum(22); + yMove.setMinimum(0); + yMove.setThumb(2); + yMove.setPageIncrement(2); + yMove.setSelection(10); + yMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + yPos = yMove.getSelection() - 10; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Z:"); + final Slider zMove = new Slider(movementGroup, SWT.NONE); + zMove.setIncrement(1); + zMove.setMaximum(22); + zMove.setMinimum(0); + zMove.setThumb(2); + zMove.setPageIncrement(2); + zMove.setSelection(10); + zMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + zPos = zMove.getSelection() - 20; + } + }); + + Composite textGroup = new Composite(composite,SWT.NONE); + GridLayout layout = new GridLayout(2,false); + layout.marginHeight = 0; + layout.marginWidth = 0; + textGroup.setLayout(layout); + textGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); + + new Label(textGroup, SWT.NONE).setText("Text:"); + messageText = new Text(textGroup, SWT.BORDER); + GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); + data.grabExcessHorizontalSpace = true; + messageText.setLayoutData(data); + messageText.setText("OpenGL - SWT"); + + new Label(composite, SWT.NONE).setText("Extrude:"); + final Slider extrudeSlider = new Slider(composite, SWT.NONE); + extrudeSlider.setIncrement(1); + extrudeSlider.setMaximum(22); + extrudeSlider.setMinimum(0); + extrudeSlider.setThumb(2); + extrudeSlider.setPageIncrement(2); + extrudeSlider.setSelection(0); + extrudeSlider.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + extrude = (float) extrudeSlider.getSelection() / 10; + getContext().loadOutlineFont( + fontData, extrudeSlider.getDisplay(), + listIndexBase, 0, 255, 0.0f, extrude, + fill ? GL.GL_POLYGON : GL.GL_LINE, + null); + } + }); + + final Button fontSelectButton = new Button(composite, SWT.NONE); + fontSelectButton.setText("Set Font"); + final ColorSelectionGroup colorGroup = + new ColorSelectionGroup(composite, SWT.NONE); + colorGroup.setText("Text color"); + colorGroup.addColorSelectionListener(new IColorSelectionListener() { + public void handleColorSelection(RGB rgb) { + GL.glColor3ub((byte) rgb.red, (byte) rgb.green, (byte) rgb.blue); + } + }); + + fontSelectButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + FontDialog fontDialog = new FontDialog(fontSelectButton.getShell()); + fontDialog.setText("Choose Font Options"); + double[] currentColor = new double[4]; + GL.glGetDoublev(GL.GL_CURRENT_COLOR, currentColor); + fontDialog.setRGB( + new RGB( + (int) currentColor[0] * 255, + (int) currentColor[1] * 255, + (int) currentColor[2] * 255)); + fontDialog.setFontData(fontData); + FontData result = fontDialog.open(); + if (result != null) { + fontData = result; + RGB rgb = fontDialog.getRGB(); + GL.glColor3ub((byte) rgb.red, (byte) rgb.green, (byte) rgb.blue); + colorGroup.setRGB(rgb); + getContext().loadOutlineFont( + fontData, fontSelectButton.getDisplay(), + listIndexBase, 0, 255, 0.0f, extrude, + fill ? GL.GL_POLYGON : GL.GL_LINE, + null); + } + } + }); + + final Button fillButton = new Button(composite, SWT.CHECK); + fillButton.setText("Fill"); + fillButton.setSelection(true); + fillButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + fill = fillButton.getSelection(); + getContext().loadOutlineFont( + fontData, fillButton.getDisplay(), + listIndexBase, 0, 255, 0.0f, extrude, + fill ? GL.GL_POLYGON : GL.GL_LINE, + null); + } + }); + } + + /** + * @see OpenGLTab#dispose() + */ + void dispose() { + super.dispose(); + GL.glDeleteLists(listIndexBase, LIST_INDEX_SIZE); + } + + /** + * Draws the text to the screen + * + * @param string the text to draw + */ + void drawText(String string) { + char[] stringChars = string.toCharArray(); + int[] text = new int[stringChars.length]; + for (int i = 0; i < text.length; i++) { + text[i] = (int) stringChars[i]; + } + GL.glPushAttrib(GL.GL_LIST_BIT); + GL.glListBase(listIndexBase); + GL.glCallLists(text.length, GL.GL_UNSIGNED_INT, text); + GL.glPopAttrib(); + } + + /** + * @see OpenGLTab#getSleepLength() + */ + int getSleepLength() { + return SLEEP_LENGTH; + } + + /** + * @see OpenGLTab#getTabText() + */ + String getTabText() { + return "Outline Text"; + } + + /** + * @see OpenGLTab#init() + */ + void init() { + GL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + GL.glColor3fv(textColor); + GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); + GL.glEnable(GL.GL_DEPTH_TEST); + GL.glEnable(GL.GL_BLEND); + // build the initial font + listIndexBase = GL.glGenLists(LIST_INDEX_SIZE); + fontData = new FontData(); + fontData.setHeight(DEFAULT_FONT_SIZE); + fontData.setName(DEFAULT_FONT_NAME); + getContext().loadOutlineFont( + fontData, getGlCanvas().getDisplay(), + listIndexBase, 0, 255, 0f, extrude, + fill ? GL.GL_POLYGON : GL.GL_LINE, + null); + } + + /** + * @see OpenGLTab#renderScene() + */ + void renderScene() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + GL.glLoadIdentity(); + GL.glTranslatef(xPos, yPos, zPos); + GL.glRotatef(xRot, 1.0f, 0.0f, 0.0f); // rotate on X axis + // use GL.glScalef to change size since outline fonts + // don't support size + float size = (float) fontData.getHeight() / 24; + GL.glScalef(size, size, 1.0f); + // draw the text, assuming that we're running on win32 + if (messageText != null) drawText(messageText.getText()); + xRot += 1.2f; + } +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/ReflectionTab.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/ReflectionTab.java new file mode 100644 index 0000000000..883bda00f6 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/ReflectionTab.java @@ -0,0 +1,556 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.opengl.*; +import org.eclipse.swt.widgets.*; + +class ReflectionTab extends OpenGLTab { + private float ballY = 0.0f; + private float ballZ = 1.6666666f; + private float ballX = 0.0f; + private float ballRot = 0.0f; + private float cubeX = 2.5f; + private float cubeY = -2.0f; + private float cubeZ = 2.0f; + private float xPos = 0.0f; + private float yPos = 0.0f; + private float zPos = 22.0f; + private float ballRotSpeed = 0.0f; + private int quadratic; + private int[] textures = new int[3]; + private final static int + LIST_INDEX_BALL = 1,LIST_INDEX_BOX = 2, + LIST_INDEX_MIRROR = 3, LIST_INDEX_ROOM = 4; + private final static int + TEXTURE_INDEX_BALL = 0, TEXTURE_INDEX_FLOOR = 1, + TEXTURE_INDEX_BOX = 2; + private final static String[] IMAGES = { + "images/Ball.jpg", "images/Floor.jpg", "images/Box.bmp" }; + private final static int SLEEP_LENGTH = 50; + + /** + * Draws the ball + */ + void createBall() { + GL.glNewList(LIST_INDEX_BALL, GL.GL_COMPILE); + GL.glColor3f(1.0f, 1.0f, 1.0f); + GL.glBindTexture(GL.GL_TEXTURE_2D, textures[TEXTURE_INDEX_BALL]); + GLU.gluSphere(quadratic, 0.35f, 32, 16); + GL.glDisable(GL.GL_BLEND); + GL.glEndList(); + } + + /** + * Draws the box + */ + void createBox() { + float size = 1.0f; + GL.glNewList(LIST_INDEX_BOX, GL.GL_COMPILE); + GL.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + GL.glBindTexture(GL.GL_TEXTURE_2D, textures[TEXTURE_INDEX_BOX]); + GL.glBegin(GL.GL_QUADS); + // front + GL.glNormal3f(0.0f, 0.0f, 1.0f); + GL.glTexCoord2f(0.0f, 0.0f); + GL.glVertex3f(-size, -size, size); + GL.glTexCoord2f(1.0f, 0.0f); + GL.glVertex3f(size, -size, size); + GL.glTexCoord2f(1.0f, 1.0f); + GL.glVertex3f(size, size, size); + GL.glTexCoord2f(0.0f, 1.0f); + GL.glVertex3f(-size, size, size); + // back + GL.glNormal3f(0.0f, 0.0f, -1.0f); + GL.glTexCoord2f(0.0f, 0.0f); + GL.glVertex3f(-size, -size, -size); + GL.glTexCoord2f(1.0f, 0.0f); + GL.glVertex3f(size, -size, -size); + GL.glTexCoord2f(1.0f, 1.0f); + GL.glVertex3f(size, size, -size); + GL.glTexCoord2f(0.0f, 1.0f); + GL.glVertex3f(-size, size, -size); + // left + GL.glNormal3f(-1.0f, 0.0f, 0.0f); + GL.glTexCoord2f(0.0f, 0.0f); + GL.glVertex3f(-size, -size, -size); + GL.glTexCoord2f(1.0f, 0.0f); + GL.glVertex3f(-size, -size, size); + GL.glTexCoord2f(1.0f, 1.0f); + GL.glVertex3f(-size, size, size); + GL.glTexCoord2f(0.0f, 1.0f); + GL.glVertex3f(-size, size, -size); + // right + GL.glNormal3f(1.0f, 0.0f, 0.0f); + GL.glTexCoord2f(0.0f, 0.0f); + GL.glVertex3f(size, -size, -size); + GL.glTexCoord2f(1.0f, 0.0f); + GL.glVertex3f(size, -size, size); + GL.glTexCoord2f(1.0f, 1.0f); + GL.glVertex3f(size, size, size); + GL.glTexCoord2f(0.0f, 1.0f); + GL.glVertex3f(size, size, -size); + // top + GL.glNormal3f(0.0f, 1.0f, 0.0f); + GL.glTexCoord2f(0.0f, 0.0f); + GL.glVertex3f(size, size, -size); + GL.glTexCoord2f(1.0f, 0.0f); + GL.glVertex3f(-size, size, -size); + GL.glTexCoord2f(1.0f, 1.0f); + GL.glVertex3f(-size, size, size); + GL.glTexCoord2f(0.0f, 1.0f); + GL.glVertex3f(size, size, size); + // bottom + GL.glNormal3f(0.0f, -1.0f, 0.0f); + GL.glTexCoord2f(0.0f, 0.0f); + GL.glVertex3f(size, -size, -size); + GL.glTexCoord2f(1.0f, 0.0f); + GL.glVertex3f(-size, -size, -size); + GL.glTexCoord2f(1.0f, 1.0f); + GL.glVertex3f(-size, -size, size); + GL.glTexCoord2f(0.0f, 1.0f); + GL.glVertex3f(size, -size, size); + GL.glEnd(); + GL.glEndList(); + } + + /** + * @see OpenGLTab#createControls(Composite) + */ + void createControls(Composite composite) { + Group ballTranslation = new Group(composite, SWT.NONE); + ballTranslation.setLayout(new GridLayout(2, false)); + ballTranslation.setText("Ball Translation"); + + new Label(ballTranslation, SWT.NONE).setText("X:"); + final Slider ballXPosSlider = new Slider(ballTranslation, SWT.NONE); + ballXPosSlider.setIncrement(1); + ballXPosSlider.setMaximum(38); + ballXPosSlider.setMinimum(0); + ballXPosSlider.setThumb(2); + ballXPosSlider.setPageIncrement(2); + ballXPosSlider.setSelection(18); + ballXPosSlider.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + ballX = (float) (ballXPosSlider.getSelection() - 18) / 5; + } + }); + + new Label(ballTranslation, SWT.NONE).setText("Y:"); + final Slider ballYPosSlider = new Slider(ballTranslation, SWT.NONE); + ballYPosSlider.setIncrement(1); + ballYPosSlider.setMaximum(38); + ballYPosSlider.setMinimum(0); + ballYPosSlider.setThumb(2); + ballYPosSlider.setPageIncrement(2); + ballYPosSlider.setSelection(18); + ballYPosSlider.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + ballY = (float) (ballYPosSlider.getSelection() - 18) / 5; + } + }); + + new Label(ballTranslation, SWT.NONE).setText("Z:"); + final Slider ballZPosSlider = new Slider(ballTranslation, SWT.NONE); + ballZPosSlider.setIncrement(1); + ballZPosSlider.setMaximum(24); + ballZPosSlider.setMinimum(0); + ballZPosSlider.setThumb(1); + ballZPosSlider.setPageIncrement(2); + ballZPosSlider.setSelection(4); + ballZPosSlider.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + ballZ = (float) (ballZPosSlider.getSelection() + 1) / 3; + } + }); + + Group cubeTranslation = new Group(composite, SWT.NONE); + cubeTranslation.setLayout(new GridLayout(2, false)); + cubeTranslation.setText("Cube Translation"); + + new Label(cubeTranslation, SWT.NONE).setText("X:"); + final Slider cubeXPosSlider = new Slider(cubeTranslation, SWT.NONE); + cubeXPosSlider.setIncrement(1); + cubeXPosSlider.setMaximum(14); + cubeXPosSlider.setMinimum(0); + cubeXPosSlider.setThumb(2); + cubeXPosSlider.setPageIncrement(2); + cubeXPosSlider.setSelection(11); + cubeXPosSlider.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + cubeX = (float) (cubeXPosSlider.getSelection() - 6) / 2; + } + }); + + new Label(cubeTranslation, SWT.NONE).setText("Y:"); + final Slider cubeYPosSlider = new Slider(cubeTranslation, SWT.NONE); + cubeYPosSlider.setIncrement(1); + cubeYPosSlider.setMaximum(14); + cubeYPosSlider.setMinimum(0); + cubeYPosSlider.setThumb(2); + cubeYPosSlider.setPageIncrement(2); + cubeYPosSlider.setSelection(2); + cubeYPosSlider.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + cubeY = (float) (cubeYPosSlider.getSelection() - 6) / 2; + } + }); + + new Label(cubeTranslation, SWT.NONE).setText("Z:"); + final Slider cubeZPosSlider = new Slider(cubeTranslation, SWT.NONE); + cubeZPosSlider.setIncrement(1); + cubeZPosSlider.setMaximum(10); + cubeZPosSlider.setMinimum(1); + cubeZPosSlider.setThumb(1); + cubeZPosSlider.setPageIncrement(2); + cubeZPosSlider.setSelection(2); + cubeZPosSlider.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + cubeZ = (float) (cubeZPosSlider.getSelection() + 2) / 2; + } + }); + + Group movementGroup = new Group(composite, SWT.NONE); + movementGroup.setLayout(new GridLayout(2, false)); + movementGroup.setText("Room Rotation"); + + new Label(movementGroup, SWT.NONE).setText("X:"); + final Slider screenXPosSlider = new Slider(movementGroup, SWT.NONE); + screenXPosSlider.setIncrement(1); + screenXPosSlider.setMaximum(42); + screenXPosSlider.setMinimum(0); + screenXPosSlider.setThumb(2); + screenXPosSlider.setPageIncrement(2); + screenXPosSlider.setSelection(20); + screenXPosSlider.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + xPos = screenXPosSlider.getSelection() - 20; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Y:"); + final Slider screenYPosSlider = new Slider(movementGroup, SWT.NONE); + screenYPosSlider.setIncrement(1); + screenYPosSlider.setMaximum(42); + screenYPosSlider.setMinimum(0); + screenYPosSlider.setThumb(2); + screenYPosSlider.setPageIncrement(2); + screenYPosSlider.setSelection(20); + screenYPosSlider.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + yPos = screenYPosSlider.getSelection() - 20; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Z:"); + final Slider screenZPosSlider = new Slider(movementGroup, SWT.NONE); + screenZPosSlider.setIncrement(1); + screenZPosSlider.setMaximum(42); + screenZPosSlider.setMinimum(0); + screenZPosSlider.setThumb(2); + screenZPosSlider.setPageIncrement(2); + screenZPosSlider.setSelection(20); + screenZPosSlider.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + zPos = screenZPosSlider.getSelection() + 2; + } + }); + + new Label(composite, SWT.NONE).setText("Ball Rotation Speed:"); + final Slider ballRotSlider = new Slider(composite, SWT.NONE); + ballRotSlider.setIncrement(1); + ballRotSlider.setMaximum(22); + ballRotSlider.setMinimum(0); + ballRotSlider.setThumb(2); + ballRotSlider.setPageIncrement(2); + ballRotSlider.setSelection(0); + ballRotSlider.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + ballRotSpeed = ballRotSlider.getSelection(); + } + }); + } + + /** + * Draws the mirror + */ + void createMirror() { + GL.glNewList(LIST_INDEX_MIRROR, GL.GL_COMPILE); + GL.glDisable(GL.GL_TEXTURE_2D); + GL.glColor4f(1.0f, 1.0f, 1.0f, 0.4f); + GL.glNormal3f(0.0f, 0.0f, 1.0f); + GL.glBegin(GL.GL_QUADS); + GL.glTexCoord2f(0.0f, 0.0f); + GL.glVertex3f(-2.0f, 2.0f, 0.0f); + GL.glTexCoord2f(1.0f, 0.0f); + GL.glVertex3f(-2.0f, -2.0f, 0.0f); + GL.glTexCoord2f(1.0f, 1.0f); + GL.glVertex3f(2.0f, -2.0f, 0.0f); + GL.glTexCoord2f(0.0f, 1.0f); + GL.glVertex3f(2.0f, 2.0f, 0.0f); + GL.glEnd(); + GL.glEnable(GL.GL_TEXTURE_2D); + GL.glEndList(); + } + + /** + * Draws the room + */ + void createRoom() { + GL.glNewList(LIST_INDEX_ROOM, GL.GL_COMPILE); + GL.glDisable(GL.GL_TEXTURE_2D); + GL.glBegin(GL.GL_QUADS); + // left wall + GL.glNormal3f(1.0f, 0.0f, 0.0f); + GL.glVertex3f(-4.0f, 4.0f, 0.0f); + GL.glVertex3f(-4.0f, 4.0f, 6.0f); + GL.glVertex3f(-4.0f, -4.0f, 6.0f); + GL.glVertex3f(-4.0f, -4.0f, 0.0f); + // right wall + GL.glNormal3f(-1.0f, 0.0f, 01.0f); + GL.glVertex3f(4.0f, 4.0f, 0.0f); + GL.glVertex3f(4.0f, 4.0f, 6.0f); + GL.glVertex3f(4.0f, -4.0f, 6.0f); + GL.glVertex3f(4.0f, -4.0f, 0.0f); + // ceiling + GL.glNormal3f(0.0f, -1.0f, 0.0f); + GL.glColor3f(0.9f, 0.9f, 0.9f); + GL.glVertex3f(-4.0f, 4.0f, 0.0f); + GL.glVertex3f(-4.0f, 4.0f, 6.0f); + GL.glVertex3f(4.0f, 4.0f, 6.0f); + GL.glVertex3f(4.0f, 4.0f, 0.0f); + GL.glEnd(); + // back wall with triangle strip to illiminate t-intersections + GL.glBegin(GL.GL_TRIANGLE_STRIP); + GL.glNormal3f(0.0f, 0.0f, 1.0f); + GL.glVertex3d(-4.0, -4.0, 0.0); + GL.glVertex3d(-2.2, -4.0, 0.0); + GL.glVertex3d(-2.2, -2.2, 0.0); + GL.glVertex3d(2.2, -4.0, 0.0); + GL.glVertex3d(2.2, -2.2, 0.0); + GL.glVertex3d(4.0, -4.0, 0.0); + GL.glVertex3d(2.2, -2.2, 0.0); + GL.glVertex3d(4.0, -2.2, 0.0); + GL.glVertex3d(2.2, 2.2, 0.0); + GL.glVertex3d(4.0, 2.2, 0.0); + GL.glVertex3d(2.2, 2.2, 0.0); + GL.glVertex3d(4.0, 4.0, 0.0); + GL.glVertex3d(2.2, 2.2, 0.0); + GL.glVertex3d(2.2, 4.0, 0.0); + GL.glVertex3d(-2.2, 2.2, 0.0); + GL.glVertex3d(-2.2, 4.0, 0.0); + GL.glVertex3d(-4.0, 4.0, 0.0); + GL.glVertex3d(-2.2, 2.2, 0.0); + GL.glVertex3d(-4.0, 2.2, 0.0); + GL.glVertex3d(-2.2, -2.2, 0.0); + GL.glVertex3d(-4.0, -2.2, 0.0); + GL.glVertex3d(-4.0, -4.0, 0.0); + GL.glEnd(); + GL.glEnable(GL.GL_TEXTURE_2D); + // draw floor + GL.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + GL.glBindTexture(GL.GL_TEXTURE_2D, textures[TEXTURE_INDEX_FLOOR]); + GL.glNormal3f(0.0f, 1.0f, 0.0f); + GL.glBegin(GL.GL_QUADS); + // floor + GL.glTexCoord2f(0.0f, 0.0f); + GL.glVertex3f(-4.0f, -4.0f, 6.0f); + GL.glTexCoord2f(1.0f, 0.0f); + GL.glVertex3f(-4.0f, -4.0f, 0.0f); + GL.glTexCoord2f(1.0f, 1.0f); + GL.glVertex3f(4.0f, -4.0f, 0.0f); + GL.glTexCoord2f(0.0f, 1.0f); + GL.glVertex3f(4.0f, -4.0f, 6.0f); + GL.glEnd(); + // draw mirror frame + GL.glBindTexture(GL.GL_TEXTURE_2D, textures[TEXTURE_INDEX_BOX]); + GL.glNormal3f(0.0f, 0.0f, 1.0f); + GL.glBegin(GL.GL_QUADS); + // top + GL.glTexCoord2f(0.0f, 0.0f); + GL.glVertex3f(-2.2f, 2.0f, 0.0f); + GL.glTexCoord2f(1.0f, 0.0f); + GL.glVertex3f(2.2f, 2.0f, 0.0f); + GL.glTexCoord2f(1.0f, 1.0f); + GL.glVertex3f(2.2f, 2.2f, 0.0f); + GL.glTexCoord2f(0.0f, 1.0f); + GL.glVertex3f(-2.2f, 2.2f, 0.0f); + // right + GL.glTexCoord2f(0.0f, 0.0f); + GL.glVertex3f(2.0f, 2.0f, 0.0f); + GL.glTexCoord2f(1.0f, 0.0f); + GL.glVertex3f(2.0f, -2.0f, 0.0f); + GL.glTexCoord2f(1.0f, 1.0f); + GL.glVertex3f(2.2f, -2.0f, 0.0f); + GL.glTexCoord2f(0.0f, 1.0f); + GL.glVertex3f(2.2f, 2.0f, 0.0f); + // bottom + GL.glTexCoord2f(0.0f, 0.0f); + GL.glVertex3f(-2.2f, -2.0f, 0.0f); + GL.glTexCoord2f(1.0f, 0.0f); + GL.glVertex3f(2.2f, -2.0f, 0.0f); + GL.glTexCoord2f(1.0f, 1.0f); + GL.glVertex3f(2.2f, -2.2f, 0.0f); + GL.glTexCoord2f(0.0f, 1.0f); + GL.glVertex3f(-2.2f, -2.2f, 0.0f); + // left + GL.glTexCoord2f(0.0f, 0.0f); + GL.glVertex3f(-2.0f, 2.0f, 0.0f); + GL.glTexCoord2f(1.0f, 0.0f); + GL.glVertex3f(-2.0f, -2.0f, 0.0f); + GL.glTexCoord2f(1.0f, 1.0f); + GL.glVertex3f(-2.2f, -2.0f, 0.0f); + GL.glTexCoord2f(0.0f, 1.0f); + GL.glVertex3f(-2.2f, 2.0f, 0.0f); + GL.glEnd(); + GL.glEndList(); + } + + /** + * @see OpenGLTab#dispose() + */ + void dispose() { + super.dispose(); + GLU.gluDeleteQuadric(quadratic); + GL.glDeleteLists(LIST_INDEX_BALL, 1); + GL.glDeleteLists(LIST_INDEX_BOX, 1); + GL.glDeleteLists(LIST_INDEX_MIRROR, 1); + GL.glDeleteLists(LIST_INDEX_ROOM, 1); + } + + /** + * @see OpenGLTab#getSleepLength() + */ + int getSleepLength() { + return SLEEP_LENGTH; + } + + /** + * @see OpenGLTab#getTabText() + */ + String getTabText() { + return "Reflection"; + } + + /** + * @see OpenGLTab#init() + */ + void init() { + if (!hasStencilSupport()) return; + + GL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + GL.glClearDepth(1.0f); + GL.glClearStencil(0); + GL.glGenTextures(IMAGES.length, textures); + for (int i = 0; i < IMAGES.length; i++) { + loadTexture(getContext(), IMAGES[i], i, textures); + } + + GL.glShadeModel(GL.GL_SMOOTH); + quadratic = GLU.gluNewQuadric(); + GLU.gluQuadricNormals(quadratic, GL.GL_SMOOTH); + GLU.gluQuadricTexture(quadratic, true); + GL.glDepthFunc(GL.GL_LEQUAL); + GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); + GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); + + // set up the lights + float[] LightAmb = { 0.7f, 0.7f, 0.7f, 1.0f }; + float[] LightDif = { 1.0f, 1.0f, 1.0f, 1.0f }; + float[] lightsAmbient = { 0.5f, 0.5f, 0.5f, 1.0f }; + GL.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT, lightsAmbient); + GL.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, LightAmb); + GL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, LightDif); + + GL.glEnable(GL.GL_LIGHT0); + GL.glEnable(GL.GL_LIGHTING); + GL.glEnable(GL.GL_LINE_SMOOTH); + GL.glEnable(GL.GL_DEPTH_TEST); + GL.glEnable(GL.GL_TEXTURE_2D); + GL.glEnable(GL.GL_BLEND); + + createRoom(); + createBall(); + createBox(); + createMirror(); + } + + /** + * @see OpenGLTab#isStencilSupportNeeded + */ + boolean isStencilSupportNeeded() { + return true; + } + + /** + * @see OpenGLTab#renderScene() + */ + void renderScene() { + GL.glClear( + GL.GL_COLOR_BUFFER_BIT + | GL.GL_DEPTH_BUFFER_BIT + | GL.GL_STENCIL_BUFFER_BIT); + + GL.glLoadIdentity(); + GLU.gluLookAt(xPos, yPos, zPos, 0, 0, 0, 0, 1, 0); + // clip plane equation + double clipEquation[] = { 0.0f, 0.0f, -1.0f, 0.0f }; + float[] LightPos = { 0.0f, 2.0f, 7.0f, 1.0f }; + GL.glColorMask(false, false, false, false); + // enable stencil buffer for "marking" the mirror + GL.glEnable(GL.GL_STENCIL_TEST); + // set the stencil buffer to 1 where a polygon is drawn + GL.glStencilFunc(GL.GL_ALWAYS, 1, 1); + GL.glStencilOp(GL.GL_KEEP, GL.GL_KEEP, GL.GL_REPLACE); + GL.glDisable(GL.GL_DEPTH_TEST); + GL.glCallList(LIST_INDEX_MIRROR); + GL.glEnable(GL.GL_DEPTH_TEST); + GL.glColorMask(true, true, true, true); + GL.glStencilFunc(GL.GL_EQUAL, 1, 1); + // draw where the stencil is 1 + GL.glStencilOp(GL.GL_KEEP, GL.GL_KEEP, GL.GL_KEEP); + GL.glEnable(GL.GL_CLIP_PLANE0); + GL.glClipPlane(GL.GL_CLIP_PLANE0, clipEquation); + GL.glPushMatrix(); + GL.glScalef(1.0f, 1.0f, -1.0f); + GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, LightPos); + GL.glCallList(LIST_INDEX_ROOM); // reflection + GL.glPushMatrix(); + GL.glTranslatef(cubeX, cubeY, cubeZ); + GL.glCallList(LIST_INDEX_BOX); // reflection + GL.glPopMatrix(); + GL.glTranslatef(ballX, ballY, ballZ); + GL.glRotatef(ballRot, 1.0f, 0.0f, 0.0f); + GL.glCallList(LIST_INDEX_BALL); // reflection + GL.glPopMatrix(); + GL.glDisable(GL.GL_CLIP_PLANE0); + GL.glDisable(GL.GL_STENCIL_TEST); + GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, LightPos); + GL.glEnable(GL.GL_BLEND); + GL.glDisable(GL.GL_LIGHTING); + GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); + GL.glCallList(LIST_INDEX_MIRROR); + GL.glEnable(GL.GL_LIGHTING); + GL.glDisable(GL.GL_BLEND); + GL.glCallList(LIST_INDEX_ROOM); + GL.glPushMatrix(); + GL.glTranslatef(cubeX, cubeY, cubeZ); + GL.glCallList(LIST_INDEX_BOX); + GL.glPopMatrix(); + GL.glTranslatef(ballX, ballY, ballZ); + GL.glRotatef(ballRot, 1.0f, 0.0f, 0.0f); + GL.glCallList(LIST_INDEX_BALL); + ballRot += ballRotSpeed; + } +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/SelectionTab.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/SelectionTab.java new file mode 100644 index 0000000000..e4c8fa8859 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/SelectionTab.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.opengl.*; + +abstract class SelectionTab extends OpenGLTab { + private final static int BUFFER_LENGTH = 64; + + /** + * Sets the selected object. Subclasses must override this method. + */ + abstract void processPick(int[] pSelectBuff, int hits); + + /** + * Invoke processPick for the object at the specified coordinate. + */ + int processSelection(int xPos, int yPos, int pointSize) { + int[] selectBuffer = new int[BUFFER_LENGTH]; + int[] viewport = new int[4]; + // new fix for sun jre + int ptr = getContext().getSelectBufferPtr(selectBuffer); + + GL.glSelectBuffer(BUFFER_LENGTH, ptr); + GL.glGetIntegerv(GL.GL_VIEWPORT, viewport); + GL.glRenderMode(GL.GL_SELECT); + GL.glMatrixMode(GL.GL_PROJECTION); + GL.glPushMatrix(); + GL.glLoadIdentity(); + GLU.gluPickMatrix(xPos, yPos, pointSize, pointSize, viewport); + Rectangle rect = getGlCanvas().getClientArea(); + float fAspect = (float) rect.width / (float) rect.height; + GLU.gluPerspective(45.0f, fAspect, 0.5f, 600.0f); + GL.glMatrixMode(GL.GL_MODELVIEW); + GL.glInitNames(); + render(); + int hits = GL.glRenderMode(GL.GL_RENDER); + // new fix for sun jre + // must be called after render mode is switched + getContext().getSelectBuffer(ptr, selectBuffer); + if (hits != 0) processPick(selectBuffer, hits); + GL.glMatrixMode(GL.GL_PROJECTION); + GL.glPopMatrix(); + GL.glMatrixMode(GL.GL_MODELVIEW); + return hits; + } +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/StencilTab.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/StencilTab.java new file mode 100644 index 0000000000..be5f4a4063 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/StencilTab.java @@ -0,0 +1,230 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.opengl.*; +import org.eclipse.swt.widgets.*; + +class StencilTab extends OpenGLTab { + private abstract class Shape { + private String name; + /** + * Constructor + * + * @param name the shape's name + */ + Shape(String name) { + super(); + this.name = name; + } + /** + * Draws this shape. + */ + abstract void draw(); + /** + * Returns the name. + * + * @return String + */ + String getName() { + return name; + } + } + + private Shape[] shapes = new Shape[5]; + private Shape currentShape; + private float xPos = 0, yPos = 0; + private float size = 0.45f; + private int texture; + private int quadratic; + private final static String IMAGE = "images/splash.bmp"; + private final static int SLEEP_LENGTH = 50; + + /** + * @see OpenGLTab#createControls(Composite) + */ + void createControls(Composite composite) { + Composite controls = new Composite(composite, SWT.NONE); + GridLayout layout = new GridLayout(2, false); + layout.marginHeight = 0; + layout.marginWidth = 0; + controls.setLayout(layout); + new Label(controls, SWT.NONE).setText("Object:"); + final Combo shapeCombo = new Combo(controls, SWT.READ_ONLY); + for (int i = 0; i < shapes.length; i++) { + shapeCombo.add(shapes[i].getName()); + } + shapeCombo.select(0); + shapeCombo.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + currentShape = shapes[shapeCombo.getSelectionIndex()]; + } + }); + + new Label(composite, SWT.NONE).setText("Size:"); + final Slider sizeSlider = new Slider(composite, SWT.HORIZONTAL); + sizeSlider.setValues(0, 15, 75, 5, 5, 10); + sizeSlider.setSelection(45); + sizeSlider.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + size = ((float) sizeSlider.getSelection()) / 100; + } + }); + } + + /** + * @see OpenGLTab#dispose() + */ + void dispose() { + super.dispose(); + GLU.gluDeleteQuadric(quadratic); + } + + /** + * @see OpenGLTab#getSleepLength() + */ + int getSleepLength() { + return SLEEP_LENGTH; + } + + /** + * @see OpenGLTab#getTabText() + */ + String getTabText() { + return "Stencil"; + } + + /** + * @see OpenGLTab#init() + */ + void init() { + if (!hasStencilSupport()) return; + + GL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + GL.glClearDepth(1.0f); + int[] textureOut = new int[1]; + GL.glGenTextures(1, textureOut); + loadTexture(getContext(), IMAGE, 0, textureOut); + this.texture = textureOut[0]; + quadratic = GLU.gluNewQuadric(); + GLU.gluQuadricNormals(quadratic, GLU.GLU_SMOOTH); + GL.glDepthFunc(GL.GL_LEQUAL); + GL.glEnable(GL.GL_STENCIL_TEST); + GL.glEnable(GL.GL_TEXTURE_2D); + GL.glEnable(GL.GL_DEPTH_TEST); + // create shapes + shapes[0] = new Shape("Triangle") { + public void draw() { + GL.glBegin(GL.GL_TRIANGLES); + GL.glVertex3f(0.0f, size, 0.0f); + GL.glVertex3f(-size, -size, 0.0f); + GL.glVertex3f(size, -size, 0.0f); + GL.glEnd(); + } + }; + + shapes[1] = new Shape("Disk") { + public void draw() { + GLU.gluDisk(quadratic, 0.0f, size, 32, 32); + } + }; + + shapes[2] = new Shape("Square") { + public void draw() { + GL.glRectf(-size, -size, size, size); + } + }; + + shapes[3] = new Shape("Hour Glass") { + public void draw() { + GL.glBegin(GL.GL_TRIANGLES); + GL.glVertex3f(0.0f, size, 0.0f); + GL.glVertex3f(-size, -size, 0.0f); + GL.glVertex3f(size, -size, 0.0f); + GL.glVertex3f(0.0f, -size, 0.0f); + GL.glVertex3f(size, size, 0.0f); + GL.glVertex3f(-size, size, 0.0f); + GL.glEnd(); + } + }; + + shapes[4] = new Shape("Star") { + public void draw() { + GL.glBegin(GL.GL_TRIANGLES); + GL.glVertex3f(-0.3f, 0, 0.0f); + GL.glVertex3f(2 * size - 0.3f, 0, 0.0f); + GL.glVertex3f(size - 0.3f, 2 * size * 0.85f, 0.0f); + GL.glVertex3f(2 * size - 0.3f, size, 0.0f); + GL.glVertex3f(0 - 0.3f, size, 0.0f); + GL.glVertex3f(size - 0.3f, -size * 0.85f, 0.0f); + GL.glEnd(); + } + }; + + currentShape = shapes[0]; + } + + /** + * @see OpenGLTab#isStencilSupportNeeded + */ + boolean isStencilSupportNeeded() { + return true; + } + + /** + * @see OpenGLTab#renderScene() + */ + void renderScene() { + GL.glClear( + GL.GL_COLOR_BUFFER_BIT + | GL.GL_DEPTH_BUFFER_BIT + | GL.GL_STENCIL_BUFFER_BIT); + + GL.glLoadIdentity(); + GL.glTranslatef( + (float) (1.5 * Math.cos(xPos)), + (float) (1.0 * Math.sin(yPos)), + -3.0f); + xPos += 0.15f; + yPos += 0.3f; + + GL.glColorMask(false, false, false, false); + GL.glStencilFunc(GL.GL_ALWAYS, 1, 1); + GL.glStencilOp(GL.GL_KEEP, GL.GL_KEEP, GL.GL_REPLACE); + GL.glDisable(GL.GL_DEPTH_TEST); + GL.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + + currentShape.draw(); + + GL.glEnable(GL.GL_DEPTH_TEST); + GL.glColorMask(true, true, true, true); + GL.glStencilFunc(GL.GL_EQUAL, 1, 1); + GL.glStencilOp(GL.GL_KEEP, GL.GL_KEEP, GL.GL_KEEP); + + GL.glLoadIdentity(); + // load the texture behind the stencil object + GL.glTranslatef(0.0f, 0.0f, -3.1f); + GL.glBindTexture(GL.GL_TEXTURE_2D, texture); + GL.glBegin(GL.GL_QUADS); + GL.glTexCoord2f(0.0f, 0.0f); + GL.glVertex3f(-0.85f, -0.85f, 1.0f); + GL.glTexCoord2f(1.0f, 0.0f); + GL.glVertex3f(0.85f, -0.85f, 1.0f); + GL.glTexCoord2f(1.0f, 1.0f); + GL.glVertex3f(0.85f, 0.85f, 1.0f); + GL.glTexCoord2f(0.0f, 1.0f); + GL.glVertex3f(-0.85f, 0.85f, 1.0f); + GL.glEnd(); + } +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/TextureTab.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/TextureTab.java new file mode 100644 index 0000000000..1375b6fdca --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/TextureTab.java @@ -0,0 +1,176 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.opengl.*; +import org.eclipse.swt.widgets.*; + +class TextureTab extends OpenGLTab { + private float yrot = 0, zrot = 0; + private float xPos = 0.0f, yPos = 0.0f, zPos = -20; + private int texture = 0; + private float[][][] points = new float[45][45][3]; + private final static String IMAGE = "images/splash.bmp"; + private final static int SLEEP_LENGTH = 50; + + /** + * @see OpenGLTab#createControls(Composite) + */ + void createControls(Composite composite) { + Group movementGroup = new Group(composite, SWT.NONE); + movementGroup.setText("Translation"); + movementGroup.setLayout(new GridLayout(2, false)); + + new Label(movementGroup, SWT.NONE).setText("X:"); + final Slider xMove = new Slider(movementGroup, SWT.NONE); + xMove.setIncrement(1); + xMove.setMaximum(12); + xMove.setMinimum(0); + xMove.setThumb(2); + xMove.setPageIncrement(2); + xMove.setSelection(5); + xMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + xPos = xMove.getSelection() - 5; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Y:"); + final Slider yMove = new Slider(movementGroup, SWT.NONE); + yMove.setIncrement(1); + yMove.setMaximum(12); + yMove.setMinimum(0); + yMove.setThumb(2); + yMove.setPageIncrement(2); + yMove.setSelection(5); + yMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + yPos = yMove.getSelection() - 5; + } + }); + + new Label(movementGroup, SWT.NONE).setText("Z:"); + final Slider zMove = new Slider(movementGroup, SWT.NONE); + zMove.setIncrement(1); + zMove.setMaximum(24); + zMove.setMinimum(0); + zMove.setThumb(4); + zMove.setPageIncrement(2); + zMove.setSelection(10); + zMove.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + zPos = zMove.getSelection() - 30; + } + }); + } + + /** + * @see OpenGLTab#getSleepLength() + */ + int getSleepLength() { + return SLEEP_LENGTH; + } + + /** + * @see OpenGLTab#getTabText() + */ + String getTabText() { + return "Texture"; + } + + /** + * @see OpenGLTab#init() + */ + void init() { + //GL.glClearColor(0.0f, 0.0f, 0.3f, 1.0f); + GL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + GL.glClearDepth(1.0f); + int[] textureOut = new int[1]; + GL.glGenTextures(1, textureOut); + loadTexture(getContext(), IMAGE, 0, textureOut); + texture = textureOut[0]; + GL.glDepthFunc(GL.GL_LEQUAL); + GL.glPolygonMode(GL.GL_BACK, GL.GL_FILL); + GL.glPolygonMode(GL.GL_FRONT, GL.GL_LINE); + GL.glEnable(GL.GL_LIST_MODE); + GL.glEnable(GL.GL_TEXTURE_2D); + GL.glEnable(GL.GL_DEPTH_TEST); + // initialize points + for (int x = 0; x < 45; x++) { + for (int y = 0; y < 45; y++) { + points[x][y][0] = x / 5.0f - 4.5f; + points[x][y][1] = y / 5.0f - 4.5f; + points[x][y][2] = + (float) Math.sin( + (((x / 5.0f) * 40.0f) / 360.0f) * Math.PI * 2); + } + } + } + + /** + * @see OpenGLTab#renderScene() + */ + void renderScene() { + GL.glClear( + GL.GL_COLOR_BUFFER_BIT + | GL.GL_DEPTH_BUFFER_BIT + | GL.GL_STENCIL_BUFFER_BIT); + GL.glLoadIdentity(); + GL.glTranslatef(xPos, yPos, zPos); + GL.glRotatef(yrot, 0.0f, 1.0f, 0.0f); + GL.glRotatef(zrot, 0.0f, 0.0f, 1.0f); + GL.glBindTexture(GL.GL_TEXTURE_2D, texture); + + GL.glBegin(GL.GL_QUADS); + for (int x = 0; x < 44; x++) { + for (int y = 0; y < 44; y++) { + float fx = x / 44.0f; + float fy = y / 44.0f; + float fxb = (x + 1) / 44.0f; + float fyb = (y + 1) / 44.0f; + GL.glTexCoord2f(fx, fy); + GL.glVertex3f( + points[x][y][0], + points[x][y][1], + points[x][y][2]); + GL.glTexCoord2f(fx, fyb); + GL.glVertex3f( + points[x][y + 1][0], + points[x][y + 1][1], + points[x][y + 1][2]); + GL.glTexCoord2f(fxb, fyb); + GL.glVertex3f( + points[x + 1][y + 1][0], + points[x + 1][y + 1][1], + points[x + 1][y + 1][2]); + GL.glTexCoord2f(fxb, fy); + GL.glVertex3f( + points[x + 1][y][0], + points[x + 1][y][1], + points[x + 1][y][2]); + } + } + GL.glEnd(); + // perform wave motion + for (int y = 0; y < 45; y++) { + float hold = points[0][y][2]; + for (int x = 0; x < 44; x++) { + points[x][y][2] = points[x + 1][y][2]; + } + points[44][y][2] = hold; + } + yrot += 0.9f; + zrot += 0.6f; + } +}
\ No newline at end of file diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/TransparencyTab.java b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/TransparencyTab.java new file mode 100644 index 0000000000..11f39fbe63 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/TransparencyTab.java @@ -0,0 +1,197 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.opengl.examples; + + +import org.eclipse.swt.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.opengl.*; +import org.eclipse.swt.widgets.*; + +class TransparencyTab extends SelectionTab { + private float[] alphas = { 0.3f, 0.5f, 1.0f }; + private int quadratic; + private int currentSelection = 1; + private final static int + INDEX_TRIANGLE = 1, INDEX_SPHERE = 2, INDEX_DISK = 3; + private final static String[] OBJECTS = {"Triangle", "Sphere", "Disk" }; + + /** + * @see OpenGLTab#createControls(Composite) + */ + void createControls(Composite composite) { + Composite objectGroup = new Composite(composite, SWT.NONE); + GridLayout layout = new GridLayout(2, false); + layout.marginHeight = 0; + layout.marginWidth = 0; + objectGroup.setLayout(layout); + objectGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); + + new Label(objectGroup, SWT.NONE).setText("Object:"); + final Combo objectCombo = new Combo(objectGroup, SWT.READ_ONLY); + GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); + data.grabExcessHorizontalSpace = true; + objectCombo.setLayoutData(data); + objectCombo.setItems(OBJECTS); + objectCombo.select(0); + + new Label(composite, SWT.NONE).setText("Transparency:"); + final Slider transparencySlider = new Slider(composite, SWT.HORIZONTAL); + transparencySlider.setValues(0, 0, 11, 1, 1, 2); + transparencySlider.setSelection(7); + transparencySlider.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + float alpha = transparencySlider.getSelection(); + alpha = 1.0f - alpha / 10; + alphas[currentSelection - 1] = alpha; + } + }); + objectCombo.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + currentSelection = objectCombo.getSelectionIndex() + 1; + transparencySlider.setSelection( + (int) ((1.0f - alphas[currentSelection - 1]) * 10)); + } + }); + + final Canvas glCanvas = getGlCanvas(); + glCanvas.addMouseListener(new MouseAdapter() { + public void mouseUp(MouseEvent e) { + Rectangle rect = glCanvas.getClientArea(); + e.y = rect.height - e.y; + if (e.button == 1) { + processSelection(e.x, e.y, 2); + transparencySlider.setSelection( + (int) ((1.0f - alphas[currentSelection - 1]) * 10)); + objectCombo.select(currentSelection - 1); + } + } + }); + } + + /** + * @see OpenGLTab#dispose() + */ + void dispose() { + super.dispose(); + GLU.gluDeleteQuadric(quadratic); + GL.glDeleteLists(INDEX_DISK, 1); + GL.glDeleteLists(INDEX_SPHERE, 1); + GL.glDeleteLists(INDEX_TRIANGLE, 1); + } + + /** + * @see OpenGLTab#getTabText() + */ + String getTabText() { + return "Transparency"; + } + + /** + * @see OpenGLTab#init() + */ + void init() { + GL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); + quadratic = GLU.gluNewQuadric(); + GLU.gluQuadricNormals(quadratic, GLU.GLU_SMOOTH); + GL.glEnable(GL.GL_CULL_FACE); + GL.glEnable(GL.GL_DEPTH_TEST); + GL.glEnable(GL.GL_BLEND); + // create display lists + GL.glNewList(INDEX_SPHERE, GL.GL_COMPILE); + GLU.gluQuadricDrawStyle(quadratic, GLU.GLU_FILL); + GLU.gluSphere(quadratic, 1.5, 32, 32); + GL.glEndList(); + GL.glNewList(INDEX_DISK, GL.GL_COMPILE); + GLU.gluQuadricDrawStyle(quadratic, GLU.GLU_FILL); + GLU.gluDisk(quadratic, 1.0, 3, 32, 32); + GL.glEndList(); + GL.glNewList(INDEX_TRIANGLE, GL.GL_COMPILE); + GL.glBegin(GL.GL_TRIANGLES); + GL.glVertex3f(0.0f, 2.0f, 0.0f); + GL.glVertex3f(-2.0f, -2.0f, 2.0f); + GL.glVertex3f(2.0f, -2.0f, 2.0f); + GL.glEnd(); + GL.glEndList(); + } + + /** + * @see SelectionTab.processPick (int[], int) + */ + void processPick(int[] pSelectBuff, int hits) { + int count, zDepth; + int tempSelection = pSelectBuff[3]; + zDepth = pSelectBuff[2]; + // pick object with largest z value + for (int i = 4; i <= 4 * (hits - 1); i = i + 4) { + if (zDepth > pSelectBuff[i + 2]) { + zDepth = pSelectBuff[i + 2]; + tempSelection = pSelectBuff[i + 3]; + } + } + if (tempSelection > 0) currentSelection = tempSelection; + } + + /** + * @see OpenGLTab#renderScene() + */ + void renderScene() { + // draw items with transparent qualities in reverse Z order + GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + GL.glLoadIdentity(); + GL.glTranslatef(0.0f, 0.0f, -14.0f); + GL.glPushName(0); + + // draw disk + GL.glPushMatrix(); + GL.glTranslatef(-1.0f, 0.0f, 1.0f); + GL.glColor4f(0.0f, 1.0f, 0.0f, alphas[2]); + GL.glLoadName(INDEX_DISK); + GL.glCallList(INDEX_DISK); + if (currentSelection == INDEX_DISK) { + GL.glColor3f(1.0f, 0.0f, 0.0f); + GLU.gluQuadricDrawStyle(quadratic, GLU.GLU_LINE); + GLU.gluDisk(quadratic, 0.98, 3.02, 32, 32); + } + GL.glPopMatrix(); + + // draw triangle + GL.glTranslatef(0.0f, 0.0f, +3.0f); + GL.glLoadName(INDEX_TRIANGLE); + GL.glColor4f(1.0f, 0.0f, 0.0f, alphas[0]); + GL.glCallList(INDEX_TRIANGLE); + GL.glPointSize(5.0f); + if (currentSelection == INDEX_TRIANGLE) { + GL.glBegin(GL.GL_POINTS); + GL.glColor3f(1.0f, 0.0f, 0.0f); + GL.glVertex3f(0.0f, 2.0f, 0.0f); + GL.glVertex3f(-2.0f, -2.0f, 2.0f); + GL.glVertex3f(2.0f, -2.0f, 2.0f); + GL.glEnd(); + } + + // draw sphere + GL.glPushMatrix(); + GL.glTranslatef(1.0f, 0.0f, +3.0f); + GL.glColor4f(0.0f, 0.0f, 1.0f, alphas[1]); + GL.glLoadName(INDEX_SPHERE); + GL.glCallList(INDEX_SPHERE); + if (currentSelection == INDEX_SPHERE) { + GL.glColor3f(1.0f, 0.0f, 0.0f); + GLU.gluQuadricDrawStyle(quadratic, GLU.GLU_LINE); + GLU.gluSphere(quadratic, 1.51, 8, 8); + } + GL.glPopMatrix(); + } +} diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/images/Ball.jpg b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/images/Ball.jpg Binary files differnew file mode 100644 index 0000000000..6890d26807 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/images/Ball.jpg diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/images/Box.bmp b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/images/Box.bmp Binary files differnew file mode 100644 index 0000000000..5eef6630d3 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/images/Box.bmp diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/images/Floor.jpg b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/images/Floor.jpg Binary files differnew file mode 100644 index 0000000000..4934c9c82a --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/images/Floor.jpg diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/images/splash.bmp b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/images/splash.bmp Binary files differnew file mode 100644 index 0000000000..6ec75da1a8 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/images/splash.bmp diff --git a/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/images/untitled.jpg b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/images/untitled.jpg Binary files differnew file mode 100644 index 0000000000..f5d6aa11d5 --- /dev/null +++ b/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/images/untitled.jpg |