Well, here I am again writing my next part of the Dwarf Vs Zombie game. Feels like months since I’ve actually been able to get to sit down and do some more work on this.
This one’s not going to be a big update, even though it took more work than Part 1, there isn’t really much to say.
Articles
Part 1 - Stuff to download, getting into the development
Part 2 - Setting the Scene, and Model Drawing
Downloads
Source: Click Here
Setting the Scene
Before you look too far, you will notice that I wrote about a Scene Graph back in Building a Solar System in XNA (Part 2). It’s quite a simple system to implement, and is powerful enough to handle whatever I throw at it.
I’ve been at it again, this time writing one in C++ to handle the 3d environment we are working with. Here’s a little code that shows you how we can move recursively through the graph to draw the entire tree from bottom to top, without needing to have any additional code in the ISceneNode interface.
void SceneGraph::Render(){
Render(m_BaseNode);
}
void SceneGraph::Render(ISceneNode *node){
SceneNodeIterator it;
node->Render();
it = node->begin();
while(it != node->end()){
Render(*it);
it++;
}
}
You might have recognised the sneaky use of STL to implement the node list.. why re-invent the list, when you can simply inherit it :). Here’s the code for our SceneNode interface. Please DO remember the virtual destructor, unless you would prefer some wild and zany adventures with memory leaks later.
class ISceneNode : public std::list<ISceneNode *>{
public:
virtual ~ISceneNode();
virtual void Render() = 0;
virtual void Update(float fStep) = 0;
};
typedef std::list<ISceneNode *>::iterator SceneNodeIterator;
Playing with Models (again)
So, what good is a Scene Graph without stuff to fill it?.. The next step is to get our models that we downloaded a couple of months ago showing up in the scene. To do this, I’ve written the ModelNode class that is responsible for the drawing of the models, and an MS3DLoader class that can read the data from a ‘.ms3d’ file.
I’ve basically inferred the MS3D file format from the MilkShape 3D Binary Model Viewer source code that you can download from the MilkShape site, and ignored the parts that I don’t intend to use. So, if you are after an MS3D loader, I’d suggest that you go there to find a full featured one.
Once the data is loaded, we can then use our ModelNode to render the models into our scene.
First, we will position and scale our models, so that they are about the right size and place. It is one of the problems with using art from multiple places, they are usually all different scales, and often face different directions when you get them.. this is just a simple hack to get them the same size and direction.
void ModelNode::Render(){
int nTriangleIndex;
int nVertIndex;
int nMatIndex;
glPushMatrix(); // push the matrix onto the stack
glTranslatef(m_InitialPosition[0],m_InitialPosition[1],m_InitialPosition[2]);
glRotatef(m_InitialRotation, 0.0f, 1.0, 0.0f);
glScalef(m_Scale,m_Scale,m_Scale);
// Do the rest of the rendering in here
[...]
glPopMatrix(); // pop the matrix we pushed earlier
}
Now we iterate through each of the model’s groups, and set the material information for each one. I’m almost certain that we use the glMaterial function for this.. but being that neither of the models do anything interesting with the materials, we are unlikely to find out if this is a mistake right now or not.
We also tell the system to use the appropriate texture here. If you look at the code, you will notice that I dont use the texture information from the ms3d file, but rather allocate it manually during the loading process.
glColor3f( 1.0f, 1.0f, 1.0f);
for(unsigned int i = 0; i < m_Data.Groups.size(); i++){
// get the material information
nMatIndex = m_Data.Groups[i].MaterialIndex;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, m_Data.Materials[nMatIndex].Ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, m_Data.Materials[nMatIndex].Diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, m_Data.Materials[nMatIndex].Emissive);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, m_Data.Materials[nMatIndex].Shininess);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, m_Data.Materials[nMatIndex].Specular);
// set up the texture
if(glIsTexture(m_Data.Materials[nMatIndex].TextureId)){
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_Data.Materials[nMatIndex].TextureId);
} else {
glDisable(GL_TEXTURE_2D);
}
// we draw the triangles in here
[...]
}
Finally, within each group we draw the individual triangles, complete with normals, and texture co-ordinates from the loaded data. You can just ignore the funky business going on with the texture coordinates..
I thought I was being smart earlier when I wrote the loading process, but it loaded them in the order s1-s2-s3-t1-t2-t3 rather s1-t1-s2-t2-s3-t3, so I couldn’t use the glTexCoord3fv. Not that it really matters, they are all there.
glBegin(GL_TRIANGLES);
for(unsigned int j = 0; j < m_Data.Groups[i].Indices.size(); j++){
nTriangleIndex = m_Data.Groups[i].Indices[j];
// get the vertex information
for(int k = 0; k < 3; k++){
nVertIndex = m_Data.Triangles[nTriangleIndex].Indeces[k];
glNormal3fv(&m_Data.Triangles[nTriangleIndex].Normals[k * 3]);
glTexCoord2f(
m_Data.Triangles[nTriangleIndex].TextureCoords[k],
m_Data.Triangles[nTriangleIndex].TextureCoords[k + 3]);
glVertex3fv(m_Data.Vertices[nVertIndex].verts);
}
}
glEnd();


