You are on page 1of 14

import java.applet.

Applet;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Frame;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.picking.PickRotateBehavior;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.geometry.Cone;
import com.sun.j3d.utils.geometry.NormalGenerator;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.geometry.Triangulator;
import com.sun.j3d.utils.geometry.GeometryInfo;
import com.sun.j3d.utils.image.TextureLoader;

import javax.media.j3d.*;
import javax.vecmath.*;

//a three d shape has a reference to its geometry and to its appereance
// HelloJava3Da renders a single, rotating cube.

public class threeD extends Applet implements KeyListener{

private static final int NrOfCubes = 4;


public int SelX,SelY,SelZ;
public VisualObject[] AllCubes =new
VisualObject[NrOfCubes*NrOfCubes*NrOfCubes];
public Appearance defApp,VisibleApp;
public threeD() {
//GAME VARS

SelX = 0;
SelY = 0;
SelZ = 0;
CustomBehavior Behave;
TransformGroup SceneActions;//= new TransformGroup();
Transform3D SceneAct = new Transform3D();
//eof GameVARS

//Default appearance
defApp = new Appearance();
TransparencyAttributes Transparency = new TransparencyAttributes();
Transparency.setTransparency(0.2f);
Transparency.setDstBlendFunction(Transparency.BLEND_ONE_MINUS_SRC_ALPHA);
Transparency.setTransparencyMode(Transparency.FASTEST);
PolygonAttributes polyAtt = new PolygonAttributes();
polyAtt.setPolygonMode(2);
defApp.setPolygonAttributes(polyAtt);
defApp.setTransparencyAttributes(Transparency);
//eof Default appearance
//Non selected Visible appearance
VisibleApp = new Appearance();
PolygonAttributes VisiblepolyAtt = new PolygonAttributes();
VisiblepolyAtt.setPolygonMode(2);
VisibleApp.setPolygonAttributes(VisiblepolyAtt);
//eof Non selected Visible appearance

setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config);
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);

add("Center", canvas3D);

//BranchGroup scene = createSceneGraph();


BranchGroup scene = new BranchGroup();
SceneActions = MakeTheCube();

BranchGroup TheScene;
TheScene = new BranchGroup();

//SceneAct.setTranslation(new Vector3d(-0.6,0.0,0.4));

//SceneActions.setTransform(SceneAct);

Behave = new CustomBehavior(SceneActions,this);


Behave.setSchedulingBounds(new BoundingSphere());

MouseRotate myMouseRotate = new MouseRotate();


myMouseRotate.setTransformGroup(SceneActions);
myMouseRotate.setSchedulingBounds(new BoundingSphere());

scene.addChild(myMouseRotate);

// SimpleUniverse is a Convenience Utility class

// This will move the ViewPlatform back a bit so the


// objects in the scene can be viewed.
simpleU.getViewingPlatform().setNominalViewingTransform();
SceneActions.addChild(Behave);
scene.addChild(SceneActions);
simpleU.addBranchGraph(scene);

} // end of HelloJava3Da (constructor)

private Appearance createAppearance ()


{
Appearance app;
Material mat;
mat = new Material();
app = new Appearance();
app.setMaterial(mat);

//mat.setAmbientColor(new Color3f(0.0f,1.3f,0.0f));
mat.setLightingEnable(true);
mat.setDiffuseColor( new Color3f(1.0f,1.0f,0.0f) );
mat.setLightingEnable(true);
mat.setShininess(0.5f);
mat.setSpecularColor( new Color3f(1.3f,0.0f,0.0f) );

//mat.setEmissiveColor(new Color3f(1.3f,0.0f,0.0f));

//app.setColoringAttributes( new ColoringAttributes( new


Color3f(0.0f,1.0f,1.0f), 1));
PolygonAttributes polyAttrib = new PolygonAttributes(1,1,1.0f,false);
polyAttrib.setPolygonMode(2);//1 is vertexes, 2 is lines and 3 is
filled

