PDA

View Full Version : multiply 2 matrices: whats faster: "manually" or via OpenGL?



12-09-2001, 04:46 AM
This is not a math question but a performance question:

I have a transformation matrix M.

I have a static number of transformation matrices - say N1, N2, ..Nn. These remain static, too. They're defined once.

Based on specific info I need to multiply M with Nx. And: I need the resulting matrix (R) for further "manual" number crunching. I've implemented this in two ways - my question to you is, which one is faster/better?

way 1 - do it via OpenGL:

for example N1 is defined/initialized as a DisplayList (some rotatef, etc.).

glLoadMatrix(M);
// select theDisplayList based on some
// specific data, then call this list
glCallList(theDisplayList);

glGetDoublev(GL_MODELVIEW_MATRIX, R);

No I have R and can play with it.


OR


N1 is defined in my own MatrixTypeDef, I do everything "internally", I multiply M with N1 by my own MatrixMultiplicationFunction, I get R and the only thing OpenGL has to say is:

glLoadMatrix(R)


I think the 2nd one is cleaner: more Model-View-Controller like. But the 2nd one could be faster, eventually. Is this correct? Is it relevant? Is it worth it, if the application is not meant to be highly portable?

12-09-2001, 04:49 AM
!!! In the last paragraph - the FIRST way is meant to be faster

(I'm registering right now, so I can edit my future posts, sorry)

Rob The Bloke
12-09-2001, 05:25 AM
glGet is very slow, the second should be faster.

Bob
12-09-2001, 05:26 AM
There are four reasons why you shouldn't use OpenGL as a math library.

1: It isn't a math library.
2: It involves a glGet* command, which might stall the rendering pipeline, and might cause a performance hit.
3: The code becomes less easy to read, follow and uderstand. Even though you know what it does, someone else might not. It's just a "dirty coding style".
4: You can do much better yourself, with your own code. That is, the total time needed to obtain the resulting matrix.

Even though OpenGL might perform the actual matrix multiplication faster than you can, it involves much more than just multiplying two matrices. If you do it on your own, you don't have to stall the rendering pipeline either.

dorbie
12-09-2001, 06:19 AM
I agree with the above, it is worth doing this in OpenGL only if you intend to use the matrix to transform vertices in hardware and don't need to read it back, otherwise use your own math. Using glGet for state that's deep in the pipeline is a bad thing.