void InitJPolyFromPolygon( polygon_t *p ) { int i; PolygonCenter( p, jcenter ); for ( i = 0; i < p->pointnum; i++ ) { Vec3dCopy( jpoly[i], p->p[i] ); } jpnum = i; }
void DistributePolygon( map3_t *map, polygon_t *p, cplane_t *pl ) { int i; vec2d_t v; vec2d_t min, max; projectionType type; vec3d_t right, up; fp_t x, y; vec3d_t norms[4]; fp_t dists[4]; polygon_t *poly; type = GetProjectionTypeOfPlane( pl ); GetProjectionVecs( right, up, type ); Vec2dInitBB( min, max, 999999.9 ); for ( i = 0; i < p->pointnum; i++ ) { ProjectVec3d( v, p->p[i], type ); Vec2dAddToBB( min, max, v ); } min[0] = floor(min[0]/FIELD_CELL_SIZE)*FIELD_CELL_SIZE; min[1] = floor(min[1]/FIELD_CELL_SIZE)*FIELD_CELL_SIZE; for ( x = min[0]; x < max[0]; x+=FIELD_CELL_SIZE ) { Vec3dFlip( norms[0], right ); Vec3dCopy( norms[1], right ); dists[0] = -x; dists[1] = x+FIELD_CELL_SIZE; for ( y = min[1]; y < max[1]; y+=FIELD_CELL_SIZE ) { Vec3dFlip( norms[2], up ); Vec3dCopy( norms[3], up ); dists[2] = -y; dists[3] = y+FIELD_CELL_SIZE; poly = CopyPolygon( p ); for ( i = 0; i < 4; i++ ) { ClipPolygonInPlace( &poly, norms[i], dists[i] ); if ( !poly ) break; } if ( !poly ) continue; { int xc, yc, zc; veccell_t *c; vec3d_t center; ivec3d_t icenter; vec3d_t shift; fp_t scale; PolygonCenter( poly, center ); // xc = floor(center[0]/FIELD_CELL_SIZE); // yc = floor(center[1]/FIELD_CELL_SIZE); // zc = floor(center[2]/FIELD_CELL_SIZE); for ( scale = -8.0; scale <= 0.0; scale+=8.0 ) { Vec3dMA( shift, scale, pl->norm, center ); IVec3dRint( icenter, shift ); xc = _UnitSnap( icenter[0], FIELD_CELL_SIZE ); yc = _UnitSnap( icenter[1], FIELD_CELL_SIZE ); zc = _UnitSnap( icenter[2], FIELD_CELL_SIZE ); if ( ( c = Map3FindCell( map, xc, yc, zc ) ) ) { Vec3dAdd( c->vec, c->vec, pl->norm ); } else { c = NEW( veccell_t ); c->x = xc; c->y = yc; c->z = zc; Map3InsertCell( map, c ); Vec3dCopy( c->vec, pl->norm ); fieldcellnum++; } } } fieldpatchnum++; } } }
int PCS_Model::SaveToPOF(std::string filename, AsyncProgress* progress) { PCS_Model::BSP_MAX_DEPTH = 0; PCS_Model::BSP_NODE_POLYS = 1; PCS_Model::BSP_TREE_TIME = 0; PCS_Model::BSP_COMPILE_ERROR = false; POF poffile; unsigned int i,j,k,l; progress->setTarget(6 + light_arrays.size() + ai_paths.size() + insignia.size() + shield_mesh.size() + thrusters.size() + docking.size() + turrets.size() + weapons.size() + special.size() + eyes.size() + model_info.size() + subobjects.size() + textures.size()); char cstringtemp[256]; // Update Progress progress->incrementWithMessage("Writing Header Pt1"); // --------- convert cross sections --------- std::vector<cross_section> sections; sections.resize(header.cross_sections.size()); for (i = 0; i < header.cross_sections.size(); i++) { sections[i].depth = header.cross_sections[i].depth; sections[i].radius = header.cross_sections[i].radius; } poffile.HDR2_Set_CrossSections(header.cross_sections.size(), sections); // Update Progress progress->incrementWithMessage("Writing Header Pt2"); // --------- ACEN --------- poffile.ACEN_Set_acen(POFTranslate(autocentering)); // Update Progress progress->incrementWithMessage("Writing Acen"); // --------- TXTR --------- // Update Progress progress->incrementWithMessage("Writing Textures"); for (i = 0; i < textures.size(); i++) poffile.TXTR_AddTexture(textures[i]); // --------- Sub object Consversion --------- wxLongLong time = wxGetLocalTimeMillis(); bool bsp_compiled = false; header.max_radius = 0.0f; for (i = 0; i < subobjects.size(); i++) { // Update Progress //if (subobjects[i].name == "debris08") // sprintf(cstringtemp, "Submodel %d: %s SENTINAL!", i, subobjects[i].name.c_str()); //else sprintf(cstringtemp, "Submodel %d: %s", i, subobjects[i].name.c_str()); progress->incrementWithMessage(cstringtemp); //memset((char *)&obj, 0, sizeof(OBJ2)); this is NO LONGER ALLOWED - obj2 now contains an class w/ vtable boost::scoped_ptr<OBJ2> obj(new OBJ2); obj->submodel_number = i; if (!PMFObj_to_POFObj2(i, *obj, bsp_compiled, header.max_radius)) { return 2; // error occured in bsp splitting! } poffile.OBJ2_Add(*obj); // takes over object management - including pointers } time = wxGetLocalTimeMillis() - time; // we succeeded in compiling - let's cache the result can_bsp_cache = true; bsp_cache.resize(subobjects.size()); for (i = 0; i < subobjects.size(); i++) poffile.OBJ2_Get_BSPData(i, bsp_cache[i].bsp_data); // --------- ---------------------- --------- int idx = GetModelInfoCount(); char cstrtmp[256]; wxString strtmp = PCS2_VERSION; sprintf(cstrtmp, "PMFSaveToPOF: Compiled on %s with %s\nmax BSP depth was %d\nmost polys in a single node was %d\nTotal Compile time was %ldms, tree generation time was %ldms", std::string(strtmp.mb_str()).c_str(), std::string(PCS2_COMP_VERSION.mb_str()).c_str(), PCS_Model::BSP_MAX_DEPTH,PCS_Model::BSP_NODE_POLYS, time.ToLong(), PCS_Model::BSP_TREE_TIME.ToLong()); bool found = false; for (i = 0; i < model_info.size() && !found; i++) { if (strstr(model_info[i].c_str(), "PMFSaveToPOF") != NULL) { found = true; if (bsp_compiled) // update the string model_info[i] = cstrtmp; } } if (!found) AddModelInfo(cstrtmp); j = 0; for (i = 0; i < model_info.size(); i++) j += model_info[i].length() + 1; boost::scoped_ptr<char> pinf(new char[j]); memset(pinf.get(), 0, j); j = 0; for (i = 0; i < model_info.size(); i++) { // Update Progress sprintf(cstringtemp, "Writing String %d", i); progress->incrementWithMessage(cstringtemp); strncpy(pinf.get()+j, model_info[i].c_str(), model_info[i].length()); j+= model_info[i].length() + 1; } poffile.PINF_Set(pinf.get(), j); if (found) model_info.resize(idx); // back down to size // --------- EYE --------- for (i = 0; i < eyes.size(); i++) { // Update Progress sprintf(cstringtemp, "Writing Eye %d", i); progress->incrementWithMessage(cstringtemp); poffile.EYE_Add_Eye(eyes[i].sobj_number, POFTranslate(eyes[i].sobj_offset), POFTranslate(eyes[i].normal)); } // --------- SPCL --------- for (i = 0; i < special.size(); i++) { // Update Progress sprintf(cstringtemp, "Writing Special %d", i); progress->incrementWithMessage(cstringtemp); poffile.SPCL_AddSpecial(special[i].name, special[i].properties, POFTranslate(special[i].point), special[i].radius); } k = l = 0; // --------- weapons (GPNT/MPNT) --------- for (i = 0; i < weapons.size(); i++) { // Update Progress sprintf(cstringtemp, "Writing Weapon %d", i); progress->incrementWithMessage(cstringtemp); if (weapons[i].type == GUN) { poffile.GPNT_AddSlot(); for (j = 0; j < weapons[i].muzzles.size(); j++) { poffile.GPNT_AddPoint(k, POFTranslate(weapons[i].muzzles[j].point), POFTranslate(weapons[i].muzzles[j].norm)); } k++; } else { poffile.MPNT_AddSlot(); for (j = 0; j < weapons[i].muzzles.size(); j++) { poffile.MPNT_AddPoint(l, POFTranslate(weapons[i].muzzles[j].point), POFTranslate(weapons[i].muzzles[j].norm)); } l++; } } // --------- turrets TGUN/TMIS --------- k = l = 0; for (i = 0; i < turrets.size(); i++) { // Update Progress sprintf(cstringtemp, "Writing Turret %d", i); progress->incrementWithMessage(cstringtemp); if (turrets[i].type == GUN) { poffile.TGUN_Add_Bank(turrets[i].sobj_parent, turrets[i].sobj_par_phys, POFTranslate(turrets[i].turret_normal)); for (j = 0; j < turrets[i].fire_points.size(); j++) { poffile.TGUN_Add_FirePoint(k, POFTranslate(turrets[i].fire_points[j])); } k++; } else { poffile.TMIS_Add_Bank(turrets[i].sobj_parent, turrets[i].sobj_par_phys, POFTranslate(turrets[i].turret_normal)); for (j = 0; j < turrets[i].fire_points.size(); j++) { poffile.TMIS_Add_FirePoint(l, POFTranslate(turrets[i].fire_points[j])); } l++; } } // --------- docking --------- for (i = 0; i < docking.size(); i++) { // Update Progress sprintf(cstringtemp, "Writing Docking %d", i); progress->incrementWithMessage(cstringtemp); poffile.DOCK_Add_Dock(docking[i].properties); for (j = 0; j < docking[i].dockpoints.size(); j++) { poffile.DOCK_Add_Point(i, POFTranslate(docking[i].dockpoints[j].point), POFTranslate(docking[i].dockpoints[j].norm)); } for (j = 0; j < docking[i].paths.size(); j++) { poffile.DOCK_Add_SplinePath(i, docking[i].paths[j]); } } // --------- thrusters --------- for (i = 0; i < thrusters.size(); i++) { // Update Progress sprintf(cstringtemp, "Writing Thruster %d", i); progress->incrementWithMessage(cstringtemp); poffile.FUEL_Add_Thruster(thrusters[i].properties); for (j = 0; j < thrusters[i].points.size(); j++) { poffile.FUEL_Add_GlowPoint(i, thrusters[i].points[j].radius, POFTranslate(thrusters[i].points[j].pos), POFTranslate(thrusters[i].points[j].norm)); } } // --------- shield_mesh --------- int fcs[3], nbs[3]; std::vector<vector3d> points(shield_mesh.size()*3); vector3d shldtvect; // make points list l = 0; for (i = 0; i < shield_mesh.size(); i++) { for (j = 0; j < 3; j++) { bool found_corner = false; for (auto& p : points) { if (p == POFTranslate(shield_mesh[i].corners[j])) { found_corner = true; break; } } if (!found_corner) { if (l >= points.size()) points.resize(points.size()*2); points[l] = POFTranslate(shield_mesh[i].corners[j]); l++; } } } points.resize(l); // translate shield mesh for (i = 0; i < shield_mesh.size(); i++) { // Update Progress sprintf(cstringtemp, "Writing Shield Tri %d", i); progress->incrementWithMessage(cstringtemp); // adds points to list if need, determine fcs[] for (j = 0; j < 3; j++) { shldtvect = POFTranslate(shield_mesh[i].corners[j]); fcs[j] = FindInList(points, shldtvect); } // determine neighbors (nbs[]) j=0; for (k = 0; k < shield_mesh.size() && j < 3; k++) { if (Neighbor(shield_mesh[i], shield_mesh[k]) && i != k) { nbs[j] = k; j++; } } // add poffile.SHLD_Add_Face(POFTranslate(shield_mesh[i].face_normal), fcs, nbs); } // add points // Update Progress progress->incrementWithMessage("Writing Shield Points"); for (i = 0; i < points.size(); i++) poffile.SHLD_Add_Vertex(points[i]); progress->incrementWithMessage("Writing Shield Collision Tree"); // --------------- generate shield collision tree ---------------- if (poffile.SHLD_Count_Faces() > 0) { std::vector<pcs_polygon> shldmesh(poffile.SHLD_Count_Faces()); // make a pcs_polygon mesh from the shields for (i = 0; i < shldmesh.size(); i++) { shldmesh[i].verts.resize(3); poffile.SHLD_Get_Face(i, shldmesh[i].norm, fcs, nbs); for (j = 0; j < 3; j++) { poffile.SHLD_Get_Vertex(fcs[j], shldmesh[i].verts[j].point); shldmesh[i].verts[j].norm = shldmesh[i].norm; } shldmesh[i].centeroid = PolygonCenter(shldmesh[i]); } // make the tree vector3d smin, smax; std::unique_ptr<bsp_tree_node> shld_root = MakeTree(shldmesh, smax, smin); // pack the tree int sldc_size = CalcSLDCTreeSize(shld_root.get()); std::vector<char> sldc; sldc.resize(sldc_size); PackTreeInSLDC(shld_root.get(), 0, &sldc.front(), sldc_size); poffile.SLDC_SetTree(std::move(sldc)); // POFHandler will steal our copy of the buffer } // --------- insignia --------- vector3d uv, vv; float *u = (float *)&uv, *v = (float *)&vv; for (i = 0; i < insignia.size(); i++) { // Update Progress sprintf(cstringtemp, "Writing Insignia %d", i); progress->incrementWithMessage(cstringtemp); poffile.INSG_Add_insignia(insignia[i].lod, POFTranslate(insignia[i].offset)); for (j = 0; j < insignia[i].faces.size(); j++) { for (k = 0; k < 3; k++) { while ((l = poffile.INST_Find_Vert(i, POFTranslate(insignia[i].faces[j].verts[k]))) == (unsigned)-1) { poffile.INSG_Add_Insig_Vertex(i, POFTranslate(insignia[i].faces[j].verts[k])); } fcs[k] = l; u[k] = insignia[i].faces[j].u[k]; v[k] = insignia[i].faces[j].v[k]; } poffile.INSG_Add_Insig_Face(i, fcs, uv, vv); } } // --------- ai_paths --------- for (i = 0; i < ai_paths.size(); i++) { // Update Progress sprintf(cstringtemp, "Writing Path %d", i); progress->incrementWithMessage(cstringtemp); poffile.PATH_Add_Path(ai_paths[i].name, ai_paths[i].parent); for (j = 0; j < ai_paths[i].verts.size(); j++) { poffile.PATH_Add_Vert(i, POFTranslate(ai_paths[i].verts[j].pos), ai_paths[i].verts[j].radius); } } // --------- light arrays --------- pcs_glow_array *gla; for (i = 0; i < light_arrays.size(); i++) { // Update Progress sprintf(cstringtemp, "Writing Glow %d", i); progress->incrementWithMessage(cstringtemp); gla = &light_arrays[i]; poffile.GLOW_Add_LightGroup(gla->disp_time, gla->on_time, gla->off_time, gla->obj_parent, gla->LOD, gla->type, gla->properties); for (j = 0; j < gla->lights.size(); j++) { poffile.GLOW_Add_GlowPoint(i, gla->lights[j].radius, POFTranslate(gla->lights[j].pos), POFTranslate(gla->lights[j].norm)); } } // --------- HDR2 --------- // let's make new BBoxes based on the subobjects vector3d minbox, maxbox, tmpmin, tmpmax; poffile.OBJ2_Get_BoundingMax(0, maxbox); poffile.OBJ2_Get_BoundingMin(0, minbox); for (i = 1; i < poffile.OBJ2_Count(); i++) { vector3d sobj_offset(POFTranslate(OffsetFromParent(i))); poffile.OBJ2_Get_BoundingMax(i, tmpmax); poffile.OBJ2_Get_BoundingMin(i, tmpmin); ExpandBoundingBoxes(maxbox, minbox, tmpmax + sobj_offset); ExpandBoundingBoxes(maxbox, minbox, tmpmin + sobj_offset); } for (i = 0; i < poffile.SHLD_Count_Vertices(); i++) { poffile.SHLD_Get_Vertex(i, tmpmax); ExpandBoundingBoxes(maxbox, minbox, tmpmax); } // update our bounding boxes //axis doesn't matter on the bounding boxes - it's all negative/positive values! i'm a faqing moron poffile.HDR2_Set_MinBound(header.min_bounding_overridden ? header.min_bounding_override : minbox); poffile.HDR2_Set_MaxBound(header.max_bounding_overridden ? header.max_bounding_override : maxbox); this->header.max_bounding = minbox; this->header.min_bounding = maxbox; poffile.HDR2_Set_MaxRadius(header.max_radius_overridden ? header.max_radius_override : header.max_radius); poffile.HDR2_Set_Details(header.detail_levels.size(), header.detail_levels); poffile.HDR2_Set_Debris(header.debris_pieces.size(), header.debris_pieces); poffile.HDR2_Set_Mass(header.mass); poffile.HDR2_Set_MassCenter(POFTranslate(header.mass_center)); poffile.HDR2_Set_MomentInertia(header.MOI); poffile.HDR2_Set_SOBJCount(GetSOBJCount()); std::ofstream out(filename.c_str(), std::ios::out | std::ios::binary); if (!poffile.SavePOF(out)) return 1; return 0; }
bool PCS_Model::PMFObj_to_POFObj2(int src_num, OBJ2 &dst, bool &bsp_compiled, float& model_radius) { pcs_sobj &src = subobjects[src_num]; dst.submodel_parent = src.parent_sobj; dst.offset = POFTranslate(src.offset); dst.geometric_center = POFTranslate(src.geometric_center); dst.submodel_name = APStoString(src.name); dst.properties = APStoString(src.properties); switch (src.movement_type) { case ROTATE: dst.movement_type = 1; break; default: dst.movement_type = -1; } switch (src.movement_axis) { case MV_X: dst.movement_axis = 0; break; case MV_Z: dst.movement_axis = 1; break; case MV_Y: dst.movement_axis = 2; break; default: dst.movement_axis = -1; } dst.reserved = 0; if(!can_bsp_cache || bsp_cache[src_num].changed) { // convert them to POF axis std::vector<pcs_polygon> clean_list = src.polygons; for (size_t i = 0; i < clean_list.size(); i++) { clean_list[i].norm = POFTranslate(clean_list[i].norm); for (size_t j = 0; j < clean_list[i].verts.size(); j++) { clean_list[i].verts[j].point = POFTranslate(clean_list[i].verts[j].point); clean_list[i].verts[j].norm = POFTranslate(clean_list[i].verts[j].norm); } clean_list[i].centeroid = PolygonCenter(clean_list[i]); } // BSP Compilation! // assemble points list std::vector<bsp_vert> points_list; std::vector<vector3d> pnts; std::unordered_map<vector3d, int> point_to_index; std::unordered_map<vector3d, int> normal_to_index; for (size_t i = 0; i < pnts.size(); i++) { point_to_index.insert(std::make_pair(pnts[i], i)); } bsp_vert temp; points_list.reserve(clean_list.size()); for (size_t i = 0; i < clean_list.size(); i++) { for (size_t j = 0; j < clean_list[i].verts.size(); j++) { auto point = point_to_index.find(clean_list[i].verts[j].point); if (point == point_to_index.end()) { point_to_index.insert(std::make_pair(clean_list[i].verts[j].point, points_list.size())); points_list.emplace_back(); points_list.back().point = clean_list[i].verts[j].point; pnts.push_back(points_list.back().point); } auto normal = normal_to_index.find(clean_list[i].verts[j].norm); if (normal == normal_to_index.end()) { points_list[normal_to_index.size() / 128].norms.push_back(clean_list[i].verts[j].norm); normal_to_index.insert(std::make_pair(clean_list[i].verts[j].norm, normal_to_index.size())); } } } // create our defpoints BSP_DefPoints points; MakeDefPoints(points, points_list); vector3d AvgNormal; // create tree std::unique_ptr<bsp_tree_node> root = MakeTree(clean_list, dst.bounding_box_max_point, dst.bounding_box_min_point); // allocate buffer and write the defpoints dst.bsp_data.resize(points.head.size + CalculateTreeSize(root.get(), clean_list)); if (points.Write(&dst.bsp_data.front()) != points.head.size) return false; // calculation error //std::ofstream bsp_debug("c:\\bsp.txt"); //DebugPrintTree(root, bsp_debug); // pack the tree int error_flags = 0; PackTreeInBSP(root.get(), points.head.size, &dst.bsp_data.front(), clean_list, normal_to_index, point_to_index, points, dst.geometric_center, dst.bsp_data.size(), error_flags); // we got errors! if (error_flags != BSP_NOERRORS) return false; // update the bsp_compiled to be true bsp_compiled = true; // update the BSP cache with the new result if (can_bsp_cache) { // clear the saved - stale cache bsp_cache[src_num].decache(); bsp_cache[src_num].bsp_data = dst.bsp_data; bsp_cache[src_num].changed = false; } } else // Used cached copy! { dst.bsp_data = bsp_cache[src_num].bsp_data; } dst.radius = 0.0f; dst.bounding_box_max_point = vector3d(FLT_MIN, FLT_MIN, FLT_MIN); dst.bounding_box_min_point = vector3d(FLT_MAX, FLT_MAX, FLT_MAX); vector3d global_offset(OffsetFromParent(src_num)); for(unsigned int i = 0; i<src.polygons.size(); i++){ for(unsigned int j = 0; j<src.polygons[i].verts.size(); j++){ ExpandBoundingBoxes(dst.bounding_box_max_point, dst.bounding_box_min_point, src.polygons[i].verts[j].point); float norm = Magnitude(src.polygons[i].verts[j].point); if (norm > dst.radius) { dst.radius = norm; } float global_norm = Magnitude(src.polygons[i].verts[j].point + global_offset); if (global_norm > model_radius) { model_radius = global_norm; } } } if (dst.radius == 0.0f) { dst.bounding_box_max_point = vector3d(); dst.bounding_box_min_point = vector3d(); } if (src.radius_overridden) { dst.radius = src.radius_override; } if (src.bounding_box_min_point_overridden) { dst.bounding_box_min_point = src.bounding_box_min_point_override; } if (src.bounding_box_max_point_overridden) { dst.bounding_box_max_point = src.bounding_box_max_point_override; } POFTranslateBoundingBoxes(dst.bounding_box_min_point, dst.bounding_box_max_point); return true; }