Hi, I am basically writing a program that reads in ASCII files exported from 3ds max in order to reproduce an object in OpenGL. My code works fine for one object and I've even got it working for two as long as the second object contains less vertices/facet windings than the previous. Problem is, if I try and load any more than two or if the first object is 'smaller' than any that follow, my program crashes. I don't have much experiene using C but I'm pretty sure it's something to do with the way in which I am trying to allocate and then reallocate the memory for my 3d array.

I've attached the ASCII files (as text files because I can't upload .ASE files) that I'm trying to read and a text file with the actual code(for, perhaps, easier viewing). Any help would be greatly appreciated

code below:

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "glut.h"
#include <GL\gl.h>
#include <GL\glu.h>

GLint ***facets; 
GLfloat ***vertices;

int nRows, nCols, row, col, i, j, k, numVertices, numFacets, shapeNum;
int numShapes = 2;
int numCols = 3;
GLfloat rquad;

//int totalVertices = 0;
//int totalFacets = 0;

int nFacets[10];
int nVertices[10];


GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},{1.0,0.0,1.0},{1.0,1.0,1.0},
{0.0,1.0,1.0}};

//All methods used
void display();
void adjustSize(GLsizei, GLsizei);
void animate();
//void mouse(int btn, int state, int x, int y);
void polygon(int a, int b, int c, int shapeNum);
void shape(int shapeNum);

void loadShape(char filename[20], int shapeNum);
char *readFile(FILE *sceneFile);


GLint ***allocate_memoryi(int nShapes, int nRows, int nCols)
{
	int ***shapeArray;
	shapeArray = (int***) malloc(nShapes * sizeof(int**));
	for(i=0; i<nShapes; ++i)
	{
		shapeArray[i] = (int**) malloc(nRows * sizeof(int));
		for(j=0; j<nRows; ++j)
		{
			shapeArray[i][j] = (int*) malloc(nCols * sizeof(int));
		}
	}
	return shapeArray;
}


GLfloat ***allocate_memoryf(int nShapes, int nRows, int nCols)
{
	float ***shapeArray;
	shapeArray = (float***) malloc(nShapes * sizeof(float**));
	for(i=0; i<nShapes; ++i)
	{
		shapeArray[i] = (float**) malloc(nRows * sizeof(float));
		for(j=0; j<nRows; ++j)
		{
			shapeArray[i][j] = (float*) malloc(nCols * sizeof(float));
		}
	}
	return shapeArray;
}


GLfloat ***reallocate_memoryf(int nShapes, int nRows, int nCols)
{
	float ***shapeArray;
	shapeArray = (float***) realloc(vertices, nShapes * sizeof(float**));
	for(i=0; i<nShapes; ++i)
	{
		shapeArray[i] = (float**) realloc(vertices[i], nVertices[i] * sizeof(float));
		for(j=0; j<nVertices[i]; ++j)
		{
			shapeArray[i][j] = (float*) realloc(vertices[i][j], nCols * sizeof(float));
		}
	}
	return shapeArray;
}

GLint ***reallocate_memoryi(int nShapes, int nRows, int nCols)
{
	int ***shapeArray;
	shapeArray = (int***) realloc(facets, nShapes * sizeof(int**));
	for(i=0; i<nShapes; ++i)
	{
		shapeArray[i] = (int**) realloc(facets[i], nFacets[i] * sizeof(int));
		for(j=0; j<nFacets[i]; ++j)
		{
			shapeArray[i][j] = (int*) realloc(facets[i][j], nCols * sizeof(int));
		}
	}
	return shapeArray;
}


int main(int argc, char **argv)
{
	loadShape("test_pyramid.ASE", 0);//won't work this way round as pyramid requires less vertices/facet windings
	loadShape("test_sphere.ASE", 1);

	//loadShape("test_sphere.ASE", 0);
	//loadShape("test_pyramid.ASE", 1);//does work this way round

	//loadShape("test_box.ASE", 2); as soon as a third shape is added it also crashes


	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
	glutCreateWindow("TEST READING ASCII FILE");
	glutDisplayFunc(display);
	glutReshapeFunc(adjustSize);
	//glutMouseFunc(mouse);
	glutIdleFunc(animate);
	glEnable(GL_DEPTH_TEST); //enable hidden surface removal
	glutMainLoop();
	return 0;
}

void display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glRotatef(rquad, 1.0f, 1.0f, 1.0f);
	glColor3f(0.0f, 0.0f, 1.0f);
	shape(0);
	shape(1);
	//shape(2);
	glFlush();
	glutSwapBuffers();
}

