PDA

View Full Version : writing an object loader...



sandbucket
02-09-2014, 11:29 AM
ive only written the part of the object loader that loads the vertices into the GL_ELEMENT_ARRAY_BUFFER buffer.
here is my code for the function to load the elements into a buffer:

int drawObject(char *name)
{
FILE *fp;
char line[100];
unsigned int sizebuf = 0;
unsigned int offset = 0;
GLdouble xyz[3];
GLuint buf;
GLuint indices[] = {0, 1, 2};

glGenBuffers(1, &buf);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buf);

if ((fp = fopen(name, "r")) == NULL)
{
return FILECANTOPEN;
}

while (fgets(line, 100, fp) != NULL)
{
if (line[0] == 'v')
{
sizebuf += 3;
}

if (line[0] == 'f')
{
break;
}
}

fseek(fp, 0, SEEK_SET);

glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizei)(sizeof(GLdouble) * sizebuf), NULL, GL_STREAM_DRAW);

while (fgets(line, 100, fp) != NULL)
{
if (line[0] == 'v')
{
sscanf(line, "v %lf %lf %lf", &xyz[0], &xyz[1], &xyz[2]);
printf("X = %lf; Y = %lf; z = %lf.\n", xyz[0], xyz[1], xyz[2]);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, (GLint)(sizeof(GLdouble) * offset), (GLint)(sizeof(xyz)), xyz);
offset += 3;
}
}
printf("got here\n");
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (GLvoid *)indices);
printf("got here 2");
return SUCCESS;
}

it gets to the first "got here" but crashes saying "segmentation fault (core dumped). i keep this function in its own c file seperate from my initialization function, does that make any problems? any thing would be appreciated. thanks much
edit: the last lines of codeare just to test drawing something before i get to drawing each primitive in the object file primitive by primitive.