Professional Documents
Culture Documents
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.
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 TheScene;
TheScene = new BranchGroup();
//SceneAct.setTranslation(new Vector3d(-0.6,0.0,0.4));
//SceneActions.setTransform(SceneAct);
scene.addChild(myMouseRotate);
//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));
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
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.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);
//rotator.setAxisOfRotation(MultiRot(45,45,45));
// a bounding sphere specifies a region a behavior is active
BoundingSphere bounds = new BoundingSphere();
rotator.setSchedulingBounds(bounds);
//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));
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);
return objRotate;
}
@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
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;
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;
}
image = loader.getImage();
image.setCapability(image.ALLOW_FORMAT_READ);
image.setCapability(image.ALLOW_SIZE_READ);
image.setCapability(image.ALLOW_IMAGE_READ);
//app.setMaterial(mat);
//app.setTexture(texture);
mat.setAmbientColor(new Color3f(0.3f,0.3f,0.3f));
mat.setLightingEnable(true);
//mat.setEmissiveColor(new Color3f(1.3f,0.0f,0.0f));
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
}
TransformGroup TargetGroup;
WakeupCriterion TrigerEvents;
threeD MyRoot;
float RotZVal=0.0f;
float RotXVal=0.0f;
float RotTVal=0.0f;
double px,py,pz;
}
//||<>
@Override
public void initialize()
{
genAlpha.pause();
this.wakeupOn(TrigerEvents);
}
@Override
public void processStimulus(Enumeration criteria) {
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
}
}
}
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]));
}