View Full Version : D3DXVec3TransformCoordArray
Hi,
I am currently in the process of porting a DirectX application to a platform independent engine.
My biggest time-eater are the D3DX Math functions. Does anybody have a math library that follows the D3DX math library in source?
Currently I am stuck with
D3DXVec3TransformCoordArray()
Any help would be appreciated,
- Wolf
Reedbeta
10-10-2005, 09:34 PM
It just takes two arrays of vectors, and transforms each one in the input by a given matrix and stores it in the output.
The only caveats are that the input and output strides may be specified (so when incrementing your pointers, you will need to typecast to unsigned char*, add the stride, and then typecast back to D3DXVECTOR3*); and the function is supposed to project back to w = 1, so divide each vector by its w component after transforming.
Find below my current version that should do it.
//
// Transforms a 3D vector by a given matrix, projecting the result back into w = 1.
//
// similar to D3DXVec3TransformCoord()
//
Vector3 * Vec3TransformCoord(
Vector3 * pOut,
const Vector3 * pV,
const Matrix44 * pM)
{
float rhw, x, y, z;
x = pV->x; y = pV->y; z = pV->z;
rhw = (x * pM->a.w + y * pM->b.w + z * pM->c.w + pM->d.w);
if (fabsf(rhw) < EPS5) return NULL;
rhw = 1.0f / rhw;
pOut->x = rhw * (x * pM->a.x + y * pM->b.x + z * pM->c.x + pM->d.x);
pOut->y = rhw * (x * pM->a.y + y * pM->b.y + z * pM->c.y + pM->d.y);
pOut->z = rhw * (x * pM->a.z + y * pM->b.z + z * pM->c.z + pM->d.z);
return pOut;
}
//
// Transforms an array (x, y, z, 1) by a given matrix, and projects the result back into w = 1.
//
// similar to D3DXVec3TransformCoordArrays()
//
Vector3 * Vec3TransformCoordArray(
Vector3 * pOut,
u32 OutStride,
const Vector3 * pV,
u32 VStride,
const Matrix44 * pM,
u32 n)
{
Vector3 * pOutput;
Vector3 * pVector;
pOutput = pOut;
pVector = pV;
for(u32 i = 0; i < n; i++)
{
Vec3TransformCoord(pOutput, pVector, pM);
pVector+=VStride;
pOutput+=OutStride;
}
return pOut;
}
uhmm ... actually I meant
Vector3 * Vec3TransformCoordArray(
Vector3 * pOut,
u32 OutStride, // [in] Stride between vectors in the output data stream.
const Vector3 * pV,
u32 VStride, // [in] Stride between vectors in the input data stream.
const Matrix44 * pM,
u32 n)
{
u8 * pOutput;
const u8 * pVector;
pOutput = (u8*)pOut;
pVector = (u8*)pV;
for(u32 i = 0; i < n; i++)
{
Vec3TransformCoord(pOut, pV, pM);
pVector+=VStride;
pOutput+=OutStride;
pOut = (Vector3*)pOutput;
pV = (Vector3*)pVector;
}
return pOut;
}
:-) Wolf
goyal_nitu22
09-03-2007, 03:13 AM
Hi Wolf,
I want to know what r these EPS5, u8,u32....
Plz reply as soon as possible.
I want to know what r these EPS5, u8,u32....
EPS5 is an 'epsilon' value; an extremely small value to compare to if you want to know whether a floating-point value should be treated as zero. In this case it's used to avoid division by zero. I'm not sure if Vec3TransformCoord should return NULL in this situation though. u8 is clearly an 8-bit unsigned integer (unsigned char) and u32 an unsigned 32-bit integer (unsigned int).
Plz reply as soon as possible.
Like in two years? (check the dates)
Seriously now, it is sometimes considered a bit rude to ask for an urgent reply. There are lots of professionals here who visit the forum in their free time and have no obligation to share their knowledge. So show a little patience. Else if there's a particular reason why it's urgent and we should care more about it than our own busy agenda's, please give us an incentive.
goyal_nitu22
09-18-2007, 12:52 AM
Thanks a lot Nick it helped me a lot.
goyal_nitu22
09-26-2007, 05:05 AM
Hi Nic
Can this code be available in assembly language?If yes plz post it.I have to reduce the time taken by it.It takes somewhat less time than DirectX fn(D3DXVec3TransformCoordArray) but the difference in their time is negligible.
So i want assembly code.I have written it in assembly code but this assembly code takes more time than the c++ code.Plz help me.
njdewit
09-26-2007, 10:05 AM
Try and optimize it using SIMD and take a look at the cache-friendliness of what you're doing. But perhaps it's wise to stick to C++, given the circumstances. And what's the reason you're performing so many transformations (otherwise the speed of this particular function would not be an issue)? Nothing to do about that?
That aside, you're being rather rude. You can ask for assistance but nobody's here to do your job.
Kenneth Gorking
09-26-2007, 12:22 PM
I know of optimized code that can process each vector in ~17 cycles, but I am not gonna post post it because it is freely available on the internet. A simple search is all it takes.
Also, as you have proven yourself, translating a function to assembly does not necessarily make it faster, because you have consider memory alignment access, instruction pairing, cache access and so on. Is this function even a bottleneck? Couldn't you do some optimization at a higher level?
vBulletin, Copyright ©2000-2009, Jelsoft Enterprises Ltd.