Move ColorTexture from RenderableModel to ModelGeometry

This commit is contained in:
Alexander Bock
2020-08-18 16:00:32 +02:00
parent 4f2d612abe
commit d4291163ba
32 changed files with 350 additions and 386 deletions
+54 -1
View File
@@ -30,6 +30,7 @@
#include <ghoul/filesystem/cachemanager.h>
#include <ghoul/filesystem/file.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/io/texture/texturereader.h>
#include <ghoul/logging/logmanager.h>
#include <ghoul/misc/dictionary.h>
#include <ghoul/misc/invariants.h>
@@ -43,6 +44,13 @@ namespace {
constexpr const char* KeyType = "Type";
constexpr const char* KeyGeomModelFile = "GeometryFile";
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 {
@@ -66,12 +74,17 @@ documentation:: Documentation ModelGeometry::Documentation() {
"The file that should be loaded in this ModelGeometry. The file can "
"contain filesystem tokens or can be specified relatively to the "
"location of the .mod file."
},
{
TextureInfo.identifier,
new StringVerifier,
Optional::Yes,
TextureInfo.description
}
}
};
}
std::unique_ptr<ModelGeometry> ModelGeometry::createFromDictionary(
const ghoul::Dictionary& dictionary)
{
@@ -88,6 +101,7 @@ std::unique_ptr<ModelGeometry> ModelGeometry::createFromDictionary(
ModelGeometry::ModelGeometry(const ghoul::Dictionary& dictionary)
: properties::PropertyOwner({ "ModelGeometry" })
, _colorTexturePath(TextureInfo)
{
documentation::testSpecificationAndThrow(
Documentation(),
@@ -96,12 +110,49 @@ 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
));
}
}
double ModelGeometry::boundingRadius() const {
return _boundingRadius;
}
void ModelGeometry::bindTexture() {
if (_texture) {
_texture->bind();
}
}
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);
@@ -186,6 +237,8 @@ void ModelGeometry::deinitialize() {
glDeleteBuffers(1, &_vbo);
glDeleteVertexArrays(1, &_vaoID);
glDeleteBuffers(1, &_ibo);
_texture = nullptr;
}
bool ModelGeometry::loadObj(const std::string& filename) {
+8
View File
@@ -27,7 +27,9 @@
#include <openspace/properties/propertyowner.h>
#include <openspace/properties/stringproperty.h>
#include <ghoul/opengl/ghoul_gl.h>
#include <ghoul/opengl/texture.h>
#include <memory>
namespace ghoul { class Dictionary; }
@@ -55,6 +57,8 @@ public:
virtual bool initialize(Renderable* parent);
virtual void deinitialize();
void bindTexture();
void update();
void render();
virtual bool loadModel(const std::string& filename) = 0;
@@ -73,12 +77,16 @@ 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::unique_ptr<ghoul::opengl::Texture> _texture;
std::vector<Vertex> _vertices;
std::vector<int> _indices;
+3 -49
View File
@@ -36,12 +36,10 @@
#include <openspace/scene/lightsource.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/io/texture/texturereader.h>
#include <ghoul/logging/logmanager.h>
#include <ghoul/misc/invariants.h>
#include <ghoul/misc/profiling.h>
#include <ghoul/opengl/programobject.h>
#include <ghoul/opengl/texture.h>
#include <ghoul/opengl/textureunit.h>
namespace {
@@ -55,13 +53,6 @@ namespace {
"specularIntensity"
};
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."
};
constexpr openspace::properties::Property::PropertyInfo AmbientIntensityInfo = {
"AmbientIntensity",
"Ambient Intensity",
@@ -127,12 +118,6 @@ documentation::Documentation RenderableModel::Documentation() {
Optional::No,
"This specifies the model that is rendered by the Renderable."
},
{
TextureInfo.identifier,
new StringVerifier,
Optional::Yes,
TextureInfo.description
},
{
AmbientIntensityInfo.identifier,
new DoubleVerifier,
@@ -193,7 +178,6 @@ documentation::Documentation RenderableModel::Documentation() {
RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary)
: Renderable(dictionary)
, _colorTexturePath(TextureInfo)
, _ambientIntensity(AmbientIntensityInfo, 0.2f, 0.f, 1.f)
, _diffuseIntensity(DiffuseIntensityInfo, 1.f, 0.f, 1.f)
, _specularIntensity(SpecularIntensityInfo, 1.f, 0.f, 1.f)
@@ -223,12 +207,6 @@ RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary)
_geometry = modelgeometry::ModelGeometry::createFromDictionary(dict);
}
if (dictionary.hasKey(TextureInfo.identifier)) {
_colorTexturePath = absPath(dictionary.value<std::string>(
TextureInfo.identifier
));
}
if (dictionary.hasKey(ModelTransformInfo.identifier)) {
_modelTransform = dictionary.value<glm::dmat3>(ModelTransformInfo.identifier);
}
@@ -268,10 +246,6 @@ RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary)
addPropertySubOwner(_lightSourcePropertyOwner);
addPropertySubOwner(_geometry.get());
addProperty(_colorTexturePath);
_colorTexturePath.onChange(std::bind(&RenderableModel::loadTexture, this));
addProperty(_ambientIntensity);
addProperty(_diffuseIntensity);
addProperty(_specularIntensity);
@@ -291,7 +265,7 @@ RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary)
}
bool RenderableModel::isReady() const {
return _program && _texture;
return _program;
}
void RenderableModel::initialize() {
@@ -318,8 +292,6 @@ void RenderableModel::initializeGL() {
ghoul::opengl::updateUniformLocations(*_program, _uniformCache, UniformNames);
loadTexture();
_geometry->initialize(this);
}
@@ -328,7 +300,6 @@ void RenderableModel::deinitializeGL() {
_geometry->deinitialize();
_geometry = nullptr;
}
_texture = nullptr;
BaseModule::ProgramObjectManager.release(
ProgramName,
@@ -422,7 +393,7 @@ void RenderableModel::render(const RenderData& data, RendererTasks&) {
// Bind texture
ghoul::opengl::TextureUnit unit;
unit.activate();
_texture->bind();
_geometry->bindTexture();
_program->setUniform(_uniformCache.texture, unit);
if (_disableFaceCulling) {
@@ -443,24 +414,7 @@ void RenderableModel::update(const UpdateData&) {
_program->rebuildFromFile();
ghoul::opengl::updateUniformLocations(*_program, _uniformCache, UniformNames);
}
}
void RenderableModel::loadTexture() {
_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();
}
}
_geometry->update();
}
} // namespace openspace
-9
View File
@@ -27,7 +27,6 @@
#include <openspace/rendering/renderable.h>
#include <openspace/properties/stringproperty.h>
#include <openspace/properties/matrix/mat3property.h>
#include <openspace/properties/scalar/boolproperty.h>
#include <openspace/properties/scalar/floatproperty.h>
@@ -36,8 +35,6 @@
#include <openspace/properties/matrix/dmat4property.h>
#include <openspace/properties/vector/vec3property.h>
namespace ghoul::opengl {
class ProgramObject;
class Texture;
@@ -68,14 +65,9 @@ public:
static documentation::Documentation Documentation();
protected:
void loadTexture();
private:
std::unique_ptr<modelgeometry::ModelGeometry> _geometry;
properties::StringProperty _colorTexturePath;
properties::FloatProperty _ambientIntensity;
properties::FloatProperty _diffuseIntensity;
@@ -92,7 +84,6 @@ private:
performShading, texture, ambientIntensity, diffuseIntensity,
specularIntensity) _uniformCache;
std::unique_ptr<ghoul::opengl::Texture> _texture;
std::vector<std::unique_ptr<LightSource>> _lightSources;
// Buffers for uniform uploading
@@ -65,13 +65,6 @@ namespace {
"ProjectorMatrix", "ModelTransform"
};
constexpr openspace::properties::Property::PropertyInfo ColorTextureInfo = {
"ColorTexture",
"Color Base Texture",
"This is the path to a local image file that is used as the base texture for the "
"model on which the image projections are layered."
};
constexpr openspace::properties::Property::PropertyInfo PerformShadingInfo = {
"PerformShading",
"Perform Shading",
@@ -107,12 +100,6 @@ documentation::Documentation RenderableModelProjection::Documentation() {
Optional::No,
"Contains information about projecting onto this planet."
},
{
ColorTextureInfo.identifier,
new StringVerifier,
Optional::No,
ColorTextureInfo.description
},
{
PerformShadingInfo.identifier,
new BoolVerifier,
@@ -134,7 +121,6 @@ documentation::Documentation RenderableModelProjection::Documentation() {
RenderableModelProjection::RenderableModelProjection(const ghoul::Dictionary& dictionary)
: Renderable(dictionary)
, _colorTexturePath(ColorTextureInfo)
, _performShading(PerformShadingInfo, true)
{
documentation::testSpecificationAndThrow(
@@ -146,16 +132,9 @@ RenderableModelProjection::RenderableModelProjection(const ghoul::Dictionary& di
Dictionary geometryDictionary = dictionary.value<Dictionary>(keyGeometry);
_geometry = modelgeometry::ModelGeometry::createFromDictionary(geometryDictionary);
_colorTexturePath = absPath(dictionary.value<std::string>(
ColorTextureInfo.identifier
));
addPropertySubOwner(_geometry.get());
addPropertySubOwner(_projectionComponent);
addProperty(_colorTexturePath);
_colorTexturePath.onChange(std::bind(&RenderableModelProjection::loadTextures, this));
_projectionComponent.initialize(
identifier(),
dictionary.value<ghoul::Dictionary>(keyProjection)
@@ -175,8 +154,7 @@ RenderableModelProjection::RenderableModelProjection(const ghoul::Dictionary& di
RenderableModelProjection::~RenderableModelProjection() {} // NOLINT
bool RenderableModelProjection::isReady() const {
return (_programObject != nullptr) && (_baseTexture != nullptr) &&
_projectionComponent.isReady();
return (_programObject != nullptr) && _projectionComponent.isReady();
}
void RenderableModelProjection::initializeGL() {
@@ -234,7 +212,6 @@ void RenderableModelProjection::deinitializeGL() {
}
_geometry = nullptr;
_baseTexture = nullptr;
_projectionComponent.deinitialize();
@@ -301,7 +278,7 @@ void RenderableModelProjection::render(const RenderData& data, RendererTasks&) {
ghoul::opengl::TextureUnit baseUnit;
baseUnit.activate();
_baseTexture->bind();
_geometry->bindTexture();
_programObject->setUniform(_mainUniformCache.baseTexture, baseUnit);
ghoul::opengl::TextureUnit projectionUnit;
@@ -378,6 +355,8 @@ void RenderableModelProjection::update(const UpdateData& data) {
data.modelTransform.translation;
_sunPosition = static_cast<glm::vec3>(p);
_geometry->update();
}
void RenderableModelProjection::imageProjectGPU(
@@ -489,20 +468,4 @@ void RenderableModelProjection::project() {
_shouldCapture = false;
}
bool RenderableModelProjection::loadTextures() {
_baseTexture = nullptr;
if (!_colorTexturePath.value().empty()) {
_baseTexture = ghoul::io::TextureReader::ref().loadTexture(
absPath(_colorTexturePath)
);
if (_baseTexture) {
LDEBUG(fmt::format("Loaded texture from '{}'", absPath(_colorTexturePath)));
_baseTexture->uploadTexture();
_baseTexture->setFilter(ghoul::opengl::Texture::FilterMode::LinearMipMap);
}
}
return _baseTexture != nullptr;
}
} // namespace openspace
@@ -73,8 +73,6 @@ private:
ProjectionComponent _projectionComponent;
properties::StringProperty _colorTexturePath;
std::unique_ptr<ghoul::opengl::ProgramObject> _programObject;
UniformCache(performShading, directionToSunViewSpace, modelViewTransform,
projectionTransform, projectionFading, baseTexture,
@@ -86,8 +84,6 @@ private:
std::unique_ptr<ghoul::opengl::ProgramObject> _depthFboProgramObject;
UniformCache(ProjectorMatrix, ModelTransform) _depthFboUniformCache;
std::unique_ptr<ghoul::opengl::Texture> _baseTexture;
std::unique_ptr<modelgeometry::ModelGeometry> _geometry;
glm::dmat3 _instrumentMatrix = glm::dmat3(1.0);