コード例 #1
0
bool run_lua(const std::string& filename)
{
	GRLUA_DEBUG("Importing scene from " << filename);
	
	// Start a lua interpreter
	lua_State* L = lua_open();

	GRLUA_DEBUG("Loading base libraries");
	
	// Load some base library
	luaL_openlibs(L);


	GRLUA_DEBUG("Setting up our functions");

	make_scene_lib(L);
	make_tracer_lib(L);
	make_color_lib(L);
	make_material_lib(L);

	unsigned last_slash = filename.find_last_of("/\\");

	if (last_slash != std::string::npos)
	{
		char buf[2048];
		getcwd(buf, sizeof(buf));

		lua_getglobal(L, "package");
		lua_getfield(L, -1, "path");
		std::string cur_path = lua_tostring(L, -1);
		cur_path.append(";");
		cur_path.append(buf);
		cur_path.append("/");
		cur_path.append(filename.substr(0,last_slash));
		cur_path.append("/?.lua");
		lua_pop(L, 1);
		lua_pushstring(L, cur_path.c_str());
		lua_setfield(L, -2, "path");
		lua_pop(L, 1);
	}


	GRLUA_DEBUG("Parsing the scene");
	// Now parse the actual scene
	if (luaL_loadfile(L, filename.c_str()) || lua_pcall(L, 0, 0, 0)) {
	  std::cerr << "Error loading " << filename << ": " << lua_tostring(L, -1) << std::endl;
	  return false;
	}
	GRLUA_DEBUG("Closing the interpreter");
	
	// Close the interpreter, free up any resources not needed
	lua_close(L);

	return true;
}
コード例 #2
0
ファイル: scene_lua.cpp プロジェクト: strangemonad/raytrace
bool 
runLua(const std::string& filename)
{
   GRLUA_DEBUG("Importing scene from " << filename);
   
   // Start a lua interpreter
   lua_State* L = lua_open();
   
   GRLUA_DEBUG("Loading base libraries");
   
   // Load some base library
   luaopen_base(L);
   luaopen_io(L);
   luaopen_string(L);
   luaopen_math(L);
   luaopen_table(L);
   
   GRLUA_DEBUG("Setting up our functions");
   
   // Set up the metatable for gr.node
   luaL_newmetatable(L, "gr.node");
   lua_pushstring(L, "__index");
   lua_pushvalue(L, -2);
   lua_settable(L, -3);
   
   // Load the gr.node methods
   luaL_openlib(L, 0, grlib_node_methods, 0);
   
   // Load the gr functions
   luaL_openlib(L, "gr", grlib_functions, 0);
   
   GRLUA_DEBUG("Parsing the scene");
   
   // Now parse the actual scene
   if (luaL_loadfile(L, filename.c_str()) || lua_pcall(L, 0, 0, 0)) {
      std::cerr << "Error loading " << filename << ": " << lua_tostring(L, -1) << std::endl;
      return false;
   }
   GRLUA_DEBUG("Closing the interpreter");
   
   // Close the interpreter, free up any resources not needed
   lua_close(L);
   
   return true;
}
コード例 #3
0
ファイル: scene_lua.cpp プロジェクト: shaon0000/raytracer
int gr_mesh_cmd(lua_State* L)
{
  GRLUA_DEBUG_CALL;
  
  gr_node_ud* data = (gr_node_ud*)lua_newuserdata(L, sizeof(gr_node_ud));
  data->node = 0;

  const char* name = luaL_checkstring(L, 1);

  std::vector<Point3D> verts;
  std::vector< std::vector<int> > faces;

  luaL_checktype(L, 2, LUA_TTABLE);
  int vert_count = luaL_getn(L, 2);
  
  luaL_argcheck(L, vert_count >= 1, 2, "Tuple of vertices expected");

  for (int i = 1; i <= vert_count; i++) {
    lua_rawgeti(L, 2, i);

    Point3D vertex;
    get_tuple(L, -1, &vertex[0], 3);
    
    verts.push_back(vertex);
    lua_pop(L, 1);
  }

  luaL_checktype(L, 3, LUA_TTABLE);
  int face_count = luaL_getn(L, 3);
  
  luaL_argcheck(L, face_count >= 1, 3, "Tuple of faces expected");

  faces.resize(face_count);
  
  for (int i = 1; i <= face_count; i++) {
    lua_rawgeti(L, 3, i);

    luaL_checktype(L, -1, LUA_TTABLE);
    int index_count = luaL_getn(L, -1);

    luaL_argcheck(L, index_count >= 3, 3, "Tuple of indices expected");

    faces[i - 1].resize(index_count);
    get_tuple(L, -1, &faces[i - 1][0], index_count);
    
    lua_pop(L, 1);
  }

  Mesh* mesh = new Mesh(verts, faces);
  GRLUA_DEBUG(*mesh);
  data->node = new GeometryNode(name, mesh);

  luaL_getmetatable(L, "gr.node");
  lua_setmetatable(L, -2);

  return 1;
}
コード例 #4
0
ファイル: scene_lua.cpp プロジェクト: jemartti/Puppeteer
// This function calls the lua interpreter to do the actual importing
SceneNode* import_lua(const std::string& filename)
{
  GRLUA_DEBUG("Importing scene from " << filename);
  
  // Start a lua interpreter
  lua_State* L = lua_open();

  GRLUA_DEBUG("Loading base libraries");
  
  // Load some base library
  luaL_openlibs(L);


  GRLUA_DEBUG("Setting up our functions");

  // Set up the metatable for gr.node
  luaL_newmetatable(L, "gr.node");
  lua_pushstring(L, "__index");
  lua_pushvalue(L, -2);
  lua_settable(L, -3);

  // Load the gr.node methods
  luaL_openlib(L, 0, grlib_node_methods, 0);

  // Load the gr functions
  luaL_openlib(L, "gr", grlib_functions, 0);

  GRLUA_DEBUG("Parsing the scene");
  // Now parse the actual scene
  if (luaL_loadfile(L, filename.c_str()) || lua_pcall(L, 0, 1, 0)) {
    std::cerr << "Error loading " << filename << ": " << lua_tostring(L, -1) << std::endl;
    return 0;
  }

  GRLUA_DEBUG("Getting back the node");
  
  // Pull the returned node off the stack
  gr_node_ud* data = (gr_node_ud*)luaL_checkudata(L, -1, "gr.node");
  if (!data) {
    std::cerr << "Error loading " << filename << ": Must return the root node." << std::endl;
    return 0;
  }

  // Store it
  SceneNode* node = data->node;

  GRLUA_DEBUG("Closing the interpreter");
  
  // Close the interpreter, free up any resources not needed
  lua_close(L);

  // And return the node
  return node;
}
コード例 #5
0
int scene_f_mesh_cmd(lua_State *L)
{
	GRLUA_DEBUG_CALL;

	scene_node_ud *data = (scene_node_ud*)lua_newuserdata(L, sizeof(scene_node_ud));
	data->node = 0;

	const char* name = luaL_checkstring(L, 1);

	std::vector<Point3D> verts;
	std::vector<Point2D> uvs;
	std::vector<Vector3D> normals;
	std::vector<std::vector<int> > face_v;
	std::vector<std::vector<int> > face_uv;
	std::vector<std::vector<int> > face_norms;

	luaL_checktype(L, 2, LUA_TTABLE);
	int vert_count = luaL_getn(L, 2);

	luaL_argcheck(L, vert_count >= 1, 2, "Tuple of vertices expected");

	for (int i = 1; i <= vert_count; i++)
	{
		lua_rawgeti(L, 2, i);

		Point3D vertex;
		get_tuple(L, -1, &vertex[0], 3);
		verts.push_back(vertex);
		lua_pop(L, 1);
	}

	luaL_checktype(L, 3, LUA_TTABLE);
	int uv_count = luaL_getn(L, 3);

	for (int i = 1; i <= uv_count; ++i)
	{
		lua_rawgeti(L, 3, i);

		Point2D uv;
		get_tuple(L, -1, &uv[0], 2);
		uv[1] = 1-uv[1];
		uvs.push_back(uv);
		lua_pop(L, 1);
	}

	luaL_checktype(L, 4, LUA_TTABLE);
	int norm_count = luaL_getn(L, 4);

	for (int i = 1; i <= norm_count; ++i)
	{
		lua_rawgeti(L, 4, i);

		Vector3D norm;
		get_tuple(L, -1, &norm[0], 3);
		norm.normalize();
		normals.push_back(norm);
		lua_pop(L, 1);
	}

	luaL_checktype(L, 5, LUA_TTABLE);
	int face_count = luaL_getn(L, 5);

	luaL_argcheck(L, face_count >= 1, 5, "Tuple of faces expected");

	face_v.resize(face_count);
	face_uv.resize(face_count);
	face_norms.resize(face_count);

	for (int i = 1; i <= face_count; ++i)
	{
		lua_rawgeti(L, 5, i);

		luaL_checktype(L, -1, LUA_TTABLE);
		int attrib_count = luaL_getn(L, -1);

		luaL_argcheck(L, attrib_count >= 1, 5, "Vertices expected.");

		// Get the vertices
		lua_rawgeti(L, -1, 1);
		int index_count = luaL_getn(L, -1);
		luaL_argcheck(L, index_count >= 3, 5, "Tuple of indices expected");
		face_v[i-1].resize(index_count);
		get_tuple(L, -1, &face_v[i-1][0], index_count);
		lua_pop(L, 1);

		if (attrib_count >= 2)
		{
			lua_rawgeti(L, -1, 2);
			index_count = luaL_getn(L, -1);
			face_uv[i-1].resize(index_count);
			get_tuple(L, -1, &face_uv[i-1][0], index_count);
			lua_pop(L, 1);
		}

		if (attrib_count >= 3)
		{
			lua_rawgeti(L, -1, 3);
			index_count = luaL_getn(L, -1);
			face_norms[i-1].resize(index_count);
			get_tuple(L, -1, &face_norms[i-1][0], index_count);
			lua_pop(L, 1);
		}

		lua_pop(L, 1);
	}

	Mesh *mesh = new Mesh(verts, uvs, normals,
			face_v, face_uv, face_norms);
	GRLUA_DEBUG(*mesh);
	data->node = new GeometryNode(name, mesh);

	luaL_getmetatable(L, SCENE_META);
	lua_setmetatable(L, -2);

	return 1;
}