More cleanup of WaveFrontGeometry and RenderableModel

This commit is contained in:
Alexander Bock
2015-02-25 23:58:30 +01:00
parent bd382f9e8e
commit a4e4df3d28
4 changed files with 29 additions and 45 deletions

View File

@@ -25,13 +25,11 @@
#ifndef __RENDERABLEMODEL_H__
#define __RENDERABLEMODEL_H__
// open space includes
#include <openspace/rendering/renderable.h>
#include <openspace/properties/stringproperty.h>
#include <openspace/util/updatestructures.h>
// ghoul includes
#include <ghoul/opengl/programobject.h>
#include <ghoul/opengl/texture.h>
@@ -44,7 +42,6 @@ class ModelGeometry;
class RenderableModel : public Renderable {
public:
RenderableModel(const ghoul::Dictionary& dictionary);
~RenderableModel();
bool initialize() override;
bool deinitialize() override;
@@ -70,7 +67,8 @@ private:
std::string _source;
std::string _destination;
double _time;
psc _sunPosition;
properties::BoolProperty _performShading;
};

View File

@@ -59,8 +59,6 @@ private:
std::vector<Vertex> _vertices;
std::vector<int> _indices;
std::vector<int> _shapeCounts;
};
} // namespace modelgeometry

View File

@@ -58,12 +58,14 @@ RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary)
, _performShading("performShading", "Perform Shading", true)
{
std::string name;
bool success = dictionary.getValue(constants::scenegraphnode::keyName, name);
ghoul_assert(success, "Name was not passed to RenderableModel");
std::string path;
dictionary.getValue(constants::scenegraphnode::keyName, name);
dictionary.getValue(constants::scenegraph::keyPathModule, path);
success = dictionary.getValue(constants::scenegraph::keyPathModule, path);
ghoul_assert(success, "Module path was not passed to RenderableModel");
ghoul::Dictionary geometryDictionary;
bool success = dictionary.getValue(
success = dictionary.getValue(
constants::renderablemodel::keyGeometry, geometryDictionary);
if (success) {
geometryDictionary.setValue(constants::scenegraphnode::keyName, name);
@@ -87,10 +89,6 @@ RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary)
setBoundingSphere(pss(1.f, 9.f));
}
RenderableModel::~RenderableModel(){
}
bool RenderableModel::isReady() const {
bool ready = true;
ready &= (_programObject != nullptr);
@@ -98,7 +96,7 @@ bool RenderableModel::isReady() const {
return ready;
}
bool RenderableModel::initialize(){
bool RenderableModel::initialize() {
bool completeSuccess = true;
if (_programObject == nullptr)
completeSuccess
@@ -114,7 +112,7 @@ bool RenderableModel::initialize(){
return completeSuccess;
}
bool RenderableModel::deinitialize(){
bool RenderableModel::deinitialize() {
if (_geometry) {
_geometry->deinitialize();
delete _geometry;
@@ -127,12 +125,9 @@ bool RenderableModel::deinitialize(){
return true;
}
void RenderableModel::render(const RenderData& data)
{
// activate shader
void RenderableModel::render(const RenderData& data) {
_programObject->activate();
// scale the planet to appropriate size since the planet is a unit sphere
glm::mat4 transform = glm::mat4(1);
glm::mat4 tmp = glm::mat4(1);
@@ -144,16 +139,7 @@ void RenderableModel::render(const RenderData& data)
transform *= tmp;
//glm::mat4 modelview = data.camera.viewMatrix()*data.camera.modelMatrix();
//glm::vec3 camSpaceEye = (-(modelview*data.position.vec4())).xyz;
// setup the data to the shader
// _programObject->setUniform("camdir", camSpaceEye);
psc sun_pos;
double lt;
openspace::SpiceManager::ref().getTargetPosition("SUN", _source, "GALACTIC", "NONE", _time, sun_pos, lt);
_programObject->setUniform("sun_pos", sun_pos.vec3());
_programObject->setUniform("sun_pos", _sunPosition.vec3());
_programObject->setUniform("ViewProjection", data.camera.viewProjectionMatrix());
_programObject->setUniform("ModelTransform", transform);
setPscUniforms(_programObject, &data.camera, data.position);
@@ -172,21 +158,16 @@ void RenderableModel::render(const RenderData& data)
_programObject->deactivate();
}
void RenderableModel::update(const UpdateData& data){
#ifndef NDEBUG
if (_source.empty() || _destination.empty())
return;
#endif
void RenderableModel::update(const UpdateData& data) {
// set spice-orientation in accordance to timestamp
if (!_source.empty())
openspace::SpiceManager::ref().getPositionTransformMatrix(_source, _destination, data.time, _stateMatrix);
_time = data.time;
double lt;
openspace::SpiceManager::ref().getTargetPosition("SUN", _source, "GALACTIC", "NONE", data.time, _sunPosition, lt);
}
void RenderableModel::loadTexture()
{
void RenderableModel::loadTexture() {
delete _texture;
_texture = nullptr;
if (_colorTexturePath.value() != "") {

View File

@@ -157,18 +157,22 @@ bool WavefrontGeometry::loadModel(const std::string& filename) {
LWARNING("Loading models with more than one shape is currently untested");
}
_shapeCounts.resize(shapes.size());
for (int i = 0; i < shapes.size(); ++i)
_shapeCounts[i] = shapes[i].mesh.indices.size();
int totalSize = std::accumulate(_shapeCounts.begin(), _shapeCounts.end(), 0);
int totalSizeIndex = 0;
int totalSizeVertex = 0;
for (int i = 0; i < shapes.size(); ++i) {
totalSizeIndex += shapes[i].mesh.indices.size();
totalSizeVertex += shapes[i].mesh.positions.size();
}
_vertices.resize(totalSize);
_indices.resize(totalSize);
_vertices.resize(totalSizeVertex);
_indices.resize(totalSizeIndex);
// We add all shapes of the model into the same vertex array, one after the other
// The _shapeCounts array stores for each shape, how many vertices that shape has
int p = 0;
for (int i = 0; i < shapes.size(); ++i) {
//for (int j = 0; j < shapes[i].mesh.positions.size(); ++j) {
for (int index : shapes[i].mesh.indices) {
_vertices[index + p].location[0] = shapes[i].mesh.positions[3 * index + 0];
_vertices[index + p].location[1] = shapes[i].mesh.positions[3 * index + 1];
@@ -188,13 +192,14 @@ bool WavefrontGeometry::loadModel(const std::string& filename) {
shapes[i].mesh.indices.end(),
_indices.begin() + p
);
p += _shapeCounts[i];
p += shapes[i].mesh.indices.size();
}
return true;
}
bool WavefrontGeometry::saveCachedFile(const std::string& filename) {
return true;
std::ofstream fileStream(filename, std::ofstream::binary);
if (fileStream.good()) {
fileStream.write(reinterpret_cast<const char*>(&CurrentCacheVersion),
@@ -204,6 +209,7 @@ bool WavefrontGeometry::saveCachedFile(const std::string& filename) {
fileStream.write(reinterpret_cast<const char*>(&vSize), sizeof(int64_t));
fileStream.write(reinterpret_cast<const char*>(_vertices.data()), sizeof(Vertex) * vSize);
fileStream.write(reinterpret_cast<const char*>(_indices.data()), sizeof(int) * vSize);
return fileStream.good();
}
@@ -230,6 +236,7 @@ bool WavefrontGeometry::loadCachedFile(const std::string& filename) {
fileStream.read(reinterpret_cast<char*>(&iSize), sizeof(int64_t));
_vertices.resize(vSize);
_indices.resize(vSize);
fileStream.read(reinterpret_cast<char*>(_vertices.data()), sizeof(Vertex) * vSize);