--- /dev/null
+
+/*] Copyright (c) 2009-2010, Charles McGarvey [**************************
+**] All rights reserved.
+*
+* vi:ts=4 sw=4 tw=75
+*
+* Distributable under the terms and conditions of the 2-clause BSD license;
+* see the file COPYING for a complete text of the license.
+*
+**************************************************************************/
+
+#include "log.hh"
+#include "mesh.hh"
+#include "script.hh"
+
+
+namespace moof {
+
+
+static int mesh_new(script& script)
+{
+ script::slot name = script[2].require_string("mesh name");
+
+ std::string str;
+ name.get(str);
+
+ mesh_handle mesh = resource::load(str, "ac");
+ if (!mesh) name.raise("unable to load mesh from file");
+
+ script.push(mesh);
+ return 1;
+}
+
+static int mesh_draw(script& script)
+{
+ mesh_handle* mesh;
+ script[1].require_object<mesh_handle>("mesh").get(mesh);
+
+ scalar alpha;
+ if (script[2].get(alpha)) (*mesh)->draw(alpha);
+ else (*mesh)->draw();
+
+ return 0;
+}
+
+static int mesh_index(script& script)
+{
+ mesh_handle* mesh;
+ script[1].require_object<mesh_handle>("mesh").get(mesh);
+
+ unsigned index;
+ script[2].require_number("index").get(index);
+ --index;
+
+ script.push((**mesh)[index]);
+ script::slot e = script[-1].push_environment();
+ e.set_field("mesh", *mesh);
+ e.pop();
+ return 1;
+}
+
+
+static int object_new(script& script)
+{
+ return script.raise("cannot construct a mesh object without a mesh");
+}
+
+static int object_draw(script& script)
+{
+ mesh::object_ptr* object;
+ script[1].require_object<mesh::object_ptr>("mesh object").get(object);
+
+ scalar alpha;
+ bool recurse;
+ if (script[2].get(alpha))
+ {
+ if (script[3].get(recurse)) (*object)->draw(alpha, recurse);
+ else (*object)->draw(alpha);
+ }
+ else
+ {
+ if (script[2].get(recurse)) (*object)->draw(SCALAR(0.0), recurse);
+ else (*object)->draw();
+ }
+
+ return 0;
+}
+
+static int object_index(script& script)
+{
+ mesh::object_ptr* object;
+ script[1].require_object<mesh::object_ptr>("mesh object").get(object);
+
+ if (script[2].is_number())
+ {
+ unsigned index;
+ script[2].get(index);
+ --index;
+ script.push((*object)->kids[index]);
+ }
+ else if (script[2].is_string())
+ {
+ std::string name;
+ script[2].get(name);
+ script.push((*object)->kids_byname[name]);
+ }
+ else
+ {
+ script[2].raise_type_error("index or name");
+ }
+ return 1;
+}
+
+
+void mesh::import(script& script, const std::string& nspace)
+{
+ script.check_stack(8);
+
+ script::slot parent = script.push_table(nspace);
+ script::slot meta = script.push_class<mesh_handle>(mesh_new);
+
+ meta.set_field("draw", mesh_draw);
+ meta.set_field("object", mesh_index);
+
+ script::slot object_meta = script.push_class<mesh::object_ptr>(object_new);
+ object_meta.set_field("draw", object_draw);
+ object_meta.set_field("kid", object_index);
+ meta.set_field("__object");
+
+ parent.set_field("mesh");
+ parent.pop();
+}
+
+
+} // namespace moof
+