Skip to main content
gamedev.net
🔒 Locked

Code Organization

Started by dcox Mar 30, 2011 at 3:59 PM 1 replies 1.1k views
Original Post
dcox
dcox
I am new to DirectX and have been reading quite a few books and online tutorials. Most start by describing how to create a simple, one window application to render a simple object such as a box. Generally, the code is contained in a single file and often in a single C++ class. For more complex software with lots of windows and objects, I was looking for some guidance on how to organize one's C++ code.

For example, consider input layouts. In the tutorials there is usually only one (or maybe two) globally defined input layout to describe corresponding vertex layouts. Would it be more common for vertex layouts to vary by object? A simple box might use one vertex layout while a more complex object might be rendered using several vertex layouts. I suppose the same could be said for the type of topology being employed as well as lighting or other elements.

If a C++ class is used to represent a type of object, would it be good practice to include vertex layouts, topology, etc. with the object? Or is it the case that these elements usually are set during application initialization and don't vary from object to object? I'm not looking for a hard rule to follow. I was just curious to learn what others do when writing software that is more complex than introductory tutorials.
Buckeye
Buckeye
It is fairly common to have just a few* vertex structures (is that what you mean by vertex "layouts"?). It is pretty uncommon to have more than one for a single object, as you would normally want to render the entire object with a single draw call. Different vertex structures for different parts of the object would require multiple draw calls.

With regard to where you define the vertex structures (the expected answer): it depends. It's not uncommon to define all the needed vertex structures in one place, particularly if you're going to use vertex declarations, rather than FVF codes, and there are several objects which will use the same vertex declaration. I've also seen objects defined in a class, defining it's own vertex structure.

*Consider: all your objects will have a vertex position. In addition to position, many of them will have, at least, normals and a diffuse color. Many will also have texture coordinates. So, just a few vertex structures will serve for most objects.
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.
dcox
dcox
Great! Thanks for these insights. They make tremendous sense.


It is fairly common to have just a few* vertex structures (is that what you mean by vertex "layouts"?). It is pretty uncommon to have more than one for a single object, as you would normally want to render the entire object with a single draw call. Different vertex structures for different parts of the object would require multiple draw calls.

With regard to where you define the vertex structures (the expected answer): it depends. It's not uncommon to define all the needed vertex structures in one place, particularly if you're going to use vertex declarations, rather than FVF codes, and there are several objects which will use the same vertex declaration. I've also seen objects defined in a class, defining it's own vertex structure.

*Consider: all your objects will have a vertex position. In addition to position, many of them will have, at least, normals and a diffuse color. Many will also have texture coordinates. So, just a few vertex structures will serve for most objects.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.