Camera Matrix

Started by hayden621, February 10, 2012, 11:14:58 AM

Previous topic - Next topic

hayden621

Hi there,

Mind asking is there a way to get the camera view matrix and projection matrix?

Thanks a lot!

Hayden

Vi_O


EgonOlsen

But that's only the rotational part...may i ask what you want to do with that matrix? Maybe there's a better way of doing it...

hayden621

Hi,

We would like to get the screen co-ordinate of the 3d object as well the projected size. Is there any way we can get this?

P.S. I am from the same company as Russell, mind asking your something through email?

Hayden

hayden621

Hi,

By the way, mind asking is there any particle system that can be used with jpct?

Thanks!

EgonOlsen

You can use the methods in Interact2D to project from 3D (world space) into screen space: http://www.jpct.net/jpct-ae/doc/com/threed/jpct/Interact2D.html#project3D2D(com.threed.jpct.Camera, com.threed.jpct.FrameBuffer, com.threed.jpct.SimpleVector).

About the particle system: There's nothing "official" but you'll find different, similar implementations in various sources of mine. The sources for Alien Runner should contain a simple one IIRC. However, they are all pretty basic and work with one object/particle. This isn't very efficient but was sufficient for me so far. You'll find some discussion about this topic somewhere here in the forum too.

hayden621

Hi,

But when I project the 3D world space to screen co-ordinate, I can only get the position but not the size. Is there a way to do so?

Hayden

EgonOlsen

You can get the bounding box (in min/max-form) of an object in object space from the Mesh, project that into world space and project into screen space using these methods. Just make sure that you store the bounding box, because getting it always triggers a recalculation.

hayden621

Thanks a lot!

As we are going make an android version of an iOS app, but we are not sure if JPCT can provide such support for that, I have sent you an email about the issue.

Thanks again!

EgonOlsen

