Pointer attributes, cleanup.

This commit is contained in:
Andrzej Kapolka 2013-12-13 14:33:18 -08:00
parent a10aca59f3
commit 50e4691568
4 changed files with 98 additions and 41 deletions

View file

@ -37,8 +37,10 @@ void MetavoxelSystem::init() {
AttributePointer color = AttributeRegistry::getInstance()->getAttribute("color");
void* white = encodeInline(qRgba(0xFF, 0xFF, 0xFF, 0xFF));
_data.setAttributeValue(p1, AttributeValue(color, &white));
_data.setAttributeValue(p1, AttributeValue(color, encodeInline(qRgba(0xFF, 0xFF, 0xFF, 0xFF))));
bool blerp = true;
_data.setAttributeValue(p1, AttributeValue(AttributeRegistry::getInstance()->getAttribute("voxelizer"), &blerp));
_buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
_buffer.create();
@ -102,7 +104,8 @@ void MetavoxelSystem::render() {
MetavoxelSystem::PointVisitor::PointVisitor(QVector<Point>& points) :
MetavoxelVisitor(QVector<AttributePointer>() <<
AttributeRegistry::getInstance()->getColorAttribute() <<
AttributeRegistry::getInstance()->getNormalAttribute()),
AttributeRegistry::getInstance()->getNormalAttribute() <<
AttributeRegistry::getInstance()->getVoxelizerAttribute()),
_points(points) {
}
@ -112,6 +115,7 @@ bool MetavoxelSystem::PointVisitor::visit(const MetavoxelInfo& info) {
}
QRgb color = info.attributeValues.at(0).getInlineValue<QRgb>();
QRgb normal = info.attributeValues.at(1).getInlineValue<QRgb>();
bool blerp = *info.attributeValues.at(2).getPointerValue<bool>();
int alpha = qAlpha(color);
if (alpha > 0) {
Point point = { glm::vec4(info.minimum + glm::vec3(info.size, info.size, info.size) * 0.5f, info.size),

View file

@ -12,7 +12,8 @@ AttributeRegistry AttributeRegistry::_instance;
AttributeRegistry::AttributeRegistry() :
_colorAttribute(registerAttribute(new QRgbAttribute("color"))),
_normalAttribute(registerAttribute(new QRgbAttribute("normal", qRgb(0, 127, 0)))) {
_normalAttribute(registerAttribute(new QRgbAttribute("normal", qRgb(0, 127, 0)))),
_voxelizerAttribute(registerAttribute(new PointerAttribute<bool>("voxelizer", false))) {
}
AttributePointer AttributeRegistry::registerAttribute(AttributePointer attribute) {
@ -23,39 +24,16 @@ AttributePointer AttributeRegistry::registerAttribute(AttributePointer attribute
return pointer;
}
AttributeValue::AttributeValue(const AttributePointer& attribute, void* const* value) :
_attribute(attribute) {
if (_attribute) {
_value = _attribute->create(value);
}
AttributeValue::AttributeValue(const AttributePointer& attribute) :
_attribute(attribute), _value(attribute ? attribute->getDefaultValue() : NULL) {
}
AttributeValue::AttributeValue(const AttributeValue& other) :
_attribute(other._attribute) {
if (_attribute) {
_value = _attribute->create(&other._value);
}
}
AttributeValue::~AttributeValue() {
if (_attribute) {
_attribute->destroy(_value);
}
}
AttributeValue& AttributeValue::operator=(const AttributeValue& other) {
if (_attribute) {
_attribute->destroy(_value);
}
if ((_attribute = other._attribute)) {
_value = _attribute->create(&other._value);
}
AttributeValue::AttributeValue(const AttributePointer& attribute, void* value) :
_attribute(attribute), _value(value) {
}
void* AttributeValue::copy() const {
return _attribute->create(&_value);
return _attribute->create(_value);
}
bool AttributeValue::isDefault() const {
@ -70,6 +48,33 @@ bool AttributeValue::operator==(void* other) const {
return _attribute && _attribute->equal(_value, other);
}
OwnedAttributeValue::OwnedAttributeValue(const AttributePointer& attribute) :
AttributeValue(attribute, attribute ? attribute->create() : NULL) {
}
OwnedAttributeValue::OwnedAttributeValue(const AttributePointer& attribute, void* value) :
AttributeValue(attribute, attribute ? attribute->create(value) : NULL) {
}
OwnedAttributeValue::OwnedAttributeValue(const AttributeValue& other) :
AttributeValue(other.getAttribute(), other.getAttribute() ? other.copy() : NULL) {
}
OwnedAttributeValue::~OwnedAttributeValue() {
if (_attribute) {
_attribute->destroy(_value);
}
}
OwnedAttributeValue& OwnedAttributeValue::operator=(const AttributeValue& other) {
if (_attribute) {
_attribute->destroy(_value);
}
if ((_attribute = other.getAttribute())) {
_value = _attribute->create(other.getValue());
}
}
Attribute::Attribute(const QString& name) : _name(name) {
}

View file

@ -48,6 +48,9 @@ public:
/// Returns a reference to the standard QRgb "normal" attribute.
const AttributePointer& getNormalAttribute() const { return _normalAttribute; }
/// Returns a reference to the standard "voxelizer" attribute.
const AttributePointer& getVoxelizerAttribute() const { return _voxelizerAttribute; }
private:
static AttributeRegistry _instance;
@ -55,6 +58,7 @@ private:
QHash<QString, AttributePointer> _attributes;
AttributePointer _colorAttribute;
AttributePointer _normalAttribute;
AttributePointer _voxelizerAttribute;
};
/// Converts a value to a void pointer.
@ -71,11 +75,8 @@ template<class T> inline T decodeInline(void* value) {
class AttributeValue {
public:
AttributeValue(const AttributePointer& attribute = AttributePointer(), void* const* value = NULL);
AttributeValue(const AttributeValue& other);
~AttributeValue();
AttributeValue& operator=(const AttributeValue& other);
AttributeValue(const AttributePointer& attribute = AttributePointer());
AttributeValue(const AttributePointer& attribute, void* value);
AttributePointer getAttribute() const { return _attribute; }
void* getValue() const { return _value; }
@ -83,6 +84,8 @@ public:
template<class T> void setInlineValue(T value) { _value = encodeInline(value); }
template<class T> T getInlineValue() const { return decodeInline<T>(_value); }
template<class T> T* getPointerValue() const { return static_cast<T*>(_value); }
void* copy() const;
bool isDefault() const;
@ -90,12 +93,24 @@ public:
bool operator==(const AttributeValue& other) const;
bool operator==(void* other) const;
private:
protected:
AttributePointer _attribute;
void* _value;
};
// Assumes ownership of an attribute value.
class OwnedAttributeValue : public AttributeValue {
public:
OwnedAttributeValue(const AttributePointer& attribute = AttributePointer());
OwnedAttributeValue(const AttributePointer& attribute, void* value);
OwnedAttributeValue(const AttributeValue& other);
~OwnedAttributeValue();
OwnedAttributeValue& operator=(const AttributeValue& other);
};
/// Represents a registered attribute.
class Attribute {
public:
@ -107,7 +122,8 @@ public:
const QString& getName() const { return _name; }
virtual void* create(void* const* copy = NULL) const = 0;
void* create() const { return create(getDefaultValue()); }
virtual void* create(void* copy) const = 0;
virtual void destroy(void* value) const = 0;
virtual bool read(Bitstream& in, void*& value) const = 0;
@ -130,7 +146,7 @@ public:
InlineAttribute(const QString& name, T defaultValue = T()) : Attribute(name), _defaultValue(encodeInline(defaultValue)) { }
virtual void* create(void* const* copy = NULL) const { return (copy == NULL) ? _defaultValue : *copy; }
virtual void* create(void* copy) const { return copy; }
virtual void destroy(void* value) const { /* no-op */ }
virtual bool read(Bitstream& in, void*& value) const { value = getDefaultValue(); in.read(&value, bits); return false; }
@ -165,4 +181,36 @@ public:
virtual void* createAveraged(void* values[]) const;
};
/// An attribute class that stores pointers to its values.
template<class T> class PointerAttribute : public Attribute {
public:
PointerAttribute(const QString& name, T defaultValue = T()) : Attribute(name), _defaultValue(defaultValue) { }
virtual void* create(void* copy) const { new T(*static_cast<T*>(copy)); }
virtual void destroy(void* value) const { delete static_cast<T*>(value); }
virtual bool read(Bitstream& in, void*& value) const { in >> *static_cast<T*>(value); return true; }
virtual bool write(Bitstream& out, void* value) const { out << *static_cast<T*>(value); return true; }
virtual bool equal(void* first, void* second) const { return *static_cast<T*>(first) == *static_cast<T*>(second); }
virtual void* createAveraged(void* values[]) const;
virtual void* getDefaultValue() const { return const_cast<void*>((void*)&_defaultValue); }
private:
T _defaultValue;
};
template<class T> inline void* PointerAttribute<T>::createAveraged(void* values[]) const {
T* total = new T();
for (int i = 0; i < AVERAGE_COUNT; i++) {
*total += *static_cast<T*>(values[i]);
}
*total /= AVERAGE_COUNT;
return total;
}
#endif /* defined(__interface__AttributeRegistry__) */

View file

@ -150,7 +150,7 @@ void MetavoxelNode::setAttributeValue(const AttributeValue& attributeValue) {
}
AttributeValue MetavoxelNode::getAttributeValue(const AttributePointer& attribute) const {
return AttributeValue(attribute, &_attributeValue);
return AttributeValue(attribute, _attributeValue);
}
bool MetavoxelNode::isLeaf() const {