Honestly, I wasn't requesting that anybody look at my code. I learned my first programming language around the same time that key punch machines were being phased out, though I did cart around my share of punch cards. Granted, I've only coded with java for the past 12 years but its no different then the programming and countless scripting languages I've been required to learn over the years.
If I required anybody to look at my code I probably would have given up 33 years ago.
What I thought I had pointed out in my post was the fact that
Morph doesn't work with Model. If my post was vague in this respect I am sorry.
Since time is such a valuable commodity I have taken the MorphPyramid2CubeTest class and modified it to load a model instead of a hard-coded class. I have copied the modified code below so that it can be copied and pasted if required. I have also included an .obj file for a basic cube.
When a model is used Morphing action stops. If this is normal then just say so and I'll figure another way to implement it.
The only change I made to the original class was to comment the following lines and add code to get three models from disk. Original code that is commented:
g[ 0 ] = new ColorPyramidUp();
g[ 1 ] = new ColorCube();
g[ 2 ] = new ColorPyramidDown();
Code that is added:
ResourceLocator resLoc = ResourceLocator.create( "resources/" );
resLoc.createAndAddTSL( "textures" );
for ( int a = 0; a < 3; a++) {
model[a] = ModelLoader.getInstance().loadModel(resLoc.getResource( "cubeModel" + String.valueOf(a).trim() + ".obj" ) );
g[ 0 ] = model[0].getShape(0).getGeometry();
g[ 1 ] = model[1].getShape(0).getGeometry();
g[ 2 ] = model[2].getShape(0).getGeometry();
package org.xith3d.test.render;
import java.io.IOException;
import java.net.MalformedURLException;
import org.jagatoo.input.InputSystem;
import org.jagatoo.input.devices.components.Key;
import org.jagatoo.input.events.KeyPressedEvent;
import org.jagatoo.util.errorhandling.IncorrectFormatException;
import org.jagatoo.util.errorhandling.ParsingException;
import org.openmali.FastMath;
import org.xith3d.base.Xith3DEnvironment;
import org.xith3d.loaders.models.Model;
import org.xith3d.loaders.models.ModelLoader;
import org.xith3d.loop.CanvasFPSListener;
import org.xith3d.loop.opscheduler.Animator;
import org.xith3d.render.Canvas3D;
import org.xith3d.render.RenderPass;
import org.xith3d.resources.ResourceLocator;
import org.xith3d.scenegraph.Appearance;
import org.xith3d.scenegraph.BranchGroup;
import org.xith3d.scenegraph.Geometry;
import org.xith3d.scenegraph.Morph;
import org.xith3d.scenegraph.PolygonAttributes;
import org.xith3d.scenegraph.QuadArray;
import org.xith3d.scenegraph.Shape3D;
import org.xith3d.scenegraph.Transform3D;
import org.xith3d.scenegraph.TransformGroup;
import org.xith3d.scenegraph.View.ProjectionPolicy;
import org.xith3d.schedops.movement.RotatableGroup;
import org.xith3d.schedops.movement.TransformationDirectives;
import org.xith3d.test.Xith3DTest;
import org.xith3d.test.util.TestUtils;
import org.xith3d.utility.commandline.BasicApplicationArguments;
import org.xith3d.utility.events.WindowClosingRenderLoopEnder;
@Xith3DTest.Description
(
fulltext = {
"Simple Xith3D Morph Pyramid2Cube test"
},
authors = {
"Yuri Vl. Gushchin (aka YVG)"
}
)
public class MorphPyramid2CubeTest extends Xith3DTest //InputAdapterRenderLoop
{
private RenderPass renderPass;
private Morph morph;
private BranchGroup createSceneGraph( Animator animator )
{
BranchGroup rootBranch = new BranchGroup();
RotatableGroup testRotateYGroup = new RotatableGroup( new TransformationDirectives( 0f, 0.1f, 0f ) );
// Make extra transfrom we may want to play with (scale etc.)
TransformGroup scaleTransform = new TransformGroup();
Transform3D t = new Transform3D();
t.setIdentity();
scaleTransform.setTransform( t );
testRotateYGroup.addChild( scaleTransform );
// Create the transform group nodes for the 3 original objects
// and the morphed object. Add them to the root of the
// branch graph.
TransformGroup objTrans[] = new TransformGroup[ 4 ];
for ( int i = 0; i < 4; i++ )
{
objTrans[ i ] = new TransformGroup();
scaleTransform.addChild( objTrans[ i ] );
}
Transform3D tr = new Transform3D();
Transform3D rotY15 = new Transform3D();
rotY15.rotY( FastMath.toRad( 15f ) );
objTrans[ 0 ].getTransform( tr );
tr.setTranslation( -3.0f, 1.5f, 0f );
tr.mul( rotY15 );
objTrans[ 0 ].setTransform( tr );
objTrans[ 1 ].getTransform( tr );
tr.setTranslation( 0.0f, 1.5f, 0f );
tr.mul( rotY15 );
objTrans[ 1 ].setTransform( tr );
objTrans[ 2 ].getTransform( tr );
tr.setTranslation( 3.0f, 1.5f, 0f );
tr.mul( rotY15 );
objTrans[ 2 ].setTransform( tr );
objTrans[ 3 ].getTransform( tr );
tr.setTranslation( 0.0f, -2.0f, 0f );
tr.mul( rotY15 );
objTrans[ 3 ].setTransform( tr );
// Now create simple geometries.
// QuadArray[] g = new QuadArray[ 3 ];
Geometry[] g = new Geometry[ 3 ];
Shape3D[] shape = new Shape3D[ 3 ];
for ( int i = 0; i < 3; i++ )
{
g[ i ] = null;
shape[ i ] = null;
}
Model model[] = new Model[3];
try {
ResourceLocator resLoc = ResourceLocator.create( "resources/" );
resLoc.createAndAddTSL( "textures" );
for ( int a = 0; a < 3; a++) {
model[a] = ModelLoader.getInstance().loadModel(resLoc.getResource( "cubeModel" + String.valueOf(a).trim() + ".obj" ) );
}
} catch (IncorrectFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParsingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g[ 0 ] = model[0].getShape(0).getGeometry();
g[ 1 ] = model[1].getShape(0).getGeometry();
g[ 2 ] = model[2].getShape(0).getGeometry();
// g[ 0 ] = new ColorPyramidUp();
// g[ 1 ] = new ColorCube();
// g[ 2 ] = new ColorPyramidDown();
Appearance a = new Appearance();
a.setPolygonAttributes( new PolygonAttributes( PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE ) );
for ( int i = 0; i < 3; i++ )
{
shape[ i ] = new Shape3D( g[ i ], a );
objTrans[ i ].addChild( shape[ i ] );
}
// Create a Morph node, and set the appearance and input geometry
// arrays.
morph = new Morph( g, a );
objTrans[ 3 ].addChild( morph );
rootBranch.addChild( testRotateYGroup );
// animator.addAnimatableObject( testRotateYGroup );
return ( rootBranch );
}
@Override
public void onKeyPressed( KeyPressedEvent e, Key key )
{
switch ( key.getKeyID() )
{
case ESCAPE:
this.end();
break;
case SPACE:
// Change the projection policy
if ( renderPass.getConfig().getProjectionPolicy() == ProjectionPolicy.PERSPECTIVE_PROJECTION )
{
renderPass.getConfig().setProjectionPolicy( ProjectionPolicy.PARALLEL_PROJECTION );
}
else
{
renderPass.getConfig().setProjectionPolicy( ProjectionPolicy.PERSPECTIVE_PROJECTION );
}
break;
}
}
private double[] weights = { 1.0, 0.0, 0.0 };
@Override
protected void prepareNextFrame( long gameTime, long frameTime, TimingMode timingMode )
{
super.prepareNextFrame( gameTime, frameTime, timingMode );
final long micros = timingMode.getMicroSeconds( gameTime );
final double a = (double)(micros % 2500000L) / 2500000.0;
if ( a < 0.25 )
{
weights[ 0 ] = (1.0 - (a - 0.0) * 4.0);
weights[ 1 ] = ((a - 0.0) * 4.0);
weights[ 2 ] = 0.0;
}
else if ( a < 0.5 )
{
weights[ 0 ] = 0.0;
weights[ 1 ] = (1.0 - (a - 0.25) * 4.0);
weights[ 2 ] = ((a - 0.25) * 4.0);
}
else if ( a < 0.75 )
{
weights[ 0 ] = 0.0;
weights[ 1 ] = ((a - 0.5) * 4.0);
weights[ 2 ] = (1.0 - (a - 0.5) * 4.0);
}
else
{
weights[ 0 ] = ((a - 0.75) * 4.0);
weights[ 1 ] = (1.0 - (a - 0.75) * 4.0);
weights[ 2 ] = 0.0;
}
morph.setWeights( weights );
}
public MorphPyramid2CubeTest( BasicApplicationArguments arguments ) throws Throwable
{
super( arguments.getMaxFPS() );
Xith3DEnvironment env = new Xith3DEnvironment( 0f, 0f, 7f,
0f, 0f, 0f,
0f, 1f, 0f,
this
);
ResourceLocator resLoc = TestUtils.createResourceLocator();
resLoc.createAndAddTSL( "textures" );
BranchGroup rootBranch = createSceneGraph( this.getAnimator() );
this.renderPass = env.addPerspectiveBranch( rootBranch );
Canvas3D canvas = createCanvas( arguments.getCanvasConstructionInfo(), getClass().getSimpleName() );
env.addCanvas( canvas );
canvas.addWindowClosingListener( new WindowClosingRenderLoopEnder( this ) );
this.addFPSListener( new CanvasFPSListener( canvas ) );
InputSystem.getInstance().registerNewKeyboardAndMouse( canvas.getPeer() );
System.out.println( "Hit SPACE to toggle projection policy, or ESC to exit" );
}
public static void main( String[] args ) throws Throwable
{
MorphPyramid2CubeTest test = new MorphPyramid2CubeTest( parseCommandLine( args ) );
test.begin();
}
}
class ColorCube extends QuadArray
{
private static final float[] verts =
{
// front face
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
// back face
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
// right face
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
// left face
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
// top face
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
// bottom face
-1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
};
private static final float[] colors =
{
// front face (red)
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
// back face (green)
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
// right face (blue)
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
// left face (yellow)
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
// top face (magenta)
1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
// bottom face (cyan)
0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f,
};
ColorCube()
{
super( 24 );
setCoordinates( 0, verts );
setColors( 0, 3, colors );
}
}
class ColorPyramidDown extends QuadArray
{
private static final float[] verts =
{
// front face
0.0f, -1.0f, 0.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
0.0f, -1.0f, 0.0f,
// back face
0.0f, -1.0f, 0.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
0.0f, -1.0f, 0.0f,
// right face
0.0f, -1.0f, 0.0f,
1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
0.0f, -1.0f, 0.0f,
// left face
0.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
0.0f, -1.0f, 0.0f,
// top face
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
// bottom face
0.0f, -1.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, -1.0f, 0.0f,
};
private static final float[] colors =
{
// front face (green)
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
// back face (red)
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
// right face (yellow)
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
// left face (magenta)
1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
// top face (blue)
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
// bottom face (cyan)
0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f,
};
ColorPyramidDown()
{
super( 24 );
setCoordinates( 0, verts );
setColors( 0, 3, colors );
}
}
class ColorPyramidUp extends QuadArray
{
private static final float[] verts =
{
// front face
1.0f, -1.0f, 1.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 1.0f,
// back face
-1.0f, -1.0f, -1.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
1.0f, -1.0f, -1.0f,
// right face
1.0f, -1.0f, -1.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
1.0f, -1.0f, 1.0f,
// left face
-1.0f, -1.0f, 1.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
-1.0f, -1.0f, -1.0f,
// top face
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
// bottom face
-1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
};
private static final float[] colors =
{
// front face (cyan)
0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f,
// back face (magenta)
1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
// right face (yellow)
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
// left face (blue)
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
// top face (green)
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
// bottom face (red)
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
};
ColorPyramidUp()
{
super( 24 );
setCoordinates( 0, verts );
setColors( 0, 3, colors );
}
}
# Blender3D v249 OBJ File:
# www.blender3d.org
mtllib man-walking0.mtl
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
usemtl Material
s off
f 1 2 3 4
f 5 8 7 6
f 1 5 6 2
f 2 6 7 3
f 3 7 8 4
f 5 1 4 8