polyAttrib.setCullFace(PolygonAttributes.CULL_BACK);
//polyAttrib.setCapability(3);
app.setPolygonAttributes(polyAttrib);

//app.setLineAttributes(new LineAttributes(3,3,true));
//app.setMaterial(new Material(new Color3f(0.3f,0.2f,0.1f), new
Color3f(0.3f,0.2f,0.1f), new Color3f(0.5f,0.2f,1.1f), new
Color3f(0.3f,0.2f,0.1f),1.0f));
return app;
// code to create default appearance of visual object
}
//||<>
public TransformGroup MakeTheCube()
{
int i,j,k = 0;
int counter = 0;
double CenterX,CenterY,CenterZ;
CenterX = ((NrOfCubes*0.2)/2);
CenterY = ((NrOfCubes*0.2)/2);
CenterZ = ((NrOfCubes*0.2)/2);
VisualObject C;
VisualObject[] allCubes = new VisualObject[NrOfCubes];
BranchGroup CubeContainer= new BranchGroup();

TransformGroup TG;
Transform3D T3D;
TransformGroup objRoot = new TransformGroup();
double px,py,pz;
px = 0.0;
py = 0.0;
pz = 0.0;

for (k=0;k<NrOfCubes;k++)
{
for (i=0;i<NrOfCubes;i++)
{
for (j=0;j<NrOfCubes;j++)
{
C = new VisualObject(this);
C.setCapability(C.ALLOW_APPEARANCE_WRITE);
TG = new TransformGroup();
T3D = new Transform3D();
TG.addChild(C);

px = i*0.2;
py = j*0.2;
pz = k*0.2;
T3D.setTranslation(new Vector3d(px-CenterX,py-CenterY,pz-
CenterZ));
TG.setTransform(T3D);

objRoot.addChild(TG);
AllCubes[counter] = C;
counter++;
//System.out.println(C);
}

}
}
objRoot.setCapability(objRoot.ALLOW_TRANSFORM_WRITE);
objRoot.setCapability(objRoot.ALLOW_TRANSFORM_READ);

return objRoot;
}
public BranchGroup createSceneGraph()
{
// Create the root of the branch graph

Appearance defaultApp = new Appearance();


CustomBehavior Act;
ColoringAttributes colAtt = new ColoringAttributes();
Material defaultMat = new Material();
PolygonAttributes polyAtt = new PolygonAttributes();
RenderingAttributes renderingAtt = new RenderingAttributes();
colAtt.setColor(new Color3f(0.2f,1.3f,0.1f));
colAtt.setShadeModel(colAtt.SHADE_FLAT);
defaultMat.setEmissiveColor(new Color3f(0.1f,0.1f,1.1f));
//colAtt.setCapability(2);
polyAtt.setPolygonMode(polyAtt.POLYGON_FILL);

defaultApp.setPolygonAttributes(polyAtt);

defaultApp.setColoringAttributes(colAtt);
defaultMat.setShininess(1.0f);
defaultMat.setAmbientColor(new Color3f(0.1f,0.1f,0.1f));
defaultMat.setDiffuseColor(new Color3f(0.0f,1.0f,1.0f));
defaultMat.setSpecularColor(new Color3f(0.0f,0.2f,0.2f));
defaultMat.setLightingEnable(true);

defaultApp.setMaterial(defaultMat);

defaultApp.setRenderingAttributes(renderingAtt);
BranchGroup objRoot = new BranchGroup();
DirectionalLight DirLight;
DirLight = new DirectionalLight();
AmbientLight ambLight = new AmbientLight();
ambLight.setColor(new Color3f(0.3f,0.2f,0.3f));
DirLight.setColor(new Color3f(0.3f,0.3f,0.3f));

//spotLight.addScope(objRoot);
DirLight.setEnable(true);

//DirLight.setCapability(2);

Sphere sphere = new Sphere();

sphere.setAppearance(new Appearance());
//objRoot.addChild(sphere);
//objRoot.addChild(DirLight);
//objRoot.addChild();

//objRoot.addChild(new ColorCube(0.2));
// rotate object has composite transformation matrix

TransformGroup rotation,mover,ActorGroup,MoverGroup;
ActorGroup = new TransformGroup();
rotation=Rotate(45,0,45);//how much to rotate
//rotation.addChild(new VisualObject());//things to rotate
rotation.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

Alpha rotationAlpha = new Alpha(-1, 1000);


RotationInterpolator rotator = new RotationInterpolator(rotationAlpha,
rotation,MultiRot(45,0,45),0.0f,(float) Math.PI*2.0f);

//rotator.setAxisOfRotation(MultiRot(45,45,45));
// a bounding sphere specifies a region a behavior is active
BoundingSphere bounds = new BoundingSphere();
rotator.setSchedulingBounds(bounds);

rotation.addChild(rotator);//the interpolator goes into the transform


group to berform the trans...

//DirLight.setDirection(new Vector3f(0.0f,0.2f,0.5f));

//mover=MoveTo(new Point3f(-3.0f,1.0f,-4.0f));
//mover.addChild(sphere);
MoverGroup = MoveTo(new Point3f(90.0f,90.0f,99.0f));
MoverGroup.addChild(new ColorCube(0.2));

Transform3D M = new Transform3D();


M.setTranslation(new Vector3d(0.0,0.0,1.0));
MoverGroup.setTransform(M);

objRoot.addChild(MoverGroup);
//objRoot.addChild(mover);
objRoot.addChild(DirLight);
objRoot.addChild(ambLight);
objRoot.addChild(rotation);
ActorGroup.addChild(new VisualObject(this));
ActorGroup.setCapability(ActorGroup.ALLOW_TRANSFORM_WRITE);
ActorGroup.setCapability(ActorGroup.ALLOW_TRANSFORM_READ);
Act = new CustomBehavior(ActorGroup,this);
Act.setSchedulingBounds(new BoundingSphere());

objRoot.addChild(ActorGroup);
objRoot.addChild(Act);

ambLight.setInfluencingBounds(new BoundingSphere(new Point3d(0.0,0.0,-


40.0),40.0));
DirLight.setInfluencingBounds(new BoundingSphere(new Point3d(0.0,0.0,-
40.0),40.0));
DirLight.setBounds(new BoundingSphere());
return objRoot;//finished scene that goes into the universe

} // end of CreateSceneGraph method of HelloJava3Da


