Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

If I add a math library (for example containing a Matrix class) and use it in my program drawing with OpenGL, will my be work slower than if I used standard OpenGL functions for matrix calculations? Does the same hold true for DirectX?

share|improve this question
1  
Related question, with interesting answers: stackoverflow.com/questions/2713417/… –  Dan Oct 27 '11 at 10:43
add comment

2 Answers

up vote 5 down vote accepted

Matrix manipulations are always done on the CPU. It doesn't really matter though, the total amount of floating point operations you'll do involving matrices will be less than 1000 and any modern CPU will deal with that in a few nanoseconds. Using an external library which is probably optimized is absolutely worth it, if only for the readability improvements. Not to mention that matrix manipulations in OpenGL are deprecated so you're obligated to write your own.

I can't give you any real data, but I profiled a program I wrote some time ago. It used a matrix library I wrote myself, without any optimizations. The matrix calculations didn't even show up in the profile.

So, don't worry about it unless you're absolutely sure it's a problem.

share|improve this answer
3  
+1 and just to add - the final multiplication of position by MVP is what's carried out on the GPU. –  Jimmy Shelter Oct 27 '11 at 11:00
3  
...the final multiplication of position by MVP... ... and of tangents and bitangents... and skeletal animations and ... Well, all matrix multiplications DOES NOT occur on the CPU. –  Valmond Oct 27 '11 at 11:23
    
If he's talking about OpenGL then he can only transform his geometry on the GPU and the question is moot anyway. –  kmm Oct 27 '11 at 14:41
    
'the total amount of floating point operations you'll do involving matrices will be less than 1000' - that's sadly not true in most complex games. You might do more than that per character. –  Kylotan Oct 27 '11 at 17:04
    
It is worth noting that many GL fixed function matrix implementations shortcut multiply operations when possible. i.e. glIdentity doesn't necessarily set any values, it can flip a bit to flag itself as identity and the next matrix operation will replace the matrix instead of multiplying. I've studied the mesa source code and saw all sorts of shortcuts. Using someone else's optimized library is usually better than writing your own. –  5ound Oct 28 '11 at 6:49
show 2 more comments

To answer the first question: on both

Usually not at all much on the CPU and then at least once per vertex (usually more) on the GPU.

I'd not care about speed except if you do heavy work with matrices in the (c++) code. If you don't (you just calculate worldView projection and so on, say some handfuls for each model at most) just use what you feel best at ease with.

The day you start to use a Lot of matrix multiplications, that day you'd know yourself what you need to do to speed up code (and if you need to speed up the "code" or the shader or maybe other things like profiling bandwith). If that day you don't know, then you are probably doing something wrong.

What about DirectX?

Well, if you want to be compatible with both then go for a library (I use Irrlicht which is a whole graphic engine) so you can switch easily (beware though, OpenGL is righthanded and DirectX is lefthanded, shaders usually don't accept a simple switch between those if you use "complex" calculations like normal maps).

share|improve this answer
2  
Actually neither are either-handed these days. D3D has RH versions of it's projection matrix setup functions (and has done since at least D3D8) whereas in OpenGL you can glLoadMatrix (or glUniform) any handedness you want. The importance of LH vs RH so far as API differences are concerned is hugely overrated. –  Jimmy Shelter Oct 27 '11 at 12:26
    
You say that again when you have exported stuff from blender and try to use it in a DX environment ;-) I did learn something though +1 –  Valmond Oct 27 '11 at 12:36
    
Been there, done that, it works. D3D9 renderer using the -RH projection matrix functions. The important thing is that you're consistent; if you try to mix data formats that assume LH with data formats that assume RH you'll get trouble; if all your data formats assume the same handedness then it's totally unimportant. –  Jimmy Shelter Oct 27 '11 at 20:26
    
You're right of course but when I have been there it was always with cheap bought models and so on which might always be some sort of inferno ^^ –  Valmond Oct 28 '11 at 7:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.