int open_ply(const char* filename) { /* bounding box */ int I; struct triangle_t* t,**cursor; float x1=+1e18f,x2=-1e18f; float y1=+1e18f,y2=-1e18f; float z1=+1e18f,z2=-1e18f; PlyProperty vert_prop[3]={ { "x", Float32, Float32, 0, 0, 0, 0, 0}, { "y", Float32, Float32, 4, 0, 0, 0, 0}, { "z", Float32, Float32, 8, 0, 0, 0, 0} }; PlyProperty face_prop ={"vertex_indices", Int32, Int32, 4,PLY_LIST, Int32, Int32, 0}; PlyProperty** props=NULL; int numvertices,numproperties,numtriangles,numstrips,numfaces; int clockwise; struct {int nverts,*verts;} face; int h,k; float maxdim; PlyFile* ply=0; FILE* file = fopen( filename, "rb" ); if (!file) return 0; ply = read_ply(file); if (!ply) { fclose(file); return 0; } cursor=&(mesh.triangles); props = get_element_description_ply(ply, "vertex", &numvertices, &numproperties); mesh.vertices=(struct vertex_t*)malloc(sizeof(struct vertex_t)*numvertices); mesh.numvertices=numvertices; get_element_setup_ply(ply, "vertex", 3,vert_prop); for( k=0;k<numvertices; ++k) { struct vertex_t* v=mesh.vertices+k; get_element_ply( ply, (void*)v); x1=min2(x1,v->x);x2=max2(x2,v->x); y1=min2(y1,v->y);y2=max2(y2,v->y); z1=min2(z1,v->z);z2=max2(z2,v->z); } /* normalize to unit box to [-1,+1],[-1,+1],[-1,+1] mantaining proportions */ maxdim=max2(x2-x1,y2-y1); maxdim=max2(maxdim,z2-z1); for( k=0;k<numvertices; ++k) { struct vertex_t* v=mesh.vertices+k; v->x=2*((v->x-x1)/maxdim-0.5f); v->y=2*((v->y-y1)/maxdim-0.5f); v->z=2*((v->z-z1)/maxdim-0.5f); } numtriangles=0; if (props = get_element_description_ply( ply, "face", &numfaces, &numproperties)) { get_element_setup_ply( ply, "face", 1, &face_prop); for(h=0; h<numfaces; h++ ) { get_element_ply( ply, (void*)&face); for(k=2; k<face.nverts;++k) { t=(struct triangle_t*)malloc(sizeof(struct triangle_t)); t->i0=face.verts[0 ]; t->i1=face.verts[k-1]; t->i2=face.verts[k ]; t->next=0; (*cursor)=t; cursor=&(t->next); } free(face.verts); } } else { props = get_element_description_ply( ply, "tristrips", &numstrips, &numproperties); get_element_setup_ply(ply,"tristrips",1,&face_prop); for(k=0; k<numstrips;++k ) { get_element_ply( ply, (void*)&face); clockwise=1; for (I=2;I< face.nverts; I++) { if (face.verts[I] == -1) { I += 2; clockwise = 1; } else { t=(struct triangle_t*)malloc(sizeof(struct triangle_t)); t->i0=face.verts[I-2]; t->i1=face.verts[I-1]; t->i2=face.verts[I ]; if (!clockwise) swap_int(t->i1,t->i2); t->next=0; (*cursor)=t; cursor=&(t->next); clockwise = 1-clockwise; } } free(face.verts); } } //close_ply( ply ); fclose(file); set_normals(); return 1; /* ok */ }
GL_quad::GL_quad(vector3 v0, vector3 v1, vector3 v2, vector3 v3, vector2 t0, vector2 t1, vector2 t2, vector2 t3, vector3 n0, vector3 n1, vector3 n2, vector3 n3) : Quad(v0, v1, v2, v3) { set_tex_coords(t0, t1, t2, t3); set_normals(n0, n1, n2, n3); }
GL_quad::GL_quad(vector3 v0, vector3 v1, vector3 v2, vector3 v3, vector2 t0, vector2 t1, vector2 t2, vector2 t3) : Quad(v0, v1, v2, v3) { set_tex_coords(t0, t1, t2, t3); vector3 normal = Quad::get_normal(); set_normals(normal, normal, normal, normal); }
static void generate_square(struct fv_map_painter *painter, struct tile_data *data, int x, int y) { fv_map_block_t block = fv_map.blocks[y * FV_MAP_WIDTH + x]; struct vertex *v; int i; int z, oz; v = reserve_quad(data); z = get_block_height(block); set_tex_coords_for_image(painter, v, FV_MAP_GET_BLOCK_TOP_IMAGE(block), 1.0f); set_normals(v, FV_MAP_PAINTER_NORMAL_UP); for (i = 0; i < 4; i++) v[i].z = z; v->x = x; v->y = y; v++; v->x = x + 1; v->y = y; v++; v->x = x; v->y = y + 1; v++; v->x = x + 1; v->y = y + 1; /* Add the side walls */ if (z > (oz = get_position_height(x, y + 1))) { v = add_horizontal_side(data, y + 1, x + 1, oz, x, z); set_normals(v, FV_MAP_PAINTER_NORMAL_NORTH); set_tex_coords_for_image(painter, v, FV_MAP_GET_BLOCK_NORTH_IMAGE(block), z - oz); } if (z > (oz = get_position_height(x, y - 1))) { v = add_horizontal_side(data, y, x, oz, x + 1, z); set_normals(v, FV_MAP_PAINTER_NORMAL_SOUTH); set_tex_coords_for_image(painter, v, FV_MAP_GET_BLOCK_SOUTH_IMAGE(block), z - oz); } if (z > (oz = get_position_height(x - 1, y))) { v = add_vertical_side(data, x, y + 1, oz, y, z); set_normals(v, FV_MAP_PAINTER_NORMAL_WEST); set_tex_coords_for_image(painter, v, FV_MAP_GET_BLOCK_WEST_IMAGE(block), z - oz); } if (z > (oz = get_position_height(x + 1, y))) { v = add_vertical_side(data, x + 1, y, oz, y + 1, z); set_normals(v, FV_MAP_PAINTER_NORMAL_EAST); set_tex_coords_for_image(painter, v, FV_MAP_GET_BLOCK_EAST_IMAGE(block), z - oz); } }