// The following allows this to be run as an application
// as well as an applet
public TransformGroup MoveTo(Point3f TargetPoint)
{
TransformGroup Mover = new TransformGroup();
Transform3D TotalMotion = new Transform3D();
TotalMotion.transform(TargetPoint);
Mover.setTransform(TotalMotion);
return Mover;
}
public TransformGroup Rotate(double X,double Y,double Z)
{
X = X*(Math.PI/180);
Y = Y*(Math.PI/180);
Z = Z*(Math.PI/180);
TransformGroup objRotate = new TransformGroup();

Transform3D Total = new Transform3D();


Transform3D RotX = new Transform3D();
RotX.rotX(X);
Transform3D RotY = new Transform3D();
RotY.rotY(Y);
Transform3D RotZ = new Transform3D();
RotZ.rotZ(Z);
Total.mul(RotX);
Total.mul(RotY);
Total.mul(RotZ);
objRotate.setTransform(Total);

return objRotate;
}

public Transform3D MultiRot(double X,double Y,double Z)


{
X = X*(Math.PI/180);
Y = Y*(Math.PI/180);
Z = Z*(Math.PI/180);

Transform3D Move = new Transform3D();


//Move.setScale(new Vector3d(3,3,1));
Move.setScale(1.3);
Transform3D Total = new Transform3D();
Transform3D RotX = new Transform3D();
RotX.rotX(X);
Transform3D RotY = new Transform3D();
RotY.rotY(Y);
Transform3D RotZ = new Transform3D();
RotZ.rotZ(Z);
Total.mul(RotX);
Total.mul(RotY);
Total.mul(RotZ);
Total.add(Move);
return Total;
}

