openrw/rwlib/source/data/Clump.hpp

283 lines
5.6 KiB
C++
Raw Normal View History

2013-09-25 09:05:18 +01:00
#pragma once
#ifndef _MODEL_HPP_
#define _MODEL_HPP_
#include <algorithm>
2014-02-09 03:14:43 +00:00
#include <glm/glm.hpp>
2013-09-25 09:05:18 +01:00
#include <memory>
#include <string>
#include <vector>
2013-09-25 09:05:18 +01:00
#include <gl/DrawBuffer.hpp>
#include <gl/GeometryBuffer.hpp>
#include <gl/TextureData.hpp>
#include <loaders/RWBinaryStream.hpp>
2013-09-25 09:05:18 +01:00
#include <rw/forward.hpp>
2014-02-13 10:55:11 +00:00
/**
* ModelFrame stores transformation hierarchy
2014-02-13 10:55:11 +00:00
*/
2014-02-09 03:14:43 +00:00
class ModelFrame {
unsigned int index;
glm::mat3 defaultRotation;
glm::vec3 defaultTranslation;
glm::mat4 matrix;
glm::mat4 worldtransform_;
ModelFrame* parent_;
std::string name;
std::vector<ModelFramePtr> children_;
2014-02-09 03:14:43 +00:00
public:
ModelFrame(unsigned int index = 0, glm::mat3 dR = glm::mat3(),
glm::vec3 dT = glm::vec3());
void reset();
void setTransform(const glm::mat4& m) {
matrix = m;
updateHierarchyTransform();
}
const glm::mat4& getTransform() const {
return matrix;
}
2014-02-09 03:14:43 +00:00
void setName(const std::string& fname) {
name = fname;
}
2014-02-09 03:14:43 +00:00
unsigned int getIndex() const {
return index;
}
2014-02-09 03:14:43 +00:00
glm::vec3 getDefaultTranslation() const {
return defaultTranslation;
}
2014-02-09 03:14:43 +00:00
glm::mat3 getDefaultRotation() const {
return defaultRotation;
}
2014-02-09 03:14:43 +00:00
void setTranslation(const glm::vec3& t) {
matrix[3] = glm::vec4(t, matrix[3][3]);
updateHierarchyTransform();
}
void setRotation(const glm::mat3& r) {
for (unsigned int i = 0; i < 3; i++) {
matrix[i] = glm::vec4(r[i], matrix[i][3]);
}
updateHierarchyTransform();
}
/**
* Updates the cached matrix
*/
void updateHierarchyTransform();
/**
* @return the cached world transformation for this Frame
*/
const glm::mat4& getWorldTransform() const {
return worldtransform_;
}
2014-02-09 03:14:43 +00:00
ModelFrame* getParent() const {
return parent_;
}
2014-02-09 03:14:43 +00:00
void addChild(ModelFramePtr child);
const std::vector<ModelFramePtr>& getChildren() const {
return children_;
}
2014-02-09 03:14:43 +00:00
const std::string& getName() const {
return name;
}
ModelFrame* findDescendant(const std::string& name) const;
ModelFramePtr cloneHierarchy() const;
2014-02-09 03:14:43 +00:00
};
2014-02-13 10:55:11 +00:00
/**
* Subgeometry
2014-02-13 10:55:11 +00:00
*/
struct SubGeometry {
GLuint start = 0;
size_t material = 0;
std::vector<uint32_t> indices;
size_t numIndices = 0;
};
struct GeometryVertex {
glm::vec3 position; /* 0 */
glm::vec3 normal; /* 24 */
glm::vec2 texcoord; /* 48 */
glm::u8vec4 colour; /* 64 */
/** @see GeometryBuffer */
static const AttributeList vertex_attributes() {
return {{ATRS_Position, 3, sizeof(GeometryVertex), 0ul},
{ATRS_Normal, 3, sizeof(GeometryVertex), sizeof(float) * 3},
{ATRS_TexCoord, 2, sizeof(GeometryVertex), sizeof(float) * 6},
{ATRS_Colour, 4, sizeof(GeometryVertex), sizeof(float) * 8,
GL_UNSIGNED_BYTE}};
}
GeometryVertex(glm::vec3 _position, glm::vec3 _normal, glm::vec2 _texcoord, glm::u8vec4 _colour)
: position(_position)
, normal(_normal)
, texcoord(_texcoord)
, colour(_colour) {
}
GeometryVertex() {
}
};
/**
* Geometry
*/
struct Geometry {
enum FaceType { Triangles = 0, TriangleStrip = 1 };
struct Texture {
std::string name;
std::string alphaName;
TextureData::Handle texture;
};
enum { MTF_PrimaryColour = 1 << 0, MTF_SecondaryColour = 1 << 1 };
struct Material {
std::vector<Texture> textures;
glm::u8vec4 colour;
uint8_t flags;
float diffuseIntensity;
float ambientIntensity;
};
DrawBuffer dbuff;
GeometryBuffer gbuff;
2013-09-25 09:05:18 +01:00
GLuint EBO;
RW::BSGeometryBounds geometryBounds;
uint32_t clumpNum;
FaceType facetype;
2013-09-25 09:05:18 +01:00
uint32_t flags;
2013-09-25 09:05:18 +01:00
std::vector<Material> materials;
std::vector<SubGeometry> subgeom;
Geometry();
~Geometry();
};
/**
* @brief The Atomic struct
*/
class Atomic {
ModelFramePtr frame_;
GeometryPtr geometry_;
uint32_t flags_;
public:
enum {
// If this is set, the atomic will be rendered
ATOMIC_RENDER = 0x04
};
void setFrame(ModelFramePtr frame) {
frame_ = frame;
}
const ModelFramePtr& getFrame() const {
return frame_;
}
void setGeometry(GeometryPtr geom) {
geometry_ = geom;
}
const GeometryPtr& getGeometry() const {
return geometry_;
}
void setFlags(uint32_t flags) {
flags_ = flags;
}
uint32_t getFlags() const {
return flags_;
}
void setFlag(uint32_t flag, bool set) {
if (set) {
flags_ |= flag;
}
else {
flags_ &= ~flag;
}
}
AtomicPtr clone() const;
};
/**
* A clump is a collection of Frames and Atomics
*/
class Clump {
public:
/**
* @brief findFrame Locates frame with name anywhere in the hierarchy
* @param name
* @return
*/
ModelFrame* findFrame(const std::string& name) const;
2017-01-03 14:18:06 +00:00
~Clump();
void recalculateMetrics();
float getBoundingRadius() const {
return boundingRadius;
}
void addAtomic(AtomicPtr& atomic) {
atomics_.push_back(atomic);
}
const AtomicList& getAtomics() const {
return atomics_;
}
void setFrame(ModelFramePtr& root) {
rootframe_ = root;
}
const ModelFramePtr& getFrame() const {
return rootframe_;
}
/**
* @return A Copy of the frames and atomics in this clump
*/
Clump* clone() const;
private:
float boundingRadius;
AtomicList atomics_;
ModelFramePtr rootframe_;
2013-09-25 09:05:18 +01:00
};
2014-03-01 04:12:35 +00:00
#endif