Computer Graphics Math Macros C/C++

This topic is 4488 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, I am looking to condense some of my code. I am interested in computer graphics math macros written in C or C++. Does anyone have any they would be willing to share? For example, dot product, cross product, matrix multiplication, etc.. Thanks.

Share on other sites
I would recommend you make these formal operators or methods that act on vector and matrix objects, but if you're in the black market for some dirty unsafe macros, I might have a few lying around:

#define VectorDot(a,b,c) (c = a[0]*b[0] + a[1]*b[1] + a[2]*b[2];) #define VectorCross(a,b,c) (c[0] = a[1]*b[2]-a[2]*b[1]; c[1] = a[2]*b[0]-a[0]*b[2]; c[2] = a[0]*b[1]-a[1]*b[0];) #define VectorNormalize(a,c) (c[0] = 1.0f / sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]); c[1] = a[1] * c[0]; c[2] = a[2] * c[0]; c[0] *= a[0];)

This assumes you're using some form of array vector, or God forbid you're using these macros on objects that overload those operators. While I don't condone their usage, here are a couple that I had lying around.

Share on other sites
ooh, thanks. Just what I was looking for.

If you have the time, could you explain to me the advantage of making these macros formal operators? Why would this be safer? Is there a performance penalty or advantage?

Thanks.

Share on other sites
Quote:
 Original post by jdaniel...could you explain to me the advantage of making these macros formal operators? Why would this be safer? Is there a performance penalty or advantage?
Some of those questions are addressed here.

Share on other sites
Quote:
 Original post by jdanielooh, thanks. Just what I was looking for.If you have the time, could you explain to me the advantage of making these macros formal operators? Why would this be safer? Is there a performance penalty or advantage?Thanks.

It would be safer because macro's are just copy/paste. Everything can go wrong there. If you give it unexpected input, you *might* get compile errors, or it might compile, and just return the wrong output. It doesn't enforce that the arguments should be vectors. You could probably give it a semicolon among the arguments, and it'd insert that, which'd cause some very interesting results... [wink]
But it might still compile!

If you do the same as a function (or operator), you would have type checking. You would only be able to pass vectors as input, anything else would be rejected.

A function call would be a few cycles slower, but unless you call this function at least 100,000 times per second, it wouldn't make a noticeable difference. Or you could inline the function, and it would be exactly as fast as the macro, while still giving you type safety, and be able to work with your compilers intellisense/autocompletion/whatever else.

Share on other sites
IIRC, in the the quake1 or quake2 engine source you will find a lot of them. For a more decent math library I recommend looking at the doom 3 code...

1. 1
Rutin
25
2. 2
3. 3
JoeJ
19
4. 4
5. 5

• 10
• 11
• 9
• 9
• 10
• Forum Statistics

• Total Topics
631753
• Total Posts
3002098
×