public static void main(String[] args) {


Frame frame = new MainFrame(new threeD(), 256, 256);
} // end of main (method of HelloJava3Da)

@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode());

@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

} // end of class HelloJava3Da

class VisualObject extends Shape3D{

private Geometry voGeometry;


private Appearance voAppearance;
GeometryInfo geoInfo;
private FileReader fReader;
public int[] MyPos = new int[2];
public boolean Visible = true;
public int frame = 0;
public threeD MyRoot;
// create Shape3D with geometry and appearance
// the geometry is created in method createGeometry
// the appearance is created in method createAppearance
public VisualObject(threeD myRoot,int x,int y,int z)
{
fReader = new FileReader("E:\\Documents and
Settings\\Rainer\\Desktop\\JavaPolyExportAll.txt",":--:");
MyRoot = myRoot;
MyPos[0]=x;
MyPos[0]=y;
MyPos[0]=z;
//reads all 3d data from file
//int vertCount = 13;
//int[]stripCounts = new int[1];
//stripCounts[0] = 13;

voGeometry = createGeometry();
//voAppearance = createAppearance();

this.setGeometry(voGeometry);
this.setAppearance(MyRoot.VisibleApp);
//this.setBoundsAutoCompute(true);

@SuppressWarnings("deprecation")
private Geometry createGeometry()
{
String Method = "QuadArray";
TriangleFanArray tfa;
TriangleStripArray tsa;
QuadArray qa;
TriangleArray ta;
TriangleArray ga;
NormalGenerator NormGen;

//tfa = new TriangleFanArray (totalN,


//TriangleFanArray.COORDINATES,
//stripCounts);
//if (Method == "TriangleStripArray")
//{
// tsa = new TriangleStripArray (fReader.NrOfVerts,
// TriangleStripArray.COORDINATES,
// );
// tsa.setCoordinates(0, coords);
// return tsa;
//}
if (Method == "QuadArray")
{
qa = new QuadArray(fReader.NrOfVerts,QuadArray.COORDINATES |
QuadArray.COLOR_3 | QuadArray.NORMALS | QuadArray.TEXTURE_COORDINATE_2);
qa.setCoordinates(0, fReader.Coords);
qa.setTextureCoordinates(0, fReader.TextCoords);
//geoInfo = new GeometryInfo(qa);

//NormGen = new NormalGenerator();


//NormGen.generateNormals(geoInfo);
//qa.setColors(0, vCol);
//int i;
//for (i = 0;i < 279;i++)
//{

qa.setColors(0, fReader.Colors);
//}
//geoInfo.setColors(vCol);

qa.setNormals(0, fReader.Normals);
return qa;
}
//||<>
else if (Method == "GeometryArray")
{
ga = new TriangleArray(3,2);
ga.setCoordinate(0,new Point3f(0.0f,0.0f,0.0f));
ga.setCoordinate(1,new Point3f(0.0f,1.0f,0.0f));
ga.setCoordinate(2,new Point3f(1.0f,1.0f,0.0f));

ga.setColor(0,new Color3f(0.0f,0.0f,1.0f));
ga.setColor(1,new Color3f(1.0f,0.0f,0.0f));
ga.setColor(2,new Color3f(0.0f,1.0f,0.0f));
return ga;
}

// code to create default geometry of visual object


return null;
}
//||<>
private Appearance createAppearance ()
{
Appearance app;
Material mat;
mat = new Material();
app = new Appearance();
Canvas Observer = new Canvas();
TextureLoader loader = new TextureLoader("E:\\Documents and
Settings\\Rainer\\Desktop\\Pics\\tex.gif","shit",Observer);
ImageComponent2D image = new
ImageComponent2D(ImageComponent2D.FORMAT_RGBA,256,256);

image = loader.getImage();
image.setCapability(image.ALLOW_FORMAT_READ);
image.setCapability(image.ALLOW_SIZE_READ);
image.setCapability(image.ALLOW_IMAGE_READ);

Texture2D texture = new Texture2D(Texture.ANISOTROPIC_SINGLE_VALUE,


Texture.RGBA,256,256);
texture.setEnable(true);
texture.setImage(0,image);

//app.setMaterial(mat);

//app.setTexture(texture);
mat.setAmbientColor(new Color3f(0.3f,0.3f,0.3f));
mat.setLightingEnable(true);

mat.setDiffuseColor( new Color3f(0.2f,0.2f,0.2f) );


mat.setShininess(0.4f);
mat.setSpecularColor( new Color3f(1.0f,1.0f,1.0f) );

//mat.setEmissiveColor(new Color3f(1.3f,0.0f,0.0f));

//app.setColoringAttributes( new ColoringAttributes( new


Color3f(0.0f,1.0f,1.0f), 1));
PolygonAttributes polyAttrib = new PolygonAttributes();
polyAttrib.setPolygonMode(2);//1 is vertexes, 2 is lines and 3 is
filled

polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
polyAttrib.setCapability(polyAttrib.ALLOW_MODE_WRITE);
app.setPolygonAttributes(polyAttrib);

app.setLineAttributes(new LineAttributes(3,3,true));
//app.setMaterial(new Material(new Color3f(0.3f,0.2f,0.1f), new
Color3f(0.3f,0.2f,0.1f), new Color3f(0.5f,0.2f,1.1f), new
Color3f(0.3f,0.2f,0.1f),1.0f));
return app;
// code to create default appearance of visual object
}

} // end of class VisualObject


