Jump to content
  • Advertisement

Archived

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

Klown9

.MD2 model rendering problems

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

Hi, The past couple of days of have been playing around with a md2 model loading/rendering class in c++ and vb and I am getting some strange rendering problems in both cases. It seems to be leaving out some polygons. When I change the render state to points only, it appears to be rendering the points correctly and their position seems to be right and there seem to be the right amount of vertices, so I'm guessing its something to do with the order im rendering them in. I have basically followed this tutorial ( here) but I haven't used any of the texture coordinates code yet as I just wanted to get the polygons rendering right first. I hate to post a heap of code but I think the problem might be in the following.
  for(framenum = 0;framenum < m_HeaderInfo.m_numframes;framenum++)
    {
        
        for(verticenum = 0;verticenum < m_HeaderInfo.m_numvertices; verticenum++)
        {
            m_ConvertedFrames[framenum].m_vertices[verticenum].m_Position.x = (m_Frames[framenum].m_vertices[verticenum].m_vertex[0] * 
                m_Frames[framenum].m_scale[0]) + m_Frames[framenum].m_translate[0];
                
                sprintf(tmpstring,"m_ConvertedFrames[framenum].m_vertices[verticenum].m_Position.x = %f", m_ConvertedFrames[framenum].m_vertices[verticenum].m_Position.x);
               

            m_ConvertedFrames[framenum].m_vertices[verticenum].m_Position.y = (m_Frames[framenum].m_vertices[verticenum].m_vertex[1] * 
                m_Frames[framenum].m_scale[1]) + m_Frames[framenum].m_translate[1];

            m_ConvertedFrames[framenum].m_vertices[verticenum].m_Position.z = (m_Frames[framenum].m_vertices[verticenum].m_vertex[2] * 
                m_Frames[framenum].m_scale[2]) + m_Frames[framenum].m_translate[2];

        }
    }    

    int i = 0; //Vertice counter

    for(framenum = 0;framenum< m_HeaderInfo.m_numframes;framenum++)
    {
        i = 0;
        for(trianglenum = 0;trianglenum < m_HeaderInfo.m_numtriangles; trianglenum++)
        {            
            m_RenderVertices[framenum].m_vertices[i].m_Position.x = m_ConvertedFrames[framenum].m_vertices[m_index_list[trianglenum].m_a].m_Position.x;
            m_RenderVertices[framenum].m_vertices[i].m_Position.y = m_ConvertedFrames[framenum].m_vertices[m_index_list[trianglenum].m_a].m_Position.z;
            m_RenderVertices[framenum].m_vertices[i].m_Position.z = m_ConvertedFrames[framenum].m_vertices[m_index_list[trianglenum].m_a].m_Position.y;
            m_RenderVertices[framenum].m_vertices[i].m_DiffuseColor = D3DCOLOR_XRGB(255,255,255);
            sprintf(tmpstring,"m_RenderVertices[framenum].m_vertices[i].m_Position.x = %f", m_RenderVertices[framenum].m_vertices[i].m_Position.x);
            
            i++;
           
            m_RenderVertices[framenum].m_vertices[i].m_Position.x = m_ConvertedFrames[framenum].m_vertices[m_index_list[trianglenum].m_b].m_Position.x;
            m_RenderVertices[framenum].m_vertices[i].m_Position.y = m_ConvertedFrames[framenum].m_vertices[m_index_list[trianglenum].m_b].m_Position.z;
            m_RenderVertices[framenum].m_vertices[i].m_Position.z = m_ConvertedFrames[framenum].m_vertices[m_index_list[trianglenum].m_b].m_Position.y;
            m_RenderVertices[framenum].m_vertices[i].m_DiffuseColor = D3DCOLOR_XRGB(255,255,255);
            i++;
            
            m_RenderVertices[framenum].m_vertices[i].m_Position.x = m_ConvertedFrames[framenum].m_vertices[m_index_list[trianglenum].m_c].m_Position.x;
            m_RenderVertices[framenum].m_vertices[i].m_Position.y = m_ConvertedFrames[framenum].m_vertices[m_index_list[trianglenum].m_c].m_Position.z;
            m_RenderVertices[framenum].m_vertices[i].m_Position.z = m_ConvertedFrames[framenum].m_vertices[m_index_list[trianglenum].m_c].m_Position.y;
            m_RenderVertices[framenum].m_vertices[i].m_DiffuseColor = D3DCOLOR_XRGB(255,255,255);
        }    
    }   

