1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-07 11:22:45 +01:00

refactored Instances into createInstance, added create-instance

This commit is contained in:
Daniel Evans 2013-09-29 23:12:03 +01:00
parent 691928aec1
commit 6445bc50e9
6 changed files with 109 additions and 66 deletions

View File

@ -27,22 +27,7 @@ bool GTAEngine::load()
broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
gameData.load();
// Loade all of the IDEs.
for(std::map<std::string, std::string>::iterator it = gameData.ideLocations.begin();
it != gameData.ideLocations.end();
++it) {
defineItems(it->second);
}
// Load the .zon IPLs since we want to have the zones loaded
for(std::map<std::string, std::string>::iterator it = gameData.iplLocations.begin();
it != gameData.iplLocations.end();
++it) {
loadZone(it->second);
placeItems(it->second);
}
return true;
}
@ -140,37 +125,9 @@ bool GTAEngine::placeItems(const std::string& name)
// Find the object.
for( size_t i = 0; i < ipll.m_instances.size(); ++i) {
LoaderIPLInstance& inst = ipll.m_instances[i];
auto oi = objectTypes.find(inst.id);
if( oi != objectTypes.end()) {
// Make sure the DFF and TXD are loaded
if(! oi->second->modelName.empty()) {
gameData.loadDFF(oi->second->modelName + ".dff");
}
if(! oi->second->textureName.empty()) {
gameData.loadTXD(oi->second->textureName + ".txd");
}
glm::quat instanceRot(-inst.rotW, inst.rotX, inst.rotY, inst.rotZ);
instanceRot = glm::normalize(instanceRot);
objectInstances.push_back(std::shared_ptr<GTAInstance>(new GTAInstance(
this,
glm::vec3(inst.posX, inst.posY, inst.posZ),
instanceRot,
gameData.models[inst.model],
glm::vec3(inst.scaleX, inst.scaleY, inst.scaleZ),
inst, oi->second, nullptr
)));
if( !oi->second->modelName.empty() ) {
modelInstances.insert({
oi->second->modelName,
objectInstances.back()
});
}
}
else {
glm::quat rot(-inst.rotW, inst.rotX, inst.rotY, inst.rotZ);
rot = glm::normalize(rot);
if(! createInstance(inst.id, glm::vec3(inst.posX, inst.posY, inst.posZ), rot)) {
std::cerr << "No object for instance " << inst.id << " (" << path << ")" << std::endl;
}
}
@ -216,6 +173,42 @@ bool GTAEngine::loadZone(const std::string& path)
return false;
}
GTAInstance *GTAEngine::createInstance(const uint16_t id, const glm::vec3& pos, const glm::quat& rot)
{
auto oi = objectTypes.find(id);
if( oi != objectTypes.end()) {
// Make sure the DFF and TXD are loaded
if(! oi->second->modelName.empty()) {
gameData.loadDFF(oi->second->modelName + ".dff");
}
if(! oi->second->textureName.empty()) {
gameData.loadTXD(oi->second->textureName + ".txd");
}
auto instance = std::shared_ptr<GTAInstance>(new GTAInstance(
this,
pos,
rot,
gameData.models[oi->second->modelName],
glm::vec3(1.f, 1.f, 1.f),
oi->second, nullptr
));
objectInstances.push_back(instance);
if( !oi->second->modelName.empty() ) {
modelInstances.insert({
oi->second->modelName,
objectInstances.back()
});
}
return instance.get();
}
return nullptr;
}
GTAVehicle *GTAEngine::createVehicle(const uint16_t id, const glm::vec3& pos, const glm::quat& rot)
{
auto vti = vehicleTypes.find(id);

View File

@ -7,11 +7,10 @@ GTAInstance::GTAInstance(
const glm::quat& rot,
Model* model,
const glm::vec3& scale,
LoaderIPLInstance inst,
std::shared_ptr<LoaderIDE::OBJS_t> obj,
std::shared_ptr<GTAInstance> lod
)
: GTAObject(engine, pos, rot, model), scale(scale), instance(inst), object(obj), LODinstance(lod)
: GTAObject(engine, pos, rot, model), scale(scale), object(obj), LODinstance(lod)
{
btRigidBody* body = nullptr;
auto phyit = engine->gameData.collisions.find(obj->modelName);
@ -20,10 +19,10 @@ GTAInstance::GTAInstance(
btDefaultMotionState* msta = new btDefaultMotionState;
msta->setWorldTransform(btTransform(
btQuaternion(
inst.rotX, inst.rotY, inst.rotZ, inst.rotW
rot.w, rot.x, rot.y, rot.z
).inverse(),
btVector3(
inst.posX, inst.posY, inst.posZ
pos.x, pos.y, pos.z
)
));
btRigidBody::btRigidBodyConstructionInfo info(0.f, msta, cmpShape);
@ -67,7 +66,7 @@ GTAInstance::GTAInstance(
engine->dynamicsWorld->addRigidBody(body);
}
auto pathit = engine->objectNodes.find(inst.id);
auto pathit = engine->objectNodes.find(obj->ID);
if( pathit != engine->objectNodes.end() ) {
auto& pathlist = pathit->second;
for( size_t p = 0; p < pathlist.size(); ++p ) {

View File

@ -280,7 +280,6 @@ void GTARenderer::renderWorld()
for(size_t i = 0; i < engine->objectInstances.size(); ++i) {
GTAInstance& inst = *engine->objectInstances[i];
LoaderIPLInstance &obj = inst.instance;
if(((inst.object->flags & LoaderIDE::OBJS_t::NIGHTONLY) | (inst.object->flags & LoaderIDE::OBJS_t::DAYONLY)) != 0) {
//continue;
@ -288,7 +287,7 @@ void GTARenderer::renderWorld()
if(!inst.model)
{
std::cout << "model " << obj.model << " not loaded (" << engine->gameData.models.size() << " models loaded)" << std::endl;
std::cout << "model " << inst.model << " not loaded (" << engine->gameData.models.size() << " models loaded)" << std::endl;
}
glm::mat4 matrixModel;

View File

@ -83,6 +83,11 @@ public:
*/
bool loadZone(const std::string& path);
/**
* Creates an instance
*/
GTAInstance *createInstance(const uint16_t id, const glm::vec3& pos, const glm::quat& rot = glm::quat());
/**
* Creates a vehicle
*/

View File

@ -10,7 +10,6 @@
struct GTAInstance : public GTAObject
{
glm::vec3 scale;
LoaderIPLInstance instance;
std::shared_ptr<LoaderIDE::OBJS_t> object;
std::shared_ptr<GTAInstance> LODinstance;
@ -20,7 +19,6 @@ struct GTAInstance : public GTAObject
const glm::quat& rot,
Model* model,
const glm::vec3& scale,
LoaderIPLInstance inst,
std::shared_ptr<LoaderIDE::OBJS_t> obj,
std::shared_ptr<GTAInstance> lod
);

View File

@ -86,6 +86,40 @@ void command(const std::string& line)
playerCharacter = gta->createPedestrian(1, plyPos);
player = new GTAPlayerAIController(playerCharacter);
}
else if("list-ipl" == cmd) {
for(std::map<std::string, std::string>::iterator it = gta->gameData.iplLocations.begin();
it != gta->gameData.iplLocations.end();
++it) {
gta->logInfo(it->second);
}
}
else if("load-ipl" == cmd) {
if(line.find(' ') != line.npos) {
std::string ipl = line.substr(line.find(' ')+1);
auto iplit = gta->gameData.iplLocations.find(ipl);
if(iplit != gta->gameData.iplLocations.end()) {
gta->logInfo("Loading: " + iplit->second);
gta->loadZone(iplit->second);
gta->placeItems(iplit->second);
}
else {
gta->logInfo("Not found: " + ipl);
}
}
}
else if("create-instance" == cmd) {
if(line.find(' ') != line.npos) {
std::string ID = line.substr(line.find(' ')+1);
int intID = atoi(ID.c_str());
auto archit = gta->objectTypes.find(intID);
if(archit != gta->objectTypes.end()) {
gta->createInstance(archit->first, plyPos);
}
else {
gta->logInfo("Unkown Object: " + ID);
}
}
}
else {
gta->logInfo("Unkown command: " + cmd);
}
@ -188,7 +222,7 @@ void handleCommandEvent(sf::Event &event)
}
}
void init(std::string gtapath)
void init(std::string gtapath, bool loadWorld)
{
// GTA GET
gta = new GTAEngine(gtapath);
@ -198,13 +232,24 @@ void init(std::string gtapath)
gta->load();
// Test out a known IPL.
/*gta->placeItems(gtapath + "/data/maps/industsw/industSW.ipl");
gta->placeItems(gtapath + "/data/maps/industnw/industNW.ipl");
gta->placeItems(gtapath + "/data/maps/industse/industSE.ipl");
gta->placeItems(gtapath + "/data/maps/industne/industNE.ipl");*/
// Loade all of the IDEs.
for(std::map<std::string, std::string>::iterator it = gta->gameData.ideLocations.begin();
it != gta->gameData.ideLocations.end();
++it) {
gta->defineItems(it->second);
}
plyPos = gta->itemCentroid / (float) gta->itemCount + glm::vec3(0, 0, 2);
if(loadWorld) {
// Load IPLs
for(std::map<std::string, std::string>::iterator it = gta->gameData.iplLocations.begin();
it != gta->gameData.iplLocations.end();
++it) {
gta->loadZone(it->second);
gta->placeItems(it->second);
}
plyPos = gta->itemCentroid / (float) gta->itemCount + glm::vec3(0, 0, 2);
}
glm::vec3 spawnPos = plyPos + glm::vec3(-5, -20, 0.0);
size_t k = 1;
@ -402,10 +447,11 @@ int main(int argc, char *argv[])
glewExperimental = GL_TRUE;
glewInit();
bool loadWorld = false;
size_t w = WIDTH, h = HEIGHT;
int c;
while( (c = getopt(argc, argv, "w:h:")) != -1) {
while( (c = getopt(argc, argv, "w:h:l")) != -1) {
switch(c) {
case 'w':
w = atoi(optarg);
@ -413,6 +459,9 @@ int main(int argc, char *argv[])
case 'h':
h = atoi(optarg);
break;
case 'l':
loadWorld = true;
break;
}
}
@ -421,7 +470,7 @@ int main(int argc, char *argv[])
window.create(sf::VideoMode(w, h), "GTA3 Viewer", sf::Style::Close, cs);
window.setVerticalSyncEnabled(true);
init(argv[optind]);
init(argv[optind], loadWorld);
sf::Clock clock;