Advertisement

access violation.

Started by November 24, 2002 05:32 PM
7 comments, last by tHiSiSbOb 22 years, 3 months ago
I am writing an md2 loader and get an access violation when I render it. Here is my code. definitions:
  #ifndef _md2_h_
#define _md2_h_
#include <windows.H>
#include <stdio.h>

struct MD2_HEADER
{
   int magic; 
   int version; 
   int skinWidth; 
   int skinHeight; 
   int frameSize; 
   int numSkins; 
   int numVertices; 
   int numTexCoords; 
   int numTriangles; 
   int numGlCommands; 
   int numFrames; 
   int offsetSkins; 
   int offsetTexCoords; 
   int offsetTriangles; 
   int offsetFrames; 
   int offsetGlCommands; 
   int offsetEnd; 
};

struct MD2_VERTEX
{
   byte vertex[3];
   byte lightNormalIndex;
};
struct MD2_FRAME
{
   float scale[3];
   float translate[3];
   char name[16];
   MD2_VERTEX *verts;
};

class MD2
{
public:
	MD2_HEADER header;
	MD2_FRAME *frames;
	__declspec(dllexport) bool LoadMD2(char *fileName);
	__declspec(dllexport) void RenderMD2();

};
#endif  
code
  
#include <md2.h>
#include <gl\gl.h>										// Header File For The OpenGL32 Library
#include <gl\glu.h>	


__declspec(dllexport) bool MD2::LoadMD2(char *fileName)
{
	int i;
	FILE *filePointer;
	filePointer=fopen(fileName,"rb");
	if(!filePointer)
	{
		return FALSE;
	}
	fread(&header,1,sizeof(MD2_HEADER),filePointer);
	frames=new MD2_FRAME[header.numFrames];
	fseek(filePointer,header.offsetFrames,SEEK_SET);
	for(i=0;i<header.numFrames;i++)
	{	
		frames[i].verts=new MD2_VERTEX[header.numVertices];
		fread(&frames[i],1,header.frameSize,filePointer);
	}
	fclose(filePointer);
	return TRUE;
}

__declspec(dllexport) void MD2::RenderMD2()
{
	int i,j;
	glBegin(GL_TRIANGLES);
	for(i=0;i<header.numFrames;i++)
	{
		for (j=0;j<header.numVertices;j++)
		{
			
			glVertex3f(frames[i].verts[j].vertex[0]*frames[i].scale[0]+frames[i].translate[0],
					   frames[i].verts[j].vertex[2]*frames[i].scale[2]+frames[i].translate[2],
					   -1*(frames[i].verts[j].vertex[1]*frames[i].scale[1]+frames[i].translate[1]));
		}
	}
	glEnd();
}




		  
"Computers are only as smart as their programers" My Site-> Still working on it.
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.
The problem has to do with that your MD2 reader is a lib(DLL on windowz). Theres a special thing you have to do on win I havnt done any win libs since my summer job and I use linux so I cannt help you there. :\ It might have to be MD2Load (class name then another name for the funtion name). Not sure though win libs are screwy. Try running it with out the class being in a lib and see what happens. Do realize that you code doesnt have any texture code? Look at NeHe''s example its great, thats what I use.
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind
Advertisement
i know about the texture code.
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.
At least debug it and tell us what line it crashes on...sheesh.

[twitter]warrenm[/twitter]

nope....I took it out of the dll and till does the same thing.
It crashes on this line:

  glVertex3f(frames[i].verts[j].vertex[0]*frames[i].scale[0]+frames[i].translate[0],					   frames[i].verts[j].vertex[2]*frames[i].scale[2]+frames[i].translate[2],					   -1*(frames[i].verts[j].vertex[1]*frames[i].scale[1]+frames[i].translate[1]));   


"Computers are only as smart as their programers"
My Site-> Still working on it.

[edited by - tHiSiSbOb on November 24, 2002 11:30:41 PM]
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.
Im pretty sure it is an array boundary error, maybe you have more items in the array than its size? Check that.
GraphicsWare|RenderTechhttp://www.graphicsware.com3D Graphics & Solutions
Advertisement
Thats what I thouight but i can;t find n e thing. But I will continue my search.
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.
IT is DEFINETLY AN ARRAY BOUNDARY ERROR !!!!!!!
MOST COMMON MISTAKE: your loop goes one more turn than its supposed to
SOLUTION: Try to make your loop go one turn less and see what happens

This happened to me a lot during my programing experience
.....Yup...I think i have it nailed down...I tried to define everything staticlyz(no pointers:: MD2_VERTEX verts[3]) and it worked. My question now is...why is it now making my array the right size when i do frames= new MD2_FRAMES[header.numFrames]


[edited by - tHiSiSbOb on November 26, 2002 6:28:47 PM]
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.

This topic is closed to new replies.

Advertisement