JeffM2501's picture

Matrix4 questions

I am working on a simple octree system patterned after some XNA samples I found, and am building up a series of generic bounding classes( BoundingBox, BoundingSphere, BoundingFrustum, etc..) that use OpenTK's math system rather then MSs directX code.

I'm running into some issues when dealing with the frustums and their associated matrices.

1) All the C docs for openGL I have seen show that GL stores it's matrix info in column major format, yet Matrix4 seems to be row major. Is there something in the GL layer for OpenTK that changes this? or am I mistaken?

2) I don't see an easy way to use GL.GetFloat(GetPName.ModelviewMatrix... to load the current GL matrix into a Matrix4, but I see that there are overloads for GL.LoadMatrix and MultiMatrix, etc.. Is there an easy way to do this? or is it simpler just to keep a set of Matrix4 that are "synced" with GL (using Matrix4.Perspective, Matrix4.LookAt, etc..). Do people generally build up a "camera class" with a matrix and then shove it into GL? or do they simply compute a LookAt vector?

3) Does anyone have any code for extracting the view planes from a frustum matrix (ViewMatrix * PerspectiveMatrix) that uses Matrix4 classes? I've got code that does it from the base GL float arrays, but since I'm not sure if those match the Matrix4 format, I'm looking for options.

4) Are there plans for higher level math classes like this? such as planes, rays, frustums etc...? I am mostly just curious if this work might be useful to others, or just a stopgap for me until OpenTK supports it.

Thanks for any help/opinions anyone can offer:)


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
objarni's picture

Planes, rays etc. have been discussed at some point in time, I don't think there was any consensus.

Rant: Personally I'm alright with just vector and matrix, they will work for 100% of the applications out there. More high-level structures are bound to be more application-specific and trying to incorporate "fits-all-scenarios" structures/algorithms is not a path I'd like OpenTK to take (Microsoft has gone that road too many times, and their APIs tell us what happens then..). OpenTK is a low-level-wrapper. Not high-level "engine". For such things, look at the tons of such libraries out there..

JeffM2501's picture

I agree with you, keeping it low level keeps it simple, the vector and matrix items are enough for making dealing with the just the rendering solution easier, and keeping the higher level stuff in a separate lib makes sense, given how easy it is to pull them into a C# app.

Just so long as those libs can be written to work ;)

Inertia's picture

1) Take a look at

GL.MultTransposedMatrix( ref MyOpenTKMatrix.Row0.X );
GL.GetFloat( GetPName.TransposedModelViewMatrix, out MyOpenTKMatrix.Row0,X );

2) Depends whether you want to target OpenGL 3.1 and later, or earlier versions of OpenGL. (No matrix stacks in GL 3.1, so you must use OpenTK.Math)

3) I'm using column-major matrix layout to extract frustum planes, too. You can use Matrix4 as a container for column-major matrices as long as you never do any arithmetic with them - until you called Matrix4.Transpose() - it works fine.

4) IIRC the conclusion was that collision detection will quickly outgrow the "simple helper" scope and requires it's own library.

the Fiddler's picture

Also, take a look at the code in the OpenTK math branch. It contains code salvaged from Mono.XNA and rewritten to use OpenTK.Math> The API is XNA-compatible (BoundingBox/Frustum/Sphere, Ray, Plane etc etc), but far from complete. I don't know how much work this can shave, but it's worth looking into.

Have you considered open-sourcing your work, once you are happy with it? An XNA-inspired math library would be a great addition, since many samples rely on XNA out there.

JeffM2501's picture

Thanks for the tips, I will defiantly look at the math branch, as it looks similar to what I've done. Perhaps I can add more to it and use it for my stuff.

And yes I always open source what I can :) it's all in a very messy Google Gode project at the moment :) specifically.

JeffM2501's picture

Well got it to work finally :)

Ended up taking the files from that Math branch, and adding to them, then deriving off a "VisibleFrustum" class that also took care of the projection and modelview matrix logic for GL, then used it in a Camera class.

The octree is pretty basic, but it works so that makes me happy :)

Again thanks for all the help.

Jeffery Myers
A silly experiment in C#

objarni's picture