//||<>
class CustomBehavior extends Behavior
{

TransformGroup TargetGroup;
WakeupCriterion TrigerEvents;
threeD MyRoot;
float RotZVal=0.0f;
float RotXVal=0.0f;
float RotTVal=0.0f;
double px,py,pz;

Alpha genAlpha=new Alpha(3,1000);

Transform3D rotation = new Transform3D();


Transform3D rotationX = new Transform3D();
Transform3D rotationZ = new Transform3D();

Transform3D Motion = new Transform3D();

//PositionInterpolator Move = new PositionInterpolator(genAlpha,


TargetGroup, Motion, 1, 5);

//constructor that takes in its target transform group


public CustomBehavior(TransformGroup targetGroup,threeD myRoot)
{
this.TargetGroup = targetGroup;
this.MyRoot = myRoot;
TargetGroup.setCapability(TargetGroup.ALLOW_TRANSFORM_WRITE);
TrigerEvents = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
//TargetGroup.addChild(Move);
//Move.setSchedulingBounds(new BoundingSphere());
px = 0.0;
py = 0.0;
pz = 0.0;

}
//||<>
@Override
public void initialize()
{
genAlpha.pause();
this.wakeupOn(TrigerEvents);
}

@Override
public void processStimulus(Enumeration criteria) {

//String[] event = new String[3];

java.awt.AWTEvent[] event;
int e=0;
event = ((WakeupOnAWTEvent)criteria.nextElement()).getAWTEvent();
if (event[0].getID() == java.awt.event.KeyEvent.KEY_PRESSED)
{
e = ((java.awt.event.KeyEvent)event[0]).getKeyCode() ;
if (e == 37)
{
//RotZVal += 0.1;
//rotation.rotZ(RotZVal);
//TargetGroup.setTransform(rotation);
}
else if (e == 39)
{
//RotZVal -= 0.1;
//rotation.rotZ(RotZVal);
//TargetGroup.setTransform(rotation);
}
else if (e == 38)
{
//RotXVal -= 0.1;
//rotation.rotX(RotXVal);
//TargetGroup.setTransform(rotation);
}
else if (e == 40)
{
//RotXVal += 0.1;
//rotation.rotX(RotXVal);
//TargetGroup.setTransform(rotation);
}
else if (e == 32)
{

//px+=0.1;
Motion.setTranslation(new Vector3d(px,py,pz));
MyRoot.AllCubes[0].setAppearance(MyRoot.defApp);
//TargetGroup.setTransform(Motion);

rotationX.rotX(RotXVal);
rotationZ.rotZ(RotZVal);
rotation.mul(rotationX);
rotation.mul(rotationZ);

rotation.mul(Motion);
TargetGroup.setTransform(rotation);
//TargetGroup.setTransform(Motion);

RotXVal=0;
RotZVal=0;
px = 0;

System.out.println( ((java.awt.event.KeyEvent)event[0]).getKeyCode() );

}
//System.out.println(criteria.nextElement().getAWTEvent());
//reset the condition
this.wakeupOn(TrigerEvents);
}
//||<>
public VisualObject GetCubeAtPos()
{
int i=0;
int co[] = new int[2];
for (i = 0;i < MyRoot.AllCubes.length ;i++)
{

if()
{

}
}
return
}
}

