Quote:

can anyone tell me when it's wise to use float3 and when float4? .... why a 4-dimensional vector for space?




Position is calculated in four dimensions because some technical reasons...essentially, the 3d position vector has to be transformed to homogenous perspective space, and a fourth component, w, is needed..

from Dr. Math:
"For technical reasons, you need a fourth coordinate to do this
efficiently, but the idea is this: The 3D point you would call
(x, y, z) is represented by any 4D "homogeneous" vector like this:
(ax, ay, az, a), where a is non-zero. We usually pick a to be
1, so it is represented by (x, y, z, 1). But if after multiplying
your input 4D vector by the matrix you get something like this:
(2, 11, 18, 2), the a is 2, so this corresponds to (2/2, 11/2, 18/2)
in 3 dimensions."

In any case, normally you dont need to worry about it, just output a float4 vector for you final transform ... you just cast the positon input as a 4d vector and do all the mul using that, and there you go.. mul(inpos,matWorldViewproj); where inpos is a 4d vector..

As far as other uses of vectors, always be aware of what components you really need.. if all you care about in a vector is the xyz or rgb values, you should use a float3, unless you need to use these vectors to operate on a different float4.

For instance, this operation is illegal: c1.xyzw*=c2.xyz; ...where c1 is a float4 and c2 is float3)

to properly do this operation do this instead:
c1.xyz*=c2.xyz; or you can do it in a dumber way like this
c1.xyzw*=float4(c2.xyz,1); ..we just recast the float3 c2 into a float 4, adding a one to the w component, meaning it will unchanged by the multiplication.. if you wanted an addition, just 0 instead of 1...

There is an exception to this rule however, you can always multiply a vector by a scalar.. therefore: c1.xyzw*=2.0f; ...is legal..this just mulitplies each component of the vector by the scalar value...

Interestingly, in shaders, scalars can be subsituted for vectors in any case.. such that: float value=c1.rgb; is legal.. this is the easy way to get a monotone value from a color vector.. which is very useful in many shaders.

The basic rule is to be aware of what the numbers your vectors represent actually mean, and use the appropriate dimensions.


Sphere Engine--the premier A6 graphics plugin.