void adjustSize(GLsizei w, GLsizei h)
{
	glViewport(0,0,w,h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	if (w <= h)
		glOrtho(-30.0, 30.0, -30.0 * (GLfloat) h / (GLfloat) w,30.0 * (GLfloat) h / (GLfloat) w, -100.0, 100.0);
	else if (h <= w)
		glOrtho(-30.0 * (GLfloat) w / (GLfloat) h, 30.0 * (GLfloat) w / (GLfloat) h, -30.0, 30.0, -100.0, 100.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void animate()
{
	rquad -= 0.15f;
	display();
}

void polygon(int a, int b, int c, int shapeNum)
{
	//for(i = 0; i < numShapes; ++i)
	//{
	//glBegin(GL_POLYGON);
	glBegin(GL_LINE_LOOP);
		glColor3f(0.0,0.0,1.0);
		//glColor3fv(colors[a]);
		glVertex3fv(vertices[shapeNum][a]);
		//glColor3fv(colors[b]);
		glVertex3fv(vertices[shapeNum][b]);
		//glColor3fv(colors[c]);
		glVertex3fv(vertices[shapeNum][c]);
	glEnd();
	//}
}

void shape(int shapeNum)
{
		for(row = 0; row < nFacets[shapeNum]; ++row)
		{
			polygon(facets[shapeNum][row][0], facets[shapeNum][row][1], facets[shapeNum][row][2], shapeNum);
		}
}

void loadShape(char filename[20], int shapeNum)
{
	char *fullFile = readFile(fopen (filename, "rb"));
	char *pch;
	int v, f;
	char *delim = " *\x9\xA\xD\\:";

	pch = strtok(fullFile, delim);
	while(pch != NULL)
	{
		if(strcmp(pch, "MESH_NUMVERTEX") == NULL)
		{
			numVertices = atoi(strtok (NULL, delim));
			printf("numVertices%d\n", numVertices);
			//totalVertices += numVertices;
			nVertices[shapeNum] = numVertices;
			if(shapeNum == 0)
				vertices = allocate_memoryf(numShapes, nVertices[shapeNum], numCols);
			else
				vertices = reallocate_memoryf(numShapes, numVertices, numCols);
		}

		if(strcmp(pch, "MESH_NUMFACES") == NULL)
		{
			numFacets = atoi(strtok (NULL, delim));
			printf("numFacets%d\n", numFacets);
			//totalFacets += numFacets;
			nFacets[shapeNum] = numFacets;
			if(shapeNum == 0)
				facets = allocate_memoryi(numShapes, nFacets[shapeNum], numCols);
			else
				facets = reallocate_memoryi(numShapes, numFacets, numCols);
		}

		if(strcmp(pch, "MESH_VERTEX") == NULL)
		{
			v = atoi(strtok (NULL, delim));
			printf("v=%d\n", v);
			for(i = 0; i < numCols; ++i)
			{
				vertices[shapeNum][v][i] = atof(strtok (NULL, delim));
				printf("%f ", vertices[shapeNum][v][i]);
			}
			printf("\n");
		}

		if(strcmp(pch, "MESH_FACE") == NULL)
		{
			f = atoi(strtok (NULL, delim));
			strtok (NULL, delim);
			for(i = 0; i < numCols; ++i)
			{
				facets[shapeNum][f][i] = atoi(strtok (NULL, delim));
				printf("%d: ", facets[shapeNum][f][i]);
				strtok(NULL, delim);
			}
		}
		pch = strtok (NULL, delim);
	}
	free (fullFile);
}

char *readFile(FILE *sceneFile)
{
	long fileSize;
	char *fullFile;
	size_t result;

	if(sceneFile==NULL)
	{
		fputs ("Error 1", stderr);
		exit(1);
	}

	fseek(sceneFile, 0, SEEK_END);
	fileSize = ftell(sceneFile);
	rewind(sceneFile);

	fullFile = (char*)malloc(sizeof(char)*fileSize);
	if(fullFile == NULL)
	{
		fputs("Error 2", stderr);
		exit(2);
	}

	result = fread(fullFile,1,fileSize,sceneFile);
	if(result != fileSize)
	{
		fputs("Error 3", stderr);
		exit(3);
	}

	fclose(sceneFile);

	return fullFile;
}
Attachments
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "glut.h"
#include <GL\gl.h>
#include <GL\glu.h>

GLint ***facets; 
GLfloat ***vertices;

int nRows, nCols, row, col, i, j, k, numVertices, numFacets, shapeNum;
int numShapes = 2;
int numCols = 3;
GLfloat rquad;

//int totalVertices = 0;
//int totalFacets = 0;

int nFacets[10];
int nVertices[10];


GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},{1.0,0.0,1.0},{1.0,1.0,1.0},
{0.0,1.0,1.0}};

//All methods used
void display();
void adjustSize(GLsizei, GLsizei);
void animate();
//void mouse(int btn, int state, int x, int y);
void polygon(int a, int b, int c, int shapeNum);
void shape(int shapeNum);

void loadShape(char filename[20], int shapeNum);
char *readFile(FILE *sceneFile);


GLint ***allocate_memoryi(int nShapes, int nRows, int nCols)
{
	int ***shapeArray;
	shapeArray = (int***) malloc(nShapes * sizeof(int**));
	for(i=0; i<nShapes; ++i)
	{
		shapeArray[i] = (int**) malloc(nRows * sizeof(int));
		for(j=0; j<nRows; ++j)
		{
			shapeArray[i][j] = (int*) malloc(nCols * sizeof(int));
		}
	}
	return shapeArray;
}


GLfloat ***allocate_memoryf(int nShapes, int nRows, int nCols)
{
	float ***shapeArray;
	shapeArray = (float***) malloc(nShapes * sizeof(float**));
	for(i=0; i<nShapes; ++i)
	{
		shapeArray[i] = (float**) malloc(nRows * sizeof(float));
		for(j=0; j<nRows; ++j)
		{
			shapeArray[i][j] = (float*) malloc(nCols * sizeof(float));
		}
	}
	return shapeArray;
}


GLfloat ***reallocate_memoryf(int nShapes, int nRows, int nCols)
{
	float ***shapeArray;
	shapeArray = (float***) realloc(vertices, nShapes * sizeof(float**));
	for(i=0; i<nShapes; ++i)
	{
		shapeArray[i] = (float**) realloc(vertices[i], nVertices[i] * sizeof(float));
		for(j=0; j<nVertices[i]; ++j)
		{
			shapeArray[i][j] = (float*) realloc(vertices[i][j], nCols * sizeof(float));
		}
	}
	return shapeArray;
}

GLint ***reallocate_memoryi(int nShapes, int nRows, int nCols)
{
	int ***shapeArray;
	shapeArray = (int***) realloc(facets, nShapes * sizeof(int**));
	for(i=0; i<nShapes; ++i)
	{
		shapeArray[i] = (int**) realloc(facets[i], nFacets[i] * sizeof(int));
		for(j=0; j<nFacets[i]; ++j)
		{
			shapeArray[i][j] = (int*) realloc(facets[i][j], nCols * sizeof(int));
		}
	}
	return shapeArray;
}


int main(int argc, char **argv)
{
	loadShape("test_pyramid.ASE", 0);//won't work this way round as pyramid requires less vertices/facet windings
	loadShape("test_sphere.ASE", 1);

	//loadShape("test_sphere.ASE", 0);
	//loadShape("test_pyramid.ASE", 1);//does work this way round

	//loadShape("test_box.ASE", 2); as soon as a third shape is added it also crashes


	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
	glutCreateWindow("TEST READING ASCII FILE");
	glutDisplayFunc(display);
	glutReshapeFunc(adjustSize);
	//glutMouseFunc(mouse);
	glutIdleFunc(animate);
	glEnable(GL_DEPTH_TEST); //enable hidden surface removal
	glutMainLoop();
	return 0;
}

void display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glRotatef(rquad, 1.0f, 1.0f, 1.0f);
	glColor3f(0.0f, 0.0f, 1.0f);
	shape(0);
	shape(1);
	//shape(2);
	glFlush();
	glutSwapBuffers();
}

