converted Engine to use PhyObj for loading PHY files

git-svn-id: svn://anubis/anaglym/trunk@240 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2010-02-10 15:41:41 +00:00
parent cfa0ac5822
commit 9d9c16f3ad
3 changed files with 27 additions and 73 deletions

View File

@ -3,6 +3,7 @@
#include "anaglym.h" #include "anaglym.h"
#include "Engine.h" #include "Engine.h"
#include "Video.h" #include "Video.h"
#include "PhyObj/PhyObj.h"
#include "sdl_keymap.h" #include "sdl_keymap.h"
#include <lua.hpp> #include <lua.hpp>
#include <stdlib.h> /* exit() */ #include <stdlib.h> /* exit() */
@ -30,7 +31,6 @@ using namespace std;
#define doClearHandler(event) \ #define doClearHandler(event) \
do { EVENT_PRESENT_FLAG(event) = false; } while(0) do { EVENT_PRESENT_FLAG(event) = false; } while(0)
#define WHITESPACE " \t\r\n\f"
#define HOOK_TIMEOUT 10000u #define HOOK_TIMEOUT 10000u
#define HOOK_STEPS 250000000 #define HOOK_STEPS 250000000
#define FONT_NAME "FreeSans.ttf" #define FONT_NAME "FreeSans.ttf"
@ -41,25 +41,6 @@ using namespace std;
Engine * g_engine; Engine * g_engine;
/* static helper functions */ /* static helper functions */
static string trim(const string & orig)
{
string result = orig;
size_t pos = result.find_first_not_of(WHITESPACE);
if (pos == string::npos)
{
result = "";
}
else
{
if (pos > 0)
result = result.substr(pos, result.length() - pos);
pos = result.find_last_not_of(WHITESPACE);
if (pos < result.length() - 1)
result = result.substr(0, pos + 1);
}
return result;
}
static void cross_product(dVector3 result, dVector3 first, dVector3 second) static void cross_product(dVector3 result, dVector3 first, dVector3 second)
{ {
result[0] = first[1] * second[2] - first[2] * second[1]; result[0] = first[1] * second[2] - first[2] * second[1];
@ -549,7 +530,7 @@ int Engine::loadModel(const string & name, bool static_data, float scale)
Engine::Object * engine_obj = getObject(id); Engine::Object * engine_obj = getObject(id);
if (engine_obj != NULL) if (engine_obj != NULL)
{ {
engine_obj->loadPhy(phys_path); engine_obj->loadPhy(m_fileLoader, phys_path);
} }
return id; return id;
} }
@ -1427,61 +1408,30 @@ void Engine::Object::render()
checkGLError(); checkGLError();
} }
void Engine::Object::loadPhy(const FileLoader::Path & path) void Engine::Object::loadPhy(FileLoader * fl, const FileLoader::Path & path)
{ {
FileLoader::Buffer buff = g_engine->m_fileLoader->load(path); PhyObj po;
if (buff.size <= 0) po.load(fl, path);
return; for (int i = 0, sz = po.getNumGeoms(); i < sz; i++)
string str(buff.data, buff.size);
stringstream istr(str, ios_base::in);
while (!istr.eof())
{ {
string line; refptr<PhyObj::Geom> geom = po.getGeom(i);
getline(istr, line); refptr< vector<float> > args = geom->getArgs();
line = trim(line); switch (geom->getType())
if (line == "" || line[0] == '#')
continue;
size_t pos = line.find_first_of(WHITESPACE);
if (pos == string::npos)
continue;
string type = line.substr(0, pos);
pos = line.find("\"", pos);
if (pos == string::npos)
continue;
size_t pos2 = line.find("\"", pos + 1);
if (pos2 == string::npos)
continue;
string name = line.substr(pos + 1, pos2 - pos - 1);
pos = pos2 + 1;
refptr< vector<float> > args = new vector<float>();
for (;;)
{ {
pos = line.find_first_not_of(WHITESPACE, pos); case PhyObj::BOX:
if (pos == string::npos) m_ode_object->addBox(args);
break; break;
pos2 = line.find_first_of(WHITESPACE, pos); case PhyObj::SPHERE:
string n = line.substr(pos, pos2 - pos); m_ode_object->addSphere(args);
float f = atof(n.c_str()); break;
args->push_back(f); case PhyObj::CAPSULE:
if (pos2 == string::npos) m_ode_object->addCapsule(args);
break;
case PhyObj::PLANE:
m_ode_object->addPlane(args);
break;
default:
break; break;
pos = pos2 + 1;
}
if (type == "cube")
{
m_ode_object->addBox(args);
}
else if (type == "sphere")
{
m_ode_object->addSphere(args);
}
else if (type == "capsule")
{
m_ode_object->addCapsule(args);
}
else if (type == "plane")
{
m_ode_object->addPlane(args);
} }
} }
m_ode_object->finalize(); m_ode_object->finalize();

View File

@ -36,7 +36,7 @@ class Engine
{ {
m_ode_object->getPosition(x, y, z); m_ode_object->getPosition(x, y, z);
} }
void loadPhy(const FileLoader::Path & path); void loadPhy(FileLoader * fl, const FileLoader::Path & path);
void setVisible(bool visible) { m_is_visible = visible; } void setVisible(bool visible) { m_is_visible = visible; }
bool getVisible() { return m_is_visible; } bool getVisible() { return m_is_visible; }
void addForce(dReal fx, dReal fy, dReal fz) void addForce(dReal fx, dReal fy, dReal fz)

View File

@ -55,7 +55,7 @@ endif
all: $(TARGET) all: $(TARGET)
$(TARGET): $(OBJS) wfobj/WFObj.o OdeWorld/OdeWorld.o TextureCache/TextureCache.o $(TARGET): $(OBJS) wfobj/WFObj.o OdeWorld/OdeWorld.o PhyObj/PhyObj.o TextureCache/TextureCache.o
$(CXX) -o $@ $^ $(LDFLAGS) $(CXX) -o $@ $^ $(LDFLAGS)
ag_lua.c: ag.lua ag_lua.c: ag.lua
@ -69,6 +69,10 @@ wfobj/WFObj.o:
OdeWorld/OdeWorld.o: OdeWorld/OdeWorld.o:
$(MAKE) -C OdeWorld $(MAKE) -C OdeWorld
.PHONY: PhyObj/PhyObj.o
PhyObj/PhyObj.o:
$(MAKE) -C PhyObj
.PHONY: TextureCache/TextureCache.o .PHONY: TextureCache/TextureCache.o
TextureCache/TextureCache.o: TextureCache/TextureCache.o:
$(MAKE) -C TextureCache $(MAKE) -C TextureCache