Hello,
I'm obviously a newbie in J3D...
I'm adapting some code for a static terrain generation app and I'm having
trouble with the normals (or whatever!). I can't get a proper shading,
despite having followed all the counselling I could find in the net.
The followig doesn't seem to work (as I imagine it should):
GeometryInfo gi = new GeometryInfo(ga);
NormalGenerator norm = new NormalGenerator();
norm.generateNormals( gi );
I've put an example together. Hopefully someone will find what I'm doing
wrong.
Thanks for the help, and sorry to bother you!
Cheers,
Ugo
import java.applet.Applet;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.geometry.GeometryInfo;
import com.sun.j3d.utils.geometry.NormalGenerator;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
// MouseRotate2App renders a single, interactively rotatable cube.
public class TestTri extends Applet {
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
TransformGroup objRotate = null;
MouseRotate myMouseRotate = null;
Transform3D transform = new Transform3D();
// create ColorCube and MouseRotate behvaior objects
//transform.setTranslation(new Vector3f(-0.6f, 0.0f, -0.6f));
objRotate = new TransformGroup(transform);
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
//objRoot.addChild(objRotate);
//objRotate.addChild(new ColorCube(0.4));
myMouseRotate = new MouseRotate();
myMouseRotate.setTransformGroup(objRotate);
myMouseRotate.setSchedulingBounds(new BoundingSphere());
objRoot.addChild(myMouseRotate);
// create ColorCube and MouseRotate behvaior objects
transform.setTranslation(new Vector3f( -.50f, -.50f, 0.0f));
objRotate = new TransformGroup(transform);
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objRoot.addChild(objRotate);
GeometryArray ga = (GeometryArray)createGeom();
GeometryInfo gi = new GeometryInfo(ga);
NormalGenerator norm = new NormalGenerator();
//norm.setCreaseAngle((float)Math.toRadians(180)); //??
norm.generateNormals( gi );
Shape3D shape = new Shape3D();
shape.setGeometry(gi.getGeometryArray());
shape.setAppearance(createAppear() );
objRotate.addChild( shape );
myMouseRotate = new MouseRotate();
myMouseRotate.setTransformGroup(objRotate);
myMouseRotate.setSchedulingBounds(new BoundingSphere());
objRoot.addChild(myMouseRotate);
DirectionalLight lightD = new DirectionalLight();
lightD.setDirection(new Vector3f(0.0f,-0.7f,-0.7f));
//lightD.setInfluencingBounds(bounds);
//objRoot.addChild(lightD);
objRoot.compile();
return objRoot;
}
public TestTri() {
setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config);
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
}
public Geometry createGeom(){
float[][] pts = null;
pts = new float[][]{
{0,0,0},{1,0,0},{ 0.5f,0.5f,0.5f},
{ 0,0,0},{ 0,1,0},{ 0.5f,0.5f,0.5f},
{1,0,0},{ 1,1,0},{ 0.5f,0.5f,0.5f},
{1,1,0},{ 0,1,0},{ 0.5f,0.5f,0.5f}
};
TriangleArray ta = new TriangleArray( 12,
GeometryArray.COORDINATES | GeometryArray.COLOR_4 |
GeometryArray.TEXTURE_COORDINATE_2 |
GeometryArray.NORMALS );
float coord[] = new float[3];
float texCoord[]= new float[2];
int coordCnt = 0;
for (int i=0; i<pts.length; i++) {
coord[0] = pts[i][ 0 ];
coord[1] = pts[i][ 1 ];
coord[2] = pts[i][ 2 ];
texCoord[0] = pts[i][ 0 ];
texCoord[1] = pts[i][ 1 ];
ta.setCoordinate( i , coord );
ta.setTextureCoordinate(0, i , texCoord);
coordCnt++;
}
return ta;
}
public Appearance createAppear(){
/*
TextureLoader loader = null;
try {
loader = new TextureLoader(
new
URL("file:///E:/java/source/java3d/tutorial/examples1_6/texture/brick.gif"),
this);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ImageComponent2D image = loader.getImage();
Texture2D texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA,
image.getWidth(), image.getHeight());
texture.setImage(0, image);
*/
PolygonAttributes polyAppear = new PolygonAttributes();
polyAppear.setPolygonMode(PolygonAttributes.POLYGON_FILL );
//polyAppear.setBackFaceNormalFlip( false );
polyAppear.setCullFace(PolygonAttributes.CULL_NONE);
PointAttributes patts= new PointAttributes();
patts.setPointSize(3f);
Material material = new Material();
Color3f white = new Color3f( 0.7f, 0.7f, 0.7f );
material.setAmbientColor( white );
material.setDiffuseColor( white );
material.setSpecularColor( white );
material.setShininess( 1f );
material.setLightingEnable( true );
material.setEmissiveColor( 0.72f, 0.72f, 0.72f );
ColoringAttributes ca = new ColoringAttributes();
//ca.setShadeModel( ColoringAttributes.NICEST );
Appearance appear = new Appearance();
//appear.setTexture(texture);
appear.setPolygonAttributes(polyAppear);
appear.setPointAttributes(patts);
appear.setMaterial(material);
appear.setColoringAttributes(ca);
return appear;
}
public static void main(String[] args) throws IOException {
Frame frame = new MainFrame(new TestTri(), 256, 256);
} // end of main (method of MouseRotate2App)
} // end of class MouseRotate2App
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".