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

Added some test code for rendering vehicles

This commit is contained in:
Daniel Evans 2013-07-02 18:37:19 +01:00
parent e02e889745
commit 39475deae7
6 changed files with 117 additions and 5 deletions

View File

@ -164,11 +164,11 @@ void GTAData::loadIMG(const std::string& name)
}
else
{
// Load TXDs immediatley
auto filetype = filename.substr(filename.size() - 3);
for(size_t t = 0; t < filetype.size(); ++t)
// Enter the asset twice..
fileLocations.insert({ filename, { true, archivePath }});
for(size_t t = 0; t < filename.size(); ++t)
{
filetype[t] = tolower(filetype[t]);
filename[t] = tolower(filename[t]);
}
fileLocations.insert({ filename, { true, archivePath }});
}

View File

@ -61,6 +61,14 @@ bool GTAEngine::defineItems(const std::string& name)
std::shared_ptr<LoaderIDE::OBJS_t>(new LoaderIDE::OBJS_t(idel.OBJSs[o]))
});
}
for( size_t v = 0; v < idel.CARSs.size(); ++v) {
std::cout << "Vehicle ID " << idel.CARSs[v].ID << ": " << idel.CARSs[v].gameName << std::endl;
vehicleTypes.insert({
idel.CARSs[v].ID,
std::shared_ptr<LoaderIDE::CARS_t>(new LoaderIDE::CARS_t(idel.CARSs[v]))
});
}
}
else {
std::cerr << "Failed to load IDE " << path << std::endl;
@ -136,3 +144,20 @@ bool GTAEngine::loadZone(const std::string& path)
return false;
}
void GTAEngine::createVehicle(const uint16_t id, const glm::vec3& pos)
{
auto vti = vehicleTypes.find(id);
if(vti != vehicleTypes.end()) {
std::cout << "Creating Vehicle ID " << id << " (" << vti->second->gameName << ")" << std::endl;
if(! vti->second->modelName.empty()) {
gameData.loadDFF(vti->second->modelName + ".dff");
}
if(! vti->second->textureName.empty()) {
gameData.loadTXD(vti->second->textureName + ".txd");
}
vehicleInstances.push_back({ pos, vti->second });
}
}

View File

@ -153,4 +153,56 @@ void GTARenderer::renderWorld(GTAEngine* engine)
}
}
}
for(size_t v = 0; v < engine->vehicleInstances.size(); ++v) {
GTAEngine::GTAVehicle& inst = engine->vehicleInstances[v];
std::string modelname = inst.vehicle->modelName;
std::unique_ptr<Model> &model = engine->gameData.models[modelname];
if(!model)
{
std::cout << "model " << modelname << " not there (" << engine->gameData.models.size() << " models loaded)" << std::endl;
}
for (size_t g = 0; g < model->geometries.size(); g++)
{
RW::BSGeometryBounds& bounds = model->geometries[g].geometryBounds;
if(! camera.frustum.intersects(bounds.center + inst.position, bounds.radius)) {
culled++;
continue;
}
else {
rendered++;
}
// This is a hack I have no idea why negating the quaternion fixes the issue but it does.
glm::mat4 matrixModel;
matrixModel = glm::translate(matrixModel, inst.position);
//matrixModel = glm::scale(matrixModel, scale);
////matrixModel = matrixModel * glm::mat4_cast(rot);
glUniformMatrix4fv(uniModel, 1, GL_FALSE, glm::value_ptr(matrixModel));
glBindBuffer(GL_ARRAY_BUFFER, model->geometries[g].VBO);
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 0, (void*)(model->geometries[g].vertices.size() * sizeof(float) * 3));
glEnableVertexAttribArray(posAttrib);
glEnableVertexAttribArray(texAttrib);
for(size_t sg = 0; sg < model->geometries[g].subgeom.size(); ++sg)
{
if (model->geometries[g].materials.size() > model->geometries[g].subgeom[sg].material) {
// std::cout << model->geometries[g].textures.size() << std::endl;
// std::cout << "Looking for " << model->geometries[g].textures[0].name << std::endl;
if(model->geometries[g].materials[model->geometries[g].subgeom[sg].material].textures.size() > 0) {
textureLoader.bindTexture(model->geometries[g].materials[model->geometries[g].subgeom[sg].material].textures[0].name);
}
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model->geometries[g].subgeom[sg].EBO);
glDrawElements(GL_TRIANGLES, model->geometries[g].subgeom[sg].indices.size(), GL_UNSIGNED_INT, NULL);
}
}
}
}

View File

@ -53,7 +53,7 @@ char* LoaderIMG::loadToMemory(const std::string& assetname)
bool found = false;
for(size_t i = 0; i < m_assets.size(); ++i)
{
if(strcmp(m_assets[i].name, assetname.c_str()) == 0)
if(strcasecmp(m_assets[i].name, assetname.c_str()) == 0)
{
assetInfo = m_assets[i];
found = true;

View File

@ -60,6 +60,16 @@ public:
std::shared_ptr<LoaderIDE::OBJS_t> object;
};
/**
* @class GTAVehicle
* Stores references to the vehicle data and the instance
* @todo some kind of VehicleInstance data so we can save that.
*/
struct GTAVehicle {
glm::vec3 position;
std::shared_ptr<LoaderIDE::CARS_t> vehicle;
};
/**
* Loads an IDE into the game
*/
@ -76,6 +86,11 @@ public:
*/
bool loadZone(const std::string& path);
/**
* Creates a vehicle
*/
void createVehicle(const uint16_t id, const glm::vec3& pos);
/**
* Roughly the middle of everything
*/
@ -107,11 +122,21 @@ public:
*/
std::map<uint16_t, std::shared_ptr<LoaderIDE::OBJS_t>> objectTypes;
/**
* Vehicle definitions
*/
std::map<uint16_t, std::shared_ptr<LoaderIDE::CARS_t>> vehicleTypes;
/**
* Game Objects!
*/
std::vector<GTAInstance> objectInstances;
/**
* Game Vehicles!
*/
std::vector<GTAVehicle> vehicleInstances;
};
#endif

View File

@ -72,6 +72,16 @@ void init(std::string gtapath)
gta->placeItems(gtapath + "/data/maps/industne/industNE.ipl");*/
plyPos = gta->itemCentroid / (float) gta->itemCount;
glm::vec3 spawnPos = plyPos + glm::vec3(0, 0, 100);
size_t k = 1;
// Spawn every vehicle, cause why not.
for(std::map<uint16_t, std::shared_ptr<LoaderIDE::CARS_t>>::iterator it = gta->vehicleTypes.begin();
it != gta->vehicleTypes.end(); ++it) {
gta->createVehicle(it->first, spawnPos);
spawnPos += glm::vec3(5, 0, 0);
if((k++ % 4) == 0) { spawnPos += glm::vec3(-20, 10, 0); }
}
}
void update(float dt)