lwObject *lw_object_read(const char *lw_file) { FILE *f = NULL; lwObject *lw_object = NULL; gint32 form_bytes = 0; gint32 read_bytes = 0; /* open file */ f = fopen(lw_file, "rb"); if (f == NULL) { g_warning("can't open file %s", lw_file); return NULL; } /* check for headers */ if (read_long(f) != ID_FORM) { g_warning("file %s is not an IFF file", lw_file); fclose(f); return NULL; } form_bytes = read_long(f); read_bytes += 4; if (read_long(f) != ID_LWOB) { g_warning("file %s is not a LWOB file", lw_file); fclose(f); return NULL; } /* create new lwObject */ lw_object = g_malloc0(sizeof(lwObject)); /* read chunks */ while (read_bytes < form_bytes) { gint32 id = read_long(f); gint32 nbytes = read_long(f); read_bytes += 8 + nbytes + (nbytes%2); switch (id) { case ID_PNTS: read_pnts(f, nbytes, lw_object); break; case ID_POLS: read_pols(f, nbytes, lw_object); break; case ID_SRFS: read_srfs(f, nbytes, lw_object); break; case ID_SURF: read_surf(f, nbytes, lw_object); break; default: fseek(f, nbytes + (nbytes%2), SEEK_CUR); } } fclose(f); return lw_object; }
lwObject *lw_object_read(const char *lw_file) { /* open file */ FILE *f = fopen(lw_file, "rb"); if (f == NULL) { return NULL; } /* check for headers */ if (read_long(f) != ID_FORM) { fclose(f); return NULL; } wxInt32 read_bytes = 0; wxInt32 form_bytes = read_long(f); read_bytes += 4; if (read_long(f) != ID_LWOB) { fclose(f); return NULL; } /* create new lwObject */ lwObject *lw_object = (lwObject*) calloc(sizeof(lwObject),1); /* read chunks */ while (read_bytes < form_bytes) { wxInt32 id = read_long(f); wxInt32 nbytes = read_long(f); read_bytes += 8 + nbytes + (nbytes%2); switch (id) { case ID_PNTS: read_pnts(f, nbytes, lw_object); break; case ID_POLS: read_pols(f, nbytes, lw_object); break; case ID_SRFS: read_srfs(f, nbytes, lw_object); break; case ID_SURF: read_surf(f, nbytes, lw_object); break; default: fseek(f, nbytes + (nbytes%2), SEEK_CUR); } } fclose(f); return lw_object; }
lwObject *lw_object_read(const char *lw_file, std::ostream& output) { FILE *f = NULL; lwObject *lw_object = NULL; gint32 form_bytes = 0; gint32 read_bytes = 0; /* open file */ f = osgDB::fopen(lw_file, "rb"); if (f == NULL) { output << "can't open file "<<lw_file<<std::endl; return NULL; } /* check for headers */ if (read_long(f) != ID_FORM) { output << "file "<<lw_file<<" is not an IFF file"<<std::endl; fclose(f); return NULL; } form_bytes = read_long(f); read_bytes += 4; if (read_long(f) != ID_LWOB) { output << "file "<<lw_file<<" is not a LWOB file"<<std::endl; fclose(f); return NULL; } /* create new lwObject */ lw_object = (lwObject*) g_malloc0(sizeof(lwObject)); lw_object->init(); /* read chunks */ while (read_bytes < form_bytes) { gint32 id = read_long(f); gint32 nbytes = read_long(f); read_bytes += 8 + nbytes + (nbytes%2); switch (id) { case ID_PNTS: read_pnts(f, nbytes, lw_object); break; case ID_POLS: read_pols(f, nbytes, lw_object); break; case ID_SRFS: read_srfs(f, nbytes, lw_object); break; case ID_SURF: read_surf(f, nbytes, lw_object); break; default: fseek(f, nbytes + (nbytes%2), SEEK_CUR); } } fclose(f); for (int i = 0; i < lw_object->face_cnt; i++) { int mati = lw_object->face[i].material; if (mati == 0) continue; /* fetch material */ lwMaterial* mat = &lw_object->material[mati]; unsigned int flags = mat->ctex.flags; if (flags == 0) continue; /* calculate texture coordinates */ lwFace* face = &lw_object->face[i]; face->texcoord = (float*) g_malloc0(face->index_cnt * sizeof(float) * 2); for (int j = 0; j < face->index_cnt; j++) { int vi = face->index[j]; GLfloat* vtx = &lw_object->vertex[vi*3]; GLfloat u,v; u = v = 0.0f; if (flags & X_AXIS) { u = (vtx[1] - mat->ctex.cy) / mat->ctex.sy; v = (vtx[2] - mat->ctex.cz) / mat->ctex.sz; } else if (flags & Y_AXIS) { u = (vtx[0] - mat->ctex.cx) / mat->ctex.sx; v = (vtx[2] - mat->ctex.cz) / mat->ctex.sz; } else if (flags & Z_AXIS) { u = (vtx[0] - mat->ctex.cx) / mat->ctex.sx; v = (vtx[1] - mat->ctex.cy) / mat->ctex.sy; } face->texcoord[j*2] = u + 0.5f; face->texcoord[j*2+1] = v + 0.5f; //printf("%.2f %.2f\n", u,v); } } return lw_object; }