Jump to content

  • Log In with Google      Sign In   
  • Create Account


JohnBolton

Member Since 03 Apr 2002
Offline Last Active Jul 06 2009 05:19 AM

#3995872 Double dispatch in C++

Posted by JohnBolton on 28 June 2007 - 10:49 AM

This is how you might do double dispatch in C++.
    class A;
class B;

class Collidable
{
public:
virtual void Collide( Collidable * x ) = 0;
virtual void Collide( A * x ) = 0;
virtual void Collide( B * x ) = 0;
};

class A : public Collidable
{
public:
virtual void Collide( Collidable * x )
{
x->Collide( this );
}
virtual void Collide( A * a )
{
CollideAA( this, a ); // A-A
}
virtual void Collide( B * b )
{
CollideAB( this, b ); // A-B
}
};

class B : public Collidable
{
public:
virtual void Collide( Collidable * x )
{
x->Collide( this );
}
virtual void Collide( B * b )
{
CollideBB( this, b ); // B-B
}
virtual void Collide( A * a )
{
CollideAB( a, this ); // A-B
}
};

void CollideAA( A * x, A * y )
{
// Do A-A collision
}

void CollideBB( B * x, B * y )
{
// Do B-B collision
}

void CollideAB( A * x, B * y )
{
// Do A-B collision
}

// Main collision function
void Collide( Collidable * x, Collidable * y )
{
x->Collide( y );
}
Obviously, it could get very unwieldy with a lot of types and that could be a problem. The solution is to use a map and RTTI to determine the proper collision function to call. The disadvantage is that it is slower.

[Edited by - JohnBolton on June 28, 2007 5:49:55 PM]


#3281440 Maximum vertex buffer size

Posted by JohnBolton on 30 September 2005 - 01:30 PM

There are maximums on primitives and vertexes.

MaxPrimitiveCount - "An Int32 value that indicates the maximum number of primitives for each Device.DrawPrimitives call."
MaxVertexIndex - "Maximum size of indices supported for hardware vertex processing"



#3241690 Prefix Class Names with 'C' Good Practice?

Posted by JohnBolton on 03 September 2005 - 07:29 AM

Putting a 'C' on the front of every class name is about as useful as putting a 'V' on the front of every variable name and an 'F' on the front of every function name.


#2817579 Matrix in d3d and Opengl?

Posted by JohnBolton on 14 December 2004 - 04:16 PM

There is some misleading (and wrong) information in these replies.

First of all, there is no reason why you can't use the D3DX matrix and matrix functions for OpenGL (though there isn't really a good reason to do it).

Matrices in D3D and OpenGL both functionally have the same layout in memory: Xx,Xy,Xz,Xw,Yx,Yy,Yz,Yw,Zx,Zy,Zz,Zw,Tx,Ty,Tz,Tw. X, Y, Z, and T are the basis vectors.

Since there is no difference between D3DX and OpenGL matrices in memory, if you have to use D3DXMatrixTranspose, you are probably doing something wrong.

OpenGL is right-handed and D3D is left-handed, but that is totally irrelevant to matrix operations. Matrix operations give the same result for right-handed and left-handed systems.

What is important is matching the order of operation in OpenGL to the order of the parameters in D3DX. In OpenGL, matrix operations are pre-concatenated. That is, operation M1 followed by M2 results in this: v' = M1M2v, which is D3DXMatrixMultiply( &M, &M2, &M1 ) in D3DX.

Now I will show you an example using your code sample.

TP = transformation matrix equivalent of glTranslatef(position[0],position[1],position[2])
TC = transformation matrix equivalent of glTranslatef(centerd[0],centerd[1],centerd[2])
RA = transformation matrix equivalent of glRotatef(rotateAngle,axis[0],axis[1],axis[2])
RS = transformation matrix equivalent of glRotatef(ScaleRotateAngle,Saxis[0],Saxis[1],Saxis[2])
S = transformation matrix equivalent of glScalef(scalef[0],scalef[1],scalef[2])
RS-1 = transformation matrix equivalent of glRotatef(0-ScaleRotateAngle,Saxis[0],Saxis[1],Saxis[2])
TC-1 = transformation matrix equivalent of glTranslatef(0-centerd[0],0-centerd[1],0-centerd[2])

If you expressed that code in column-major notation (used by OpenGL), you get this:

v' = TPTCRARSSRS-1TC-1v

The row-major (used by D3D) equivalent is this:

v' = vTC-1RS-1SRSRATCTP

The D3DX code equivalent is this:
D3DXTranslation( &TCI, -centerd );
D3DXRotationAxis( &RSI, Saxis, -ScaleRotateAngle );
D3DXScaling( &S, scalef );
D3DXRotationAxis( &RS, Saxis, ScaleRotateAngle );
D3DXRotationAxis( &RA, axis, rotateAngle );
D3DXTranslation( &TC, centerd );
D3DXTranslation( &TP, position );

D3DXMatrixIdentity( &M );
D3DXMatrixMultiply( &M, &TP, &M );
D3DXMatrixMultiply( &M, &TC, &M );
D3DXMatrixMultiply( &M, &RA, &M );
D3DXMatrixMultiply( &M, &RS, &M );
D3DXMatrixMultiply( &M, &S, &M );
D3DXMatrixMultiply( &M, &RSI, &M );
D3DXMatrixMultiply( &M, &TCI, &M );
glLoadMatrixf( &M );
... or ...
D3DXMatrixIdentity( &M );
D3DXMatrixMultiply( &M, &M, &TCI );
D3DXMatrixMultiply( &M, &M, &RSI );
D3DXMatrixMultiply( &M, &M, &S );
D3DXMatrixMultiply( &M, &M, &RS );
D3DXMatrixMultiply( &M, &M, &RA );
D3DXMatrixMultiply( &M, &M, &TC );
D3DXMatrixMultiply( &M, &M, &TP );
glLoadMatrixf( &M );


Notice how the order of the first D3DX implementation is exactly the same as in the OpenGL code. Notice how the difference in order between the two D3DX implementations simply depends on the order of the parameters. Notice how the values for tranlation, rotation and scale are the same.

[Edited by - JohnBolton on December 14, 2004 10:16:28 PM]


PARTNERS