Like I said all of the vertice data seems ok and all the header info is loading correctly so it is possibly something to do with the order the polygons are being rendered. And if you are interested the actual rendering code:
void CMD2::Render(long frame)
{   
    g_pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE);
    g_pD3DDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
    g_pD3DDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);
    g_pD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, m_HeaderInfo.m_numtriangles, &m_RenderVertices[frame].m_vertices[0], sizeof(CVertex));
  
}
m_ConvertedFrames is the vertice data after the scaling/translating from the compressed format has been made. m_RenderedVertices is the final vertex data to be rendered. Slightly dodgy variable names but its fairly unplanned I have searched these forums and it doesn't seem like anyone else has had this problem as far as I could see. I have looked at other MD2 loading code and the only thing that is different from what I can see is that some of the other code uses the glcommands which i'm not. Any help is greatly appreciated. If you require any more info please let me know. Thanks, Harley. BreakoutX [edited by - klown9 on August 16, 2003 8:19:46 AM] [edited by - klown9 on August 16, 2003 8:25:45 AM] [edited by - klown9 on August 16, 2003 8:34:28 AM]

Share this post


Link to post
Share on other sites
Advertisement
I have downloaded some time ago the MD2 file format. I code in Visualbasic, so I can''t help you with source code, but i also had some problems rendering Md2 animation.

I have created an array of vertex, based on the information inside the md2 file, then render this.

Also, you might try to set up a blank texture for your geometry,
especially if you don''t handle texturecoordinates.
I also had this problem, because my mesh was black, and I did not see the model, although it was there.

Oh, and try to clean up your code, look for either a small mistake, or rewrite the code entirely. (i had to rewrite myself)

ANyiway, for further info, try to be more specific.

Share this post


Link to post
Share on other sites
I am currently having this problem (Im using VC++ and DX8.1).
I used the turorial from gametutorials.com, i used the MD2 loading and animation tutorial.

Since all of the tutorials there are in OpenGL, i convertd it to direct3d. no matter what i tried i get the same problem as you - a 3d md2 model with incorrect polygon drawing.
i tried disabling culling, CW, CCW, triangle lists, strips, fans, and went back to the code to figure out what could be wrong.

Sadly, I cant see anything wrong.
Im afraid that I will have to look into the format of the MD2 file to narrow down the problem''s source.

Share this post


Link to post
Share on other sites
Dark_Guy,
I dont suppose you have some code for vb do you? I am also coding this in VB with exactly the same rendering problems (Im making a simple viewer in VB just for learning purposes and making a class in c++ for the game im currently working on)

Sorry I cant really be more specific other than it looks like its drawing them in the wrong order. If i change renderstate to solid polygons are missing, but also it looks like some polygon's vertices are joined to others it shouldn't (in either renderstate). If you would like a screen shot just let me know and I will put one on my site (unless it can be done in the forums?)

GekkoCube - hehe at least someone else IS having the same troubles as me. I have looked at about 3 different tuts on this and cant figure out what im doing wrong.


[edited by - klown9 on August 16, 2003 8:10:29 PM]

Share this post


Link to post
Share on other sites
Dark_Guy,
Just curious but what would be the purpose of setting a blank texture for geometry when im not using textures. In fact i would be happy just to get the wireframe rendering properly! Do the vertices need it?
This wasn''t intended as a flame by the way.

BTW I just redid the code based on another md2 loader I downloaded from somewhere (cant remember, just found it on my hdd) and STILL get the same results. Its driving me nuts.

Thanks
Harley


BreakoutX

Share this post


Link to post
Share on other sites
Klown9, why dont u use indexed primitives?

Your first code will load the vertexbuffers of each frame.

next copy the indices into a index buffer, heres my code
   
for(j=0; j < currentFrame->numFaces; j++)
{

currentFrame->pFaces[j].vertIndex[0] = m_pTriangles[j].vertexIndices[0];
currentFrame->pFaces[j].vertIndex[1] = m_pTriangles[j].vertexIndices[1];
currentFrame->pFaces[j].vertIndex[2] = m_pTriangles[j].vertexIndices[2]; // vertex indices


currentFrame->pTexFaces[j].coordIndex[0] = m_pTriangles[j].textureIndices[0];
currentFrame->pTexFaces[j].coordIndex[1] = m_pTriangles[j].textureIndices[1];
currentFrame->pTexFaces[j].coordIndex[2] = m_pTriangles[j].textureIndices[2]; // tex coord indices

}


