2011-04-27 11:58:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011, Blender Foundation.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mesh.h"
|
|
|
|
#include "attribute.h"
|
|
|
|
|
|
|
|
#include "util_debug.h"
|
|
|
|
#include "util_foreach.h"
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/* Attribute */
|
|
|
|
|
|
|
|
void Attribute::set(ustring name_, TypeDesc type_, Element element_)
|
|
|
|
{
|
|
|
|
name = name_;
|
|
|
|
type = type_;
|
|
|
|
element = element_;
|
2012-04-30 12:49:26 +00:00
|
|
|
std = ATTR_STD_NONE;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* string and matrix not supported! */
|
|
|
|
assert(type == TypeDesc::TypeFloat || type == TypeDesc::TypeColor ||
|
|
|
|
type == TypeDesc::TypePoint || type == TypeDesc::TypeVector ||
|
|
|
|
type == TypeDesc::TypeNormal);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Attribute::reserve(int numverts, int numtris)
|
|
|
|
{
|
|
|
|
buffer.resize(buffer_size(numverts, numtris), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Attribute::data_sizeof()
|
|
|
|
{
|
2011-05-31 11:31:00 +00:00
|
|
|
if(type == TypeDesc::TypeFloat)
|
|
|
|
return sizeof(float);
|
|
|
|
else
|
|
|
|
return sizeof(float3);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t Attribute::element_size(int numverts, int numtris)
|
|
|
|
{
|
|
|
|
if(element == VERTEX)
|
|
|
|
return numverts;
|
|
|
|
else if(element == FACE)
|
|
|
|
return numtris;
|
|
|
|
else
|
|
|
|
return numtris*3;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Attribute::buffer_size(int numverts, int numtris)
|
|
|
|
{
|
|
|
|
return element_size(numverts, numtris)*data_sizeof();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Attribute::same_storage(TypeDesc a, TypeDesc b)
|
|
|
|
{
|
|
|
|
if(a == b)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(a == TypeDesc::TypeColor || a == TypeDesc::TypePoint ||
|
|
|
|
a == TypeDesc::TypeVector || a == TypeDesc::TypeNormal)
|
|
|
|
if(b == TypeDesc::TypeColor || b == TypeDesc::TypePoint ||
|
|
|
|
b == TypeDesc::TypeVector || b == TypeDesc::TypeNormal)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-30 12:49:26 +00:00
|
|
|
ustring Attribute::standard_name(AttributeStandard std)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-04-30 12:49:26 +00:00
|
|
|
if(std == ATTR_STD_VERTEX_NORMAL)
|
2011-04-27 11:58:34 +00:00
|
|
|
return ustring("N");
|
2012-04-30 12:49:26 +00:00
|
|
|
else if(std == ATTR_STD_FACE_NORMAL)
|
2011-04-27 11:58:34 +00:00
|
|
|
return ustring("Ng");
|
2012-04-30 12:49:26 +00:00
|
|
|
else if(std == ATTR_STD_UV)
|
2011-04-27 11:58:34 +00:00
|
|
|
return ustring("uv");
|
2012-04-30 12:49:26 +00:00
|
|
|
else if(std == ATTR_STD_GENERATED)
|
2011-04-27 11:58:34 +00:00
|
|
|
return ustring("generated");
|
2012-04-30 12:49:26 +00:00
|
|
|
else if(std == ATTR_STD_POSITION_UNDEFORMED)
|
2011-04-27 11:58:34 +00:00
|
|
|
return ustring("undeformed");
|
2012-04-30 12:49:26 +00:00
|
|
|
else if(std == ATTR_STD_POSITION_UNDISPLACED)
|
2011-04-27 11:58:34 +00:00
|
|
|
return ustring("undisplaced");
|
2012-04-30 12:49:26 +00:00
|
|
|
else if(std == ATTR_STD_MOTION_PRE)
|
|
|
|
return ustring("motion_pre");
|
|
|
|
else if(std == ATTR_STD_MOTION_POST)
|
|
|
|
return ustring("motion_post");
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
return ustring();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attribute Set */
|
|
|
|
|
2012-02-28 16:45:01 +00:00
|
|
|
AttributeSet::AttributeSet()
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-02-28 16:45:01 +00:00
|
|
|
mesh = NULL;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AttributeSet::~AttributeSet()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Attribute *AttributeSet::add(ustring name, TypeDesc type, Attribute::Element element)
|
|
|
|
{
|
|
|
|
Attribute *attr = find(name);
|
|
|
|
|
|
|
|
if(attr) {
|
|
|
|
/* return if same already exists */
|
|
|
|
if(attr->type == type && attr->element == element)
|
|
|
|
return attr;
|
|
|
|
|
|
|
|
/* overwrite attribute with same name but different type/element */
|
|
|
|
remove(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes.push_back(Attribute());
|
|
|
|
attr = &attributes.back();
|
|
|
|
|
|
|
|
if(element == Attribute::VERTEX)
|
|
|
|
attr->set(name, type, element);
|
|
|
|
else if(element == Attribute::FACE)
|
|
|
|
attr->set(name, type, element);
|
|
|
|
else if(element == Attribute::CORNER)
|
|
|
|
attr->set(name, type, element);
|
|
|
|
|
2012-02-28 16:45:01 +00:00
|
|
|
if(mesh)
|
|
|
|
attr->reserve(mesh->verts.size(), mesh->triangles.size());
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
return attr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Attribute *AttributeSet::find(ustring name)
|
|
|
|
{
|
|
|
|
foreach(Attribute& attr, attributes)
|
|
|
|
if(attr.name == name)
|
|
|
|
return &attr;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttributeSet::remove(ustring name)
|
|
|
|
{
|
|
|
|
Attribute *attr = find(name);
|
|
|
|
|
|
|
|
if(attr) {
|
|
|
|
list<Attribute>::iterator it;
|
|
|
|
|
|
|
|
for(it = attributes.begin(); it != attributes.end(); it++) {
|
|
|
|
if(&*it == attr) {
|
|
|
|
attributes.erase(it);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 12:49:26 +00:00
|
|
|
Attribute *AttributeSet::add(AttributeStandard std, ustring name)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
Attribute *attr = NULL;
|
|
|
|
|
|
|
|
if(name == ustring())
|
|
|
|
name = Attribute::standard_name(std);
|
|
|
|
|
2012-04-30 12:49:26 +00:00
|
|
|
if(std == ATTR_STD_VERTEX_NORMAL)
|
2011-04-27 11:58:34 +00:00
|
|
|
attr = add(name, TypeDesc::TypeNormal, Attribute::VERTEX);
|
2012-04-30 12:49:26 +00:00
|
|
|
else if(std == ATTR_STD_FACE_NORMAL)
|
2011-04-27 11:58:34 +00:00
|
|
|
attr = add(name, TypeDesc::TypeNormal, Attribute::FACE);
|
2012-04-30 12:49:26 +00:00
|
|
|
else if(std == ATTR_STD_UV)
|
2011-04-27 11:58:34 +00:00
|
|
|
attr = add(name, TypeDesc::TypePoint, Attribute::CORNER);
|
2012-04-30 12:49:26 +00:00
|
|
|
else if(std == ATTR_STD_GENERATED)
|
2011-04-27 11:58:34 +00:00
|
|
|
attr = add(name, TypeDesc::TypePoint, Attribute::VERTEX);
|
2012-04-30 12:49:26 +00:00
|
|
|
else if(std == ATTR_STD_POSITION_UNDEFORMED)
|
2011-04-27 11:58:34 +00:00
|
|
|
attr = add(name, TypeDesc::TypePoint, Attribute::VERTEX);
|
2012-04-30 12:49:26 +00:00
|
|
|
else if(std == ATTR_STD_POSITION_UNDISPLACED)
|
|
|
|
attr = add(name, TypeDesc::TypePoint, Attribute::VERTEX);
|
|
|
|
else if(std == ATTR_STD_MOTION_PRE)
|
|
|
|
attr = add(name, TypeDesc::TypePoint, Attribute::VERTEX);
|
|
|
|
else if(std == ATTR_STD_MOTION_POST)
|
2011-04-27 11:58:34 +00:00
|
|
|
attr = add(name, TypeDesc::TypePoint, Attribute::VERTEX);
|
|
|
|
else
|
|
|
|
assert(0);
|
|
|
|
|
|
|
|
attr->std = std;
|
|
|
|
|
|
|
|
return attr;
|
|
|
|
}
|
|
|
|
|
2012-04-30 12:49:26 +00:00
|
|
|
Attribute *AttributeSet::find(AttributeStandard std)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
foreach(Attribute& attr, attributes)
|
|
|
|
if(attr.std == std)
|
|
|
|
return &attr;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-30 12:49:26 +00:00
|
|
|
void AttributeSet::remove(AttributeStandard std)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
Attribute *attr = find(std);
|
|
|
|
|
|
|
|
if(attr) {
|
|
|
|
list<Attribute>::iterator it;
|
|
|
|
|
|
|
|
for(it = attributes.begin(); it != attributes.end(); it++) {
|
|
|
|
if(&*it == attr) {
|
|
|
|
attributes.erase(it);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Attribute *AttributeSet::find(AttributeRequest& req)
|
|
|
|
{
|
2012-04-30 12:49:26 +00:00
|
|
|
if(req.std == ATTR_STD_NONE)
|
2011-04-27 11:58:34 +00:00
|
|
|
return find(req.name);
|
|
|
|
else
|
|
|
|
return find(req.std);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttributeSet::reserve(int numverts, int numtris)
|
|
|
|
{
|
|
|
|
foreach(Attribute& attr, attributes)
|
|
|
|
attr.reserve(numverts, numtris);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttributeSet::clear()
|
|
|
|
{
|
|
|
|
attributes.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AttributeRequest */
|
|
|
|
|
|
|
|
AttributeRequest::AttributeRequest(ustring name_)
|
|
|
|
{
|
|
|
|
name = name_;
|
2012-04-30 12:49:26 +00:00
|
|
|
std = ATTR_STD_NONE;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
type = TypeDesc::TypeFloat;
|
|
|
|
element = ATTR_ELEMENT_NONE;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
2012-04-30 12:49:26 +00:00
|
|
|
AttributeRequest::AttributeRequest(AttributeStandard std_)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
name = ustring();
|
|
|
|
std = std_;
|
|
|
|
|
|
|
|
type = TypeDesc::TypeFloat;
|
|
|
|
element = ATTR_ELEMENT_NONE;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AttributeRequestSet */
|
|
|
|
|
|
|
|
AttributeRequestSet::AttributeRequestSet()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AttributeRequestSet::~AttributeRequestSet()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AttributeRequestSet::modified(const AttributeRequestSet& other)
|
|
|
|
{
|
|
|
|
if(requests.size() != other.requests.size())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for(size_t i = 0; i < requests.size(); i++) {
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
for(size_t j = 0; j < requests.size() && !found; j++)
|
|
|
|
if(requests[i].name == other.requests[j].name &&
|
|
|
|
requests[i].std == other.requests[j].std)
|
|
|
|
found = true;
|
|
|
|
|
|
|
|
if(!found)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttributeRequestSet::add(ustring name)
|
|
|
|
{
|
|
|
|
foreach(AttributeRequest& req, requests)
|
|
|
|
if(req.name == name)
|
|
|
|
return;
|
|
|
|
|
|
|
|
requests.push_back(AttributeRequest(name));
|
|
|
|
}
|
|
|
|
|
2012-04-30 12:49:26 +00:00
|
|
|
void AttributeRequestSet::add(AttributeStandard std)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
foreach(AttributeRequest& req, requests)
|
|
|
|
if(req.std == std)
|
|
|
|
return;
|
|
|
|
|
|
|
|
requests.push_back(AttributeRequest(std));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttributeRequestSet::add(AttributeRequestSet& reqs)
|
|
|
|
{
|
|
|
|
foreach(AttributeRequest& req, reqs.requests) {
|
2012-04-30 12:49:26 +00:00
|
|
|
if(req.std == ATTR_STD_NONE)
|
2011-04-27 11:58:34 +00:00
|
|
|
add(req.name);
|
|
|
|
else
|
|
|
|
add(req.std);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AttributeRequestSet::find(ustring name)
|
|
|
|
{
|
|
|
|
foreach(AttributeRequest& req, requests)
|
|
|
|
if(req.name == name)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-30 12:49:26 +00:00
|
|
|
bool AttributeRequestSet::find(AttributeStandard std)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
foreach(AttributeRequest& req, requests)
|
|
|
|
if(req.std == std)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t AttributeRequestSet::size()
|
|
|
|
{
|
|
|
|
return requests.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttributeRequestSet::clear()
|
|
|
|
{
|
|
|
|
requests.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|