Skip to main content

3d object rendering

No replies
litpuvn
Offline
Joined: 2009-06-17

Hi all,

I am a new bie in Java 3d, could you help me to figure out why my rendering is totally black. i used SimpleUniverse and this is how the BranchGroup is created:

private BranchGroup createSceneGraph() {

BranchGroup objRoot = new BranchGroup();
objRoot.addChild(new Tetrahedron(1).myTetrahedron());
return objRoot;

}

the TetraHedron class passes scale param for constructor and call "myTetrahedron() " which is a function that returns a 3D object.

here is the TetraHedron class:

package convexhull.View3D;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.*;

public class Tetrahedron extends Shape3D
{
private float scale_XYZ;
private Point3f vertices[];
private int lengthVertices = 4, lengthTetraFaceIndices = 12;
private int tetraFaceIndices[], textCoordIndices[];
private GeometryInfo tetra_GeometryInfo;
private NormalGenerator normalGenerator;
private Stripifier stripifier;
private GeometryArray tetra_GeometryArray;
private TexCoord2f textCoord2f[]; // for triangles
private boolean crAngle;
private Appearance appearance;
private Color3f diffAmb, reflDiff, reflSpec, emittedLight;
private Material material;
private TransparencyAttributes trAttr;
private NewTextureLoader newTextureLoader;
private Texture texture;
private TextureAttributes textAttr;

/**
Constructor that allows to specify the desired scale factor.
@param type float s_XYZ - the scale factor to adjust the edges's
length of the tetrahedron
*/
public Tetrahedron(float s_XYZ)
{
scale_XYZ = s_XYZ;
}

/**
Construction of the desired tetrahedron.
@return javax.media.j3d.Shape3D myTetrahedron - the constructed
tetrahedron
*/
public Shape3D myTetrahedron()
{

////////////////////// Geometric part ///////////////////////////

// The 4 vertices p0, p1, p2 and p3 of the tetrahedron.
vertices = new Point3f[lengthVertices]; // 4
vertices[0] = new Point3f(0.0f, 0.0f, 0.0f);
vertices[1] = new Point3f(1.0f, 0.0f, 0.0f);
vertices[2] = new Point3f(0.0f, 1.0f, 0.0f);
vertices[3] = new Point3f(0.0f, 0.0f, 1.0f);

// Scaling of vertices
for (int i = 0; i < lengthVertices; i++) // lengthVertices = 4
vertices[i].scale(scale_XYZ);

// Set the face's indices for the tetrahedron (referenced to the array of vertices
// by setCoordinates(vertices) and setCoordinateIndices(tetraFaceIndices)).
tetraFaceIndices = new int[lengthTetraFaceIndices]; // 12
// From the camera in the view coordinate system
// bottom
tetraFaceIndices[0] = 0;
tetraFaceIndices[1] = 1;
tetraFaceIndices[2] = 3;
// back-left face
tetraFaceIndices[3] = 0;
tetraFaceIndices[4] = 3;
tetraFaceIndices[5] = 2;
// back face
tetraFaceIndices[6] = 0;
tetraFaceIndices[7] = 2;
tetraFaceIndices[8] = 1;
// front face
tetraFaceIndices[9] = 1;
tetraFaceIndices[10]= 2;
tetraFaceIndices[11]= 3;

// Create the GeometryInfo instance and set the vertices
tetra_GeometryInfo = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY );
tetra_GeometryInfo.setCoordinates(vertices);
tetra_GeometryInfo.setCoordinateIndices(tetraFaceIndices);

// Set the parameters (1 texture with dimension 2) for the texture's coordinates
tetra_GeometryInfo.setTextureCoordinateParams(1, 2);

// The coordinates of the 3 points in the 2D texture space.
textCoord2f = new TexCoord2f[3];
textCoord2f[0] = new TexCoord2f(0.0f, 0.2f);
textCoord2f[1] = new TexCoord2f(0.5f, 1.0f);
textCoord2f[2] = new TexCoord2f(1.0f, 0.5f);

// Set the texture coordinate's indices (referenced to the array of 2D points
// in the texture space by setTextureCoordinates(0, textCoord2f) and
// setTextureCoordinateIndices(0, textCoordIndices)).
textCoordIndices = new int[lengthTetraFaceIndices]; // 12

// From the camera in the view coordinate system (inverse of tetraFaceIndices !!!)
// front face
textCoordIndices[0] = 0;
textCoordIndices[1] = 1;
textCoordIndices[2] = 2;
// back face
textCoordIndices[3] = 0;
textCoordIndices[4] = 1;
textCoordIndices[5] = 2;
// back-left face
textCoordIndices[6] = 2;
textCoordIndices[7] = 0;
textCoordIndices[8] = 1;
// bottom
textCoordIndices[9] = 0;
textCoordIndices[10]= 1;
textCoordIndices[11]= 2;

// just one set
tetra_GeometryInfo.setTextureCoordinates(0, textCoord2f);
// just one set
tetra_GeometryInfo.setTextureCoordinateIndices(0, textCoordIndices);

normalGenerator = new NormalGenerator();
normalGenerator.generateNormals(tetra_GeometryInfo);

if (crAngle) normalGenerator.setCreaseAngle(0.0f); // with 0 radian ===> creased

stripifier = new Stripifier();
stripifier.stripify(tetra_GeometryInfo);

tetra_GeometryArray = tetra_GeometryInfo.getGeometryArray();

// The geonometry is passed to the instance this of the tetrahedron.
this.setGeometry(tetra_GeometryArray);

////////////////////// Appearance part ///////////////////////////

appearance = new Appearance();

// Optical properties of the tetrahedron.

// Ambient-diffuse-reflection coefficient
diffAmb = new Color3f(1.0f, 0.5f, 1.0f);
// Diffuse-reflection coefficient
reflDiff = new Color3f(1.0f, 0.5f, 1.0f);
// Specular-reflection coefficient (reflectance function)
reflSpec = new Color3f(1.0f, 0.5f, 1.0f);
// c = shininess: cos^c in the specular reflection
float c = 15;
// Emitted light
emittedLight = new Color3f(0.0f, 0.0f, 0.0f);

material = new Material(diffAmb, emittedLight, reflDiff, reflSpec, c);
appearance.setMaterial(material);

// This instance acts only on the tetrahedron and not on its texture.
trAttr = new TransparencyAttributes(TransparencyAttributes.NICEST, 0.0f);
// 0.0 = fully opaque
// 1.0 = fully transparent
appearance.setTransparencyAttributes(trAttr);

// Loading the texture
newTextureLoader = new NewTextureLoader("Images/myimage.jpg");
newTextureLoader.setImageObserver(newTextureLoader.getImageObserver());

texture = newTextureLoader.getTexture();

appearance.setTexture(texture);

// Application mode of the texture
textAttr = new TextureAttributes();
textAttr.setTextureMode(TextureAttributes.MODULATE);

appearance.setTextureAttributes(textAttr);

// The appearance is passed to the instance this of the tetrahedron.
this.setAppearance(appearance);

return this;
}
}

thank you for your response,m,