To render this u have to render each face individually, unless u change the tex coord indices so they represent the tex coords of the vertices in the index buffer.
Or in D3D u can assign the texcoords to the vertices (in the MD2 the texcoords are stored only for frame 0 so you'll have to copy them for the other frames). If u do this then animation will be just vertex blending between 2 VBs





Death to the gaming industry! Long live games. /*ilici*/

[edited by - Ilici on August 17, 2003 5:33:38 AM]

Share this post


Link to post
Share on other sites
Ilici,
Thanks for the suggestion but im not too sure it will fix my problem however (i could be wrong, im certainly no expert here)
Im just using the DrawPrimitiveUP function for simplicity and then once thats working i will look at optimisations. I have tried looping through each triangle in the frame and calling DrawPrimitiveUP for each triangle (slooooooow hehe) but that didn't make a difference.

I know im asking a lot here, but is someone able to maybe explain the steps involved in loading the models? I think maybe im getting confused with the triangle index lists. Or maybe im missing a step or something. I'm sure that my code matches the others ive looked at but I must be missing something. Bah I feel so frustated and stupid. 3 tuts and I still cant get it!!!
Thanks for listening to me whinge,
Harley.

[edited by - klown9 on August 17, 2003 6:20:57 AM]

Share this post


Link to post
Share on other sites
IIRC, md2 stores the triangles as indices into the vertex-list, so if you just use indexed primitives, you should be alright.
Until you get to texturing, once there you''ll have to duplicate vertices that has more than one set of texture coordinates, but that''s later.

Share this post


Link to post
Share on other sites

ok, i''ll post the entire MD2 class so u can get an ideea about the loading (i actually modified the MD2 loading tutorial on gametutorials)

But, about the MD2 format
Each frame contains the vertices, the vertex indices and the tex coord indices. Only the first frame contains the tex coords.

Loading is quite easy (as in the MD2 tutorial, i first load the data with MD2 format structures so it''s easyer, and then convert them to my structures, which are very similar to the ones in the tutorial)
So:
1. Load the header

fread(&m_Header, 1, sizeof(tMd2Header), fp);

if(m_Header.version != 8)
{
lprintf("Invalid file format (Version not 8): %s!", strFileName);
return SYS_ERR;
}

2. Load the skins, tex coords, indices and vertices

unsigned char buffer[MD2_MAX_FRAMESIZE];

m_pSkins = new tMd2Skin [m_Header.numSkins];
m_pTexCoords = new tMd2TexCoord [m_Header.numTexCoords];
m_pTriangles = new tMd2Face [m_Header.numTriangles];
m_pFrames = new tMd2Frame [m_Header.numFrames];

if (!m_pSkins || !m_pTexCoords || !m_pTriangles || !m_pFrames)
{
lprintf("ERROR: Cannot allocate memory for model\n");
return SYS_ERR;
}

fseek(fp, m_Header.offsetSkins, SEEK_SET);
fread(m_pSkins, sizeof(tMd2Skin), m_Header.numSkins, fp);

fseek(fp, m_Header.offsetTexCoords, SEEK_SET);
fread(m_pTexCoords, sizeof(tMd2TexCoord), m_Header.numTexCoords, fp);

fseek(fp, m_Header.offsetTriangles, SEEK_SET);
fread(m_pTriangles, sizeof(tMd2Face), m_Header.numTriangles, fp);

fseek(fp, m_Header.offsetFrames, SEEK_SET);

for (i = 0; i < m_Header.numFrames; i++)
{
tMd2AliasFrame *pFrame = (tMd2AliasFrame *) buffer;

m_pFrames[i].pVertices = NULL;
m_pFrames[i].pVertices = new tMd2Triangle [m_Header.numVertices];
if (!m_pFrames[i].pVertices)
{
lprintf("ERROR: Cannot allocate memory for model\n");
return SYS_ERR;
}

fread(pFrame, 1, m_Header.frameSize, fp);

strcpy(m_pFrames[i].strName, pFrame->name);

tMd2Triangle *pVertices = m_pFrames[i].pVertices;

for (int j=0; j < m_Header.numVertices; j++)
{
pVertices[j].vertex[0] = pFrame->aliasVertices[j].vertex[0] * pFrame->scale[0] + pFrame->translate[0];
pVertices[j].vertex[2] = -1 * (pFrame->aliasVertices[j].vertex[1] * pFrame->scale[1] + pFrame->translate[1]);
pVertices[j].vertex[1] = pFrame->aliasVertices[j].vertex[2] * pFrame->scale[2] + pFrame->translate[2];
}
}


3. Parse the anims

memset(pModel, 0, sizeof(t3DModel));
pModel->numObjects = m_Header.numFrames;

tAnimationInfo animation;
string strLastName = "";

for(i = 0; i < pModel->numObjects; i++)
{
string strName = m_pFrames[i].strName;
int frameNum = 0;

for(j = 0; j < strName.length(); j++)
{
if( isdigit(strName[j]) && j >= strName.length() - 2)
{
frameNum = atoi(&strName[j]);
strName.erase(j, strName.length() - j);
for (int k = 0; k < strName.size(); k++) strName[k] = tolower(strName[k]);
break;
}
}

if(strName != strLastName || i == pModel->numObjects - 1)
{
if(strLastName != "
")
{
strcpy(animation.strName, strLastName.c_str());
animation.endFrame = i - 1;
pModel->pAnimations.push_back(animation);
memset(&animation, 0, sizeof(tAnimationInfo));

pModel->numOfAnimations++;
}

animation.startFrame = frameNum - 1 + i;
}
strLastName = strName;
}


4. Convert the structures to the app''s structures

Model.numObjects = pModel->numObjects;
Model.numMaterials = pModel->numMaterials;

Model.pObjects = new t3DObject[Model.numObjects];

for (i = 0; i < pModel->numObjects; i++)
{
t3DObject* currentFrame = &Model.pObjects[i];

currentFrame->numVerts = m_Header.numVertices;
currentFrame->numTexCoords = m_Header.numTexCoords;
currentFrame->numFaces = m_Header.numTriangles;

currentFrame->pVerts = new CVector3 [currentFrame->numVerts];
if (!currentFrame->pVerts)
{
lprintf("ERROR: Cannot allocate memory for model\n");
return SYS_ERR;
}

for (j=0; j < currentFrame->numVerts; j++)
{
currentFrame->pVerts[j].x = m_pFrames[i].pVertices[j].vertex[0];
currentFrame->pVerts[j].y = m_pFrames[i].pVertices[j].vertex[1];
currentFrame->pVerts[j].z = m_pFrames[i].pVertices[j].vertex[2];
}

delete [] m_pFrames[i].pVertices;
m_pFrames[i].pVertices = NULL;

currentFrame->pFaces = new tFace [currentFrame->numFaces];
currentFrame->pTexFaces = new tTexFace [ currentFrame->numFaces];
for(j=0; j < currentFrame->numFaces; j++)
{

currentFrame->pFaces[j].vertIndex[0] = m_pTriangles[j].vertexIndices[0];
currentFrame->pFaces[j].vertIndex[1] = m_pTriangles[j].vertexIndices[1];
currentFrame->pFaces[j].vertIndex[2] = m_pTriangles[j].vertexIndices[2];

currentFrame->pTexFaces[j].coordIndex[0] = m_pTriangles[j].textureIndices[0];
currentFrame->pTexFaces[j].coordIndex[1] = m_pTriangles[j].textureIndices[1];
currentFrame->pTexFaces[j].coordIndex[2] = m_pTriangles[j].textureIndices[2];
}

if(i > 0)
{
continue;
}

currentFrame->pTexCoords = new CVector2 [currentFrame->numTexCoords];
for(j = 0; j < currentFrame->numTexCoords; j++)
{
currentFrame->pTexCoords[j].x = (float)m_pTexCoords[j].u / float(m_Header.skinWidth);
currentFrame->pTexCoords[j].y = 1.0f - (float)m_pTexCoords[j].v / float(m_Header.skinHeight);
}
}

fclose(fp); // Close the current file pointer


5. Free temp stuff

int last = strlen(strFileName) - 1;

strFileName[last ] = ''p'';
strFileName[last - 1] = ''m'';
strFileName[last - 2] = ''b'';

if (tex.LoadFromFile(strFileName)) return SYS_ERR;

if(m_pSkins) delete [] m_pSkins;
if(m_pTexCoords) delete m_pTexCoords;
if(m_pTriangles) delete m_pTriangles;
if(m_pFrames) delete m_pFrames;

m_pSkins = NULL;
m_pTexCoords = NULL;
m_pTriangles = NULL;
m_pFrames = NULL;

return SYS_OK;


The rendering uses GL so u have to convert to D3D:

void CModelMD2::Render()
{
tAnim3DModel* pModel = &Model;
if(pModel->numObjects <= 0) return;

tAnimationInfo *pAnim = &(pModel->pAnimations[pModel->currentAnim]);

int nextFrame;

if(pModel->currentFrame == pAnim->endFrame)
{
if (loopAnim)
{
nextFrame = pAnim->startFrame;
}
else
{
nextFrame = pAnim->endFrame;
}
}
else
{
nextFrame = pModel->currentFrame + 1;
}

t3DObject *pFrame = &pModel->pObjects[pModel->currentFrame];
t3DObject *pNextFrame = &pModel->pObjects[nextFrame];
t3DObject *pFirstFrame = &pModel->pObjects[0];

CVector3 vPoint1;
CVector3 vPoint2;

glFrontFace(GL_CW);
glPushMatrix();
glTranslatef(vPos.x, vPos.y + 32, vPos.z);
glRotatef(ang, 0, 1, 0);

glDisable(GL_LIGHTING);

glEnable(GL_TEXTURE_2D);
glColor3f(1.0f, 1.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, tex.texnum);

glBegin(GL_TRIANGLES);
for(int j = 0; j < pFrame->numFaces; j++)
{
for(int whichVertex = 0; whichVertex < 3; whichVertex++)
{
int vertIndex = pFirstFrame->pFaces[j].vertIndex[whichVertex];
int texIndex = pFirstFrame->pTexFaces[j].coordIndex[whichVertex];

float f = t; //(1 - cos(t * PI)) / 2 ;


vPoint1 = pFrame->pVerts[ vertIndex ];
vPoint2 = pNextFrame->pVerts[ vertIndex ];

glTexCoord2fv((float *)&pFirstFrame->pTexCoords[ texIndex ]);
glVertex3fv((float *)&(vPoint1 + (vPoint2 - vPoint1) * f));
}
}
glEnd();

glEnable(GL_LIGHTING);

glPopMatrix();
glFrontFace(GL_CCW);

}


To do it in D3D u have to assign the tex coords to each of the verts: loop across the texcoord indices and assign them to the verices.


for (i = 0; i < Model.NumObjects; i++) // for each object

{
pObj = &Mode.pObjects[i]; //obj pointer

for(int j = 0; j < pObj->numFaces; j++)
{
for(int whichVertex = 0; whichVertex < 3; whichVertex++)
{
//now assign the tex coord to the vertex

int vertIndex = pFirstFrame->pFaces[j].vertIndex[whichVertex];
// pFirstFrame is a pointer to the first frame obj

int texIndex = pFirstFrame->pTexFaces[j].coordIndex[whichVertex];
//in the D3DCustomVertex u have to put the u and v coords

/* Start: This is code for D3D (pVertices is a D3DCUSTOMVERTEX array */
pObject->pVertices[vertIndex].u = pFirstFrame->pTexCoords[texindex].x;
pObject->pVertices[vertIndex].v = pFirstFrame->pTexCoords[texindex].y; //

/*end code for d3d*/
}
}


the entire source for MD2.cpp:


#include "stdafx.h"
#include "Md2.h"
#include "log.h"
#include "opengl.h"
#include <string>
#include "3dm.h"

struct tMd2Header
{
int magic; // This is used to identify the file

int version; // The version number of the file (Must be 8)

int skinWidth; // The skin width in pixels

int skinHeight; // The skin height in pixels

int frameSize; // The size in bytes the frames are

int numSkins; // The number of skins associated with the model

int numVertices; // The number of vertices (constant for each frame)

int numTexCoords; // The number of texture coordinates

int numTriangles; // The number of faces (polygons)

int numGlCommands; // The number of gl commands

int numFrames; // The number of animation frames

int offsetSkins; // The offset in the file for the skin data

int offsetTexCoords; // The offset in the file for the texture data

int offsetTriangles; // The offset in the file for the face data

int offsetFrames; // The offset in the file for the frames data

int offsetGlCommands; // The offset in the file for the gl commands data

int offsetEnd; // The end of the file offset

};


struct tMd2AliasTriangle
{
byte vertex[3];
byte lightNormalIndex;
};

struct tMd2Triangle
{
float vertex[3];
float normal[3];
};

struct tMd2Face
{
short vertexIndices[3];
short textureIndices[3];
};

struct tMd2TexCoord
{
short u, v;
};

struct tMd2AliasFrame
{
float scale[3];
float translate[3];
char name[16];
tMd2AliasTriangle aliasVertices[1];
};

struct tMd2Frame
{
char strName[16];
tMd2Triangle *pVertices;
};

typedef char tMd2Skin[64];

void CModelMD2::Render()
{
tAnim3DModel* pModel = &Model;
if(pModel->numObjects <= 0) return;

tAnimationInfo *pAnim = &(pModel->pAnimations[pModel->currentAnim]);

int nextFrame;

if(pModel->currentFrame == pAnim->endFrame)
{
if (loopAnim)
{
nextFrame = pAnim->startFrame;
}
else
{
nextFrame = pAnim->endFrame;
}
}
else
{
nextFrame = pModel->currentFrame + 1;
}

t3DObject *pFrame = &pModel->pObjects[pModel->currentFrame];
t3DObject *pNextFrame = &pModel->pObjects[nextFrame];
t3DObject *pFirstFrame = &pModel->pObjects[0];

CVector3 vPoint1;
CVector3 vPoint2;

glFrontFace(GL_CW);
glPushMatrix();
glTranslatef(vPos.x, vPos.y + 32, vPos.z);
glRotatef(ang, 0, 1, 0);

glDisable(GL_LIGHTING);

glEnable(GL_TEXTURE_2D);
glColor3f(1.0f, 1.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, tex.texnum);

glBegin(GL_TRIANGLES);
for(int j = 0; j < pFrame->numFaces; j++)
{
for(int whichVertex = 0; whichVertex < 3; whichVertex++)
{
int vertIndex = pFirstFrame->pFaces[j].vertIndex[whichVertex];
int texIndex = pFirstFrame->pTexFaces[j].coordIndex[whichVertex];

float f = t; //(1 - cos(t * PI)) / 2 ;


vPoint1 = pFrame->pVerts[ vertIndex ];
vPoint2 = pNextFrame->pVerts[ vertIndex ];

glTexCoord2fv((float *)&pFirstFrame->pTexCoords[ texIndex ]);
glVertex3fv((float *)&(vPoint1 + (vPoint2 - vPoint1) * f));
}
}
glEnd();

glEnable(GL_LIGHTING);

glPopMatrix();
glFrontFace(GL_CCW);

}

void CModelMD2::SetPosition(CVector3 v)
{
vPos = v;
}

void CModelMD2::SetRotation(float a)
{
ang = a;
}

CModelMD2::CModelMD2()
{
t = 0.0f;
loopAnim = true;
}

void CModelMD2::Destroy()
{
DestroyModel(&Model);
}

void CModelMD2::SetAnimation(int anim)
{
char aName[255];

switch (anim)
{
case MD2_ANIM_STAND: strcpy(aName, "stand"); loopAnim = true; break;
case MD2_ANIM_WALK: strcpy(aName, "run"); loopAnim = true; break;
case MD2_ANIM_DEATH1: strcpy(aName, "death1"); loopAnim = false; break;
case MD2_ANIM_DEATH2: strcpy(aName, "death2"); loopAnim = false; break;
case MD2_ANIM_DEATH3: strcpy(aName, "death3"); loopAnim = false; break;
}

for (int i = 0; i < Model.pAnimations.size(); i++)
{
if (strcmp(Model.pAnimations[i].strName, aName) == 0)
{
Model.currentFrame = Model.pAnimations[i].startFrame;
Model.currentAnim = i;
return;
}
}
}

int CModelMD2::GetAnimation()
{
char str[255];
strcpy(str, Model.pAnimations[Model.currentAnim].strName);

if (strcmp(str, "stand") == 0) return MD2_ANIM_STAND;
if (strcmp(str, "run") == 0) return MD2_ANIM_WALK;
if (strcmp(str, "death1") == 0) return MD2_ANIM_DEATH1;
if (strcmp(str, "death2") == 0) return MD2_ANIM_DEATH2;
if (strcmp(str, "death3") == 0) return MD2_ANIM_DEATH3;

return -1;
}

void CModelMD2::SetAnimLoop(bool b)
{
loopAnim = b;
}

void CModelMD2::Update()
{
tAnimationInfo* pAnim = &Model.pAnimations[Model.currentAnim];

if (t > 1.0f)
{
if (Model.currentFrame == pAnim->endFrame)
{
if (loopAnim)
{
Model.currentFrame = pAnim->startFrame;
t = 0.0f;
}
else
{
Model.currentFrame = pAnim->endFrame;
t = 1.1f;
}
}
else
{
Model.currentFrame++;
t = 0.0f;
}
}
else
{
t += rFrameInterval * 5.0f;
}
}

int CModelMD2::LoadFromFile(char* fname)
{
tMd2Header m_Header;
tMd2Skin *m_pSkins = NULL;
tMd2TexCoord *m_pTexCoords = NULL;
tMd2Face *m_pTriangles = NULL;
tMd2Frame *m_pFrames = NULL;
tAnim3DModel* pModel = &Model;

char strFileName[255];
int i, j;

strcpy(strFileName, fname);

FILE* fp = fopen(strFileName, "rb");

if(!fp)
{
lprintf("Unable to find the file: %s!", strFileName);
return SYS_ERR;
}

fread(&m_Header, 1, sizeof(tMd2Header), fp);

if(m_Header.version != 8)
{
lprintf("Invalid file format (Version not 8): %s!", strFileName);
return SYS_ERR;
}

lprintf("Loading MD2 model %s", strFileName);

unsigned char buffer[MD2_MAX_FRAMESIZE];

m_pSkins = new tMd2Skin [m_Header.numSkins];
m_pTexCoords = new tMd2TexCoord [m_Header.numTexCoords];
m_pTriangles = new tMd2Face [m_Header.numTriangles];
m_pFrames = new tMd2Frame [m_Header.numFrames];

if (!m_pSkins || !m_pTexCoords || !m_pTriangles || !m_pFrames)
{
lprintf("ERROR: Cannot allocate memory for model\n");
return SYS_ERR;
}

fseek(fp, m_Header.offsetSkins, SEEK_SET);
fread(m_pSkins, sizeof(tMd2Skin), m_Header.numSkins, fp);

fseek(fp, m_Header.offsetTexCoords, SEEK_SET);
fread(m_pTexCoords, sizeof(tMd2TexCoord), m_Header.numTexCoords, fp);

fseek(fp, m_Header.offsetTriangles, SEEK_SET);
fread(m_pTriangles, sizeof(tMd2Face), m_Header.numTriangles, fp);

fseek(fp, m_Header.offsetFrames, SEEK_SET);

for (i = 0; i < m_Header.numFrames; i++)
{
tMd2AliasFrame *pFrame = (tMd2AliasFrame *) buffer;

m_pFrames[i].pVertices = NULL;
m_pFrames[i].pVertices = new tMd2Triangle [m_Header.numVertices];
if (!m_pFrames[i].pVertices)
{
lprintf("ERROR: Cannot allocate memory for model\n");
return SYS_ERR;
}

fread(pFrame, 1, m_Header.frameSize, fp);

strcpy(m_pFrames[i].strName, pFrame->name);

tMd2Triangle *pVertices = m_pFrames[i].pVertices;

for (int j=0; j < m_Header.numVertices; j++)
{
pVertices[j].vertex[0] = pFrame->aliasVertices[j].vertex[0] * pFrame->scale[0] + pFrame->translate[0];
pVertices[j].vertex[2] = -1 * (pFrame->aliasVertices[j].vertex[1] * pFrame->scale[1] + pFrame->translate[1]);
pVertices[j].vertex[1] = pFrame->aliasVertices[j].vertex[2] * pFrame->scale[2] + pFrame->translate[2];
}
}

memset(pModel, 0, sizeof(t3DModel));
pModel->numObjects = m_Header.numFrames;

tAnimationInfo animation;
string strLastName = "";

for(i = 0; i < pModel->numObjects; i++)
{
string strName = m_pFrames[i].strName;
int frameNum = 0;

for(j = 0; j < strName.length(); j++)
{
if( isdigit(strName[j]) && j >= strName.length() - 2)
{
frameNum = atoi(&strName[j]);
strName.erase(j, strName.length() - j);
for (int k = 0; k < strName.size(); k++) strName[k] = tolower(strName[k]);
break;
}
}

if(strName != strLastName || i == pModel->numObjects - 1)
{
if(strLastName != "
")
{
strcpy(animation.strName, strLastName.c_str());
animation.endFrame = i - 1;
pModel->pAnimations.push_back(animation);
memset(&animation, 0, sizeof(tAnimationInfo));

pModel->numOfAnimations++;
}

animation.startFrame = frameNum - 1 + i;
}
strLastName = strName;
}

Model.numObjects = pModel->numObjects;
Model.numMaterials = pModel->numMaterials;

Model.pObjects = new t3DObject[Model.numObjects];

for (i = 0; i < pModel->numObjects; i++)
{
t3DObject* currentFrame = &Model.pObjects[i];

currentFrame->numVerts = m_Header.numVertices;
currentFrame->numTexCoords = m_Header.numTexCoords;
currentFrame->numFaces = m_Header.numTriangles;

currentFrame->pVerts = new CVector3 [currentFrame->numVerts];
if (!currentFrame->pVerts)
{
lprintf("ERROR: Cannot allocate memory for model\n");
return SYS_ERR;
}

for (j=0; j < currentFrame->numVerts; j++)
{
currentFrame->pVerts[j].x = m_pFrames[i].pVertices[j].vertex[0];
currentFrame->pVerts[j].y = m_pFrames[i].pVertices[j].vertex[1];
currentFrame->pVerts[j].z = m_pFrames[i].pVertices[j].vertex[2];
}

delete [] m_pFrames[i].pVertices;
m_pFrames[i].pVertices = NULL;

currentFrame->pFaces = new tFace [currentFrame->numFaces];
currentFrame->pTexFaces = new tTexFace [ currentFrame->numFaces];
for(j=0; j < currentFrame->numFaces; j++)
{

currentFrame->pFaces[j].vertIndex[0] = m_pTriangles[j].vertexIndices[0];
currentFrame->pFaces[j].vertIndex[1] = m_pTriangles[j].vertexIndices[1];
currentFrame->pFaces[j].vertIndex[2] = m_pTriangles[j].vertexIndices[2];

currentFrame->pTexFaces[j].coordIndex[0] = m_pTriangles[j].textureIndices[0];
currentFrame->pTexFaces[j].coordIndex[1] = m_pTriangles[j].textureIndices[1];
currentFrame->pTexFaces[j].coordIndex[2] = m_pTriangles[j].textureIndices[2];
}

if(i > 0)
{
continue;
}

currentFrame->pTexCoords = new CVector2 [currentFrame->numTexCoords];
for(j = 0; j < currentFrame->numTexCoords; j++)
{
currentFrame->pTexCoords[j].x = (float)m_pTexCoords[j].u / float(m_Header.skinWidth);
currentFrame->pTexCoords[j].y = 1.0f - (float)m_pTexCoords[j].v / float(m_Header.skinHeight);
}
}

fclose(fp); // Close the current file pointer


int last = strlen(strFileName) - 1;

strFileName[last ] = ''p'';
strFileName[last - 1] = ''m'';
strFileName[last - 2] = ''b'';

if (tex.LoadFromFile(strFileName)) return SYS_ERR;

if(m_pSkins) delete [] m_pSkins;
if(m_pTexCoords) delete m_pTexCoords;
if(m_pTriangles) delete m_pTriangles;
if(m_pFrames) delete m_pFrames;

m_pSkins = NULL;
m_pTexCoords = NULL;
m_pTriangles = NULL;
m_pFrames = NULL;

return SYS_OK;
}




and for MD2.h

#ifndef _MD2_H
#define _MD2_H

#include "3dm.h"
#include "textures.h"
#include "physics.h"

#define MD2_MAX_TRIANGLES 4096
#define MD2_MAX_VERTICES 2048
#define MD2_MAX_TEXCOORDS 2048
#define MD2_MAX_FRAMES 512
#define MD2_MAX_SKINS 32
#define MD2_MAX_FRAMESIZE (MD2_MAX_VERTICES * 4 + 128)

#define kAnimationSpeed 5.0f

struct tAnimationInfo
{
char strName[255]; // This stores the name of the animation (Jump, Pain, etc..)

int startFrame; // This stores the first frame number for this animation

int endFrame; // This stores the last frame number for this animation

};

struct tAnim3DModel : public t3DModel
{
int numOfAnimations; // The number of animations in this model (NEW)

int currentAnim; // The current index into pAnimations list (NEW)

int currentFrame; // The current frame of the current animation (NEW)


vector<tAnimationInfo> pAnimations; // The list of animations (NEW)

};

#define MD2_ANIM_STAND 0
#define MD2_ANIM_WALK 1
#define MD2_ANIM_DEATH1 2
#define MD2_ANIM_DEATH2 3
#define MD2_ANIM_DEATH3 4

class CModelMD2
{
public:
CModelMD2();

int LoadFromFile(char* fname);
void Destroy();

void Render();
void Update();

void SetAnimation(int anim);
int GetAnimation();
void SetPosition(CVector3 vPos);
void SetRotation(float ang);

void SetAnimLoop(bool);
private:

tAnim3DModel Model;
CTexture tex;

float t;

bool loopAnim;

float ang;
CVector3 vPos;
};



#endif



For the anims: add some #defines for each anim (i only used run, stand, death0-3)

Hope this will make it clear 4 U


Death to the gaming industry! Long live games. /*ilici*/

Share this post


Link to post
Share on other sites
Thanks for putting in the time and effort Ilici to post that. I will have a look through this as it is different again from any of the tuts I have gone through. Really appreciate it.
Below are the steps in how I load and render the models. Im wondering if someone could pick something up that im not?

-retrieve header info (NumTriangles, NumVertices etc - all working correctly)
-retrieve triangle data (vertex indices)
-retrieve frame data (compressed vertex coords)
-Convert the compressed coords to the right coordinates by mulitplying scale and adding the translation values in the frame data, using the triangle indices in the vertex arrays as the array element (if i'm not making sense, just let me know). I put these values into my own D3d structure to make it a bit cleaner
-Render my using DrawPrimitiveUP

Is this right? Is there anything that I should be aware of in any of these steps? I could just copy and paste code if I wanted to but as I ultimately want to make my own format I would like to get this working first (and I hate giving up on things)

I will look through your code now and see if I can see what im doing wrong.

Thanks again,
Harley.





[edited by - klown9 on August 19, 2003 6:58:59 AM]

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!