serthy
16th December 2012, 11:42
Hey
i wrote this simple thing to convert *.obj to *.map!
THIS IS USING ONLY MESHES!
it is like the VMF2Map-converter, but in instead of brushes, it is using meshes
<?php
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
ini_set( "memory_limit" , "500M" );
$path = "X:\\MODDING\\COD2\\map_source\\"; //output path
$name = "objfile"; //input filename
$scale = 1; //output scale
$snapToGrid = false; //this may distort the output!
echo "Converting $name.obj!\n";
$objString = file_get_contents( $name . ".obj" );
$obj = parse_OBJ( $objString );
$map = export_OBJ2MAP( $obj , $scale , $snapToGrid );
file_put_contents( "$path$name.map" , $map );
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function parse_OBJ( $mapSourceString )
{
$obj = array();
$obj["faces"] = array();
$obj["verts"] = array();
$lines = explode( "\n" , $mapSourceString );
foreach( $lines as $line )
{
$temp = explode( " " , $line );
$val = $temp[0];
switch( $val )
{
case "#": //skip the unwanted elements like
case "usemtl": //bezier curves, splines, groubs, objects,
case "mtllib": //materials, vertexnormals, vertextestures etc
case "s": //to stay as easy as possible
case "vt":
case "vn":
case "o":
break;
case "v": //plain vertex: v xCoord yCoord zCoord weight
//strip only the 3 worldcoords
//keep the order to get the globalVertexIndex later on!
//this code ignores the lokalVertexIndex
$obj["verts"][] = array_slice( $temp , 1 , 3 );
break;
case "f": //plain face: f globalVertIndex1/globalVertParamIndex1/globalVertNormalIndex1
//this discribes 1 vertex with the globalVertexIndex of the siad vertex
//if the globalVertexIndex is negative it will use the lokalVertexIndex! (not used here)
$temp = array_slice( $temp , 1 , 3 );
//strip the elements and leave the globalVertexIndex
foreach( $temp as $key => $val )
{
$val = explode( "/" , $val );
$temp[$key] = $val[0];
}
$obj["faces"][] = $temp;
break;
}
}
return $obj;
}
function export_OBJ2MAP( $obj , $scale = 1 , $snapToGrid = false )
{
$map = "";
$map = "iwmap 4\n";
$map .= "{\n";
$map .= "\t\"classname\" \"worldspawn\"\n";
foreach( $obj["faces"] as $face )
{
$vertStrArray = array();
foreach( $face as $vertIndex )
{
$vertIndex = (int)$vertIndex - 1;
if( $vertIndex < 0 || !isset( $obj["verts"][$vertIndex] ) )
break;
$vert = $obj["verts"][$vertIndex];
$vertStr = "v ";
foreach( $vert as $coord )
{
$coord = (float)$coord * $scale;
if( $snapToGrid )
$coord = round( $coord );
$vertStr .= $coord;
$vertStr .= " ";
}
$vertStr .= "t -160 -160 -0.625 0.625\n";
$vertStrArray[] = $vertStr;
}
if( count( $vertStrArray ) != 3 ) //this code handles only trinagles!
continue; //so your *.obj file should consists out of them!
$faceStr = "";
$faceStr .= "\t{\n";
$faceStr .= "\t\tmesh\n";
$faceStr .= "\t\t{\n";
$faceStr .= "\t\t\tcaulk\n";
$faceStr .= "\t\t\tlightmap_grey\n";
$faceStr .= "\t\t\t2 2 16 8\n";
$faceStr .= "\t\t\t(\n";
$faceStr .= "\t\t\t\t$vertStrArray[0]"; //play with the indexes here
$faceStr .= "\t\t\t\t$vertStrArray[1]"; //there must be 3 uniques (0,1,2)
$faceStr .= "\t\t\t)\n";
$faceStr .= "\t\t\t(\n";
$faceStr .= "\t\t\t\t$vertStrArray[2]"; //and one duplicate > quad to triangle!
$faceStr .= "\t\t\t\t$vertStrArray[2]"; //this is the best for me right now
$faceStr .= "\t\t\t)\n";
$faceStr .= "\t\t}\n";
$faceStr .= "\t}\n";
$map .= $faceStr;
}
$map .= "}\n";
return $map;
}
usage: VMF2Map (http://killtube.org/showthread.php?1003-VMF2Map)
cheers Serthy
i wrote this simple thing to convert *.obj to *.map!
THIS IS USING ONLY MESHES!
it is like the VMF2Map-converter, but in instead of brushes, it is using meshes
<?php
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
ini_set( "memory_limit" , "500M" );
$path = "X:\\MODDING\\COD2\\map_source\\"; //output path
$name = "objfile"; //input filename
$scale = 1; //output scale
$snapToGrid = false; //this may distort the output!
echo "Converting $name.obj!\n";
$objString = file_get_contents( $name . ".obj" );
$obj = parse_OBJ( $objString );
$map = export_OBJ2MAP( $obj , $scale , $snapToGrid );
file_put_contents( "$path$name.map" , $map );
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function parse_OBJ( $mapSourceString )
{
$obj = array();
$obj["faces"] = array();
$obj["verts"] = array();
$lines = explode( "\n" , $mapSourceString );
foreach( $lines as $line )
{
$temp = explode( " " , $line );
$val = $temp[0];
switch( $val )
{
case "#": //skip the unwanted elements like
case "usemtl": //bezier curves, splines, groubs, objects,
case "mtllib": //materials, vertexnormals, vertextestures etc
case "s": //to stay as easy as possible
case "vt":
case "vn":
case "o":
break;
case "v": //plain vertex: v xCoord yCoord zCoord weight
//strip only the 3 worldcoords
//keep the order to get the globalVertexIndex later on!
//this code ignores the lokalVertexIndex
$obj["verts"][] = array_slice( $temp , 1 , 3 );
break;
case "f": //plain face: f globalVertIndex1/globalVertParamIndex1/globalVertNormalIndex1
//this discribes 1 vertex with the globalVertexIndex of the siad vertex
//if the globalVertexIndex is negative it will use the lokalVertexIndex! (not used here)
$temp = array_slice( $temp , 1 , 3 );
//strip the elements and leave the globalVertexIndex
foreach( $temp as $key => $val )
{
$val = explode( "/" , $val );
$temp[$key] = $val[0];
}
$obj["faces"][] = $temp;
break;
}
}
return $obj;
}
function export_OBJ2MAP( $obj , $scale = 1 , $snapToGrid = false )
{
$map = "";
$map = "iwmap 4\n";
$map .= "{\n";
$map .= "\t\"classname\" \"worldspawn\"\n";
foreach( $obj["faces"] as $face )
{
$vertStrArray = array();
foreach( $face as $vertIndex )
{
$vertIndex = (int)$vertIndex - 1;
if( $vertIndex < 0 || !isset( $obj["verts"][$vertIndex] ) )
break;
$vert = $obj["verts"][$vertIndex];
$vertStr = "v ";
foreach( $vert as $coord )
{
$coord = (float)$coord * $scale;
if( $snapToGrid )
$coord = round( $coord );
$vertStr .= $coord;
$vertStr .= " ";
}
$vertStr .= "t -160 -160 -0.625 0.625\n";
$vertStrArray[] = $vertStr;
}
if( count( $vertStrArray ) != 3 ) //this code handles only trinagles!
continue; //so your *.obj file should consists out of them!
$faceStr = "";
$faceStr .= "\t{\n";
$faceStr .= "\t\tmesh\n";
$faceStr .= "\t\t{\n";
$faceStr .= "\t\t\tcaulk\n";
$faceStr .= "\t\t\tlightmap_grey\n";
$faceStr .= "\t\t\t2 2 16 8\n";
$faceStr .= "\t\t\t(\n";
$faceStr .= "\t\t\t\t$vertStrArray[0]"; //play with the indexes here
$faceStr .= "\t\t\t\t$vertStrArray[1]"; //there must be 3 uniques (0,1,2)
$faceStr .= "\t\t\t)\n";
$faceStr .= "\t\t\t(\n";
$faceStr .= "\t\t\t\t$vertStrArray[2]"; //and one duplicate > quad to triangle!
$faceStr .= "\t\t\t\t$vertStrArray[2]"; //this is the best for me right now
$faceStr .= "\t\t\t)\n";
$faceStr .= "\t\t}\n";
$faceStr .= "\t}\n";
$map .= $faceStr;
}
$map .= "}\n";
return $map;
}
usage: VMF2Map (http://killtube.org/showthread.php?1003-VMF2Map)
cheers Serthy