Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Dirk Gregorius

Casting / Interfaces

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Lets say I want to implement a skinning system. The system should be able to create a matrix palette with some simple function calls. Maybe I want to use this system later in an OpenGL application and even later with DirectX. When I use DirectX I will use the IMesh, ISkinInfo interfaces as well as the mathlib provided with the D3DX COMObject. With OpenGL I probably use my own classes. So internally I use my own math classes like Matrix4, Decomposer, Vector3, etc, but what is the best way to define the interface? Are there common techniques? What format should the matrix palette be? Maybe I decide that the client passes the palette to a function? Example: void UpdateScene(void) { pAnimator->AdvanceInTime(dt); pAnimator->GetMatrixPalette( /* What shall be here? */ ); } Lets say I pass a Matrix4 array reference. Then to use the palette with the ID3DXSkinInfo interface I have to cast the array to D3DXMATRIX. How expensive and dangerous is it to cast an array of 64 floats into an array of four matrices or a cast from Matrix4 array to D3DXMATRIX array? Are there any run-time penalties for such a cast? How do you handle type differences between subsystems? Maybe some of you gurus can give me some deeper inside into what happens behind the scene during a cast? Again references are very welcome! -Dirk

Share this post


Link to post
Share on other sites
Advertisement
What happens during a cast depends on the type of cast and implementation of the caster and castee. There are 3 types of casts in C++:

static_cast
reinterpret_cast
dynamic_cast

If you're using C, then you can probably assume the cast will work in a similar manner to a static_cast. If you're using C-style casts in C++, then my sorrow for you knows no bounds .

The difference between a static_cast and a reinterpret_cast is that the static_cast actually performs a conversion. The reinterpret_cast just makes the compiler look at the data in a different way (at the binary level, it remains the same).

For your type of casting, you would probably use a static_cast. Depending on the implementation of your types, a constructor or a conversion operator may be called. In this case, how expensive a conversion is depends on the implementation of the constructor or the casting operator.

But for converting an array of 64 floats to an array of 4 4x4 float matrices, there shouldn't be any real conversion happening and the cast will be free. As for the Matrix4 to D3DXMATRIX casts, I don't know enough about Direct3D to comment on that. Try it out with a profiler if you care, but I doubt that it will be very expensive at all.

Edit: Added comment about C since language wasn't specified.

[edited by - dcosborn on February 29, 2004 9:35:22 PM]

Share this post


Link to post
Share on other sites
You wrote:
>>The difference between a static_cast and a reinterpret_cast is >>that the static_cast actually performs a conversion. The >>reinterpret_cast just makes the compiler look at the data in a >>different way (at the binary level, it remains the same).

So if I use a static cast and there is no conversion defined (whether through a consructor or conversion operator) I will get a compile-time error, correct?

If I have two structures with the same binary representation I can use reinterpret_cast, so I can chose how I will "access" the struture, correct?

Thanx for your help!

-Dirk

Share this post


Link to post
Share on other sites
quote:
There's also const_cast.

Doh you're right. I had a nagging feeling that I was forgetting one, but I waved it away.

quote:
So if I use a static cast and there is no conversion defined (whether through a consructor or conversion operator) I will get a compile-time error, correct?

You should. Try it and see.

quote:
If I have two structures with the same binary representation I can use reinterpret_cast, so I can chose how I will "access" the struture, correct?

Yes, depending on what you mean by "access".

[edited by - dcosborn on March 1, 2004 7:32:45 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!