Author Topic: JPCT+odejava and Skybox  (Read 6263 times)

Offline Chico de las poeías

  • byte
  • *
  • Posts: 2
    • View Profile

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: JPCT+odejava and Skybox
« Reply #1 on: July 03, 2009, 02:43:42 pm »
Nice work. However, i had some trouble figuring out why the boxes in the ODE-example are actually moving until i realized that the rotation matrix you are using actually isn't a rotation matrix, but a combined transformation matrix. Without digging further into ODE or your code, i suggest to split the matrix into a rotational and a translational part somehow like so:

Code: [Select]
import com.threed.jpct.*;
import javax.swing.*;
import java.awt.*;
import javax.vecmath.Matrix4f;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;

/**
 *
 * @author Enrique Bermúdez E-mail: enbe11_3@hotmail.com
 */

public class JPCTODE {

    public static void main(String[] args) throws Exception {
        JPCTODE JO = new JPCTODE();
        JO.doIt();
    }

    private void doIt() throws Exception {

        JFrame frame = new JFrame();
        frame.setSize(640, 480);
        frame.setVisible(true);
        ODE ode = new ODE();

        FrameBuffer fb = new FrameBuffer(640, 480, FrameBuffer.SAMPLINGMODE_NORMAL);
        fb.enableRenderer(IRenderer.RENDERER_SOFTWARE);

        World w = new World();
        w.setAmbientLight(255, 255, 255);

        TextureManager tm = TextureManager.getInstance();
        Texture base = new Texture("top.jpg");
        tm.addTexture("base", base);
        Object3D box = Primitives.getCube(2.0F);

        box.setTexture("base");
        box.setEnvmapMode(box.ENVMAP_ENABLED);
        box.setEnvmapped(true);

        Object3D sp = Primitives.getCube(2.0F);
        sp.setTexture("base");
        sp.setEnvmapMode(box.ENVMAP_ENABLED);
        sp.setEnvmapped(true);

        sp.rotateY((float) Math.PI / 4f);
        sp.rotateMesh();
        box.rotateY((float) Math.PI / 4f);
        box.rotateMesh();
        w.addObject(sp);
        w.addObject(box);

        w.buildAllObjects();
        w.getCamera().moveCamera(Camera.CAMERA_MOVEOUT, 40);

        w.getCamera().lookAt(box.getCenter());
        w.addLight(new SimpleVector(30, 30, 03), 30, 30, 03);

        Matrix n = new Matrix();
        Matrix m = new Matrix();

        while (frame.isActive()) {

            n.setDump(getMatrix(ode.boxBody.getQuaternion(), ode.boxBody.getPosition(), 1.0f));
            m.setDump(getMatrix(ode.boxBody2.getQuaternion(), ode.boxBody2.getPosition(), 1.0f));
            ode.step();

            box.setRotationMatrix(extractRotation(n));
            sp.setRotationMatrix(extractRotation(m));
            
            box.setTranslationMatrix(extractTranslation(n));
            sp.setTranslationMatrix(extractTranslation(m));

            fb.clear(new Color(200, 200, 200));
            w.renderScene(fb);
            w.draw(fb);

            fb.display(frame.getGraphics());
            Thread.sleep(30);
        }

        fb.disableRenderer(IRenderer.RENDERER_OPENGL);
        System.exit(0);
    }

    private Matrix extractRotation(Matrix m) {
      Matrix m2=m.cloneMatrix();
      m2.set(3, 0, 0);
      m2.set(3, 1, 0);
      m2.set(3, 2, 0);
      m2.set(3, 3, 1);
      return m2;
    }
    
    private Matrix extractTranslation(Matrix m) {
      Matrix m2=m.cloneMatrix();
      m2.set(0, 0, 1);
      m2.set(1, 1, 1);
      m2.set(2, 2, 1);
      
      m2.set(1, 0, 0);
      m2.set(2, 0, 0);
      m2.set(2, 1, 0);
      
      m2.set(0, 1, 0);
      m2.set(0, 2, 0);
      m2.set(1, 2, 0);
      
      return m2;
    }
    
    private float[] getMatrix(Quat4f q, Vector3f v, float f) {

        Matrix4f m = new Matrix4f(q, v, f);

        return new float[]{m.m00, m.m10, m.m20, m.m30,
                    m.m01, m.m11, m.m21, m.m31,
                    m.m02, m.m12, m.m22, m.m32,
                    -m.m03, -m.m13, -m.m23, m.m33
                };
    }
}


Giving jPCT a rotation matrix to work with that actually isn't one may bite you sooner or later...

Offline Chico de las poeías

  • byte
  • *
  • Posts: 2
    • View Profile
Re: JPCT+odejava and Skybox
« Reply #2 on: July 03, 2009, 06:16:42 pm »
Thanks! ;D



PD:
Oviusly, you can also work with diferents shapes.
For example:
   
ODE.java:   
   
Quote
boxBody2 = new Body("box2", world, new GeomSphere("esfera",2f));
JPCTODE.java:
   
Quote
Object3D sp = Primitives.getSphere(2.0F);

   
(if anybody wants odejava supports, you can find it here: http://www.javagaming.org/ )