Main Menu

Version 0.90 has been released!

Started by EgonOlsen, July 01, 2003, 11:20:27 PM

Previous topic - Next topic

EgonOlsen

Some minor things like an improved collision detection performance for ray-polygon collisions and some cleanup work. A major addition is the possibility to now build yourself a VertexController to modify vertices of already constructed objects. This is something that wasn't possible in jPCT before. The usage of the VertexController may not be very intuitive, but i'm planning to add a demo of that. For now, look at this source code to see a sample implementation:

public class DemoVertexController extends GenericVertexController {

    int count;

    DemoVertexController() {
      count=0;
    }

    public void apply() {
      SimpleVector[] srcMesh=this.getSourceMesh();
      SimpleVector[] dstMesh=this.getDestinationMesh();

      int size=this.getMeshSize();

      for (int i=0; i<size; i++) {
        float z=srcMesh[i].z;
        float x=0.25f*(srcMesh[i].x+count);
        float sin=(float) Math.sin(x);

        dstMesh[i].z=sin*3f+z;
      }
      count++;
    }
}


Yeah right! The VertexController stuff may sound more complicated than it really is when it comes to implementing one (note that this implementation skips updating the vertex normals, which is not quite correct).

Use your new controller like this:


     IVertexController demoControl=new DemoVertexController();
     Object3D obj=Primitives.getPlane(20,5);
     myWorld.addObject(obj);
     obj.setTexture("texture");
     obj.build();
     obj.getMesh().setVertexController(demoControl, IVertexController.PRESERVE_SOURCE_MESH);


Note that the call to build() has to be placed before the getMesh() to make sure that the normals are calculated. This isn't mentioned in the current docs, so i'm mentioning it here.

The result will be a plane whose vertices are being modified by the VertexController in realtime. Can't show it in motion ATM, so here's a quick and dirty picture of it (not that it's very impressive but it may help to understand what a VertexController is supposed to do):


dear_stephen

Hi

Can you please post the entire code for this mesh example?

cheers

dear_stephen



import java.awt.Color;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.JFrame;

import com.threed.jpct.Camera;
import com.threed.jpct.Config;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.IRenderer;
import com.threed.jpct.IVertexController;
import com.threed.jpct.Lights;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;
import com.threed.jpct.util.KeyMapper;
import com.threed.jpct.util.KeyState;

public class HelloWorld extends JFrame {
/**
* Are we rendering in wireframe mode?
*/
private boolean wireframe = false;

private FrameBuffer buffer = null;

private World world = null;

private TextureManager textureManager = null;

private Camera camera = null;

private int width = 640;

private int height = 480;

private Graphics g = null;

/**
* main method
*/
private static final long serialVersionUID = 1L;

public static void main(String[] args) {
HelloWorld h = new HelloWorld();
}

private boolean exit = false;

private int titleBarHeight;

private int leftBorderWidth;

private KeyMapper keyMapper;

/**
*/
public HelloWorld() {
setFrameStuff();
add3DStuff();
gameLoop();
}

private void gameLoop() {
World.setDefaultThread(Thread.currentThread());

buffer = new FrameBuffer(width, height, FrameBuffer.SAMPLINGMODE_NORMAL);
buffer.enableRenderer(IRenderer.RENDERER_SOFTWARE);

while (!exit) {
buffer.clear();
world.renderScene(buffer);
if (!wireframe) {
world.draw(buffer);
} else {
world.drawWireframe(buffer, Color.white);
}
buffer.update();
poll();
buffer.display(g, leftBorderWidth, titleBarHeight);
Thread.yield();

try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
}

System.exit(0);
}

private void setFrameStuff() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("jPCT Hello World (" + Config.getVersion() + ")");
pack();
Insets insets = getInsets();
titleBarHeight = insets.top;
leftBorderWidth = insets.left;
setSize(width + leftBorderWidth + insets.right, height + titleBarHeight
+ insets.bottom);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
g = getGraphics();
keyMapper = new KeyMapper(this);
}

private void add3DStuff() {
/**
* Load the textures needed and add them to the TextureManager. We are
* loading the "numbers" texture for blitting the framerate and the
* texture for the ground. Textures used by the game entities are loaded
* by them.
*/
char c = File.separatorChar;

world = new World();
textureManager = TextureManager.getInstance();
Texture rocks = new Texture("textures" + c + "rocks.jpg");
textureManager.addTexture("rocks", rocks);

Object3D box = Primitives.getBox(50, 1);
box.setOrigin(new SimpleVector(0, 0, 0));
//world.addObject(box);

IVertexController demoControl = new DemoVertexController();
Object3D obj = Primitives.getPlane(50, 3);
//obj.calcTextureWrap();
obj.build();
obj.getMesh().setVertexController(demoControl,
IVertexController.PRESERVE_SOURCE_MESH);
obj.setOrigin(new SimpleVector(0, 0, 0));
obj.getMesh().applyVertexController();
obj.rotateZ(3.14f/4);
obj.setTexture("rocks");
world.addObject(obj);

world.buildAllObjects();
SimpleVector cameraPosition;
cameraPosition = obj.getCenter();
cameraPosition.y = cameraPosition.y + 140;
cameraPosition.z = cameraPosition.z - 105;
camera = world.getCamera();
camera.setPosition(cameraPosition);
camera.lookAt(obj.getCenter());

/**
* Place the camera at the starting position.
*/
//setCamera(obj);
setLight();

}

private void setCamera(Object3D box) {
camera = world.getCamera();
camera.setPosition(0, -100, 200);
camera.lookAt(box.getCenter());
}

private void setLight() {
// Config.fadeoutLight = false;
world.getLights().setOverbrightLighting(
Lights.OVERBRIGHT_LIGHTING_DISABLED);
world.getLights().setRGBScale(Lights.RGB_SCALE_2X);
world.setAmbientLight(100, 100, 100);

/**
* Place the lightsources...
*/
world.addLight(new SimpleVector(0, -150, 0), 25, 22, 19);
world.addLight(new SimpleVector(-100, -150, 100), 22, 5, 4);
world.addLight(new SimpleVector(100, -150, -100), 4, 2, 22);

}

/**
* Use the KeyMapper to poll the keyboard
*/
private void poll() {
KeyState state = null;
do {
state = keyMapper.poll();
if (state != KeyState.NONE) {
keyAffected(state);
}
} while (state != KeyState.NONE);
}

private void keyAffected(KeyState state) {
int code = state.getKeyCode();
boolean event = state.getState();

switch (code) {
case (KeyEvent.VK_ESCAPE) : {
exit = event;
break;
}
}
}
}

EgonOlsen

I don't quite understand...have you answered your own question by posting the code?

dear_stephen

Sorry! You are right. I answered my own question after I managed to get it working.