Sign in to follow this  

Draw bounding volumes fast for debugging

This topic is 4091 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

I'm in the process of learning C# and Managed DirectX so this is a stupid question :p I can't figure out how to draw bounding volumes for axis aligned boxes! It should be simple but it's not for me! I tried DrawUserPrimitives but it resultet in crashed, 0 fps and odd behavior. Right now I'm trying with Mesh.Box but that cause very low fps and I see no boxes :( So how do I render a simple wireframe box? Also another question. How do I copy objects so they stop referencing to each other? MyObj foo = new MyObj; foo.value = "cat" MyObj bar = foo; bar value = "dog"; This would cause both to be "dog" right? How do I make a new instance (clone?) of the object?

Share this post


Link to post
Share on other sites
As far as I know, you have to create new geometry to render bounding boxes. It's not difficult though - you only need six quads with various permutations of the bounding coordinates as their vertices.
If you fancied being clever, you could simply create a single unit-cube and modify the transformation stack to fit it to coordinates specified at run-time. This way, with a little encapsulation, there's no need for duplicate geometry and you'll have a 'one size fits all' bounding-box drawing routine.

To address the second issue, that code you quoted does create a new instance of the object.
The first two lines create and alter a MyObj, called foo. The third line creates another MyObj (in its own space) called bar, and calls the assignment operator to copy foo over to bar. The fourth line modifies bar: foo remains unchanged.

However, I suspect you are seeing contradictory behaviour because of a shallow duplication: If you have a class that contains any pointers and you need to copy it to another instance (as is the case here, if 'value' is a char*) you'll need to overload the assignment operator, at a minimum. You should also specify a copy constructor of your own.
The default assignment operator (provided by the compiler) will perform a member-wise copy, meaning that every member of foo gets copied directly across to bar. This is fine until you realise that MyObj has a pointer, in which case you'll have two objects pointing to the same data. Now when one object modifies the object it is pointing to, the other member will suffer the consequences. This is probably what is currently happening.

One way to solve this, with minimal work would be to use a std::string instead of a char*. This way, when MyObj is duplicated, the std::string copy-constructor will be called, which will allocate more space and duplicate the string's data.

If you insist on using char pointers, you'll need to overload the copy constructor/assignment operator (ideally both, with the constructor calling the operator) to allocate new space (if necessary) and perform a string copy on the contents. Then you'll have one instance of the string in memory for each instance of the class.

Regards
Admiral

Share this post


Link to post
Share on other sites
Thanks for the idea with the unit sized cube! :)

But the thing about how the objects worked confused me a lot. You mixed in pointers, chars and std::string! That sounds like C++ to me hehe. Afaik there are no pointers in C#.

Share this post


Link to post
Share on other sites
Quote:
Original post by DanielH
That sounds like C++ to me hehe. Afaik there are no pointers in C#.

Oops. I guess I should have paid more attention [rolleyes]. I can't answer your question, in that case.
But I'm glad you liked the deformed cube method. Just holler if you have trouble getting it to work out.

Regards
Admiral

Share this post


Link to post
Share on other sites

This topic is 4091 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this