The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by danleahy]

Hi all,
i have created a working program using the simpleuniverse class, i need
to add multiple viewplatforms to my application so i have tried to use the
normal classes instead, creating the virtual universe, locale, branch graph
and transformGroup, view, canvas3D objects etc and all i got on screen was a
blank screen.
I need help, i need a simple .java file that creates a working view branchgraph
from scratch, below is a sample program i wrote, i have tried to add my own
viewbranch graph to a simple universe, ( i was getting desperate ).
if someone could send me a file with a working viewbranch graph created
without using the simpleuniverse i would be eternally grateful.
Best regards
Daniel Leahy.

/*
* Test10.java
*
* Created on 13 November 2002, 15:33
*/
// Java core packages
import java.awt.event.*;
import java.awt.*;
import java.net.*;

// Java extension packages
import javax.swing.event.*;
import javax.swing.*;
import javax.media.j3d.*;
import javax.vecmath.*;

// Java3D utility packages
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.behaviors.mouse.*;
/**
*
* @author Daniel Leahy
*/
public class Test11 extends JFrame {
private Appearance appearance; // 3D object's appearance
private Light ambientLight; // ambient scene lighting

private Color3f lightColor; // Light color
private Light directionalLight;
private Material material; // 3D objects color object
private SimpleUniverse simpleUniverse; // 3D scene environment
private TextureLoader textureLoader; // 3D object's texture
TransformGroup tg;

// holds 3D transformation information
private TransformGroup transformGroup;

private String imageName = "images/earthg2.jpg"; // texture image file name
//private String imageName_2 = "images/star_background.jpg";
private String imageName_2 = "images/earthg2.jpg";


public Test11() {
super("Test Program");
initComponents();
}


public void initComponents() {
getContentPane().setLayout( new BorderLayout() );

GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();

Canvas3D canvas3D = new Canvas3D(config);









// create SimpleUniverse ( 3D Graphics environment )
simpleUniverse = new SimpleUniverse( canvas3D );

// below creates background sphere and textures image in it.
//simpleUniverse.addBranchGraph( createBackground() );

// set viewing distance for 3D scene
ViewingPlatform viewPlatform =
simpleUniverse.getViewingPlatform();

viewPlatform.setNominalViewingTransform();

// create 3D scene
BranchGroup content_branchGroup = createScene();

// attach BranchGroup to SimpleUniverse // makes branchGroup "Live"
simpleUniverse.addBranchGraph( content_branchGroup );
// getContentPane().add( canvas3D , BorderLayout.CENTER );



// tenth attempt to add another canvas to the SimpleUniverse class
BranchGroup vpBranchGroup = new BranchGroup();

// create a transform to scale the ViewPlatform ( and hence the view )
TransformGroup tg = new TransformGroup();


// create the ViewPlatform
ViewPlatform vp = new ViewPlatform();
vp.setViewAttachPolicy( View.RELATIVE_TO_FIELD_OF_VIEW );

// attach the ViewPlatform to the TransformGroup
tg.addChild( vp );

tg = new TransformGroup();


vpBranchGroup.addChild( tg );


Transform3D t3d = new Transform3D();
t3d.setTranslation( new Vector3d( 0.0, 0.0, -20.0 ));
tg.setTransform( t3d );

// create the view object
View view = new View();

// create the PhysicalBody and PhysicalEnvironment for the View
// and attach to the view
PhysicalBody pb = new PhysicalBody();
PhysicalEnvironment pe = new PhysicalEnvironment();
view.setPhysicalEnvironment( pe );
view.setPhysicalBody( pb );

// attach the View to the ViewPlatform
view.attachViewPlatform( vp );

// set the near and far clipping planes for the View
view.setBackClipDistance( 100 );
view.setFrontClipDistance( 10 );

GraphicsConfigTemplate3D gc3D = new GraphicsConfigTemplate3D();
gc3D.setSceneAntialiasing( GraphicsConfigTemplate.PREFERRED );
GraphicsDevice gd[] =
GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();

Canvas3D c3d = new Canvas3D( gd[0].getBestConfiguration( gc3D ) );

c3d.setSize( 500, 500 );
view.addCanvas3D( c3d );







// add teh viewPlatform BranchGroup to the simpleuniverse
simpleUniverse.addBranchGraph( vpBranchGroup );
getContentPane().add( c3d , BorderLayout.EAST );






















setSize( 700, 700 );
setVisible( true );
}






public static void main(String[] args) {

new Test11();




}




/**
* @return BranchGroup */
public BranchGroup createScene()
{
BranchGroup scene = new BranchGroup();

// initialize TransformGroup
transformGroup = new TransformGroup();



// Background back = createBackgroundColor();
scene.addChild( createBackgroundColor() );

// set TransformGroup's WRITE permission
transformGroup.setCapability(
TransformGroup.ALLOW_TRANSFORM_WRITE );

transformGroup.setCapability(
TransformGroup.ALLOW_TRANSFORM_READ );

// add TransformGroup to BranchGroup
scene.addChild( transformGroup );

// create BoundingSphere
BoundingSphere bounds = new BoundingSphere(
new Point3d( 0.0f, 0.0f, 0.0f ), 100.0 );

appearance = new Appearance(); // create object appearance
material = new Material(); // create texture matieral
appearance.setMaterial( material );

String rgb = new String( "RGB" );

// load texture for scene object
textureLoader = new TextureLoader( Java3DWorld.class.getResource(
imageName ), rgb, this );

// set capability bits for enabling texture
textureLoader.getTexture().setCapability(
Texture.ALLOW_ENABLE_WRITE );





/** initial texture will show
* make sure to also set checkbox in controlPanel class if you change
the
* textureLoader.getTexture().setEnable( true ); as it will be have to
be set to be disabled
* if you set the default texture not to be enabled
*/
textureLoader.getTexture().setEnable( true );

// set object's texture
appearance.setTexture( textureLoader.getTexture() );

// create object geometry
// Box shape = new Box( 0.3f, 0.3f, 0.3f,
// Box.GENERATE_NORMALS | Box.GENERATE_TEXTURE_COORDS,
// appearance );


Sphere shape2 = new Sphere( 0.5f, Sphere.GENERATE_NORMALS |
Sphere.GENERATE_TEXTURE_COORDS, 80,
appearance );

// add geometry to TransformGroup
transformGroup.addChild( shape2 );

// initialize Ambient lighting
ambientLight = new AmbientLight();
ambientLight.setInfluencingBounds( bounds );

// initialize directionalLight
directionalLight = new DirectionalLight();

lightColor = new Color3f(); // initialize light color

// set initial DirectionalLight color
directionalLight.setColor( lightColor );

// set capability bits to allow DirectionalLight's
// Color and Direction to be changed
directionalLight.setCapability(
DirectionalLight.ALLOW_DIRECTION_WRITE );

directionalLight.setCapability(
DirectionalLight.ALLOW_DIRECTION_READ );

directionalLight.setCapability(
DirectionalLight.ALLOW_COLOR_WRITE );

directionalLight.setCapability(
DirectionalLight.ALLOW_COLOR_READ );

directionalLight.setInfluencingBounds( bounds );

// add light nodes to BranchGroup
scene.addChild( ambientLight );
scene.addChild( directionalLight );

// initialize rotation behavior
MouseRotate rotateBehavior = new MouseRotate();
rotateBehavior.setTransformGroup( transformGroup );
rotateBehavior.setSchedulingBounds( bounds );

// initialize translation behavior
MouseTranslate translateBehavior = new MouseTranslate();
translateBehavior.setTransformGroup( transformGroup );
translateBehavior.setSchedulingBounds(
new BoundingBox( new Point3d( -1.0f, -1.0f, -1.0f ),
new Point3d( 1.0f, 1.0f, 1.0f ) ) );

// initialize scaling behavior
MouseZoom scaleBehavior = new MouseZoom();
scaleBehavior.setTransformGroup( transformGroup );
scaleBehavior.setSchedulingBounds( bounds );

// add behaviors to BranchGroup
scene.addChild( scaleBehavior );
scene.addChild( rotateBehavior );
scene.addChild( translateBehavior );

scene.compile();

return scene;

} // end method createScene





/**
* @param color
*/
public void changeColor( Color color )
{
lightColor.set( color );
directionalLight.setColor( lightColor );
}







/**
* change geometry surface to textured image or material color
* @param textureValue */
public void updateTexture( boolean textureValue )
{
textureLoader.getTexture().setEnable( textureValue );
}

// change image used for texture
public void setImageName( String imageFileName )
{
imageName = imageFileName;
}

// get image file name
public String getImageName()
{
return imageName;
}

// return preferred dimensions of Container
public Dimension getPreferredSize()
{
return new Dimension( 100,100 );
}

// return minimum size of Container
public Dimension getMinimumSize()
{
return getPreferredSize();
}




protected Bounds createApplicationBounds( )
{
BoundingSphere m_ApplicationBounds = new BoundingSphere( new Point3d(
0.0,0.0,0.0 ), 100.0 );
return m_ApplicationBounds;
}

protected Background createBackgroundColor()
{
//ImageComponent2D image = new TextureLoader( Java3DWorld.class.getResource(
imageName_2 ), this).getScaledImage( 1300,1300 );
// Background back = new Background( image );


Background back = new Background( new Color3f( 0.0f, 0.0f, 0.0f ) );
//original below
// Background back = new Background( new Color3f( 0.9f, 0.9f,
0.9f ) );
back.setApplicationBounds( createApplicationBounds());
return back;
}

/*
public BranchGroup createBackground()
{
BranchGroup backgroundGroup = new BranchGroup();

Background back = new Background();

back.setApplicationBounds( backgroundgetBoundingSphere() );

BranchGroup bgGeometry = new BranchGroup();

Appearance app = new Appearance();

Texture tex = new TextureLoader( Java3DWorld.class.getResource(
imageName_2 ), this).getTexture();

app.setTexture( tex );

Sphere sphere = new Sphere( 1.0f, Primitive.GENERATE_TEXTURE_COORDS
| Primitive.GENERATE_NORMALS_INWARD, app );

bgGeometry.addChild( sphere );
back.setGeometry( bgGeometry );
backgroundGroup.addChild( back);
return backgroundGroup;
} */

BoundingSphere backgroundgetBoundingSphere()
{
return new BoundingSphere( new Point3d( 0.0, 0.0, 0.0), 200.0 );
}

}