11011110110011011001100110011001000110011001100111010001011

April 12, 2007

Pos3D to Pos2D in M3G

Filed under: Programming

to 大河马~~emoticon

  Camera m_camera;               //current camera
  Transform m_camTransform;  //current camera transform
  Transform m_objTransform;   //transform of the render obj

  void Pos3D2Pos2D(float[] pos3D, float[]pos2D)
  {
      float pos[] = new float[]{pos3D[0], pos3D[1], pos3D[2], 1}  
     
      //get current position
      m_objTransform.transform(pos);
     
      //apply camera transform
      Transform invTrans = new Transform(m_camTransform);     
      invTrans.invert();
      invTrans.transform(pos);
     
      //get z
      float z = -pos[2];
      float x = 0;
      float y = 0;

      //projection
      Transform transProjection = new Transform();
      camera.getProjection(transProjection);
      transProjection.transform(pos);     
     
      // NDC to View
      x = pos[0] * getWidth()/ (2 * z);
      y = pos[1] * getHeight()/ (2 * z);     
     

      //convert to screen pos.
      pos2D[0] = (int)(getWidth()/2 + x);
      pos2D[1] = (int)(getHeight()/2 - y);
     
  }

2 Comments »

The URI to TrackBack this entry is: http://lyo.blogsome.com/2007/04/12/pos3d-to-pos2d-in-m3g/trackback/

  1. ding!!!!!

    Comment by 大河马 — April 12, 2007 @ 2:24 pm

  2. 指定z的Pos2D2Pos3D也容易了….

    Comment by Lyo — April 12, 2007 @ 11:03 pm

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.