D3DXVec3TransformCoordArray

63be0aec80b9d71a09bf383b896e5a0c
0
wolf 101 Oct 11, 2005 at 02:33

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

9 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 168 Oct 11, 2005 at 03:34

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.

63be0aec80b9d71a09bf383b896e5a0c
0
wolf 101 Oct 11, 2005 at 18:52

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;
}
63be0aec80b9d71a09bf383b896e5a0c
0
wolf 101 Oct 13, 2005 at 02:54

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

0b1e41994f7d69f85f34d79971230262
0
goyal_nitu22 101 Sep 03, 2007 at 09:13

Hi Wolf,
I want to know what r these EPS5, u8,u32….
Plz reply as soon as possible.

99f6aeec9715bb034bba93ba2a7eb360
0
Nick 102 Sep 03, 2007 at 12:33

@goyal_nitu22

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.

0b1e41994f7d69f85f34d79971230262
0
goyal_nitu22 101 Sep 18, 2007 at 06:52

Thanks a lot Nick it helped me a lot.

0b1e41994f7d69f85f34d79971230262
0
goyal_nitu22 101 Sep 26, 2007 at 11:05

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.

6d6e06654a85934959c1a98813731360
0
njdewit 101 Sep 26, 2007 at 16:05

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.

46407cc1bdfbd2db4f6e8876d74f990a
0
Kenneth_Gorking 101 Sep 26, 2007 at 18:22

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?