×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    5

    A new Text node in M3G

    Hi everyone...

    I would like to create a new type of node for the M3G so I can display text as another Transformable. My first approach to achieve so has been to extend from Group (I don't mind to have chindren ) and then create my own render method that I will call after the whole scene has been rendered.

    My problem comes when I want to emulate the M3G rendering pipeline. This is what I do:

    1.- Get active camera's transform and invert it.
    2.- Get active camera's projection transform.
    3.- Get the group's transform (my node's transform)
    4.- Postmultiply the inverted active camera's transform by the group's transform. Let's call it modelViewTransform.
    5.- PostMultiply the active camera's projection transform by the new modelViewTransform. Let's call it projectedTransform.
    6.- Extract the position from the final projectedTransform using it's matrix: x = matrix[3], y = matrix[7], z = matrix[11]

    Should I divide each x and y by the z right to obtain the final x and y to draw the text shouldn't I? I even use the Graphics3D viewport values to obtain the final x and y to draw the text.

    Well... I think that I have the theory right but the thing is that I cannot obtain the correct results. If I have the new TextNode in the same position as my Mesh nodes, it should be drawn in the same visual position but it doesn't. I must have something wrong. Anyone could help me?

    Here you go the code for rendering the text:

    public class TextNode extends Group
    {
    private Font font = null;
    private int color = 0xFFFFFFFF;
    private int x = 0, y = 0;
    private String text = "";
    private int anchor = Graphics.BASELINE | Graphics.HCENTER;
    private Transform cameraTransform = new Transform();
    private Transform textTransform = new Transform();
    private Transform projectionTransform = new Transform();
    private float[] textMatrix = new float[16];

    public Text()
    {
    }

    public void render(Graphics g)
    {
    Graphics3D g3D = Graphics3D.getInstance();
    // g3D.bindTarget(g);
    // Get the camera transform, the projection transform and this Transformable's transform
    Camera camera = g3D.getCamera(cameraTransform);
    camera.getProjection(projectionTransform);
    getCompositeTransform(textTransform);
    // Invert the camera transform
    cameraTransform.invert();
    // Multiply the inverted camera transform by the text transform
    cameraTransform.postMultiply(textTransform);
    // Multiply the projection transform by the result of the last multiplication
    projectionTransform.postMultiply(cameraTransform);
    // Retrieve the final camera inverted and projected matrix
    projectionTransform.get(textMatrix);
    // Extract the position from the matrix
    float x = textMatrix[3];
    float y = textMatrix[7];
    float z = textMatrix[11];
    // Get the viewport information
    int viewportX = g3D.getViewportX();
    int viewportY = g3D.getViewportY();
    int viewportWidth = g3D.getViewportWidth();
    int viewportHeight = g3D.getViewportHeight();
    // Calculate the final 2D position where to render the text
    this.x = viewportX + (viewportWidth >> 1) + (int)(x / z);
    this.y = viewportY + (viewportHeight >> 1) - (int)(y / z);
    // Render the text
    g.setColor(color);
    g.drawString(text, this.x, this.y, anchor);
    }
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    5

    Re: A new Text node in M3G

    I already found the mistake... The modelView transformation and projection transformation algorithm was correct but the viewport transformation was incorrect. Here is the new code (I just need to know how to obtain the near/far planes from a projection matrix and I'll be done!):

    public class TextNode extends Group
    {
    public static float NEAR = 0.001f;
    public static float FAR = 100.0f;

    private Font font = Font.getDefaultFont();
    private int color = 0xFFFFFFFF;
    private int x = 0, y = 0;
    private String text = "";
    private int anchor = Graphics.BASELINE | Graphics.HCENTER;
    private Transform cameraTransform = new Transform();
    private Transform textTransform = new Transform();
    private Transform projectionTransform = new Transform();
    private float[] textMatrix = new float[16];

    public Text()
    {
    }

    public void render(Graphics g)
    {
    Graphics3D g3D = Graphics3D.getInstance();
    // g3D.bindTarget(g);
    // Get the camera transform, the projection transform and this Transformable's transform
    Camera camera = g3D.getCamera(cameraTransform);
    camera.getProjection(projectionTransform);
    getCompositeTransform(textTransform);
    // Invert the camera transform
    cameraTransform.invert();
    // Multiply the inverted camera transform by the text transform
    cameraTransform.postMultiply(textTransform);
    // Multiply the projection transform by the result of the last multiplication
    projectionTransform.postMultiply(cameraTransform);
    // Retrieve the final camera inverted and projected matrix
    projectionTransform.get(textMatrix);
    // Extract the position from the matrix
    float x = textMatrix[3];
    float y = textMatrix[7];
    float z = textMatrix[11];
    // The text should be only drawn if it's projected z in inside the NEAR-FAR planes
    // TODO: Could we obtain these NEAR-FAR planes from the camera?
    if (z >= NEAR && z <= FAR)
    {
    // Get the view port information
    int viewportX = g3D.getViewportX();
    int viewportY = g3D.getViewportY();
    int middleViewportWidth = g3D.getViewportWidth() >> 1;
    int middleViewportHeight = g3D.getViewportHeight() >> 1;
    // Calculate the final 2D position where to render the text
    this.x = viewportX + middleViewportWidth + (int)(middleViewportWidth * x / z);
    this.y = viewportY + middleViewportHeight - (int)(middleViewportHeight * y / z);
    // Render the text
    g.setColor(color);
    g.setFont(font);
    g.drawString(text, this.x, this.y, anchor);
    }
    }
    }

  3. #3
    Nokia Developer Expert
    Join Date
    Mar 2003
    Posts
    7

    Smile Re: A new Text node in M3G

    You could get the near and far planes by using getProjection on the Camera object. That will give you the parameters back (including the type of projection you are using). You will get an array of the parameters you provided in setPerspective or setParallel.

    That should work for you.

Similar Threads

  1. TextWrapper example class
    By miohtama in forum Python
    Replies: 6
    Last Post: 2008-03-08, 11:46
  2. Exit form from edit mode
    By tqchcm in forum Symbian User Interface
    Replies: 13
    Last Post: 2007-10-31, 06:06
  3. Writing chinese text on emulator
    By TheLittleBird in forum Symbian
    Replies: 1
    Last Post: 2007-06-15, 11:41
  4. Rendering text in multiple languages
    By TheLittleBird in forum Symbian C++
    Replies: 0
    Last Post: 2007-06-05, 12:52
  5. Replies: 0
    Last Post: 2004-11-24, 18:49

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •