From 15c41d137de605dc2eb8c68a40cd65bfbb488136 Mon Sep 17 00:00:00 2001 From: Daniel Evans Date: Thu, 19 Jun 2014 16:31:01 +0100 Subject: [PATCH] Improve material rendering, fix texture loading --- rwengine/src/loaders/TextureLoader.cpp | 12 +++++++++--- rwengine/src/render/GameRenderer.cpp | 10 +++++++--- rwviewer/ModelFramesWidget.cpp | 19 ++++++++++++++++--- rwviewer/ModelFramesWidget.hpp | 2 +- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/rwengine/src/loaders/TextureLoader.cpp b/rwengine/src/loaders/TextureLoader.cpp index cca3647e..44f9f33c 100644 --- a/rwengine/src/loaders/TextureLoader.cpp +++ b/rwengine/src/loaders/TextureLoader.cpp @@ -116,7 +116,7 @@ GLuint createTexture(RW::BSTextureNative& texNative, RW::BinaryStreamSection& ro auto coldata = rootSection.raw() + sizeof(RW::BSTextureNative); coldata += sizeof(uint32_t); - GLenum type, format; + GLenum type = GL_UNSIGNED_BYTE, format = GL_RGBA; switch(texNative.rasterformat) { case RW::BSTextureNative::FORMAT_1555: @@ -132,6 +132,8 @@ GLuint createTexture(RW::BSTextureNative& texNative, RW::BinaryStreamSection& ro format = GL_BGRA; type = GL_UNSIGNED_BYTE; break; + default: + break; } glGenTextures(1, &textureName); @@ -211,11 +213,15 @@ bool TextureLoader::loadFromMemory(char *data, GameData *gameData) std::string alpha = std::string(texNative.alphaName); std::transform(name.begin(), name.end(), name.begin(), ::tolower ); std::transform(alpha.begin(), alpha.end(), alpha.begin(), ::tolower ); + bool transparent = false; GLuint id = createTexture(texNative, rootSection, &transparent); - gameData->textures.insert({{name, alpha}, {id, transparent}}); - gameData->textures.insert({{name, ""}, {id, transparent}}); + gameData->textures[{name, alpha}] = {id, transparent}; + + if( !alpha.empty() ) { + gameData->textures[{name, ""}] = {id, transparent}; + } } return true; diff --git a/rwengine/src/render/GameRenderer.cpp b/rwengine/src/render/GameRenderer.cpp index 9c53a57c..b77a8498 100644 --- a/rwengine/src/render/GameRenderer.cpp +++ b/rwengine/src/render/GameRenderer.cpp @@ -246,6 +246,7 @@ void GameRenderer::renderWorld(float alpha) rendered = culled = 0; + glActiveTexture(GL_TEXTURE0); glUniform1i(uniTexture, 0); for(size_t i = 0; i < engine->pedestrians.size(); ++i) { @@ -574,16 +575,19 @@ bool GameRenderer::renderSubgeometry(Model* model, size_t g, size_t sg, const gl if(tex.transparent && queueTransparent) { return false; } - glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex.texName); } else { // Texture pair is missing? } } + else { + glBindTexture(GL_TEXTURE_2D, 0); + } if( (model->geometries[g]->flags & RW::BSGeometry::ModuleMaterialColor) == RW::BSGeometry::ModuleMaterialColor) { auto col = mat.colour; + if(col.a < 255 && queueTransparent) return false; if( object && object->type() == GameObject::Vehicle ) { auto vehicle = static_cast(object); if( (mat.flags&Model::MTF_PrimaryColour) != 0 ) { @@ -593,11 +597,11 @@ bool GameRenderer::renderSubgeometry(Model* model, size_t g, size_t sg, const gl oudata.colour = glm::vec4(vehicle->colourSecondary, 1.f); } else { - oudata.colour = {col.r/255.f, col.g/255.f, col.b/255.f, 1.f}; + oudata.colour = {col.r/255.f, col.g/255.f, col.b/255.f, col.a/255.f}; } } else { - oudata.colour = {col.r/255.f, col.g/255.f, col.b/255.f, 1.f}; + oudata.colour = {col.r/255.f, col.g/255.f, col.b/255.f, col.a/255.f}; } } diff --git a/rwviewer/ModelFramesWidget.cpp b/rwviewer/ModelFramesWidget.cpp index ea2c4b78..61e8bd72 100644 --- a/rwviewer/ModelFramesWidget.cpp +++ b/rwviewer/ModelFramesWidget.cpp @@ -2,22 +2,35 @@ #include #include -void ModelFramesWidget::updateInfoBox(ModelFrame *f) +void ModelFramesWidget::updateInfoBox(Model* model, ModelFrame *f) { if( f == nullptr ) { _frameLabel->setText(""); } else { - auto labText = QString("Name: %1\nTranslation: %2") + auto labText = QString("Name: %1\nTranslation: %2\nTextures:%3") .arg(QString::fromStdString(f->getName())) .arg(QString::fromStdString(glm::to_string(f->getDefaultTranslation()))); + QString geomString; + for(size_t gi : f->getGeometries()) { + auto& g = model->geometries[gi]; + //for(Model::SubGeometry& sg : g->subgeom) + for(Model::Material& m : g->materials) { + for(Model::Texture& t : m.textures) { + geomString += QString("\n %1 (%2)") + .arg(t.name.c_str()) + .arg(t.alphaName.c_str()); + } + } + } + labText = labText.arg(geomString); _frameLabel->setText(labText); } } void ModelFramesWidget::selectedModelChanged(const QModelIndex & n, const QModelIndex &) { - updateInfoBox( (ModelFrame*) n.internalPointer() ); + updateInfoBox( gmodel, (ModelFrame*) n.internalPointer() ); } ModelFramesWidget::ModelFramesWidget(QWidget* parent, Qt::WindowFlags flags) diff --git a/rwviewer/ModelFramesWidget.hpp b/rwviewer/ModelFramesWidget.hpp index 197d520e..211eefff 100644 --- a/rwviewer/ModelFramesWidget.hpp +++ b/rwviewer/ModelFramesWidget.hpp @@ -22,7 +22,7 @@ class ModelFramesWidget : public QDockWidget private slots: - void updateInfoBox(ModelFrame* f); + void updateInfoBox(Model* model, ModelFrame* f); void selectedModelChanged(const QModelIndex&,const QModelIndex&);