Converting LOCAL coordinates to WORLD coordinates - How do I do it?

Hi there ppl…

I’m building an OpenGL application for my Computer Graphics Orientation assignment that is supposed to render 3D trees. So far so good, nothing special.

I’ve defined a recursive procedure in Delphi that can draw binary trees given a couple of parameters and it works perfectly, but this procedure is currently called every time the tree is drawn. This is, of course, unnecessary and I would like to save my tree as an interleaved vertex/color-array or as a display list. But now I’m stuck with a problem. This is my recursive procedure (I’ve left out a couple of details):

Procedure DrawTree(Depth, MaxDepth: Byte; Scale, Length, Angle, Twist: Double)

// Some Const en Var declarations…

Begin

// Setting up some variables and constants and drawing a cylinder at the desired position as the current branch…

If Depth<>0 Then Begin //Call DrawTree again?

glTranslatef(0, Length, 0);

glPushMatrix(); //New branch number one…
glRotatef(Angle, 0, 0, 1);
glRotatef(Twist, 0, 1, 0);
DrawTree(Depth - 1, MaxDepth, Scale, Length * Scale, Angle, Twist);
glPopMatrix();

glPushMatrix(); //New branch number two…
glRotatef(-Angle, 0, 0, 1);
glRotatef(Twist, 0, 1, 0);
DrawTree(Depth - 1, MaxDepth, Scale, Length * Scale, Angle, Twist);
glPopMatrix();

End;

End;

As you can see, every new branch has its own, new local coordinate system, but if I want to save my tree, I actually want to save every knot at which a fork occurs as a vertex in my array in WORLD coordinates. So I have to figure out how to convert the current local coordinates to world coordinates based on the current modelviewmatrix on the stack and that’s the part were I’m stuck. How do I convert local to world coordinates :wink: ?

A thing worth mentioning might be that the camera also resides on the modelviewmatrix (although I don’t know whether or not this makes any difference for my problem), the procedure that redraws the scene is as follows (again with a couple of details left out):

Procedure Redraw(Sender: TObject);
Begin

glClear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
With Camera Do Begin
gluLookAt(EyeX, EyeY, EyeZ, TarX, TarY, TarZ, UpX, UpY, UpZ);
glRotatef(OrbX, 1, 0, 0); //Camera rotations
glRotatef(OrbY, 0, 1, 0);

// Some lighting routines…

End;

DrawTree(DepthBar.Position, DepthBar.Position, ScaleBar.Position / 100, LengthBar.Position / 100, AngleBar.Position, TwistBar.Position);
PageFlip;

End;

I hope all of this makes sense and someone out there knows what I’m trying to do, I’d really appreciate some help at transforming the local coords to world coords ;-).

GRTZ and thanks in advance,

VuurSnikkel.

I’m confused by your question, I think. You want to display list your drawing - that is the end goal, right? If that is what you wnat, then the coordinate conversion is unneccessary. Put you glNewList() call before you enter the recursive function. Next, let that function do its thing. When it’s done, call glEndList(). Voila! A display list with your tree.

Chris

Hi there…

Nope, I really need the world coordinates, since I want to save the model in different 3D formats, I want to do some spline lofting along the tree-branches and want to apply randomization functions at the worldspace level, among other things. But you’re absolutely right about the fact that if putting things in a display list was the only goal, I didn’t need the conversion. Unfortunately, that isn’t the only goal ;-).

GRTZ,

VuurSnikkel.

At each node, get the current modelview matrix. That is the matrix that OpenGL uses to transform the local coordinates into view space.

If you don’t apply any camera transforms (e.g. gluLookAt), then view space is the same as world space.

If you do apply camera transforms, then you need to save a copy of the modelview matrix after applying the camera transforms so you can use its inverse to transform teh coordiantes from view space back to world space.

Hi there…

Yes, I thought about that, but isn’t it quite expensive to take the inverse of a matrix during every recursion of the tree? Even with a fast algorithm it still takes a considerable amount of time if I’m not mistaking.

But I’ve already created my own transformation matrices that I’m going to use so I don’t have to alter the OpenGL coordinate system. They’re pretty fast (SSE optimized) and should do the trick.

GRTZ,

VuurSnikkel.