help with segmentation fault on MD2 file

Started by
10 comments, last by Michael2006 17 years, 8 months ago
I am trying to display an array of MD2 characters in a world and I am getting a segmentation fault when building the mipmap for the skin file ................... CTargaImage image; image.Load(skinFile); glGenTextures(1,&m_texID); glBindTexture(GL_TEXTURE_2D,m_texID); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR); gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGB,image.GetWidth(),image.GetHeight(),GL_RGB,GL_UNSIGNED_BYTE,image.GetImage()); ............................................ the gluBuild2DMipmap gives the segmentation fault as reported by gdb under Linux. the Meta properties for the Targa file are 432x142 pixels? 24bpp RGB Runlength encoded would this cause the problem? should the image be smaller 64x64 etc??
Advertisement
Try resizing the image with dimensions of "power of 2" for example 512x128...
GFX cards (not some new ones) require the image to have dimensions that follow this.

EDIT: Oh and are you sure your CTargaImage::Load() function works correctly? You could get faulty data otherwise...
----------------------------------------------------------------------------------------------------------------------"Ask not what humanity can do for you, ask what you can do for humanity." - By: Richard D. Colbert Jr.
Yea my thoughts exactly.
If your image.Load function failes, you are likely to get segmentation faults when accessing the image member functions later on
I have a skybox and a textured landscape and my tga seems to be working for these
I scaled the images for the binding to 3Dmodel and still get a segmentation fault??
Any suggestions are welcome!
Well...


gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGB,image.GetWidth(),image.GetHeight(),GL_RGB,GL_UNSIGNED_BYTE,image.GetImage());

Looking at MSDN i see that the second param is a value between 1-4, and GL_RGB isn't defined as 3 (but 0x1907) so try replacing the second param with a 3.

Try to not RLE your file, just to see if that's the problem.

Quote:
I have a skybox and a textured landscape and my tga seems to be working for these

Make sure the skybox and MD2 texture is the same format (no RLE and stuff). If the skybox works but not the texture, and they're not the same format, this could very well be a problem with CTargaImage::Load().


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);

Try to set just GL_LINEAR on both. This has acted somewhat wierd for me at times. Just for debugging.
----------------------------------------------------------------------------------------------------------------------"Ask not what humanity can do for you, ask what you can do for humanity." - By: Richard D. Colbert Jr.
I have tried the following
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGB,image.GetWidth(),image.GetHeight(),GL_RGB,GL_UNSIGNED_BYTE,image.GetImage());
^setting to 3

and still segmentation fault
my skybox code is
....................
CTargaImage image;

