struct mesh * make_cylinder(int density, float radius, float height) { struct mesh *mesh; struct vertex *vtx; struct polygon *poly; int i, j; float a, da; mesh = mesh_make(); mesh->nvtx = 2*density; mesh->npoly = density; vtx = mesh->vtx = malloc(mesh->nvtx * sizeof *mesh->vtx); poly = mesh->poly = malloc(mesh->npoly * sizeof *mesh->poly); da = 2.*M_PI/density; for (a = 0., i = 0; i < density; i++, a += da) { init_coord(&vtx[i], radius, a, 0.f); init_coord(&vtx[i + density], radius, a, height); } for (i = 0; i < density; i++) { j = (i + 1)%density; init_quad(poly++, i, j, j + density, i + density); } return mesh; }
void _op_obj_import(void) { t_context *C=ctx_get(); char *filename=C->event->standby_string; if(filename) { // init list OBJECTS=lst_new("lst"); // parse file t_file *file = file_new(filename); free(C->event->standby_string); //C->event->standby_function=NULL; C->event->callback=NULL; file_read(file); file_read_lines(file); //parse words t_link *link; t_link *l; t_word *word; for(link=file->lines->first;link;link=link->next) { t_line *line = link->data; line_read_words(line); } // parse tokens int object_start; int line_object; int is_face; //int tot_object; int tot_vert; int tot_face; int tot_tri; int tot_quad; int tot_indice; char *object_name; //tot_object=0; tot_face=0; tot_quad=0; tot_tri=0; object_start=0; for(link=file->lines->first;link;link=link->next) { // LINE t_line *line = link->data; // RESET is_face=0; for(l=line->words->first;l;l=l->next) { // WORD word=l->data; if(word_equal(word,"o")) { if(object_start) { obj_add(object_name,tot_vert,tot_face,tot_quad,tot_tri); tot_vert=0; tot_face=0; tot_quad=0; tot_tri=0; free(object_name); } else { object_start=1; } tot_vert=0; line_object=1; } else if(line_object) { object_name=(char *)malloc(sizeof(char)*(strlen(word->data)+1)); strcpy(object_name,word->data); line_object=0; } else if(word_equal(word,"v")) { tot_vert++; } else if(word_equal(word,"f")) { tot_face++; is_face=1; tot_indice=0; } else if(is_face) { tot_indice++; } else if(word_equal(word,"usemtl")) { } else if(word_equal(word,"s")) { } } if(is_face) { if(tot_indice==4) { tot_quad++; } else { tot_tri++; } } } // add last object obj_add(object_name,tot_vert,tot_face,tot_quad,tot_tri); // vars int is_data=0; int indice_vertex=0; int indice_face=0; int cursor_tri=0; int cursor_quad=0; int global_cursor=0; int tmp_global_cursor=0; int face[4]; object_start=0; t_token_type token; t_link *link_object; t_obj *obj; for(link=file->lines->first;link;link=link->next) { // LINE t_line *line = link->data; // reset is_data=0; indice_face=0; for(l=line->words->first;l;l=l->next) { // WORD word=l->data; if(word_equal(word,"o")) { token=token_object; if(object_start) { if(link_object->next) link_object=link_object->next; obj=link_object->data; // global cursor global_cursor+=tmp_global_cursor; tmp_global_cursor=obj->tot_vert; } //first else { link_object=OBJECTS->first; obj=link_object->data; object_start=1; tmp_global_cursor=obj->tot_vert; } indice_vertex=0; indice_face=0; cursor_tri=0; cursor_quad=0; } else if(word_equal(word,"v")) { token=token_vertex; } else if(word_equal(word,"f")) { token=token_face; } else if(word_equal(word,"usemtl")) { token=token_material; } else if(word_equal(word,"s")) { token=token_unknown; } else { is_data=1; } if(is_data) { if(token==token_vertex) { obj->verts[indice_vertex]=atof(word->data); indice_vertex++; } else if(token==token_face) { face[indice_face]=atoi(word->data); indice_face++; } } } // store face indice if(token==token_face) { int i; if(indice_face==3) { for(i=0;i<3;i++) { obj->tris[cursor_tri]=face[i]-global_cursor-1; cursor_tri++; } } else { for(i=0;i<4;i++) { obj->quads[cursor_quad]=face[i]-global_cursor-1; cursor_quad++; } } } } // add objects to scene C->scene->store=1; for(link=OBJECTS->first;link;link=link->next) { t_obj *obj = link->data; // new mesh t_node *node_mesh=mesh_make( obj->name, //"me_obj", obj->tot_vert, obj->tot_face, obj->tot_quad, obj->tot_tri, obj->verts, obj->quads, obj->tris); // new object t_node *node_object=object_add("mesh",obj->name); // link t_object *object=node_object->data; object->cls->link(object,node_mesh); } C->scene->store=0; // free obj for(link=OBJECTS->first;link;link=link->next) { t_obj *obj = link->data; obj_free(obj); } } }