|
/**********************************************************
Copyright (C) 2001 Daniel Selman
First distributed with the book "Java 3D Programming"
by Daniel Selman and published by Manning Publications.
http://manning.com/selman
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, version 2.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
The license can be found on the WWW at:
http://www.fsf.org/copyleft/gpl.html
Or by writing to:
Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Authors can be contacted at:
Daniel Selman: daniel@selman.org
If you make changes you think others would like, please
contact one of the authors or someone at the
www.j3d.org web site.
**************************************************************/
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfigTemplate;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;
import javax.media.j3d.Alpha;
import javax.media.j3d.Appearance;
import javax.media.j3d.AudioDevice;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Bounds;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Geometry;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.GraphicsConfigTemplate3D;
import javax.media.j3d.Group;
import javax.media.j3d.Locale;
import javax.media.j3d.Node;
import javax.media.j3d.NodeComponent;
import javax.media.j3d.PhysicalBody;
import javax.media.j3d.PhysicalEnvironment;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.QuadArray;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TriangleArray;
import javax.media.j3d.View;
import javax.media.j3d.ViewPlatform;
import javax.media.j3d.VirtualUniverse;
import javax.vecmath.Color3f;
import javax.vecmath.Point2f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.TexCoord2f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.audioengines.javasound.JavaSoundMixer;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Primitive;
/**
* Creates a scene using the Java 3D Box and a custom Cuboid implementation that
* uses Quads for sides instead of TriArrays
*/
public class CuboidTest extends Java3dApplet implements ActionListener {
private static int m_kWidth = 400;
private static int m_kHeight = 400;
public CuboidTest() {
initJava3d();
}
public void actionPerformed(ActionEvent event) {
}
protected Background createBackground() {
return null;
}
protected double getScale() {
return 0.1;
}
protected BranchGroup createSceneBranchGroup() {
BranchGroup objRoot = super.createSceneBranchGroup();
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
100.0);
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0,
4000, 0, 0, 0, 0, 0);
RotationInterpolator rotator = new RotationInterpolator(rotationAlpha,
objTrans, yAxis, 0.0f, (float) Math.PI * 2.0f);
rotator.setSchedulingBounds(bounds);
objTrans.addChild(rotator);
// create an appearance
Appearance ap = new Appearance();
// render as a wireframe
PolygonAttributes polyAttrbutes = new PolygonAttributes();
polyAttrbutes.setPolygonMode(PolygonAttributes.POLYGON_LINE);
polyAttrbutes.setCullFace(PolygonAttributes.CULL_NONE);
ap.setPolygonAttributes(polyAttrbutes);
objTrans.addChild(new Cuboid(50, 30, 20, ap));
objTrans.addChild(new Box(25, 15, 10, ap));
objRoot.addChild(objTrans);
return objRoot;
}
public static void main(String[] args) {
CuboidTest cuboidTest = new CuboidTest();
cuboidTest.saveCommandLineArguments(args);
new MainFrame(cuboidTest, m_kWidth, m_kHeight);
}
}
//Based on Sun's GeomBuffer.java 1.12 98/07/08 11:30:12
//This version actually returns Quadstrips for a Quadstrip array
//unlike the newer version that returns TriangleStrips....
/**
* OldGeomBuffer allows OpenGL-like input of geometry data. It outputs Java 3D
* geometry array objects. This utility is to simplify porting of OpenGL
* programs to Java 3D.
* <p>
* Here is a sample code that use this utility to create some quads.
* <P>
* <blockquote>
*
* <pre>
*
*
* OldGeomBuffer gbuf = new OldGeomBuffer(100);
* gbuf.begin(OldGeomBuffer.QUADS);
*
* for (int i = 0; i < 5; i++) {
* gbuf.normal3d(0.0, 1.0, 0.0);
* gbuf.vertex3d(1.0, 1.0, 0.0);
*
* gbuf.normal3d(0.0, 1.0, 0.0);
* gbuf.vertex3d(0.0, 1.0, 0.0);
*
* gbuf.normal3d(0.0, 1.0, 0.0);
* gbuf.vertex3d(0.0, 0.0, 0.0);
*
* gbuf.normal3d(0.0, 1.0, 0.0);
* gbuf.vertex3d(1.0, 0.0, 0.0);
* }
* gbuf.end();
* Shape3D shape = new Shape3D(gbuf.getGeom(OldGeomBuffer.GENERATE_NORMALS));
* </pre>
*
* </blockquote> Notice, that you only need to specify some upperbound on the
* number of points you'll use at the beginning (100 in this case).
* <p>
* Currently, you are limited to one primitive type per geom buffer. Future
* versions will add support for mixed primitive types.
*
*/
class OldGeomBuffer extends Object {
//Supported Primitives
static final int QUAD_STRIP = 0x01;
static final int TRIANGLES = 0x02;
static final int QUADS = 0x04;
private int flags;
static final int GENERATE_NORMALS = 0x01;
static final int GENERATE_TEXTURE_COORDS = 0x02;
Point3f[] pts = null;
Vector3f[] normals = null;
Point2f[] tcoords = null;
int currVertCnt;
int currPrimCnt;
int[] currPrimType = null, currPrimStartVertex = null,
currPrimEndVertex = null;
GeometryArray geometry;
int numVerts = 0;
int numTris = 0;
static final int debug = 0;
/**
* Creates a geometry buffer of given number of vertices
*
* @param numVerts
* total number of vertices to allocate by this buffer. This is
* an upper bound estimate.
*/
OldGeomBuffer(int numVerts) {
pts = new Point3f[numVerts];
normals = new Vector3f[numVerts];
tcoords = new Point2f[numVerts];
// max primitives is numV/3
currPrimType = new int[numVerts / 3];
currPrimStartVertex = new int[numVerts / 3];
currPrimEndVertex = new int[numVerts / 3];
currVertCnt = 0;
currPrimCnt = 0;
}
/*
* Returns a Java 3D geometry array from the geometry buffer. You need to
* call begin, vertex3d, end, etc. before calling this, of course.
*
* @param format vertex format.
*/
GeometryArray getGeom(int format) {
GeometryArray obj;
flags = format;
numTris = 0;
//Switch based on first primitive.
switch (currPrimType[0]) {
case TRIANGLES:
obj = processTriangles();
obj.setCapability(Geometry.ALLOW_INTERSECT);
return obj;
case QUADS:
obj = processQuads();
obj.setCapability(Geometry.ALLOW_INTERSECT);
return obj;
case QUAD_STRIP:
obj = processQuadStrips();
obj.setCapability(Geometry.ALLOW_INTERSECT);
return obj;
}
return null;
}
/**
* Begins a new primitive given the primitive type.
*
* @param prim
* the primitive type (listed above).
*
*/
void begin(int prim) {
if (debug >= 1)
System.out.println("quad");
currPrimType[currPrimCnt] = prim;
currPrimStartVertex[currPrimCnt] = currVertCnt;
}
/**
* End of primitive.
*
*
*/
void end() {
if (debug >= 1)
System.out.println("end");
currPrimEndVertex[currPrimCnt] = currVertCnt;
currPrimCnt++;
}
void vertex3d(double x, double y, double z) {
if (debug >= 2)
System.out.println("v " + x + " " + y + " " + z);
pts[currVertCnt] = new Point3f();
pts[currVertCnt].x = (float) x;
pts[currVertCnt].y = (float) y;
pts[currVertCnt].z = (float) z;
currVertCnt++;
}
void normal3d(double x, double y, double z) {
if (debug >= 2)
System.out.println("n " + x + " " + y + " " + z);
double sum = x * x + y * y + z * z;
if (Math.abs(sum - 1.0) > 0.001) {
if (debug >= 2)
System.out.println("normalizing");
double root = Math.sqrt(sum) + 0.0000001;
x /= root;
y /= root;
z /= root;
}
normals[currVertCnt] = new Vector3f();
normals[currVertCnt].x = (float) x;
normals[currVertCnt].y = (float) y;
normals[currVertCnt].z = (float) z;
}
void texCoord2d(double s, double t) {
if (debug >= 2)
System.out.println("t " + s + " " + t);
tcoords[currVertCnt] = new Point2f();
tcoords[currVertCnt].x = (float) s;
tcoords[currVertCnt].y = (float) t;
}
/**
* Returns the Java 3D geometry gotten from calling getGeom.
*
*/
GeometryArray getComputedGeometry() {
return geometry;
}
int getNumTris() {
return numTris;
}
int getNumVerts() {
return numVerts;
}
private GeometryArray processQuadStrips() {
GeometryArray obj = null;
int i;
int totalVerts = 0;
for (i = 0; i < currPrimCnt; i++) {
int numQuadStripVerts;
numQuadStripVerts = currPrimEndVertex[i] - currPrimStartVertex[i];
totalVerts += (numQuadStripVerts / 2 - 1) * 4;
}
if (debug >= 1)
System.out.println("totalVerts " + totalVerts);
if (((flags & GENERATE_NORMALS) != 0)
&& ((flags & GENERATE_TEXTURE_COORDS) != 0)) {
obj = new QuadArray(totalVerts, QuadArray.COORDINATES
| QuadArray.NORMALS | QuadArray.TEXTURE_COORDINATE_2);
} else if (((flags & GENERATE_NORMALS) == 0)
&& ((flags & GENERATE_TEXTURE_COORDS) != 0)) {
obj = new QuadArray(totalVerts, QuadArray.COORDINATES
| QuadArray.TEXTURE_COORDINATE_2);
} else if (((flags & GENERATE_NORMALS) != 0)
&& ((flags & GENERATE_TEXTURE_COORDS) == 0)) {
obj = new QuadArray(totalVerts, QuadArray.COORDINATES
| QuadArray.NORMALS);
} else {
obj = new QuadArray(totalVerts, QuadArray.COORDINATES);
}
Point3f[] newpts = new Point3f[totalVerts];
Vector3f[] newnormals = new Vector3f[totalVerts];
TexCoord2f[] newtcoords = new TexCoord2f[totalVerts];
int currVert = 0;
for (i = 0; i < currPrimCnt; i++) {
for (int j = currPrimStartVertex[i] + 2; j < currPrimEndVertex[i]; j += 2) {
outVertex(newpts, newnormals, newtcoords, currVert++, pts,
normals, tcoords, j - 2);
outVertex(newpts, newnormals, newtcoords, currVert++, pts,
normals, tcoords, j - 1);
outVertex(newpts, newnormals, newtcoords, currVert++, pts,
normals, tcoords, j + 1);
outVertex(newpts, newnormals, newtcoords, currVert++, pts,
normals, tcoords, j);
numTris += 2;
}
}
numVerts = currVert;
obj.setCoordinates(0, newpts);
if ((flags & GENERATE_NORMALS) != 0)
obj.setNormals(0, newnormals);
if ((flags & GENERATE_TEXTURE_COORDS) != 0)
obj.setTextureCoordinates(0, 0, newtcoords);
geometry = obj;
return obj;
}
private GeometryArray processQuads() {
GeometryArray obj = null;
int i;
int totalVerts = 0;
for (i = 0; i < currPrimCnt; i++) {
totalVerts += currPrimEndVertex[i] - currPrimStartVertex[i];
}
if (debug >= 1)
System.out.println("totalVerts " + totalVerts);
if (((flags & GENERATE_NORMALS) != 0)
&& ((flags & GENERATE_TEXTURE_COORDS) != 0)) {
obj = new QuadArray(totalVerts, QuadArray.COORDINATES
| QuadArray.NORMALS | QuadArray.TEXTURE_COORDINATE_2);
} else if (((flags & GENERATE_NORMALS) == 0)
&& ((flags & GENERATE_TEXTURE_COORDS) != 0)) {
obj = new QuadArray(totalVerts, QuadArray.COORDINATES
| QuadArray.TEXTURE_COORDINATE_2);
} else if (((flags & GENERATE_NORMALS) != 0)
&& ((flags & GENERATE_TEXTURE_COORDS) == 0)) {
obj = new QuadArray(totalVerts, QuadArray.COORDINATES
| QuadArray.NORMALS);
} else {
obj = new QuadArray(totalVerts, QuadArray.COORDINATES);
}
Point3f[] newpts = new Point3f[totalVerts];
Vector3f[] newnormals = new Vector3f[totalVerts];
TexCoord2f[] newtcoords = new TexCoord2f[totalVerts];
int currVert = 0;
if (debug > 1)
System.out.println("total prims " + currPrimCnt);
for (i = 0; i < currPrimCnt; i++) {
if (debug > 1)
System.out.println("start " + currPrimStartVertex[i] + " end "
+ currPrimEndVertex[i]);
for (int j = currPrimStartVertex[i]; j < currPrimEndVertex[i] - 3; j += 4) {
outVertex(newpts, newnormals, newtcoords, currVert++, pts,
normals, tcoords, j);
outVertex(newpts, newnormals, newtcoords, currVert++, pts,
normals, tcoords, j + 1);
outVertex(newpts, newnormals, newtcoords, currVert++, pts,
normals, tcoords, j + 2);
outVertex(newpts, newnormals, newtcoords, currVert++, pts,
normals, tcoords, j + 3);
numTris += 2;
}
}
numVerts = currVert;
obj.setCoordinates(0, newpts);
if ((flags & GENERATE_NORMALS) != 0)
obj.setNormals(0, newnormals);
if ((flags & GENERATE_TEXTURE_COORDS) != 0)
obj.setTextureCoordinates(0, 0, newtcoords);
geometry = obj;
return obj;
}
private GeometryArray processTriangles() {
GeometryArray obj = null;
int i;
int totalVerts = 0;
for (i = 0; i < currPrimCnt; i++) {
totalVerts += currPrimEndVertex[i] - currPrimStartVertex[i];
}
if (debug >= 1)
System.out.println("totalVerts " + totalVerts);
if (((flags & GENERATE_NORMALS) != 0)
&& ((flags & GENERATE_TEXTURE_COORDS) != 0)) {
obj = new TriangleArray(totalVerts, TriangleArray.COORDINATES
| TriangleArray.NORMALS
| TriangleArray.TEXTURE_COORDINATE_2);
} else if (((flags & GENERATE_NORMALS) == 0)
&& ((flags & GENERATE_TEXTURE_COORDS) != 0)) {
obj = new TriangleArray(totalVerts, TriangleArray.COORDINATES
| TriangleArray.TEXTURE_COORDINATE_2);
} else if (((flags & GENERATE_NORMALS) != 0)
&& ((flags & GENERATE_TEXTURE_COORDS) == 0)) {
obj = new TriangleArray(totalVerts, TriangleArray.COORDINATES
| TriangleArray.NORMALS);
} else {
obj = new TriangleArray(totalVerts, TriangleArray.COORDINATES);
}
Point3f[] newpts = new Point3f[totalVerts];
Vector3f[] newnormals = new Vector3f[totalVerts];
TexCoord2f[] newtcoords = new TexCoord2f[totalVerts];
int currVert = 0;
for (i = 0; i < currPrimCnt; i++) {
for (int j = currPrimStartVertex[i]; j < currPrimEndVertex[i] - 2; j += 3) {
outVertex(newpts, newnormals, newtcoords, currVert++, pts,
normals, tcoords, j);
outVertex(newpts, newnormals, newtcoords, currVert++, pts,
normals, tcoords, j + 1);
outVertex(newpts, newnormals, newtcoords, currVert++, pts,
normals, tcoords, j + 2);
numTris += 1;
}
}
numVerts = currVert;
obj.setCoordinates(0, newpts);
if ((flags & GENERATE_NORMALS) != 0)
obj.setNormals(0, newnormals);
if ((flags & GENERATE_TEXTURE_COORDS) != 0)
obj.setTextureCoordinates(0, 0, newtcoords);
geometry = obj;
return obj;
}
void outVertex(Point3f[] dpts, Vector3f[] dnormals, TexCoord2f[] dtcoords,
int dloc, Point3f[] spts, Vector3f[] snormals, Point2f[] stcoords,
int sloc) {
if (debug >= 1)
System.out.println("v " + spts[sloc].x + " " + spts[sloc].y + " "
+ spts[sloc].z);
dpts[dloc] = new Point3f();
dpts[dloc].x = spts[sloc].x;
dpts[dloc].y = spts[sloc].y;
dpts[dloc].z = spts[sloc].z;
if ((flags & GENERATE_NORMALS) != 0) {
dnormals[dloc] = new Vector3f();
dnormals[dloc].x = snormals[sloc].x;
dnormals[dloc].y = snormals[sloc].y;
dnormals[dloc].z = snormals[sloc].z;
}
if ((flags & GENERATE_TEXTURE_COORDS) != 0) {
if (debug >= 2)
System.out.println("final out tcoord");
dtcoords[dloc] = new TexCoord2f();
dtcoords[dloc].x = stcoords[sloc].x;
dtcoords[dloc].y = stcoords[sloc].y;
}
}
}
//Based on Sun's Box.java 1.13 98/11/23 10:23:02
//Work around for the Box bug when rendered in Wireframe mode.
/**
* Cuboid is a geometry primitive created with a given length, width, and
* height. It is centered at the origin. By default, it lies within the bounding
* Cuboid, [-1,-1,-1] and [1,1,1].
*
* When a texture is applied to a Cuboid, it is map CCW like on a Cylinder. A
* texture is mapped CCW from the back of the body. The top and bottom faces are
* mapped such that the texture appears front facing when the faces are rotated
* 90 toward the viewer.
*/
class Cuboid extends Primitive {
protected int numTris = 0;
protected int numVerts = 0;
/**
* Primitive flags.
*/
protected int flags;
/**
* Used to designate the front side of the Cuboid when using getShape().
*
* @see Cuboid#getShape
*/
public static final int FRONT = 0;
/**
* Used to designate the back side of the Cuboid when using getShape().
*
* @see Cuboid#getShape
*/
public static final int BACK = 1;
/**
* Used to designate the right side of the Cuboid when using getShape().
*
* @see Cuboid#getShape
*/
public static final int RIGHT = 2;
/**
* Used to designate the left side of the Cuboid when using getShape().
*
* @see Cuboid#getShape
*/
public static final int LEFT = 3;
/**
* Used to designate the top side of the Cuboid when using getShape().
*
* @see Cuboid#getShape
*/
public static final int TOP = 4;
/**
* Used to designate the bottom side of the Cuboid when using getShape().
*
* @see Cuboid#getShape
*/
public static final int BOTTOM = 5;
float xDim, yDim, zDim;
/**
* Constructs a default Cuboid of 1.0 in all dimensions.
*/
public Cuboid() {
this(1.0f, 1.0f, 1.0f, GENERATE_NORMALS, null);
}
public Appearance getAppearance(int index) {
&nbs
|