[PyOpenGL-Users] FW: best way to set up matrices and vertex arrays?
Brought to you by:
mcfletch
|
From: Kolja K. <kol...@av...> - 2010-03-11 11:19:51
|
Hmm, my post bounced. Let me try again... sorry, if this causes a
duplicate!
Dan, Ian, Greg,
Thanks for your answers. I still have to look into the best way to do my
resizable vertex array, but other than that I am probably going to
follow Greg's advice and go with the transposed matrices, as well as
using homogeneous coordinates throughout...
I guess I wasn't too clear on the column-major vs. row-major topic. To
elaborate, if I do (in C++)
glLoadIdentity();
glTranslatef(1.0f, 2.0f, 3.0f);
float elems[16];
glGetFloatv(GL_MODELVIEW_MATRIX, elems);
The returned array reads [1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0
0.0 1.0 2.0 3.0 1.0]. Following the Red Book, this represents a matrix
in column-major layout, i.e.
1.0 0.0 0.0 1.0
0.0 1.0 0.0 2.0
0.0 0.0 1.0 3.0
0.0 0.0 0.0 1.0
Thus, vectors are transformed by multiplying from the right: v' = A*v.
This would also be how transformations in a GLSL shader are written
down.
In PyOpenGL, doing the same thing, the resulting matrix is transposed:
elems = glGetFloatv(GL_MODELVIEW_MATRIX)
results in:
[[ 1. 0. 0. 0.]
[ 0. 1. 0. 0.]
[ 0. 0. 1. 0.]
[ 1. 2. 3. 1.]]
So, I have to use a row vector and multiply from the left. Likewise for
matrix-matrix multiplications. As a PyOpenGL newbie, this is quite
surprising to me. Seems like the python code is cleanest when I accept
this behavior - just have to be extra careful when exchanging code with
other projects using the other convention.
Actually, having mentioned shaders - when I pass a matrix to a shader,
am I guessing right it will be the transpose of the python matrix?
-Kolja
-----Original Message-----
From: Dan Helfman [mailto:Dan...@no...]
Sent: Tuesday, March 09, 2010 7:14 PM
To: Kolja Kaehler
Cc: pyo...@li...
Subject: Re: [PyOpenGL-Users] best way to set up matrices and vertex
arrays?
Kolja Kaehler wrote:
> Does anyone here have some advice for me on these matters? Maybe I am
> just missing "the obvious way" to organize these things.
Hi Kolja,
I haven't had any problems using NumPy's row-major arrays with PyOpenGL.
For instance if you have a row-major NumPy array of dtype [ ( "x",
np.float32 ), ( "y", np.float32 ) ], you can pass the array directly to
PyOpenGL by putting it in a vertex buffer object and then using
glVertexPointer( 2, gl.GL_FLOAT, 0, None ) to tell GL to expect 2 floats
for each vertex. The same principle works for 3D as well.
And if you'd like to store several pieces of NumPy array data together,
then you can just use a recarray, and then pass an individual "member"
array to PyOpenGL at any point you want.
Perhaps you can give an example of what you're trying to do where the
row-major/column-major distinction gets in the way?
There really isn't a good way to dynamically grow a vertex array with
NumPy. Some options are:
* Use numpy.resize(), which just returns a new, larger array copy.
* Use numpy.ndarray.resize(), which can only resize if there are no
other references to the array.
* Accumulate and grow with a data structure other than a NumPy array,
and then convert to a NumPy array when you're done growing. Some
candidates are a Python list (slow), or if you're willing to use Cython
or C, then accumulate in a C array with realloc() for growing.
I haven't really used NumPy matrices, so I'll let someone else comment
on that one.
Dan
|