Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* GLMgroup: Structure that defines a group in a model.
- */
- typedef struct _GRRLIB_Group {
- char* name; /* name of this group */
- u32 numtriangles; /* number of triangles in this group */
- u32* triangles; /* array of triangle indices */
- u32 material; /* index to material for group */
- struct _GRRLIB_Group* next; /* pointer to next group in model */
- } GRRLIB_Group;
- /* GLMmodel: Structure that defines a model.
- */
- typedef struct {
- char* pathname; /* path to this model */
- char* mtllibname; /* name of the material library */
- u32 numvertices; /* number of vertices in model */
- guVector* vertices; /* array of vertices */
- u32 numnormals; /* number of normals in model */
- guVector* normals; /* array of normals */
- u32 numtexcoords; /* number of texcoords in model */
- guVector* texcoords; /* array of texture coordinates */
- u32 numfacetnorms; /* number of facetnorms in model */
- guVector* facetnorms; /* array of facetnorms */
- u32 numtriangles; /* number of triangles in model */
- //GLMtriangle* triangles; /* array of triangles */
- //u32 nummaterials; /* number of materials in model */
- //GLMmaterial* materials; /* array of materials */
- u32 numgroups; /* number of groups in model */
- GRRLIB_Group* groups; /* linked list of groups */
- //GLfloat position[3]; /* position of the model */
- } GRRLIB_Model;
- static void GRRLIB_FirstPass(GRRLIB_Model* model, FILE* file)
- {
- u32 numvertices; /* number of vertices in model */
- u32 numnormals; /* number of normals in model */
- u32 numtexcoords; /* number of texcoords in model */
- u32 numtriangles; /* number of triangles in model */
- GRRLIB_Group* group; /* current group */
- unsigned v, n, t;
- char buf[128];
- numvertices = numnormals = numtexcoords = numtriangles = 0;
- while(fscanf(file, "%s", buf) != EOF) {
- switch(buf[0]) {
- case '#': /* comment */
- fgets(buf, sizeof(buf), file);
- break;
- case 'v': /* v, vn, vt */
- switch(buf[1]) {
- case '\0': /* vertex */
- fgets(buf, sizeof(buf), file);
- numvertices++;
- break;
- case 'n': /* normal */
- fgets(buf, sizeof(buf), file);
- numnormals++;
- break;
- case 't': /* texcoord */
- fgets(buf, sizeof(buf), file);
- numtexcoords++;
- break;
- default:
- exit(1);
- break;
- }
- break;
- case 'm':
- fgets(buf, sizeof(buf), file);
- sscanf(buf, "%s %s", buf, buf);
- model->mtllibname = strdup(buf);
- //_glmReadMTL(model, buf);
- break;
- case 'u':
- fgets(buf, sizeof(buf), file);
- break;
- case 'g': /* group */
- fgets(buf, sizeof(buf), file);
- sscanf(buf, "%s", buf);
- //group = _glmAddGroup(model, buf);
- break;
- case 'f': /* face */
- v = n = t = 0;
- fscanf(file, "%s", buf);
- /* can be one of %d, %d//%d, %d/%d, %d/%d/%d %d//%d */
- if (strstr(buf, "//")) {
- /* v//n */
- sscanf(buf, "%d//%d", &v, &n);
- fscanf(file, "%d//%d", &v, &n);
- fscanf(file, "%d//%d", &v, &n);
- numtriangles++;
- /*
- group->numtriangles++;
- while(fscanf(file, "%d//%d", &v, &n) > 0) {
- numtriangles++;
- group->numtriangles++;
- }*/
- } else if (sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3) {
- /* v/t/n */
- fscanf(file, "%d/%d/%d", &v, &t, &n);
- fscanf(file, "%d/%d/%d", &v, &t, &n);
- numtriangles++;
- /*
- group->numtriangles++;
- while(fscanf(file, "%d/%d/%d", &v, &t, &n) > 0) {
- numtriangles++;
- group->numtriangles++;
- }*/
- } else if (sscanf(buf, "%d/%d", &v, &t) == 2) {
- /* v/t */
- fscanf(file, "%d/%d", &v, &t);
- fscanf(file, "%d/%d", &v, &t);
- numtriangles++;
- /*
- group->numtriangles++;
- while(fscanf(file, "%d/%d", &v, &t) > 0) {
- numtriangles++;
- group->numtriangles++;
- }*/
- } else {
- /* v */
- fscanf(file, "%d", &v);
- fscanf(file, "%d", &v);
- numtriangles++;
- /*
- group->numtriangles++;
- while(fscanf(file, "%d", &v) > 0) {
- numtriangles++;
- group->numtriangles++;
- }*/
- }
- break;
- default:
- fgets(buf, sizeof(buf), file);
- break;
- }
- }
- model->numvertices = numvertices;
- model->numnormals = numnormals;
- model->numtexcoords = numtexcoords;
- model->numtriangles = numtriangles;
- }
- GRRLIB_Model* GRRLIB_ReadOBJ(char* filename)
- {
- GRRLIB_Model* model;
- FILE* file;
- file = fopen(filename, "r");
- if (!file) {
- exit(1);
- }
- model = (GRRLIB_Model*)malloc(sizeof(GRRLIB_Model));
- model->pathname = strdup(filename);
- model->mtllibname = NULL;
- model->numvertices = 0;
- model->vertices = NULL;
- model->numnormals = 0;
- model->normals = NULL;
- model->numtexcoords = 0;
- model->texcoords = NULL;
- model->numfacetnorms = 0;
- model->facetnorms = NULL;
- model->numtriangles = 0;
- // Get a count of the number of stuff
- GRRLIB_FirstPass(model, file);
- // allocate memory
- model->vertices = (guVector*)malloc(sizeof(guVector) * 3 * (model->numvertices + 1));
- //model->triangles = (GLMtriangle*)malloc(sizeof(GLMtriangle) * model->numtriangles);
- if (model->numnormals) {
- model->normals = (guVector*)malloc(sizeof(guVector) * 3 * (model->numnormals + 1));
- }
- if (model->numtexcoords) {
- model->texcoords = (guVector*)malloc(sizeof(guVector) * 2 * (model->numtexcoords + 1));
- }
- fclose(file);
- return model;
- }
Add Comment
Please, Sign In to add comment