#9
Regarding your mail: Yes, you should be able to do this (i.e. draw trails behind an object), but it can become a bit tricky, because you have to use an IVertexController and a somewhat hacky container object for the trails that the controller can modify on the fly. Maybe this helps to get you started (it's for desktop jPCT though):


import java.awt.Color;

import com.threed.jpct.*;
import com.threed.jpct.util.Light;


public class TrailTest
{
  private static int TRAIL_POLYGONS = 200;

  private World world;
  private FrameBuffer buffer;
  private Object3D sphere;

  private Object3D trail;

  public static void main(String[] args)
    throws Exception
  {
    new TrailTest().loop();
  }


  public TrailTest()
    throws Exception
  {
    world = new World();
    world.setAmbientLight(100, 100, 100);

    sphere = Primitives.getSphere(20, 1f);
    sphere.build();
    sphere.compile();
    sphere.translate(-100, 0, 50);
    world.addObject(sphere);

    Camera cam = world.getCamera();
    cam.setPosition(0, -50, 0);
    cam.lookAt(sphere.getTransformedCenter());

    Light light = new Light(world);
    light.setPosition(new SimpleVector(-100, 0, -150));
    light.setAttenuation(-1);

    trail = createTrailObject(sphere);
    world.addObject(trail);
  }


  /**
   * This looks a bit hacky...it creates an object with a defined number of unique polygons that are all out of sight.
   * The vertex controller will then take polygons from this soup that arrange them to form the trail.
   *
   * @param emitter
   * @return
   */
  private Object3D createTrailObject(Object3D emitter)
  {
    Logger.log("Creating trail object...");
    Object3D trail = new Object3D(TRAIL_POLYGONS);
    trail.disableVertexSharing();
    for (int i = 0; i < TRAIL_POLYGONS / 2; i++)
    {
      trail.addTriangle(new SimpleVector(-1100000 + i, -1200000 + i, -1300000 + i), new SimpleVector(-1400000 + i,
          -1500000 + i, -1600000 + i), new SimpleVector(-1700000 + i, -1800000 + i, -1900000 + i));
      trail.addTriangle(new SimpleVector(2000000 + i, 2100000 + i, 2200000 + i), new SimpleVector(2300000 + i,
          2400000 + i, 2500000 + i), new SimpleVector(2600000 + i, 2700000 + i, 2800000 + i));
    }

    trail.calcNormals();
    trail.getMesh().setVertexController(new TrailBlazer(emitter), false);
    trail.forceGeometryIndices(false);
    trail.compile(true);
    trail.build();

    trail.setAdditionalColor(Color.YELLOW);
    trail.setCulling(Object3D.CULLING_DISABLED);
    trail.setTransparency(0);

    return trail;
  }


  private void loop()
    throws Exception
  {
    buffer = new FrameBuffer(640, 480, FrameBuffer.SAMPLINGMODE_NORMAL);
    buffer.disableRenderer(IRenderer.RENDERER_SOFTWARE);
    buffer.enableRenderer(IRenderer.RENDERER_OPENGL);

    float cnt = 0;
    float y = 0;
    float yAdd = -0.1f;

    while (!org.lwjgl.opengl.Display.isCloseRequested())
    {
      SimpleVector lastTrans = sphere.getTranslation();
      sphere.translate((float) Math.sin(cnt), yAdd, (float) Math.cos(cnt));
      SimpleVector newTrans = sphere.getTranslation();
      lastTrans.sub(newTrans);
      sphere.getRotationMatrix().setTo(lastTrans.getRotationMatrix());

      trail.getMesh().applyVertexController();
      trail.touch();
     
      cnt += 0.05f;

      if (y < -30)
      {
        yAdd = 0.1f;
      }
      else if (y > 30)
      {
        yAdd = -0.1f;
      }
      y += yAdd;

      buffer.clear(java.awt.Color.BLACK);
      world.renderScene(buffer);
      world.draw(buffer);
      buffer.update();
      buffer.displayGLOnly();
      Thread.sleep(10);
    }
    System.exit(0);
  }


  private static class TrailBlazer
    extends GenericVertexController
  {
    private int maxPos = TRAIL_POLYGONS / 2;
    private int pos = 0;
    private Object3D emitter = null;
    private SimpleVector p0 = new SimpleVector();
    private SimpleVector p1 = new SimpleVector();
    private SimpleVector p2 = new SimpleVector();
    private SimpleVector p3 = new SimpleVector();
    private SimpleVector center = new SimpleVector();
    private SimpleVector lastP2 = null;
    private SimpleVector lastP3 = null;
    private SimpleVector z = new SimpleVector();
    private SimpleVector x = new SimpleVector();
    private SimpleVector tmp = new SimpleVector();

    private static final long serialVersionUID = 1L;


    public TrailBlazer(Object3D emitter)
    {
      this.emitter = emitter;
    }


    public void apply()
    {
      center = emitter.getTransformedCenter(center);
      SimpleVector[] dest = this.getDestinationMesh();

      z = emitter.getRotationMatrix().getZAxis(z);
      x = emitter.getRotationMatrix().getXAxis(z);

      if (lastP2 == null)
      {
        tmp.set(center);
        tmp.sub(x);
        lastP2 = new SimpleVector(tmp);
        tmp.add(x);
        tmp.add(x);
        lastP3 = new SimpleVector(tmp);
      }

      p0.set(lastP2);
      p1.set(lastP3);

      int cPos = pos * 6;

      tmp.set(center);
      tmp.sub(z);
      tmp.sub(x);
      p2.set(tmp);
      tmp.add(x);
      tmp.add(x);
      p3.set(tmp);

      dest[cPos++].set(p0);
      dest[cPos++].set(p1);
      dest[cPos++].set(p3);

      dest[cPos++].set(p2);
      dest[cPos++].set(p0);
      dest[cPos].set(p3);

      pos++;
      if (pos >= maxPos)
      {
        pos = 0;
      }
      lastP2.set(p2);
      lastP3.set(p3);
    }
  }
}


hayden621

Thanks for your prompt reply!