/* export_c_plugin.a8s */ /* * This script is a simple .c format Object export plugin. * * Export scripts are not allowed to modify a project. Any * attempt to change a model is an error. * * The .c format is written to a text file that's passed to * the script from Anim8or. * * Copyright 2006 R. Steven Glanville. Permission granted for * modification and use, including commercial use. Source * distribution must include this copyright. * */ /* * Directives to install script as export plugin in Object editor. * * Required directives: * * #plugin("object", "export", , ); * #file(, [ "text" | "binary" ]); * #return(); * * - string, including initial '.' * - comment for export file open dialog. * - name of file variable. Must be declared later. * - int variable with result. 1 = success, 0 = failure. * */ #plugin("object", "export", "C source file", ".c"); #file($output, "text"); #return($result); file $output; int $result; object $curObject; $curObject = project.curObject; $output.print("// File \"%s\"\n", $output.GetName()); $output.print("// Export object \"%s\"\n", $curObject.name); $output.print("// Created with Anim8or %s\n", version); $output.print("\n"); $output.print("#include \"Anim8orExport.h\"\n"); $output.print("\n"); shape $shape, $shapes[1], $childShapes[1]; tridata $tdata; int $numPoints, $numNormals, $numTexCoords, $numTriangles, $numMaterials; int $ii, $jj; point3 $point, $normal; point2 $uv; float4x4 $transformMat, $normalTransformMat; material $material; string $names[0]; texture $tex[7]; string $name[7]; /* The shapes in $shapes are processed in reverse order so */ /* reverse the array of values that GetShapes returns: */ $curObject.GetShapes($childShapes); $shapes.size = 0; while ($childShapes.size > 0) $shapes.push($childShapes.pop()); while ($shapes.size > 0) { $shape = $shapes.pop(); /* If $shape is a group push child shapes onto stack: */ if ($shape.GetKind() == SHAPE_KIND_GROUP) { $shape.GetShapes($childShapes); $output.print("# Group \"%s\" has %d children:\n", $shape.name, $childShapes.size); while ($childShapes.size > 0) { $shapes.push($childShapes.pop()); } } else if ($shape.GetKind() == SHAPE_KIND_PATH || $shape.GetKind() == SHAPE_KIND_MODIFIER || $shape.GetKind() == SHAPE_KIND_TEXT) { /* No 3D mesh to output. */ } else { $output.print("// %s\n", $shape.name); $tdata = $shape.GetTriangleData(); $transformMat = $shape.GetGlobalTransform(); $normalTransformMat = $shape.GetGlobalNormalTransform(); $numPoints = $tdata.GetNumPoints(); $output.print("static float %s_coords[] = {\n", $shape.name); for $ii = 0 to $numPoints - 1 do { $point = $tdata.GetPoint($ii); $point = $transformMat.Project($point); $output.print(" %.6g, %.6g, %.6g,\n", $point); } $output.print("};\n"); $output.print("\n"); $output.print("static float %s_normals[] = {\n", $shape.name); for $ii = 0 to $numPoints - 1 do { $point = $tdata.GetNormal($ii); $point = $normalTransformMat.Project($point); $output.print(" %.6g, %.6g, %.6g,\n", $point); } $output.print("};\n"); $output.print("\n"); $output.print("static float %s_texcoords[] = {\n", $shape.name); for $ii = 0 to $numPoints - 1 do { $uv = $tdata.GetTexCoord($ii); $output.print(" %.6g, %.6g,\n", $uv); } $output.print("};\n"); $output.print("\n"); $numTriangles = $tdata.GetNumTriangles(); $output.print("static int %s_indices[] = {\n", $shape.name); for $ii = 0 to $numTriangles - 1 do { $output.print(" %d, %d, %d,\n", $tdata.GetIndex($ii*3), $tdata.GetIndex($ii*3 + 1), $tdata.GetIndex($ii*3 + 2)); } $output.print("};\n"); $output.print("\n"); $output.print("static unsigned char %s_matindices[] = {", $shape.name); for $ii = 0 to $numTriangles - 1 do { if ($ii % 10 == 0) $output.print("\n "); $output.print(" %d,", $tdata.GetMatIndex($ii)); } $output.print("\n"); $output.print("};\n"); $output.print("\n"); $numMaterials = $tdata.GetNumMaterials(); $output.print("static Anim8orMaterial %s_materials[] = {\n", $shape.name); for $ii = 0 to $numMaterials - 1 do { $material = $tdata.GetMaterial($ii); $output.print(" {\n"); $output.print(" { %.3g, %.3g, %.3g, 1, }, // Ambient color\n", $material.ambient); $output.print(" { %.3g, %.3g, %.3g, %.3g, }, // Diffuse color\n", $material.diffuse, $material.alpha); $output.print(" { %.3g, %.3g, %.3g, 1, }, // Specular color\n", $material.specular); $output.print(" { %.3g, %.3g, %.3g, 1, }, // Emissive color\n", $material.emissive); $output.print(" %.3g, %.3g, %.3g, %.3g, %.3g, %.3g, " + "// Ka, Kd, Ks, Ke, PhongSize, Brilliance\n", $material.Ka, $material.Kd, $material.Ks, $material.Ke, $material.roughness, $material.brilliance); $tex[0] = $material.GetTexture(TEXTURE_AMBIENT); $tex[1] = $material.GetTexture(TEXTURE_DIFFUSE); $tex[2] = $material.GetTexture(TEXTURE_SPECULAR); $tex[3] = $material.GetTexture(TEXTURE_EMISSIVE); $tex[4] = $material.GetTexture(TEXTURE_TRANSPARENCY); $tex[5] = $material.GetTexture(TEXTURE_BUMPMAP); $tex[6] = $material.GetTexture(TEXTURE_ENVIRONMENT); for $jj = 0 to 6 do { if ($tex[$jj] != NULL) { $name[$jj] = $tex[$jj].name; } else { $name[$jj] = ""; } } $output.print(" \"%s\", // ambient texture\n", $name[0]); $output.print(" \"%s\", // diffuse texture\n", $name[1]); $output.print(" \"%s\", // specular texture\n", $name[2]); $output.print(" \"%s\", // emissive texture\n", $name[3]); $output.print(" \"%s\", // transparency texture\n", $name[4]); $output.print(" \"%s\", // bumpmap texture\n", $name[5]); $output.print(" \"%s\", // environment texture\n", $name[6]); $output.print(" },\n"); } $output.print("};\n"); $output.print("\n"); $output.print("static struct Anim8orMesh %s = {\n", $shape.name); $output.print(" \"%s\", %d, %d, %s_indices, %s_matindices,\n", $shape.name, $numPoints, $numTriangles*3, $shape.name, $shape.name); $output.print(" %s_coords, %s_normals, %s_texcoords, %s_materials,\n", $shape.name, $shape.name, $shape.name, $shape.name); $output.print("};\n"); $output.print("\n"); $names.push($shape.name); } } /* Print table of shapes: */ $output.print("// Object %s\n", $curObject.name); $output.print("struct Anim8orObject object_%s = {\n", $curObject.name); $output.print(" \"%s\", %d, // Num Meshes\n", $curObject.name, $names.size); $output.print(" {\n"); for $ii = 0 to $names.size - 1 do $output.print(" &%s,\n", $names[$ii]); $output.print(" },\n"); $output.print("};\n"); $output.print("\n"); $output.print("// End of file \"%s\"\n", $output.GetName()); $result = 1; /* Report success. */