Results 1 to 5 of 5

Thread: How to render billboards in "local space"

  1. #1

    How to render billboards in "local space"

    Hey everyone

    I want to use particle-systems in my compo-entry, and I allready have a nice particlesystem module working. However, it would be great if I could define a local coordinate system for each particle-system. I could for example, emit fire particles that move parrallel to the local Z-axis. Using a matrix, I could "orient" this beam of particles anywhere in my world.

    The problem that arises is: "How to deal with the billboards".

    For billboards, My code is as follows:

    [pascal]
    var
    X, Y, Z: TVector;
    m: TMatrix;
    begin
    //Compute billboard matrix, where positive Z points away from the camera
    //CamVec := Camera.LookAt - Camera.Pos
    //Pos = position of particle

    Z := VecNorm( CamVec );
    X := VecNorm(VecCross(VecMake(0, 1, 0), Z));
    Y := VecNorm(VecCross(X, Z));

    Result := matIdentity();
    Result[0, 0] := X.x;
    Result[0, 1] := X.y;
    Result[0, 2] := X.z;
    Result[1, 0] := Y.x;
    Result[1, 1] := Y.y;
    Result[1, 2] := Y.z;
    Result[2, 0] := Z.x;
    Result[2, 1] := Z.y;
    Result[2, 2] := Z.z;
    Result[3, 0] := pos.x;
    Result[3, 1] := pos.y;
    Result[3, 2] := pos.z;

    //Rotate about Z-axis to allow particle rotation (each particle can have a different angle
    //This is the reason I didn't use PointSprites. They dont allow this kind of rotation
    m := matRotateZ(DegToRad(Angle));
    Result := matMul(m, Result);
    end;
    [/pascal]

    This code builds a matrix that places the billboard in the world. The billboard would be on the local XY plane.

    If i start adding transformations to the ModelView matrix, this code doesn't work anymore.
    That's because the camera is defined in world space, and therefore my particles need to be defined in worldspace aswell, in order for the code to work.
    If I had a seperate model matrix, I could let it transform my particles to world-space, and then use this code. But the Model and view matrices are combined into "ModelView" and it's not easy to seperate them. I need another sollution.

    Does anyone know how to make this code "transformation-proof"?

    Thanks in advance.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  2. #2

    Re: How to render billboards in "local space"

    Never mind. This is my sollution:

    Code:
    > Make backup of modelview matrix
    > Transform all particle positions with a given matrix M to world-space
    > Make sure that the modelview matrix only contains the view transformation
    > Use the code above to "orient" each billboard.
    > Render billboards
    > Restore old modelview matrix
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  3. #3

    Re: How to render billboards in "local space"

    The obvious solution would be to first calculate M for invert rotation of modelview matrix, then for each sprite do:

    glPushMatrix
    glTranslatef
    glMultMatrix(M)
    Render
    glPopMatrix

    I would imagine it to be equally fast.

  4. #4

    Re: How to render billboards in "local space"

    You basically wants to rotate the particles around the forward vector of the modelview matrix:

    [code=pascal]
    function Matrix_GetForward(const Matrix: TMatrix4f): TVector3f;
    begin
    Result.X:= Matrix[02];
    Result.Y:= Matrix[06];
    Result.Z:= Matrix[10];
    end;

    ...

    Vector:= Matrix_GetForward(BillBoardMatrix);

    MatRotate:= Matrix_Rotation(Particle.Spin, Vector);

    [/code]
    I haven't tested this code in practice, but i think it should do the trick.
    Amnoxx

    Oh, and this code appears to be an approximate replacement for return(random() & 0x01);

    Phoenix Wiki
    http://www.phoenixlib.net/

    Phoenix Forum
    http://www.pascalgamedevelopment.com/viewforum.php?f=71

  5. #5

    Re: How to render billboards in "local space"

    Hey.. Thanks for the advice. Those are very handy tricks.
    I'll test them soon.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

Bookmarks

Posting Permissions

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