Add the ability to add multiple ModelGeometry's for a RenderableModel to show models with multiple OBJ files without needing to create multiple scene graph nodes

This commit is contained in:
Alexander Bock
2020-08-18 16:34:54 +02:00
parent d4291163ba
commit 7603edf906
7 changed files with 213 additions and 93 deletions
+22 -42
View File
@@ -43,14 +43,8 @@ namespace {
constexpr const char* KeyType = "Type";
constexpr const char* KeyGeomModelFile = "GeometryFile";
constexpr const char* KeyColorTexture = "ColorTexture";
constexpr const int8_t CurrentCacheVersion = 3;
constexpr openspace::properties::Property::PropertyInfo TextureInfo = {
"ColorTexture",
"Color Texture",
"This value points to a color texture file that is applied to the geometry "
"rendered in this object."
};
} // namespace
namespace openspace::modelgeometry {
@@ -76,10 +70,11 @@ documentation:: Documentation ModelGeometry::Documentation() {
"location of the .mod file."
},
{
TextureInfo.identifier,
KeyColorTexture,
new StringVerifier,
Optional::Yes,
TextureInfo.description
"This value points to a color texture file that is applied to the "
"geometry rendered in this object."
}
}
};
@@ -99,10 +94,7 @@ std::unique_ptr<ModelGeometry> ModelGeometry::createFromDictionary(
return std::unique_ptr<ModelGeometry>(geometry);
}
ModelGeometry::ModelGeometry(const ghoul::Dictionary& dictionary)
: properties::PropertyOwner({ "ModelGeometry" })
, _colorTexturePath(TextureInfo)
{
ModelGeometry::ModelGeometry(const ghoul::Dictionary& dictionary) {
documentation::testSpecificationAndThrow(
Documentation(),
dictionary,
@@ -111,13 +103,8 @@ ModelGeometry::ModelGeometry(const ghoul::Dictionary& dictionary)
_file = absPath(dictionary.value<std::string>(KeyGeomModelFile));
_colorTexturePath.onChange([this]() { _colorTextureDirty = true; });
addProperty(_colorTexturePath);
if (dictionary.hasKey(TextureInfo.identifier)) {
_colorTexturePath = absPath(dictionary.value<std::string>(
TextureInfo.identifier
));
if (dictionary.hasKey(KeyColorTexture)) {
_colorTexturePath = absPath(dictionary.value<std::string>(KeyColorTexture));
}
}
@@ -131,28 +118,6 @@ void ModelGeometry::bindTexture() {
}
}
void ModelGeometry::update() {
if (_colorTextureDirty) {
_texture = nullptr;
if (!_colorTexturePath.value().empty()) {
_texture = ghoul::io::TextureReader::ref().loadTexture(
absPath(_colorTexturePath)
);
if (_texture) {
LDEBUGC(
"RenderableModel",
fmt::format("Loaded texture from '{}'", absPath(_colorTexturePath))
);
_texture->uploadTexture();
_texture->setFilter(ghoul::opengl::Texture::FilterMode::AnisotropicMipMap);
_texture->purgeFromRAM();
}
}
_colorTextureDirty = false;
}
}
void ModelGeometry::render() {
glBindVertexArray(_vaoID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ibo);
@@ -230,6 +195,21 @@ bool ModelGeometry::initialize(Renderable* parent) {
glBindVertexArray(0);
if (!_colorTexturePath.empty()) {
_texture = ghoul::io::TextureReader::ref().loadTexture(
absPath(_colorTexturePath)
);
if (_texture) {
LDEBUGC(
"RenderableModel",
fmt::format("Loaded texture from '{}'", absPath(_colorTexturePath))
);
_texture->uploadTexture();
_texture->setFilter(ghoul::opengl::Texture::FilterMode::AnisotropicMipMap);
_texture->purgeFromRAM();
}
}
return true;
}
+2 -8
View File
@@ -25,9 +25,6 @@
#ifndef __OPENSPACE_MODULE_BASE___MODELGEOMETRY___H__
#define __OPENSPACE_MODULE_BASE___MODELGEOMETRY___H__
#include <openspace/properties/propertyowner.h>
#include <openspace/properties/stringproperty.h>
#include <ghoul/opengl/ghoul_gl.h>
#include <ghoul/opengl/texture.h>
#include <memory>
@@ -40,7 +37,7 @@ namespace openspace::documentation { struct Documentation; }
namespace openspace::modelgeometry {
class ModelGeometry : public properties::PropertyOwner {
class ModelGeometry {
public:
struct Vertex {
GLfloat location[4];
@@ -58,7 +55,6 @@ public:
virtual bool initialize(Renderable* parent);
virtual void deinitialize();
void bindTexture();
void update();
void render();
virtual bool loadModel(const std::string& filename) = 0;
@@ -77,15 +73,13 @@ protected:
bool loadCachedFile(const std::string& filename);
bool saveCachedFile(const std::string& filename);
properties::StringProperty _colorTexturePath;
bool _colorTextureDirty = false;
GLuint _vaoID = 0;
GLuint _vbo = 0;
GLuint _ibo = 0 ;
GLenum _mode = GL_TRIANGLES;
double _boundingRadius = 0.0;
std::string _colorTexturePath;
std::unique_ptr<ghoul::opengl::Texture> _texture;
std::vector<Vertex> _vertices;
+26 -18
View File
@@ -114,7 +114,13 @@ documentation::Documentation RenderableModel::Documentation() {
{
{
KeyGeometry,
new ReferencingVerifier("base_geometry_model"),
new TableVerifier({
{
"*",
new ReferencingVerifier("base_geometry_model"),
Optional::Yes
}
}),
Optional::No,
"This specifies the model that is rendered by the Renderable."
},
@@ -204,7 +210,11 @@ RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary)
if (dictionary.hasKey(KeyGeometry)) {
ghoul::Dictionary dict = dictionary.value<ghoul::Dictionary>(KeyGeometry);
_geometry = modelgeometry::ModelGeometry::createFromDictionary(dict);
for (int i = 1; i <= dict.size(); ++i) {
std::string key = std::to_string(i);
ghoul::Dictionary geom = dict.value<ghoul::Dictionary>(key);
_geometry.push_back(modelgeometry::ModelGeometry::createFromDictionary(geom));
}
}
if (dictionary.hasKey(ModelTransformInfo.identifier)) {
@@ -244,7 +254,6 @@ RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary)
addPropertySubOwner(_lightSourcePropertyOwner);
addPropertySubOwner(_geometry.get());
addProperty(_ambientIntensity);
addProperty(_diffuseIntensity);
@@ -292,14 +301,16 @@ void RenderableModel::initializeGL() {
ghoul::opengl::updateUniformLocations(*_program, _uniformCache, UniformNames);
_geometry->initialize(this);
for (const std::unique_ptr<modelgeometry::ModelGeometry>& geom : _geometry) {
geom->initialize(this);
}
}
void RenderableModel::deinitializeGL() {
if (_geometry) {
_geometry->deinitialize();
_geometry = nullptr;
for (const std::unique_ptr<modelgeometry::ModelGeometry>& geom : _geometry) {
geom->deinitialize();
}
_geometry.clear();
BaseModule::ProgramObjectManager.release(
ProgramName,
@@ -388,20 +399,18 @@ void RenderableModel::render(const RenderData& data, RendererTasks&) {
_performShading
);
_geometry->setUniforms(*_program);
// Bind texture
ghoul::opengl::TextureUnit unit;
unit.activate();
_geometry->bindTexture();
_program->setUniform(_uniformCache.texture, unit);
if (_disableFaceCulling) {
glDisable(GL_CULL_FACE);
}
_geometry->render();
ghoul::opengl::TextureUnit unit;
unit.activate();
_program->setUniform(_uniformCache.texture, unit);
for (const std::unique_ptr<modelgeometry::ModelGeometry>& geom : _geometry) {
geom->setUniforms(*_program);
geom->bindTexture();
geom->render();
}
if (_disableFaceCulling) {
glEnable(GL_CULL_FACE);
}
@@ -414,7 +423,6 @@ void RenderableModel::update(const UpdateData&) {
_program->rebuildFromFile();
ghoul::opengl::updateUniformLocations(*_program, _uniformCache, UniformNames);
}
_geometry->update();
}
} // namespace openspace
+1 -1
View File
@@ -66,7 +66,7 @@ public:
static documentation::Documentation Documentation();
private:
std::unique_ptr<modelgeometry::ModelGeometry> _geometry;
std::vector<std::unique_ptr<modelgeometry::ModelGeometry>> _geometry;
properties::FloatProperty _ambientIntensity;