/**
* This example mixes rendering in immediate and retained (scenegraph) mode to
* produce a composite rendered frame.
*/ public class MixedTest extends Java3dApplet { private static int m_kWidth = 400;
private static int m_kHeight = 400;
public MixedTest() {
initJava3d();
}
protected Canvas3D createCanvas3D() {
// overidden this method to create a custom
// Canvas3D that will implement the Immediate Mode rendering
GraphicsConfigTemplate3D gc3D = new GraphicsConfigTemplate3D();
gc3D.setSceneAntialiasing(GraphicsConfigTemplate.PREFERRED);
GraphicsDevice gd[] = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getScreenDevices();
ImmediateCanvas3D c3d = new ImmediateCanvas3D(gd[0]
.getBestConfiguration(gc3D));
c3d.setSize(getCanvas3dWidth(c3d), getCanvas3dHeight(c3d));
// create the PointArray that we will be rendering int nPoint = 0;
for (int n = 0; n < nGridSize; n++) { for (int i = 0; i < nGridSize; i++) {
Point3f point = new Point3f(n - nGridSize / 2, i - nGridSize
/ 2, 0.0f);
m_PointArray.setCoordinate(nPoint++, point);
}
}
}
public void renderField(int fieldDesc) { super.renderField(fieldDesc);
GraphicsContext3D g = getGraphicsContext3D();
// first time initialization if (m_nRender == 0) {
// set the start time
m_StartTime = System.currentTimeMillis();
// add a light to the graphics context
DirectionalLight light = new DirectionalLight();
light.setEnable(true);
g.addLight((Light) light);
// create the material for the points
Appearance a = new Appearance();
Material mat = new Material();
mat.setLightingEnable(true);
mat.setAmbientColor(0.5f, 1.0f, 1.0f);
a.setMaterial(mat);
a.setColoringAttributes(new ColoringAttributes(1.0f, 0.5f, 0.5f,
ColoringAttributes.NICEST));
// enlarge the points
a.setPointAttributes(new PointAttributes(4, true));
// make the appearance current in the graphics context
g.setAppearance(a);
}
// set the current transformation for the graphics context
g.setModelTransform(m_t3d);
// finally render the PointArray
g.draw(m_PointArray);
// calculate and display the Frames Per Second for the
// Immediate Mode rendering of the PointArray
m_nRender++;
// update the model transformation to rotate the PointArray
// about the Y axis.
m_rot += 0.1;
m_t3d.rotY(m_rot);
// move the transform back so we can see the points
m_t3d.setTranslation(new Vector3d(0.0, 0.0, -20.0));
// scale the transformation down so we can see all of the points
m_t3d.setScale(0.3);
// force a paint (will call renderField)
paint(getGraphics());
}
}
/*******************************************************************************
* 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:
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
*
* If you make changes you think others would like, please contact one of the
* authors or someone at the www.j3d.org web site.
******************************************************************************/
//*****************************************************************************
/**
* Java3dApplet
*
* Base class for defining a Java 3D applet. Contains some useful methods for
* defining views and scenegraphs etc.
*
* @author Daniel Selman
* @version 1.0
*/
//*****************************************************************************
abstract class Java3dApplet extends Applet { public static int m_kWidth = 300;
public TransformGroup[] getViewTransformGroupArray() {
TransformGroup[] tgArray = new TransformGroup[1];
tgArray[0] = new TransformGroup();
// move the camera BACK a little...
// note that we have to invert the matrix as
// we are moving the viewer
Transform3D t3d = new Transform3D();
t3d.setScale(getScale());
t3d.setTranslation(new Vector3d(0.0, 0.0, -20.0));
t3d.invert();
tgArray[0].setTransform(t3d);
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.