[idx3d::Docu1]
Example of using idx3d

In this tutorial, we create step by step a little example Applet with the phong shaded textured Trefoil you are seeing below.

1. Preparing the scene

We prepare the scene in the init() method.

First, we need to create a new instance of idx_Scene we will work with.
We set it the same size as the applet.
idx3d_Scene scene=new idx3d_Scene(this.size().width,this.size().height);

Then, we import a material from a file called "marble.material" (generated with the material lab) and register it in the scene:
idx3d_Material marble=new idx3d_Material(getDocumentBase(),"marble.material");
scene.addMaterial("Marble",marble);

And we also need some light sources, else we won't see our object:
scene.addLight("Light1",new idx3d_Light(new idx3d_Vector(0.2f,0.2f,1f),0xFFFFFF,320,120));
scene.addLight("Light2",new idx3d_Light(new idx3d_Vector(-1f,-1f,1f),0xFFCC99,160,200));

2. Adding a new 3d object

We create a new Trefoil with the idx3d_ObjectFactory. The Trefoil is a [2,3]-Torusknot. We choose following parameters: Tube radius=0.4, Torus outer radius=1.2, Torus inner radius=0.48, Torus height=1.2, Tesselation segment=120, Tesselation steps=8.
scene.addObject("Trefoil",idx3d_ObjectFactory.TORUSKNOT(2f,3f,0.4f,1.2f,0.48f,1.2f,120,8));

Just for fun, we rotate and scale the object:
scene.object("Trefoil").rotate(0.2f,3.5f,-0.5f);
scene.object("Trefoil").scale(0.4f);

We assign our material to the object:
scene.object("Trefoil").setMaterial(scene.material("Marble"));

Finally we use idx3d_TextureProjector to project the texture on top of the trefoil:
idx3d_TextureProjector.projectTop(scene.object("Trefoil"));

3. Rendering the scene

To avoid flickering, we overwrite the paint(Graphics) method and draw the rendered Image in the update(Graphics) method instead:

public void paint(Graphics g)
{
  repaint();
}

public void update(Graphics g)
{
  scene.render();
  g.drawImage(scene.getImage(),0,0,this);
}

Very easy to use, isn't it?

4. User interaction

When the user drags the mouse, the scene should be rotated. We store the last mouse position in global variables int oldx,oldy.

public boolean mouseDown(Event evt,int x,int y)
{
  oldx=x;
  oldy=y;
  return true;
}

public boolean mouseDrag(Event evt,int x,int y)
{
  float dx=(float)(oldy-y)/50;
  float dy=(float)(x-oldx)/50;
  scene.rotate(dx,dy,0);
  oldx=x;
  oldy=y;
  repaint();
  return true;
}

5. Further improvements

When the scene is not rotated by the user, it could be displayed antialiased, which looks a little nicer. When we create the scene, we can use scene.setAntialias(true); to enable antialiasing.

Antialiasing should be disabled when the user is attempting to rotate, so we deactivate it in the public boolean mouseDown(Event evt, int x, int y) method.
To enable antialiasing when the user stops dragging, we reactivate it the public boolean mouseUp(Event evt, int x, int y) method.

6. Things that change if you write in as a java application instead of an applet

If you decide to write the same thing as an application, you simply subclass java.awt.component directly instead of java.applet.Applet. You then add this component to a new frame you have to open. You may even make the thing scaleable by adding scene.resize(this.size().width,this.size().height); to the components reshape(...) method.

7. View the running example

Run the example !

8. View the entire source code of this example

tutorial1.java

idx3d SubPages
Demos
Download
Releases
Feature list
Documentation
API [Interface]
Source Code
Class Diagrams
Links

Material Lab
idx3d.debug.Inspector

© 2000 by Peter Walser    |    proxima@active.ch     |    http://www2.active.ch/~proxima