image.Load(top);
glGenTextures(1, &m_textures[SKY_TOP]);
glBindTexture(GL_TEXTURE_2D, m_textures[SKY_TOP]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage());
image.Release();
......................
and I have tried using CLAMP_TO_EDGE and Im just plain stuck.
my Model load function goes like
.....................................
m_rhinoModel.Load("rhino/tris.md2","rhino/rhino.tga");
m_centaurModel.Load("centaur/tris.md2","centaur/centaur.tga";
m_demonModel.Load("nemocromicus/tris.md2","necromicus/necromicus.tga");
......................................
and the MD2 load is like where scale is set to a constant
bool CMD2Model::Load(const char *modelFile,const char *skinFile,GLfloat scale)
{
FILE *pF;
if((pF = fopen(modelFile,"rb"))==NULL){
return false;
}


fread(&m_info,sizeof(modelHeader_t),1,pF);

m_verts = new xyz_t[m_info.numXYZ * m_info.numFrames];



char *buffer = new char[m_info.numFrames * m_info.framesize];


//////////
// initialize member variables




fseek(pF,m_info.offsetFrames,SEEK_SET);
fread(buffer,m_info.numFrames,m_info.framesize,pF);

int i,j;
frame_t *frame;
xyz_t *pVerts;
m_miny = FLT_MAX;
GLfloat maxy = FLT_MIN;

for(j=0;j<m_info.numFrames;++j)
{
frame =(frame_t*)&buffer[m_info.framesize*j];
pVerts = (xyz_t*)&m_verts[m_info.numXYZ*j];
for(i=0;i<m_info.numXYZ;i++)
{
pVerts.x = scale*(frame->scale[0]*frame->fp.v[0]+frame->translate[0]);
pVerts.x = scale*(frame->scale[1]*frame->fp.v[1]+frame->translate[1]);
pVerts.x = scale*(frame->scale[2]*frame->fp.v[2]+frame->translate[2]);

if(j==0)
{
if(pVerts.y<m_miny)
m_miny = pVerts.y;
if(pVerts.y > maxy)
maxy = pVerts.y;
}
}
}
CalculateBoundingSphere(m_boundingSphere,m_miny,maxy);
// Move(0,0,0);

m_tris = new mesh_t[m_info.numTris];

fseek(pF,m_info.offsetTris,SEEK_SET);
fread(m_tris,m_info.numTris,sizeof(mesh_t),pF);

stIndex_t *stTemp = new stIndex_t[m_info.numST];
m_texCoords = new texCoord_t[m_info.numTris*3];

fseek(pF,m_info.offsetST,SEEK_SET);
fread(stTemp,m_info.numST,sizeof(stIndex_t),pF);

int index =0;
for(i=0;i<m_info.numTris;i++)
{
m_texCoords[index].s = (float)stTemp[m_tris.stIndex[0]].s / m_info.skinwidth;
m_texCoords[index++].t = 1.0f - (float)stTemp[m_tris.stIndex[0]].t / m_info.skinheight;
m_texCoords[index].s = (float)stTemp[m_tris.stIndex[1]].s / m_info.skinwidth;
m_texCoords[index++].t = 1.0f -(float)stTemp[m_tris.stIndex[1]].t / m_info.skinheight;
m_texCoords[index].s = (float)stTemp[m_tris.stIndex[2]].s / m_info.skinwidth;
m_texCoords[index++].t = 1.0f - (float)stTemp[m_tris.stIndex[2]].s / m_info.skinheight;
}

m_currentVerts = new xyz_t[m_info.numTris * 3];

//close file and free mem
fclose(pF);
//pFile.close();
delete[]buffer;

CTargaImage image;
image.Load(skinFile);
glGenTextures(1,&m_texID);
glBindTexture(GL_TEXTURE_2D,m_texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGB,image.GetWidth(),image.GetHeight(),GL_RGB,GL_UNSIGNED_BYTE,image.GetImage());

image.Release();

m_currentFrame = 0;
m_nextFrame = 1;
m_interpol = 0.0;

//animate least 1 frame to make sure m_currentVerts get inited
SetAnimation(IDLE,IDLE);
Animate(0);

return true;
}

If you can suggest anything I would be willing to try it.
Thanks
Michael2006
Did you try to remove the Run Length Encoding from your image files?

The only thing i can think of is that it really IS your CTargaImage::Load() func that dosn't work correctly.
You could try to load an image that you already know will load and display correctly, and use that for texture for now. If that should work then we know for sure. If not, perhaps it's something else that you havn't wrote...i mean with your compiler or something like that...
----------------------------------------------------------------------------------------------------------------------"Ask not what humanity can do for you, ask what you can do for humanity." - By: Richard D. Colbert Jr.
As i said the skybox and texture loads fine which is Targa but my 3D model and texture wont load. As a last resort I bought the book on 3D model standards and am waiting for its delivery and that should get me started!.

Many Thanks
Michael2006
Quote:
As i said the skybox and texture loads fine which is Targa

Yes, but as you also said the MD2 texture is RLE. Are you 100% sure the other images also are RLE? Did you try to load the skybox texture as the MD2 texture?
This is simple elimination.
Have you also stepped through your code during runtime, into every single little function (especially CTargaImage::Load()) and made sure< your data is correct?
Don't always trust that things will work in one place just because it did in another, until you have confirmed all data. =)

You said the gluBuild2DMipmaps() gave you the seg. fault, the you can hopefully rule out there's anything wrong woth your MD2 loading code (for now).

You could also see if you get any error returned from gluBuild2DMipmaps() (perhaps not very likely, but can't hurt to check all GL functions you have there).
int ret = gluBuild2DMimpaps();if(ret)    char *str = gluErrorString(ret);
----------------------------------------------------------------------------------------------------------------------"Ask not what humanity can do for you, ask what you can do for humanity." - By: Richard D. Colbert Jr.
Thanks

I had a typo on "Necromicus" which realy buggered me.
Now it compiles and runs but I dont see any MD3Models.

This topic is closed to new replies.

Advertisement