Browse Source

Totally reworked Trade::AbstractImporter.

AbstractImporter now provides access to the data directly and doesn't
attempt to do any OpenGL stuff, thus making everything more transparent
and testable.
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
f855d72660
  1. 1
      src/CMakeLists.txt
  2. 157
      src/Trade/AbstractImporter.h
  3. 31
      src/Trade/AbstractMaterial.h
  4. 36
      src/Trade/Camera.h
  5. 5
      src/Trade/Image.h
  6. 36
      src/Trade/Light.h
  7. 27
      src/Trade/Mesh.cpp
  8. 100
      src/Trade/Mesh.h
  9. 53
      src/Trade/Object.h
  10. 62
      src/Trade/PhongMaterial.h
  11. 36
      src/Trade/Scene.h
  12. 36
      src/Trade/Texture.h

1
src/CMakeLists.txt

@ -21,6 +21,7 @@ set(Magnum_SRCS
Texture.cpp
Trade/AbstractImporter.cpp
Trade/Mesh.cpp
Math/Math.cpp
)

157
src/Trade/AbstractImporter.h

@ -19,21 +19,11 @@
* @brief Class Magnum::Trade::AbstractImporter
*/
#include <memory>
#include "PluginManager/Plugin.h"
#include "Image.h"
namespace Magnum {
class AbstractShaderProgram;
class AbstractTexture;
class Camera;
class Light;
class Mesh;
class Object;
class Scene;
/**
@brief Data format exchange
@ -43,6 +33,12 @@ for direct access to the data.
namespace Trade {
class AbstractMaterial;
class Camera;
class Light;
class Mesh;
class Object;
class Scene;
class Texture;
/**
@brief Base class for importer plugins
@ -55,30 +51,15 @@ textures etc.
function close() and one or more pairs of data access functions, based on
which features are supported in given format.</p>
<p>For multi-data formats file opening shouldn't take long, all parsing should
be done in data parsing functions, because the user might want to import only
some data. This is obviously not the case for single-data formats like images,
as the file contains all data user wants to import.</p>
@subsection AbstractImporterMemoryManagement Memory management
<p>Every data access function returns std::shared_ptr, thus deletion of
underlying data is done automatically when last shared pointer instance is
destroyed. This allows for data reusing, e.g. one material can be used for many
meshes without the need for complex memory management.</p>
<p>Except for objects, the class should store its own copies of shared pointers
for all requested data until the file is closed, so when user requests the data
and then destroys his copy of shared pointer, the data are not deleted (and next
request will not require parsing them again).</p>
<p>As objects have their own hierarchy which doesn't involve shared pointers,
having copies of shared pointers for them will lead to dangling pointers when
any object deletes its child objects. Thus the class should store only one
shared pointer to root of each object tree.</p>
be done in data parsing functions or even in envelope classes such as Mesh,
because the user might want to import only some data. This is obviously not
the case for single-data formats like images, as the file contains all data
user wants to import.</p>
*/
class MAGNUM_EXPORT AbstractImporter: public Corrade::PluginManager::Plugin {
PLUGIN_INTERFACE("cz.mosra.magnum.Trade.AbstractImporter/0.1")
public:
struct MeshData;
/** @brief Features supported by this importer */
enum Feature {
OpenFile = 0x01, /**< Can open files specified by filename */
@ -133,10 +114,9 @@ class MAGNUM_EXPORT AbstractImporter: public Corrade::PluginManager::Plugin {
* @brief Scene
* @param id Scene ID, from range [0, sceneCount()).
*
* Returns (shared) pointer to given scene or nullptr, if no such scene
* exists.
* Returns pointer to given scene or nullptr, if no such scene exists.
*/
virtual inline std::shared_ptr<Scene> scene(size_t id) { return nullptr; }
virtual inline Scene* scene(size_t id) { return nullptr; }
/** @brief Light count */
virtual inline size_t lightCount() const { return 0; }
@ -145,10 +125,9 @@ class MAGNUM_EXPORT AbstractImporter: public Corrade::PluginManager::Plugin {
* @brief Light
* @param id Light ID, from range [0, lightCount()).
*
* Returns (shared) pointer to given light or nullptr, if no such
* light exists.
* Returns pointer to given light or nullptr, if no such light exists.
*/
virtual inline std::shared_ptr<Light> light(size_t id) { return nullptr; }
virtual inline Light* light(size_t id) { return nullptr; }
/** @brief Camera count */
virtual inline size_t cameraCount() const { return 0; }
@ -157,10 +136,10 @@ class MAGNUM_EXPORT AbstractImporter: public Corrade::PluginManager::Plugin {
* @brief Camera
* @param id Camera ID, from range [0, cameraCount()).
*
* Returns (shared) pointer to given camera or nullptr, if no such
* camera exists.
* Returns pointer to given camera or nullptr, if no such camera
* exists.
*/
virtual inline std::shared_ptr<Camera> camera(size_t id) { return nullptr; }
virtual inline Camera* camera(size_t id) { return nullptr; }
/** @brief Object count (without lights and cameras) */
virtual inline size_t objectCount() const { return 0; }
@ -169,35 +148,21 @@ class MAGNUM_EXPORT AbstractImporter: public Corrade::PluginManager::Plugin {
* @brief Object
* @param id Object ID, from range [0, objectCount()).
*
* Returns (shared) pointer to given object or nullptr, if no such
* object exists.
* Returns pointer to given object or nullptr, if no such object
* exists.
*/
virtual inline std::shared_ptr<Object> object(size_t id) { return nullptr; }
virtual inline Object* object(size_t id) { return nullptr; }
/** @brief Mesh count */
virtual inline size_t meshCount() const { return 0; }
/**
* @brief Mesh data
* @param meshId Mesh ID, from range [0, meshCount()).
* @return (Shared) pointer to given mesh data or nullptr, if no
* such mesh exists.
*
* If you modify the mesh data before mesh/object using them is
* requested, the changes will be reflected in the resulting mesh.
* This can be used for e.g. optimizing or modifying the data using
* functions from MeshTools.
*/
virtual inline std::shared_ptr<MeshData> meshData(size_t meshId) { return nullptr; }
/**
* @brief Mesh
* @param id Mesh ID, from range [0, meshCount()).
*
* Returns (shared) pointer to given mesh or nullptr, if no such
* mesh exists.
* Returns pointer to given mesh or nullptr, if no such mesh exists.
*/
virtual inline std::shared_ptr<Mesh> mesh(size_t id) { return nullptr; }
virtual inline Mesh* mesh(size_t id) { return nullptr; }
/** @brief Material count */
virtual inline size_t materialCount() const { return 0; }
@ -206,22 +171,10 @@ class MAGNUM_EXPORT AbstractImporter: public Corrade::PluginManager::Plugin {
* @brief Material
* @param id Material ID, from range [0, materialCount()).
*
* Returns (shared) pointer to given material or nullptr, if no such
* material exists.
*/
virtual inline std::shared_ptr<AbstractMaterial> material(size_t id) { return nullptr; }
/** @brief Shader count */
virtual inline size_t shaderCount() const { return 0; }
/**
* @brief Shader
* @param id Shader ID, from range [0, shaderCount()).
*
* Returns (shared) pointer to given shader or nullptr, if no such
* shader exists.
* Returns pointer to given material or nullptr, if no such material
* exists.
*/
virtual inline std::shared_ptr<AbstractShaderProgram> shader(size_t id) { return nullptr; }
virtual inline AbstractMaterial* material(size_t id) { return nullptr; }
/** @brief Texture count */
virtual inline size_t textureCount() const { return 0; }
@ -230,10 +183,10 @@ class MAGNUM_EXPORT AbstractImporter: public Corrade::PluginManager::Plugin {
* @brief Texture
* @param id Texture ID, from range [0, textureCount()).
*
* Returns (shared) pointer to given texture or nullptr, if no such
* texture exists.
* Returns pointer to given texture or nullptr, if no such texture
* exists.
*/
virtual inline std::shared_ptr<AbstractTexture> texture(size_t id) { return nullptr; }
virtual inline Texture* texture(size_t id) { return nullptr; }
/** @brief One-dimensional image count */
virtual inline size_t image1DCount() const { return 0; }
@ -242,10 +195,9 @@ class MAGNUM_EXPORT AbstractImporter: public Corrade::PluginManager::Plugin {
* @brief One-dimensional image
* @param id Image ID, from range [0, image1DCount()).
*
* Returns (shared) pointer to given image or nullptr, if no such image
* exists.
* Returns pointer to given image or nullptr, if no such image exists.
*/
virtual inline std::shared_ptr<Image1D> image1D(size_t id) { return nullptr; }
virtual inline Image1D* image1D(size_t id) { return nullptr; }
/** @brief Two-dimensional image count */
virtual inline size_t image2DCount() const { return 0; }
@ -254,10 +206,9 @@ class MAGNUM_EXPORT AbstractImporter: public Corrade::PluginManager::Plugin {
* @brief Two-dimensional image
* @param id Image ID, from range [0, image2DCount()).
*
* Returns (shared) pointer to given image or nullptr, if no such image
* exists.
* Returns pointer to given image or nullptr, if no such image exists.
*/
virtual inline std::shared_ptr<Image2D> image2D(size_t id) { return nullptr; }
virtual inline Image2D* image2D(size_t id) { return nullptr; }
/** @brief Three-dimensional image count */
virtual inline size_t image3DCount() const { return 0; }
@ -266,53 +217,13 @@ class MAGNUM_EXPORT AbstractImporter: public Corrade::PluginManager::Plugin {
* @brief Three-dimensional image
* @param id Image ID, from range [0, image3DCount()).
*
* Returns (shared) pointer to given image or nullptr, if no such image
* exists.
* Returns pointer to given image or nullptr, if no such image exists.
*/
virtual inline std::shared_ptr<Image3D> image3D(size_t id) { return nullptr; }
virtual inline Image3D* image3D(size_t id) { return nullptr; }
/*@}*/
};
/**
@brief Mesh data
Provides direct access to data of any mesh. See also
AbstractImporter::meshData().
*/
class MAGNUM_EXPORT AbstractImporter::MeshData {
public:
/**
* @brief Indices
* @return Indices or nullptr if the mesh is not indexed.
*/
virtual std::vector<unsigned int>* const indices() { return nullptr; }
/**
* @brief Vertices
* @param id ID of vertex data array
* @return Vertices or nullptr if there is no vertex array with given
* ID.
*/
virtual std::vector<Vector3>* const vertices(size_t id) { return nullptr; }
/**
* @brief Normals
* @param id ID of normal data array
* @return Vertices or nullptr if there is no normal array with given
* ID.
*/
virtual std::vector<Vector3>* const normals(size_t id) { return nullptr; }
/**
* @brief 2D texture coordinates
* @param id ID of texture coordinates array
* @return Texture coordinates or nullptr if there is no texture
* coordinates array with given ID.
*/
virtual std::vector<Vector2>* const textureCoords2D(size_t id) { return nullptr; }
};
}}
#endif

31
src/Trade/AbstractMaterial.h

@ -19,26 +19,39 @@
* @brief Class Magnum::Trade::AbstractMaterial
*/
#include "Magnum.h"
namespace Magnum { namespace Trade {
/** @brief Base class for materials */
class AbstractMaterial {
/**
@brief Base class for materials
Subclasses provide access to parameters for given material type.
*/
class MAGNUM_EXPORT AbstractMaterial {
AbstractMaterial(const AbstractMaterial& other) = delete;
AbstractMaterial(AbstractMaterial&& other) = delete;
AbstractMaterial& operator=(const AbstractMaterial& other) = delete;
AbstractMaterial& operator=(AbstractMaterial&& other) = delete;
public:
AbstractMaterial() = default;
/** @brief Material type */
enum Type {
Phong /**< Phong shading */
};
/**
* @brief Use material
*
* Uses associated shader and sets uniforms.
* @brief Constructor
* @param type Material type
*/
virtual bool use(const Matrix4& transformationMatrix, const Matrix4& projectionMatrix) = 0;
inline AbstractMaterial(Type type): _type(type) {}
/** @brief Destructor */
virtual ~AbstractMaterial() {}
/** @brief Material type */
inline Type type() const { return _type; }
private:
Type _type;
};
}}

36
src/Trade/Camera.h

@ -0,0 +1,36 @@
#ifndef Magnum_Trade_Camera_h
#define Magnum_Trade_Camera_h
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
/** @file
* @brief Class Magnum::Trade::Camera
*/
namespace Magnum { namespace Trade {
/**
@brief %Camera
*/
class MAGNUM_EXPORT Camera {
Camera(const Camera& other) = delete;
Camera(Camera&& other) = delete;
Camera& operator=(const Camera& other) = delete;
Camera& operator=(Camera&& other) = delete;
};
}}
#endif

5
src/Trade/Image.h

@ -26,9 +26,8 @@ namespace Magnum { namespace Trade {
/**
@brief %Image
Class for storing data, which are later fed to textures. It is just a
transparent envelope around the data, which holds additional information about
data type and dimensions.
Provides access to image data and additional information about data type and
dimensions.
*/
template<size_t imageDimensions> class Image {
Image<imageDimensions>(const Image<imageDimensions>& other) = delete;

36
src/Trade/Light.h

@ -0,0 +1,36 @@
#ifndef Magnum_Trade_Light_h
#define Magnum_Trade_Light_h
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
/** @file
* @brief Class Magnum::Trade::Light
*/
namespace Magnum { namespace Trade {
/**
@brief %Light
*/
class MAGNUM_EXPORT Light {
Light(const Light& other) = delete;
Light(Light&& other) = delete;
Light& operator=(const Light& other) = delete;
Light& operator=(Light&& other) = delete;
};
}}
#endif

27
src/Trade/Mesh.cpp

@ -0,0 +1,27 @@
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
#include "Mesh.h"
namespace Magnum { namespace Trade {
Mesh::~Mesh() {
delete _indices;
for(auto i: _vertices) delete i;
for(auto i: _normals) delete i;
for(auto i: _textureCoords2D) delete i;
}
}}

100
src/Trade/Mesh.h

@ -0,0 +1,100 @@
#ifndef Magnum_Trade_Mesh_h
#define Magnum_Trade_Mesh_h
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
/** @file
* @brief Class Magnum::Trade::Mesh
*/
#include "../Mesh.h"
namespace Magnum { namespace Trade {
/**
@brief %Mesh
Provides access to mesh data and additional information, such as primitive
type.
*/
class MAGNUM_EXPORT Mesh {
Mesh(const Mesh& other) = delete;
Mesh(Mesh&& other) = delete;
Mesh& operator=(const Mesh& other) = delete;
Mesh& operator=(Mesh&& other) = delete;
public:
/**
* @brief Constructor
* @param primitive Primitive
*/
inline Mesh(Magnum::Mesh::Primitive primitive, std::vector<unsigned int>* indices, std::vector<std::vector<Vector3>*> vertices, std::vector<std::vector<Vector3>*> normals, std::vector<std::vector<Vector2>*> textureCoords2D): _primitive(primitive), _indices(indices), _vertices(vertices), _normals(normals), _textureCoords2D(textureCoords2D) {}
/** @brief Destructor */
~Mesh();
/** @brief Primitive */
inline Magnum::Mesh::Primitive primitive() const { return _primitive; }
/**
* @brief Indices
* @return Indices or nullptr if the mesh is not indexed.
*/
inline std::vector<unsigned int>* indices() { return _indices; }
/** @brief Count of vertex arrays */
inline size_t vertexArrayCount() { return _vertices.size(); };
/**
* @brief Vertices
* @param id ID of vertex data array
* @return Vertices or nullptr if there is no vertex array with given
* ID.
*/
inline std::vector<Vector3>* vertices(size_t id) { return _vertices[id]; }
/** @brief Count of normal arrays */
inline size_t normalArrayCount() { return _normals.size(); };
/**
* @brief Normals
* @param id ID of normal data array
* @return Vertices or nullptr if there is no normal array with given
* ID.
*/
inline std::vector<Vector3>* normals(size_t id) { return _normals[id]; }
/** @brief Count of 2D texture coordinate arrays */
inline size_t textureCoords2DArrayCount() { return _textureCoords2D.size(); };
/**
* @brief 2D texture coordinates
* @param id ID of texture coordinates array
* @return Texture coordinates or nullptr if there is no texture
* coordinates array with given ID.
*/
inline std::vector<Vector2>* textureCoords2D(size_t id) { return _textureCoords2D[id]; }
private:
Magnum::Mesh::Primitive _primitive;
std::vector<unsigned int>* _indices;
std::vector<std::vector<Vector3>*> _vertices,
_normals;
std::vector<std::vector<Vector2>*> _textureCoords2D;
};
}}
#endif

53
src/Trade/Object.h

@ -0,0 +1,53 @@
#ifndef Magnum_Trade_Object_h
#define Magnum_Trade_Object_h
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
/** @file
* @brief Class Magnum::Trade::Object
*/
#include "Magnum.h"
namespace Magnum { namespace Trade {
/**
@brief Object
Provides access to object transformation and hierarchy.
*/
struct MAGNUM_EXPORT Object {
Object(const Object& other) = delete;
Object(Object&& other) = delete;
Object& operator=(const Object& other) = delete;
Object& operator=(Object&& other) = delete;
public:
/**
* @brief Constructor
*/
Object(size_t parent, const Matrix4& transformation): _parent(parent), _transformation(transformation) {}
inline size_t parent() const { return _parent; }
inline size_t transformation() const { return _transformation; }
private:
size_t _parent;
Matrix4 _transformation;
};
}}
#endif

62
src/Trade/PhongMaterial.h

@ -0,0 +1,62 @@
#ifndef Magnum_Trade_PhongMaterial_h
#define Magnum_Trade_PhongMaterial_h
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
/** @file
* @brief Class Magnum::Trade::PhongMaterial
*/
#include "Magnum.h"
#include "AbstractMaterial.h"
namespace Magnum { namespace Trade {
/**
@brief Phong material
*/
class MAGNUM_EXPORT PhongMaterial: public AbstractMaterial {
public:
/**
* @brief Constructor
* @param ambientColor Ambient color
* @param diffuseColor Diffuse color
* @param specularColor Specular color
* @param shininess Shininess
*/
PhongMaterial(const Vector3& ambientColor, const Vector3& diffuseColor, const Vector3& specularColor, GLfloat shininess): AbstractMaterial(Phong), _ambientColor(ambientColor), _diffuseColor(diffuseColor), _specularColor(specularColor), _shininess(shininess) {}
/** @brief Ambient color */
inline Vector3 ambientColor() const { return _ambientColor; }
/** @brief Diffuse color */
inline Vector3 diffuseColor() const { return _diffuseColor; }
/** @brief Specular color */
inline Vector3 specularColor() const { return _specularColor; }
/** @brief Shininess */
inline GLfloat shininess() const { return _shininess; }
private:
Vector3 _ambientColor,
_diffuseColor,
_specularColor;
GLfloat _shininess;
};
}}
#endif

36
src/Trade/Scene.h

@ -0,0 +1,36 @@
#ifndef Magnum_Trade_Scene_h
#define Magnum_Trade_Scene_h
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
/** @file
* @brief Class Magnum::Trade::Scene
*/
namespace Magnum { namespace Trade {
/**
@brief %Scene
*/
class MAGNUM_EXPORT Scene {
Scene(const Scene& other) = delete;
Scene(Scene&& other) = delete;
Scene& operator=(const Scene& other) = delete;
Scene& operator=(Scene&& other) = delete;
};
}}
#endif

36
src/Trade/Texture.h

@ -0,0 +1,36 @@
#ifndef Magnum_Trade_Texture_h
#define Magnum_Trade_Texture_h
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
/** @file
* @brief Class Magnum::Trade::Texture
*/
namespace Magnum { namespace Trade {
/**
@brief %Texture
*/
class MAGNUM_EXPORT Texture {
Texture(const Texture& other) = delete;
Texture(Texture&& other) = delete;
Texture& operator=(const Texture& other) = delete;
Texture& operator=(Texture&& other) = delete;
};
}}
#endif
Loading…
Cancel
Save