class FileReader extends Object


{
String[] Content = new String[200000];
String RawContent;
int ContentIndex=0;
int nrOfLines=0;
String [] Data;
//String FileLocation = ".txt";
FileInputStream fstream;
String strLine;
Point3f[] Coords;
Point2f[] TextCoords;
Vector3f[] Normals;
Color3f[] Colors;

public int NrOfVerts = 120;

public FileReader(String FileLocation,String Delimiter)


{
try
{
fstream = new FileInputStream(FileLocation);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new
InputStreamReader(in));
while ((strLine = br.readLine()) != null)
{//seni kuni fail pole l�ppu j�udnud
RawContent+=strLine;
nrOfLines++;
}
Content = RawContent.split(Delimiter);

}
catch (Exception e){//juhul kui tekkib viga
System.err.println("Error : " + e.getMessage());
}

try {
fstream.close();
} catch (IOException e) {
e.printStackTrace();
}

GetRealData();
}
public void GetRealData()
{
int i=0;
int len = 0;
String[] tempC = new String[2];
//NrOfVerts=new Integer(Content[4].split(",")[0]);
//System.out.println(NrOfVerts);
NrOfVerts = 120;
Data= new String[NrOfVerts];
float cx,cy,cz;
Coords = new Point3f[NrOfVerts];
Colors = new Color3f[NrOfVerts];
Normals = new Vector3f[NrOfVerts];
TextCoords = new Point2f[NrOfVerts];

Data = Content[0].split("-::-");
for (i=1;i<=NrOfVerts;i++)
{
tempC = Data[i].split(",");
if (tempC == null){continue;}
cx = Float.valueOf(tempC[0]);
cy = Float.valueOf(tempC[1]);
cz = Float.valueOf(tempC[2]);
//System.out.println(cx+":"+cy+":"+cz);
Coords[i-1]=new Point3f(cx,cy,cz);
}
Data = Content[1].split("-::-");
for (i=1;i<=NrOfVerts;i++)
{

tempC = Data[i].split(",");
if (tempC == null){continue;}
cx = Float.valueOf(tempC[0]);
cy = Float.valueOf(tempC[1]);
cz = Float.valueOf(tempC[2]);
//System.out.println(cx+":"+cy+":"+cz);
Colors[i-1]=new Color3f(cx,cy,cz);
}
Data = Content[2].split("-::-");
for (i=1;i<=NrOfVerts;i++)
{
tempC = Data[i].split(",");
if (tempC == null){continue;}
cx = Float.valueOf(tempC[0]);
cy = Float.valueOf(tempC[1]);
cz = Float.valueOf(tempC[2]);
//System.out.println(cx+":"+cy+":"+cz);
Normals[i-1]=new Vector3f(cx,cy,cz);
}
Data = Content[2].split("-::-");
for (i=1;i<=NrOfVerts;i++)
{
tempC = Data[i].split(",");
if (tempC == null){continue;}
cx = Float.valueOf(tempC[0]);
cy = Float.valueOf(tempC[1]);

TextCoords[i-1]=new
Point2f(Float.valueOf(tempC[0]),Float.valueOf(tempC[1]));
}

You might also like