void adjustSize(GLsizei w, GLsizei h)
{
	glViewport(0,0,w,h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	if (w <= h)
		glOrtho(-30.0, 30.0, -30.0 * (GLfloat) h / (GLfloat) w,30.0 * (GLfloat) h / (GLfloat) w, -100.0, 100.0);
	else if (h <= w)
		glOrtho(-30.0 * (GLfloat) w / (GLfloat) h, 30.0 * (GLfloat) w / (GLfloat) h, -30.0, 30.0, -100.0, 100.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void animate()
{
	rquad -= 0.15f;
	display();
}

void polygon(int a, int b, int c, int shapeNum)
{
	//for(i = 0; i < numShapes; ++i)
	//{
	//glBegin(GL_POLYGON);
	glBegin(GL_LINE_LOOP);
		glColor3f(0.0,0.0,1.0);
		//glColor3fv(colors[a]);
		glVertex3fv(vertices[shapeNum][a]);
		//glColor3fv(colors[b]);
		glVertex3fv(vertices[shapeNum][b]);
		//glColor3fv(colors[c]);
		glVertex3fv(vertices[shapeNum][c]);
	glEnd();
	//}
}

void shape(int shapeNum)
{
		for(row = 0; row < nFacets[shapeNum]; ++row)
		{
			polygon(facets[shapeNum][row][0], facets[shapeNum][row][1], facets[shapeNum][row][2], shapeNum);
		}
}

void loadShape(char filename[20], int shapeNum)
{
	char *fullFile = readFile(fopen (filename, "rb"));
	char *pch;
	int v, f;
	char *delim = " *\x9\xA\xD\\:";

	pch = strtok(fullFile, delim);
	while(pch != NULL)
	{
		if(strcmp(pch, "MESH_NUMVERTEX") == NULL)
		{
			numVertices = atoi(strtok (NULL, delim));
			printf("numVertices%d\n", numVertices);
			//totalVertices += numVertices;
			nVertices[shapeNum] = numVertices;
			if(shapeNum == 0)
				vertices = allocate_memoryf(numShapes, nVertices[shapeNum], numCols);
			else
				vertices = reallocate_memoryf(numShapes, numVertices, numCols);
		}

		if(strcmp(pch, "MESH_NUMFACES") == NULL)
		{
			numFacets = atoi(strtok (NULL, delim));
			printf("numFacets%d\n", numFacets);
			//totalFacets += numFacets;
			nFacets[shapeNum] = numFacets;
			if(shapeNum == 0)
				facets = allocate_memoryi(numShapes, nFacets[shapeNum], numCols);
			else
				facets = reallocate_memoryi(numShapes, numFacets, numCols);
		}

		if(strcmp(pch, "MESH_VERTEX") == NULL)
		{
			v = atoi(strtok (NULL, delim));
			printf("v=%d\n", v);
			for(i = 0; i < numCols; ++i)
			{
				vertices[shapeNum][v][i] = atof(strtok (NULL, delim));
				printf("%f ", vertices[shapeNum][v][i]);
			}
			printf("\n");
		}

		if(strcmp(pch, "MESH_FACE") == NULL)
		{
			f = atoi(strtok (NULL, delim));
			strtok (NULL, delim);
			for(i = 0; i < numCols; ++i)
			{
				facets[shapeNum][f][i] = atoi(strtok (NULL, delim));
				printf("%d: ", facets[shapeNum][f][i]);
				strtok(NULL, delim);
			}
		}
		pch = strtok (NULL, delim);
	}
	free (fullFile);
}

char *readFile(FILE *sceneFile)
{
	long fileSize;
	char *fullFile;
	size_t result;

	if(sceneFile==NULL)
	{
		fputs ("Error 1", stderr);
		exit(1);
	}

	fseek(sceneFile, 0, SEEK_END);
	fileSize = ftell(sceneFile);
	rewind(sceneFile);

	fullFile = (char*)malloc(sizeof(char)*fileSize);
	if(fullFile == NULL)
	{
		fputs("Error 2", stderr);
		exit(2);
	}

	result = fread(fullFile,1,fileSize,sceneFile);
	if(result != fileSize)
	{
		fputs("Error 3", stderr);
		exit(3);
	}

	fclose(sceneFile);

	return fullFile;
}
*3DSMAX_ASCIIEXPORT	200
*COMMENT "AsciiExport Version  2.00 - Wed Mar 11 19:27:54 2009"
*SCENE {
	*SCENE_FILENAME ""
	*SCENE_FIRSTFRAME 0
	*SCENE_LASTFRAME 100
	*SCENE_FRAMESPEED 30
	*SCENE_TICKSPERFRAME 160
	*SCENE_BACKGROUND_STATIC 0.0000	0.0000	0.0000
	*SCENE_AMBIENT_STATIC 0.0000	0.0000	0.0000
}
*MATERIAL_LIST {
	*MATERIAL_COUNT 0
}
*GEOMOBJECT {
	*NODE_NAME "Box01"
	*NODE_TM {
		*NODE_NAME "Box01"
		*INHERIT_POS 0 0 0
		*INHERIT_ROT 0 0 0
		*INHERIT_SCL 0 0 0
		*TM_ROW0 1.0000	0.0000	0.0000
		*TM_ROW1 0.0000	1.0000	0.0000
		*TM_ROW2 0.0000	0.0000	1.0000
		*TM_ROW3 12.8104	4.7549	0.0000
		*TM_POS 12.8104	4.7549	0.0000
		*TM_ROTAXIS 0.0000	0.0000	0.0000
		*TM_ROTANGLE 0.0000
		*TM_SCALE 1.0000	1.0000	1.0000
		*TM_SCALEAXIS 0.0000	0.0000	0.0000
		*TM_SCALEAXISANG 0.0000
	}
	*MESH {
		*TIMEVALUE 0
		*MESH_NUMVERTEX 8
		*MESH_NUMFACES 12
		*MESH_VERTEX_LIST {
			*MESH_VERTEX    0	-15.5936	-30.1129	0.0000
			*MESH_VERTEX    1	41.2144	-30.1129	0.0000
			*MESH_VERTEX    2	-15.5936	39.6227	0.0000
			*MESH_VERTEX    3	41.2144	39.6227	0.0000
			*MESH_VERTEX    4	-15.5936	-30.1129	36.0100
			*MESH_VERTEX    5	41.2144	-30.1129	36.0100
			*MESH_VERTEX    6	-15.5936	39.6227	36.0100
			*MESH_VERTEX    7	41.2144	39.6227	36.0100
		}
		*MESH_FACE_LIST {
			*MESH_FACE    0:    A:    0 B:    2 C:    3 AB:    1 BC:    1 CA:    0	 *MESH_SMOOTHING 2 	*MESH_MTLID 1
			*MESH_FACE    1:    A:    3 B:    1 C:    0 AB:    1 BC:    1 CA:    0	 *MESH_SMOOTHING 2 	*MESH_MTLID 1
			*MESH_FACE    2:    A:    4 B:    5 C:    7 AB:    1 BC:    1 CA:    0	 *MESH_SMOOTHING 3 	*MESH_MTLID 0
			*MESH_FACE    3:    A:    7 B:    6 C:    4 AB:    1 BC:    1 CA:    0	 *MESH_SMOOTHING 3 	*MESH_MTLID 0
			*MESH_FACE    4:    A:    0 B:    1 C:    5 AB:    1 BC:    1 CA:    0	 *MESH_SMOOTHING 4 	*MESH_MTLID 4
			*MESH_FACE    5:    A:    5 B:    4 C:    0 AB:    1 BC:    1 CA:    0	 *MESH_SMOOTHING 4 	*MESH_MTLID 4
			*MESH_FACE    6:    A:    1 B:    3 C:    7 AB:    1 BC:    1 CA:    0	 *MESH_SMOOTHING 5 	*MESH_MTLID 3
			*MESH_FACE    7:    A:    7 B:    5 C:    1 AB:    1 BC:    1 CA:    0	 *MESH_SMOOTHING 5 	*MESH_MTLID 3
			*MESH_FACE    8:    A:    3 B:    2 C:    6 AB:    1 BC:    1 CA:    0	 *MESH_SMOOTHING 6 	*MESH_MTLID 5
			*MESH_FACE    9:    A:    6 B:    7 C:    3 AB:    1 BC:    1 CA:    0	 *MESH_SMOOTHING 6 	*MESH_MTLID 5
			*MESH_FACE   10:    A:    2 B:    0 C:    4 AB:    1 BC:    1 CA:    0	 *MESH_SMOOTHING 7 	*MESH_MTLID 2
			*MESH_FACE   11:    A:    4 B:    6 C:    2 AB:    1 BC:    1 CA:    0	 *MESH_SMOOTHING 7 	*MESH_MTLID 2
		}
	}
	*PROP_MOTIONBLUR 0
	*PROP_CASTSHADOW 1
	*PROP_RECVSHADOW 1
	*WIREFRAME_COLOR 0.1098	0.3490	0.6941
}
*3DSMAX_ASCIIEXPORT	200
*COMMENT "AsciiExport Version  2.00 - Sun Mar 15 01:29:32 2009"
*SCENE {
	*SCENE_FILENAME ""
	*SCENE_FIRSTFRAME 0
	*SCENE_LASTFRAME 100
	*SCENE_FRAMESPEED 30
	*SCENE_TICKSPERFRAME 160
	*SCENE_BACKGROUND_STATIC 0.0000	0.0000	0.0000
	*SCENE_AMBIENT_STATIC 0.0000	0.0000	0.0000
}
*MATERIAL_LIST {
	*MATERIAL_COUNT 0
}
*GEOMOBJECT {
	*NODE_NAME "Pyramid01"
	*NODE_TM {
		*NODE_NAME "Pyramid01"
		*INHERIT_POS 0 0 0
		*INHERIT_ROT 0 0 0
		*INHERIT_SCL 0 0 0
		*TM_ROW0 1.0000	0.0000	0.0000
		*TM_ROW1 0.0000	1.0000	0.0000
		*TM_ROW2 0.0000	0.0000	1.0000
		*TM_ROW3 4.7193	-2.0914	0.0050
		*TM_POS 4.7193	-2.0914	0.0050
		*TM_ROTAXIS 0.0000	0.0000	0.0000
		*TM_ROTANGLE 0.0000
		*TM_SCALE 1.0000	1.0000	1.0000
		*TM_SCALEAXIS 0.0000	0.0000	0.0000
		*TM_SCALEAXISANG 0.0000
	}
	*MESH {
		*TIMEVALUE 0
		*MESH_NUMVERTEX 6
		*MESH_NUMFACES 8
		*MESH_VERTEX_LIST {
			*MESH_VERTEX    0	4.7193	-2.0914	37.3576
			*MESH_VERTEX    1	-16.7196	-37.1973	0.0050
			*MESH_VERTEX    2	26.1582	-37.1973	0.0050
			*MESH_VERTEX    3	26.1582	33.0146	0.0050
			*MESH_VERTEX    4	-16.7196	33.0146	0.0050
			*MESH_VERTEX    5	4.7193	-2.0914	0.0050
		}
		*MESH_FACE_LIST {
			*MESH_FACE    0:    A:    0 B:    1 C:    2 AB:    1 BC:    1 CA:    1	 *MESH_SMOOTHING 3 	*MESH_MTLID 0
			*MESH_FACE    1:    A:    0 B:    2 C:    3 AB:    1 BC:    1 CA:    1	 *MESH_SMOOTHING 2 	*MESH_MTLID 0
			*MESH_FACE    2:    A:    0 B:    3 C:    4 AB:    1 BC:    1 CA:    1	 *MESH_SMOOTHING 5 	*MESH_MTLID 0
			*MESH_FACE    3:    A:    0 B:    4 C:    1 AB:    1 BC:    1 CA:    1	 *MESH_SMOOTHING 6 	*MESH_MTLID 0
			*MESH_FACE    4:    A:    1 B:    5 C:    2 AB:    1 BC:    1 CA:    1	 *MESH_SMOOTHING 4 	*MESH_MTLID 0
			*MESH_FACE    5:    A:    2 B:    5 C:    3 AB:    1 BC:    1 CA:    1	 *MESH_SMOOTHING 4 	*MESH_MTLID 0
			*MESH_FACE    6:    A:    3 B:    5 C:    4 AB:    1 BC:    1 CA:    1	 *MESH_SMOOTHING 4 	*MESH_MTLID 0
			*MESH_FACE    7:    A:    4 B:    5 C:    1 AB:    1 BC:    1 CA:    1	 *MESH_SMOOTHING 4 	*MESH_MTLID 0
		}
	}
	*PROP_MOTIONBLUR 0
	*PROP_CASTSHADOW 1
	*PROP_RECVSHADOW 1
	*WIREFRAME_COLOR 0.1098	0.1098	0.6941
}
*3DSMAX_ASCIIEXPORT	200
*COMMENT "AsciiExport Version  2.00 - Sun Mar 15 01:27:19 2009"
*SCENE {
	*SCENE_FILENAME ""
	*SCENE_FIRSTFRAME 0
	*SCENE_LASTFRAME 100
	*SCENE_FRAMESPEED 30
	*SCENE_TICKSPERFRAME 160
	*SCENE_BACKGROUND_STATIC 0.0000	0.0000	0.0000
	*SCENE_AMBIENT_STATIC 0.0000	0.0000	0.0000
}
*MATERIAL_LIST {
	*MATERIAL_COUNT 0
}
*GEOMOBJECT {
	*NODE_NAME "Sphere01"
	*NODE_TM {
		*NODE_NAME "Sphere01"
		*INHERIT_POS 0 0 0
		*INHERIT_ROT 0 0 0
		*INHERIT_SCL 0 0 0
		*TM_ROW0 1.0000	0.0000	0.0000
		*TM_ROW1 0.0000	1.0000	0.0000
		*TM_ROW2 0.0000	0.0000	1.0000
		*TM_ROW3 -2.8387	3.3837	18.6647
		*TM_POS -2.8387	3.3837	18.6647
		*TM_ROTAXIS 0.0000	0.0000	0.0000
		*TM_ROTANGLE 0.0000
		*TM_SCALE 1.0000	1.0000	1.0000
		*TM_SCALEAXIS 0.0000	0.0000	0.0000
		*TM_SCALEAXISANG 0.0000
	}
	*MESH {
		*TIMEVALUE 0
		*MESH_NUMVERTEX 482
		*MESH_NUMFACES 960
		*MESH_VERTEX_LIST {
			*MESH_VERTEX    0	-2.8387	3.3837	36.4180
			*MESH_VERTEX    1	-2.8387	6.8472	36.0769
			*MESH_VERTEX    2	-3.5144	6.7807	36.0769
			*MESH_VERTEX    3	-4.1641	6.5836	36.0769
			*MESH_VERTEX    4	-4.7629	6.2635	36.0769
			*MESH_VERTEX    5	-5.2878	5.8328	36.0769
			*MESH_VERTEX    6	-5.7185	5.3079	36.0769
			*MESH_VERTEX    7	-6.0386	4.7091	36.0769
			*MESH_VERTEX    8	-6.2356	4.0594	36.0769
			*MESH_VERTEX    9	-6.3022	3.3837	36.0769
			*MESH_VERTEX   10	-6.2356	2.7080	36.0769
			*MESH_VERTEX   11	-6.0385	2.0583	36.0769
			*MESH_VERTEX   12	-5.7185	1.4595	36.0769
			*MESH_VERTEX   13	-5.2878	0.9346	36.0769
			*MESH_VERTEX   14	-4.7629	0.5039	36.0769
			*MESH_VERTEX   15	-4.1641	0.1838	36.0769
			*MESH_VERTEX   16	-3.5144	-0.0132	36.0769
			*MESH_VERTEX   17	-2.8387	-0.0798	36.0769
			*MESH_VERTEX   18	-2.1630	-0.0132	36.0769
			*MESH_VERTEX   19	-1.5133	0.1838	36.0769
			*MESH_VERTEX   20	-0.9145	0.5039	36.0769
			*MESH_VERTEX   21	-0.3896	0.9346	36.0769
			*MESH_VERTEX   22	0.0411	1.4595	36.0769
			*MESH_VERTEX   23	0.3612	2.0583	36.0769
			*MESH_VERTEX   24	0.5583	2.7080	36.0769
			*MESH_VERTEX   25	0.6248	3.3837	36.0769
			*MESH_VERTEX   26	0.5583	4.0594	36.0769
			*MESH_VERTEX   27	0.3612	4.7091	36.0769
			*MESH_VERTEX   28	0.0411	5.3079	36.0769
			*MESH_VERTEX   29	-0.3896	5.8328	36.0769
			*MESH_VERTEX   30	-0.9145	6.2635	36.0769
			*MESH_VERTEX   31	-1.5133	6.5836	36.0769
			*MESH_VERTEX   32	-2.1630	6.7807	36.0769
			*MESH_VERTEX   33	-2.8387	10.1776	35.0666
			*MESH_VERTEX   34	-4.1641	10.0471	35.0666
			*MESH_VERTEX   35	-5.4386	9.6605	35.0666
			*MESH_VERTEX   36	-6.6132	9.0326	35.0666
			*MESH_VERTEX   37	-7.6427	8.1877	35.0666
			*MESH_VERTEX   38	-8.4876	7.1582	35.0666
			*MESH_VERTEX   39	-9.1154	5.9836	35.0666
			*MESH_VERTEX   40	-9.5021	4.7091	35.0666
			*MESH_VERTEX   41	-9.6326	3.3837	35.0666
			*MESH_VERTEX   42	-9.5021	2.0583	35.0666
			*MESH_VERTEX   43	-9.1154	0.7838	35.0666
			*MESH_VERTEX   44	-8.4876	-0.3908	35.0666
			*MESH_VERTEX   45	-7.6427	-1.4203	35.0666
			*MESH_VERTEX   46	-6.6132	-2.2652	35.0666
			*MESH_VERTEX   47	-5.4386	-2.8931	35.0666
			*MESH_VERTEX   48	-4.1641	-3.2797	35.0666
			*MESH_VERTEX   49	-2.8387	-3.4102	35.0666
			*MESH_VERTEX   50	-1.5132	-3.2797	35.0666
			*MESH_VERTEX   51	-0.2388	-2.8930	35.0666
			*MESH_VERTEX   52	0.9358	-2.2652	35.0666
			*MESH_VERTEX   53	1.9653	-1.4203	35.0666
			*MESH_VERTEX   54	2.8103	-0.3908	35.0666
			*MESH_VERTEX   55	3.4381	0.7838	35.0666
			*MESH_VERTEX   56	3.8247	2.0583	35.0666
			*MESH_VERTEX   57	3.9552	3.3837	35.0666
			*MESH_VERTEX   58	3.8247	4.7092	35.0666
			*MESH_VERTEX   59	3.4381	5.9836	35.0666
			*MESH_VERTEX   60	2.8102	7.1582	35.0666
			*MESH_VERTEX   61	1.9653	8.1877	35.0666
			*MESH_VERTEX   62	0.9358	9.0327	35.0666
			*MESH_VERTEX   63	-0.2388	9.6605	35.0666
			*MESH_VERTEX   64	-1.5133	10.0471	35.0666
			*MESH_VERTEX   65	-2.8387	13.2470	33.4261
			*MESH_VERTEX   66	-4.7629	13.0574	33.4261
			*MESH_VERTEX   67	-6.6132	12.4962	33.4261
			*MESH_VERTEX   68	-8.3184	11.5847	33.4261
			*MESH_VERTEX   69	-9.8130	10.3581	33.4261
			*MESH_VERTEX   70	-11.0397	8.8634	33.4261
			*MESH_VERTEX   71	-11.9511	7.1582	33.4261
			*MESH_VERTEX   72	-12.5124	5.3079	33.4261
			*MESH_VERTEX   73	-12.7019	3.3837	33.4261
			*MESH_VERTEX   74	-12.5124	1.4595	33.4261
			*MESH_VERTEX   75	-11.9511	-0.3908	33.4261
			*MESH_VERTEX   76	-11.0397	-2.0960	33.4261
			*MESH_VERTEX   77	-9.8130	-3.5907	33.4261
			*MESH_VERTEX   78	-8.3184	-4.8173	33.4261
			*MESH_VERTEX   79	-6.6132	-5.7287	33.4261
			*MESH_VERTEX   80	-4.7629	-6.2900	33.4261
			*MESH_VERTEX   81	-2.8387	-6.4795	33.4261
			*MESH_VERTEX   82	-0.9145	-6.2900	33.4261
			*MESH_VERTEX   83	0.9358	-5.7287	33.4261
			*MESH_VERTEX   84	2.6410	-4.8173	33.4261
			*MESH_VERTEX   85	4.1357	-3.5906	33.4261
			*MESH_VERTEX   86	5.3623	-2.0960	33.4261
			*MESH_VERTEX   87	6.2738	-0.3908	33.4261
			*MESH_VERTEX   88	6.8350	1.4595	33.4261
			*MESH_VERTEX   89	7.0246	3.3837	33.4261
			*MESH_VERTEX   90	6.8350	5.3080	33.4261
			*MESH_VERTEX   91	6.2738	7.1582	33.4261
			*MESH_VERTEX   92	5.3623	8.8635	33.4261
			*MESH_VERTEX   93	4.1357	10.3581	33.4261
			*MESH_VERTEX   94	2.6410	11.5847	33.4261
			*MESH_VERTEX   95	0.9358	12.4962	33.4261
			*MESH_VERTEX   96	-0.9145	13.0574	33.4261
			*MESH_VERTEX   97	-2.8387	15.9372	31.2182
			*MESH_VERTEX   98	-5.2878	15.6960	31.2182
			*MESH_VERTEX   99	-7.6427	14.9817	31.2182
			*MESH_VERTEX  100	-9.8130	13.8216	31.2182
			*MESH_VERTEX  101	-11.7154	12.2604	31.2182
			*MESH_VERTEX  102	-13.2766	10.3581	31.2182
			*MESH_VERTEX  103	-14.4366	8.1877	31.2182
			*MESH_VERTEX  104	-15.1510	5.8328	31.2182
			*MESH_VERTEX  105	-15.3922	3.3837	31.2182
			*MESH_VERTEX  106	-15.1510	0.9346	31.2182
			*MESH_VERTEX  107	-14.4366	-1.4203	31.2182
			*MESH_VERTEX  108	-13.2766	-3.5907	31.2182
			*MESH_VERTEX  109	-11.7154	-5.4930	31.2182
			*MESH_VERTEX  110	-9.8130	-7.0542	31.2182
			*MESH_VERTEX  111	-7.6427	-8.2142	31.2182
			*MESH_VERTEX  112	-5.2877	-8.9286	31.2182
			*MESH_VERTEX  113	-2.8387	-9.1698	31.2182
			*MESH_VERTEX  114	-0.3896	-8.9286	31.2182
			*MESH_VERTEX  115	1.9654	-8.2142	31.2182
			*MESH_VERTEX  116	4.1357	-7.0542	31.2182
			*MESH_VERTEX  117	6.0380	-5.4930	31.2182
			*MESH_VERTEX  118	7.5992	-3.5906	31.2182
			*MESH_VERTEX  119	8.7593	-1.4203	31.2182
			*MESH_VERTEX  120	9.4736	0.9347	31.2182
			*MESH_VERTEX  121	9.7148	3.3837	31.2182
			*MESH_VERTEX  122	9.4736	5.8328	31.2182
			*MESH_VERTEX  123	8.7593	8.1878	31.2182
			*MESH_VERTEX  124	7.5992	10.3581	31.2182
			*MESH_VERTEX  125	6.0380	12.2604	31.2182
			*MESH_VERTEX  126	4.1357	13.8216	31.2182
			*MESH_VERTEX  127	1.9653	14.9817	31.2182
			*MESH_VERTEX  128	-0.3896	15.6960	31.2182
			*MESH_VERTEX  129	-2.8387	18.1451	28.5279
			*MESH_VERTEX  130	-5.7185	17.8615	28.5279
			*MESH_VERTEX  131	-8.4876	17.0215	28.5279
			*MESH_VERTEX  132	-11.0397	15.6574	28.5279
			*MESH_VERTEX  133	-13.2766	13.8216	28.5279
			*MESH_VERTEX  134	-15.1123	11.5847	28.5279
			*MESH_VERTEX  135	-16.4764	9.0326	28.5279
			*MESH_VERTEX  136	-17.3164	6.2635	28.5279
			*MESH_VERTEX  137	-17.6001	3.3837	28.5279
			*MESH_VERTEX  138	-17.3164	0.5039	28.5279
			*MESH_VERTEX  139	-16.4764	-2.2652	28.5279
			*MESH_VERTEX  140	-15.1123	-4.8173	28.5279
			*MESH_VERTEX  141	-13.2766	-7.0542	28.5279
			*MESH_VERTEX  142	-11.0397	-8.8899	28.5279
			*MESH_VERTEX  143	-8.4876	-10.2540	28.5279
			*MESH_VERTEX  144	-5.7185	-11.0940	28.5279
			*MESH_VERTEX  145	-2.8387	-11.3777	28.5279
			*MESH_VERTEX  146	0.0411	-11.0940	28.5279
			*MESH_VERTEX  147	2.8103	-10.2540	28.5279
			*MESH_VERTEX  148	5.3623	-8.8899	28.5279
			*MESH_VERTEX  149	7.5992	-7.0541	28.5279
			*MESH_VERTEX  150	9.4350	-4.8173	28.5279
			*MESH_VERTEX  151	10.7991	-2.2652	28.5279
			*MESH_VERTEX  152	11.6391	0.5039	28.5279
			*MESH_VERTEX  153	11.9227	3.3837	28.5279
			*MESH_VERTEX  154	11.6391	6.2635	28.5279
			*MESH_VERTEX  155	10.7990	9.0327	28.5279
			*MESH_VERTEX  156	9.4349	11.5847	28.5279
			*MESH_VERTEX  157	7.5992	13.8216	28.5279
			*MESH_VERTEX  158	5.3623	15.6574	28.5279
			*MESH_VERTEX  159	2.8102	17.0215	28.5279
			*MESH_VERTEX  160	0.0411	17.8615	28.5279
			*MESH_VERTEX  161	-2.8387	19.7857	25.4586
			*MESH_VERTEX  162	-6.0386	19.4705	25.4586
			*MESH_VERTEX  163	-9.1154	18.5372	25.4586
			*MESH_VERTEX  164	-11.9511	17.0215	25.4586
			*MESH_VERTEX  165	-14.4366	14.9817	25.4586
			*MESH_VERTEX  166	-16.4764	12.4962	25.4586
			*MESH_VERTEX  167	-17.9921	9.6605	25.4586
			*MESH_VERTEX  168	-18.9255	6.5836	25.4586
			*MESH_VERTEX  169	-19.2407	3.3837	25.4586
			*MESH_VERTEX  170	-18.9255	0.1838	25.4586
			*MESH_VERTEX  171	-17.9921	-2.8931	25.4586
			*MESH_VERTEX  172	-16.4764	-5.7287	25.4586
			*MESH_VERTEX  173	-14.4366	-8.2142	25.4586
			*MESH_VERTEX  174	-11.9511	-10.2540	25.4586
			*MESH_VERTEX  175	-9.1154	-11.7697	25.4586
			*MESH_VERTEX  176	-6.0385	-12.7031	25.4586
			*MESH_VERTEX  177	-2.8387	-13.0183	25.4586
			*MESH_VERTEX  178	0.3612	-12.7031	25.4586
			*MESH_VERTEX  179	3.4381	-11.7697	25.4586
			*MESH_VERTEX  180	6.2738	-10.2540	25.4586
			*MESH_VERTEX  181	8.7593	-8.2142	25.4586
			*MESH_VERTEX  182	10.7991	-5.7287	25.4586
			*MESH_VERTEX  183	12.3148	-2.8930	25.4586
			*MESH_VERTEX  184	13.2481	0.1839	25.4586
			*MESH_VERTEX  185	13.5633	3.3837	25.4586
			*MESH_VERTEX  186	13.2481	6.5836	25.4586
			*MESH_VERTEX  187	12.3148	9.6605	25.4586
			*MESH_VERTEX  188	10.7990	12.4962	25.4586
			*MESH_VERTEX  189	8.7592	14.9817	25.4586
			*MESH_VERTEX  190	6.2737	17.0215	25.4586
			*MESH_VERTEX  191	3.4380	18.5372	25.4586
			*MESH_VERTEX  192	0.3611	19.4705	25.4586
			*MESH_VERTEX  193	-2.8387	20.7960	22.1282
			*MESH_VERTEX  194	-6.2356	20.4614	22.1282
			*MESH_VERTEX  195	-9.5021	19.4705	22.1282
			*MESH_VERTEX  196	-12.5124	17.8615	22.1282
			*MESH_VERTEX  197	-15.1510	15.6960	22.1282
			*MESH_VERTEX  198	-17.3164	13.0574	22.1282
			*MESH_VERTEX  199	-18.9255	10.0471	22.1282
			*MESH_VERTEX  200	-19.9164	6.7807	22.1282
			*MESH_VERTEX  201	-20.2509	3.3837	22.1282
			*MESH_VERTEX  202	-19.9164	-0.0133	22.1282
			*MESH_VERTEX  203	-18.9255	-3.2797	22.1282
			*MESH_VERTEX  204	-17

I only looked at allocate_memoryi and reallocate_memoryi. Any problems with them probably also exist in the "f" models. Compare the code below with the original carefully to see the differences.

GLint ***allocate_memoryi(int nShapes, int nRows, int nCols)
{
	GLint ***shapeArray;
	shapeArray = (GLint***) malloc(nShapes * sizeof(GLint**));
	for(i=0; i<nShapes; ++i)
	{
		shapeArray[i] = (GLint**) malloc(nRows * sizeof(GLint*));
		for(j=0; j<nRows; ++j)
		{
			shapeArray[i][j] = (GLint*) malloc(nCols * sizeof(GLint));
		}
	}
	return shapeArray;
}

GLint ***reallocate_memoryi(int nShapes, int nRows, int nCols)
{
	GLint ***shapeArray;
	shapeArray = (GLint***) realloc(facets, nShapes * sizeof(GLint**));
	for(i=0; i<nShapes; ++i)
	{
		shapeArray[i] = (GLint**) realloc(shapeArray[i], nRows * sizeof(GLint*));
		for(j=0; j<nRows[i]; ++j)
		{
			shapeArray[i][j] = (GLint*) realloc(shapeArray[i][j], nCols * sizeof(GLint));
		}
	}
	return shapeArray;
}

Also, you really shouldn't be using all those globals. It's just lazy, and as your program gets bigger it will come back to haunt you.

I've tried adjusting my code as you suggested Nucleon but it still isn't running.

I get this error message when ever I run it with more than two shapes being loaded:

Unhandled exception at 0x60d912b4 (msvcr90d.dll) in Final_3d_read.exe: 0xC0000005: Access violation reading location 0xcdcdcdc8.

Also there is a bit of code that is highlighted in the dbgheap.c code that is produced:

/*---------------------------------------------------------------------------
 *
 * Memory management
 *
 --------------------------------------------------------------------------*/

/***
*static int CheckBytes() - verify byte range set to proper value
*
*Purpose:
*       verify byte range set to proper value
*
*Entry:
*       unsigned char *pb       - pointer to start of byte range
*       unsigned char bCheck    - value byte range should be set to
*       size_t nSize            - size of byte range to be checked
*
*Return:
*       TRUE - if all bytes in range equal bcheck
*       FALSE otherwise
*
*******************************************************************************/
extern "C" static int __cdecl CheckBytes(
        unsigned char * pb,
        unsigned char bCheck,
        size_t nSize
        )
{
        int bOkay = TRUE;
        while (nSize--)
        {
            if (*pb++ != bCheck)   //this line is highlighted with an arrow
            {
/* Internal error report is just noise; calling functions all report results - JWM */
/*                _RPT3(_CRT_WARN, "memory check error at 0x%p = 0x%02X, should be 0x%02X.\n", */
/*                    (BYTE *)(pb-1),*(pb-1), bCheck); */
                bOkay = FALSE;
            }
        }
        return bOkay;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You're right. I wasn't thinking clearly with that last piece of sh..., I mean code.

Since you do not seem to need the previous data, you may as well just free the memory and allocate it again with your allocate_memory function. So just free it like this before allocating it again.

void release_memoryi(GLint*** facets, int nShapes, int nRows) {
    int i, j;
    for(i = 0; i < nShapes; ++i) {
        for(j = 0; j < nRows; ++j) free(facets[i][j]);
        free(facets[i]);
    }
    free(facets);
}

Hmmm...it seems to be reading in the object vertices and facet windings now (or so I think) but I get an error further down the line when I try to draw the objects. I get the error:

Unhandled exception at 0x00eb22c1 in Final_3d_read.exe: 0xC0000005: Access violation reading location 0x373f5454.

I assume that this means I am accessing the data in the array incorrectly. The error occurs as soon as the line 'glVertex3fv(vertices[shapeNum][a]);' is executed in the method below:

void polygon(int a, int b, int c, int shapeNum)
{
    //glBegin(GL_POLYGON);
    glBegin(GL_LINE_LOOP);
        glColor3f(0.0,0.0,1.0);
        //glColor3fv(colors[a]);
        glVertex3fv(vertices[shapeNum][a]);  //when it reaches this line
        //glColor3fv(colors[b]);
        glVertex3fv(vertices[shapeNum][b]);
        //glColor3fv(colors[c]);
        glVertex3fv(vertices[shapeNum][c]);
    glEnd();
}

This is the method that calls the above:

void shape(int shapeNum)
{
        for(row = 0; row < nFacets[shapeNum]; ++row)
        {
            polygon(facets[shapeNum][row][0], facets[shapeNum][row][1], facets[shapeNum][row][2], shapeNum);
        }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

I've looked further into it and I think that it could perhaps be my allocation method that still isn't qute right because after making the changes that you have suggested Nucleon, it allows me to draw one object still but as soon as I try drawing more than one I get the error mentioned in the previous post.

Ok I realised that I will need to read multiple objects from a single file so I've changed it quite a bit but it still works the same and I'm still getting the exact same error as before.

Code below:

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "glut.h"
#include <GL\gl.h>
#include <GL\glu.h>

int ***facets; 
float ***vertices;
int numObjects;

int numShapes = 1;
int numCols = 3;
GLfloat rquad;

GLfloat ud = 0.0;
GLfloat lr = 0.0;
GLfloat zz = 5.0;

GLfloat rShapeY = 0.0;
GLfloat rShapeX = 0.0;

int *nFacets;
int *nVertices;


GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},{1.0,0.0,1.0},{1.0,1.0,1.0},
{0.0,1.0,1.0}};

//All methods used
void display();
void adjustSize(GLsizei, GLsizei);
void animate();
//void mouse(int btn, int state, int x, int y);
void Keyboard(unsigned char key, int x, int y);
void SpecialKeys(int key, int x, int y);
void polygon(int a, int b, int c);
void shape();

void loadShape(char filename[20]);
char *readFile(FILE *sceneFile);


void countObjects(char fileName[20])
{
	char * fullFile = readFile(fopen (fileName, "rb"));
	char * pch;
	char * delim = " *\x9\xA\xD\\:";
	int i;

	numObjects = 0;

	pch = strtok(fullFile, delim);
	while(pch != NULL)//while NEOF
	{
		if(strcmp(pch, "GEOMOBJECT")==NULL)
			numObjects++;
		pch = strtok(NULL, delim);
	}
	vertices = (float ***) calloc(numObjects, sizeof(float **));
	facets = (int ***) calloc(numObjects, sizeof(int **));
	nVertices = (int *) calloc(numObjects, sizeof(int));
	nFacets = (int *) calloc(numObjects, sizeof(int));
}


void SetupRC()
    {
    // Light values and coordinates
    GLfloat  ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
    GLfloat  diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };

    glEnable(GL_DEPTH_TEST);	// Hidden surface removal
    glFrontFace(GL_CCW);		// Counter clock-wise polygons face out
    glEnable(GL_CULL_FACE);		// Do not calculate inside of jet

    // Enable lighting
    glEnable(GL_LIGHTING);

    // Setup and enable light 0
    glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
    glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
    glEnable(GL_LIGHT0);

    // Enable color tracking
    glEnable(GL_COLOR_MATERIAL);
	
    // Set Material properties to follow glColor values
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

    // Light blue background
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f );
    
    glEnable(GL_NORMALIZE);
    }


int main(int argc, char **argv)
{

	//loadShape("test_sphere.ASE", 0);
	loadShape("club.ASE");


	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
	glutCreateWindow("TEST READING ASCII FILE");
	glutDisplayFunc(display);
	glutReshapeFunc(adjustSize);
	//glutMouseFunc(mouse);
	glutKeyboardFunc(Keyboard);
	glutSpecialFunc(SpecialKeys);
	glutIdleFunc(animate);
	glEnable(GL_DEPTH_TEST); //enable hidden surface removal
	SetupRC();
	glutMainLoop();
	return 0;
}

void display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	gluLookAt(lr,ud,zz,0.0,0.0,0.0,0.0,1.0,0.0);
	glPushMatrix();
		glTranslatef(10.0f,0.0f,0.0f);
		shape();
	glPopMatrix();
	glPushMatrix();
		//glRotatef(rquad, 1.0f, 1.0f, 1.0f);
		glRotatef(rShapeY, 0.0f, 1.0f, 0.0f);
		glRotatef(rShapeX, 1.0f, 0.0f, 0.0f);
		shape();
	glPopMatrix();
	glColor3f(0.0f, 0.0f, 1.0f);
	glFlush();
	glutSwapBuffers();
}

void adjustSize(GLsizei w, GLsizei h)
{
	/*GLfloat fAspect;
	//Prevent a divide by zero
	if(h == 0)
		h = 1;*/
	//Set viewport to window dimensions
	glViewport(0,0,w,h);
	//fAspect = (GLfloat)w / (GLfloat)h;

	//Reset coordinate system
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	/*//Produce the perspective projection
	gluPerspective(60.0f, fAspect, 1.0f, 400.0f);*/

	if (w <= h)
		glOrtho(-30.0, 30.0, -30.0 * (GLfloat) h / (GLfloat) w,30.0 * (GLfloat) h / (GLfloat) w, -200.0, 100.0);
	else if (h <= w)
		glOrtho(-30.0 * (GLfloat) w / (GLfloat) h, 30.0 * (GLfloat) w / (GLfloat) h, -30.0, 30.0, -200.0, 100.0);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void animate()
{
	//rquad -= 0.15f;
	display();
}

void polygon(int a, int b, int c)
{
	int i;
	for(i = 0; i < numObjects; ++i)
	{
		//glBegin(GL_POLYGON);
		glBegin(GL_LINE_LOOP);
			glColor3ub(255,150,10);
			glVertex3fv(vertices[i][a]);             //This is where the program crashes
			glVertex3fv(vertices[i][b]);
			glVertex3fv(vertices[i][c]);
		glEnd();
	}
}

void shape()
{
	int i, j;
	for(i = 0; i < numObjects; ++i)
	{
		for(j = 0; j < nFacets[i]; ++j)
		{
			polygon(facets[i][j][0], facets[i][j][1], facets[i][j][2]);
		}
	}
}

void loadShape(char fileName[20])
{
	char *fullFile = readFile(fopen (fileName, "rb"));
	char *pch;
	int v, f;
	char *delim = " *\x9\xA\xD\\:";
	int i, nObj;

	countObjects(fileName);
	nObj = -1;

	pch = strtok(fullFile, delim);
	while(pch != NULL)
	{
		if(strcmp(pch, "GEOMOBJECT") == NULL)
			nObj++;

		if(strcmp(pch, "MESH_NUMVERTEX") == NULL)
		{
			nVertices[nObj] = atoi(strtok (NULL, delim));
			//printf("numVertices%d\n", numVertices);
			vertices[nObj] = (float **) calloc(nVertices[nObj], sizeof(float *));
			for(i = 0; i < nVertices[nObj]; ++i)
				vertices[nObj][i] = (float *) calloc(numCols, sizeof(float));
		}

		if(strcmp(pch, "MESH_NUMFACES") == NULL)
		{
			nFacets[nObj] = atoi(strtok (NULL, delim));
			//printf("numFacets%d\n", numFacets);
			facets[nObj] = (int **) calloc(nFacets[nObj], sizeof(int *));
			for(i = 0; i < nFacets[nObj]; ++i)
				facets[nObj][i] = (int *) calloc(numCols, sizeof(int));
		}

		if(strcmp(pch, "MESH_VERTEX") == NULL)
		{
			v = atoi(strtok (NULL, delim));
			printf("v=%d\n", v);
			for(i = 0; i < numCols; ++i)
			{
				vertices[nObj][v][i] = atof(strtok (NULL, delim));
				//printf("%f ", vertices[shapeNum][v][i]);
			}
			//printf("\n");
		}

		if(strcmp(pch, "MESH_FACE") == NULL)
		{
			f = atoi(strtok (NULL, delim));
			//strtok (NULL, delim);
			for(i = 0; i < numCols; ++i)
			{
				strtok(NULL, delim);
				facets[nObj][f][i] = atof(strtok (NULL, delim));
				//printf("%d: ", facets[shapeNum][f][i]);
			}
		}
		pch = strtok (NULL, delim);
	}
	free (fullFile);
}

char *readFile(FILE *sceneFile)
{
	long fileSize;
	char *fullFile;
	size_t result;

	if(sceneFile==NULL)
	{
		fputs ("Error 1", stderr);
		exit(1);
	}

	fseek(sceneFile, 0, SEEK_END);
	fileSize = ftell(sceneFile);
	rewind(sceneFile);

	fullFile = (char*)malloc(sizeof(char)*fileSize);
	if(fullFile == NULL)
	{
		fputs("Error 2", stderr);
		exit(2);
	}

	result = fread(fullFile,1,fileSize,sceneFile);
	if(result != fileSize)
	{
		fputs("Error 3", stderr);
		exit(3);
	}

	fclose(sceneFile);

	return fullFile;
}

void Keyboard(unsigned char key, int x, int y)
{
	switch(key)
	{
	case 'd':
		//ud += 0.1f;
		rShapeY += 1.0f;
		break;
	case 'a':
		//ud -= 0.1f;
		rShapeY -= 1.0f;
		break;
	case 's':
		//lr += 0.1f;
		rShapeX += 1.0f;
		break;
	case 'w':
		//lr += 0.1f;
		rShapeX -= 1.0f;
		break;
	}
}

void SpecialKeys(int key, int x, int y)
{
	switch(key)
	{
	case GLUT_KEY_UP:
		//rShapeY += 0.1f;
		zz += 1.0f;
		break;
	case GLUT_KEY_DOWN:
		//rShapeY -= 0.1f;
		zz -= 1.0f;
		break;
	case GLUT_KEY_LEFT:
		rShapeX += 0.1f;
		break;
	case GLUT_KEY_RIGHT:
		rShapeX -= 0.1f;
		break;
	}
}

It seems to be when I try and draw the objects (shape and polygon functions) that the problem occurs, probably because I'm accessing the data in my arrays incorrectly.

Unfortunately I cannot run your code right now, but in looking it over I noted the following.

The strangest thing (if I'm not mistaken) is the loop in polygon. Shouldn't you be passing in "i" from shape? I may just not understand what's going on here, but it seems weird to be looping through numObjects (in polygon) within a loop that is doing the same thing (in shape).

In readFile, since you're treating the whole file as a string, you should null-terminate it. So malloc an extra char's worth and be sure to set it to zero. (Or use calloc.)

The following are points that probably won't change anything but are still good practice.

You should really move the fopen() call into readFile and pass in the fileName. There's no advantage to doing it the way you're doing it. Also, you should probably read the file in text mode instead of binary mode. (Change "rb" to "r".)

In loadShape, since you've already read the entire file into fullFile before calling countObjects, you may as well pass fullFile to countObjects so it doesn't have to read the file again.

Your delimiters are better expressed like this: char * delim = " *:\t\n\r\\"; You should compare the result of strcmp to 0 (not NULL).

You got it without having to run it :)
As soon as I passed "i" from shape into the polygon method (and got rid of the additional unnecessary loop) it worked. I realised that I was passing the correct facet windings from 'shape' to 'polygon' but then using them in a loop for each object when I only wanted to use them once for that particular object.

Thanks so much for your help, I really appreciate it. Oh and just out of interest do you have a career in programming?

Anyway, thanks again

> do you have a career in programming?

No, I'm just a hobby programmer, but I've been programming since the age of 8 or 9 and am 25 now and have read quite a few university texts (algorithms, operating systems, AI) as well as Knuth. My job is VERY different but I love it! You probably make more money as a programmer, though.

This question has already been answered. Start a new discussion instead.