views:

188

answers:

2

i found a library called pyeuclid and it seems to do what i want in respect to 3D math. it contins a 3D vector class and a 4X4 matrix class capable of transformations like rotate,translate and scale.

matrix creation is simple, simply pass along the arguments and the matrix is created.

>>> m = Matrix4()
>>> m.translate(50,50,50)
Matrix4([    1.00     0.00     0.00    50.00
             0.00     1.00     0.00    50.00
             0.00     0.00     1.00    50.00
             0.00     0.00     0.00     1.00])

the lbrary comes with a 3D vector class, if i wanted to translate a 3D point with this matrix i would need to first create the vector class, like this.

>>> v = Vector3(100,200,300)
>>> v
Vector3(100.00, 200.00, 300.00)
>>> v.x
100
>>> v.y
200
>>> v.z
300

Now the Matrix4() class comes with a method to transform coordinates, and its used like so.

>>> m.transform(v)
Point3(150.00, 250.00, 350.00)

this is great, however, there is no perspective projection applied, the library comes with a method called Matrix4.new_perspective(fov_y, aspect, near, far). Thats where the problem lies, i have no idea how to use this function properly, (m.transform(vector3) doesent produce anything usable )the documentation doesent show how its used with vectors or other matrices, it says something about being 'equivalent to the OpenGL call gluPerspective.fov_y', but ive never used opengl so that wont help.

this is the module (from euclid import Vector3 , Matrix4)

what i dont want, is recomendtions for some game engine/visulization library, or something thats bent on drawing something to the screen, i just want to know if i can use this library to to turn 3D vector coordnates into 2D screen coordinates, and if not what library can i use???

so get in there and help out noob in distress! :-) thank you for your help.

A: 

This tutorial explains the arguments to gluPerspective(), and should transfer over since your library is written with that as a model.

I would expect the new_perspective() method to work like a constructor, i.e. it returns a Matrix set up as a perspective transformation. You should then be able to transform world-space coordinates into perspective-projected coordinates by multiplying vectors with that matrix.

unwind
A: 

I don't know if this is relevant as you may already know this but you'll need a view matrix too. The view matrix represents the inverse of the transform of your camera in the world. You may find the matrix class also has helper methods for creating view matrices, sometimes called lookAt os similar. Alternatively just create a matrix manually that positions and orientates your (imaginary) camera and then invert it.

If you apply the projection matrix to world co-ords you're making the implicit assumption that your view matrix is the identity and so your camera is at the origin of the world and pointing down an axis. Which axis that is will depend on the projection matrix but if it's like most other systems it'll be the z-axis since convention has camera depth measured along z. It could be in either the positive or negative direction so again check the documentation for details.

So, to summarise, you need to multiply your model matrix with the view matrix and then by the projection matrix. The projection matrix is designed to work in camera coordinates rather than world coordinates.

Troubadour
i used this code instead focal_length / (focal_length + z), its 100% perfect, no need for the expensive matrix computation you've expressed, works a charm. thanks for your efforts anyway. :)
spearfire
I have no idea what you're talking about... but good to know you've got it sorted anyway :)
Troubadour
BTW, as regards the expensive matrix computation it was only a pointer to get you started with view matrices. You don't do it that way in practice.
Troubadour