From ea7832c7c1ffab00cc1168bc8c41375fdd0eae86 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 21 Mar 2021 11:40:30 +0200 Subject: [PATCH] Rename VertexComponent to VertexAttribute Also rename the related functions. Get_component_type and get_component_size in particular were problematic since pixelformat.h uses the same names, and the ones in vertexformat.h don't take an enum type. --- source/builders/primitivebuilder.cpp | 8 +-- source/builders/vertexarraybuilder.cpp | 20 ++++---- source/builders/vertexbuilder.h | 10 ++-- source/core/mesh.cpp | 8 +-- source/core/mesh.h | 2 +- source/core/program.cpp | 4 +- source/core/program.h | 2 +- source/core/vertexarray.cpp | 2 +- source/core/vertexformat.cpp | 71 +++++++++++++------------- source/core/vertexformat.h | 52 +++++++++++-------- source/core/vertexsetup.cpp | 42 +++++++-------- source/render/instancearray.cpp | 2 +- 12 files changed, 118 insertions(+), 105 deletions(-) diff --git a/source/builders/primitivebuilder.cpp b/source/builders/primitivebuilder.cpp index 4b88618a..1b369430 100644 --- a/source/builders/primitivebuilder.cpp +++ b/source/builders/primitivebuilder.cpp @@ -60,11 +60,11 @@ PrimitiveType PrimitiveBuilder::get_type() const void PrimitiveBuilder::vertex_(const Vector4 &v) { const VertexFormat &format = array.get_format(); - for(const unsigned char *c=format.begin(); c!=format.end(); ++c) + for(const unsigned char *a=format.begin(); a!=format.end(); ++a) { - unsigned t = get_component_type(*c); - if(t=attr.size()) + unsigned sem = get_attribute_semantic(*a); + unsigned sz = get_attribute_size(*a); + if(sem>=attr.size()) ptr += sz; - else if(*c==COLOR4_UBYTE) + else if(*a==COLOR4_UBYTE) { union { unsigned char c[4]; float f; } u; - u.c[0] = static_cast(attr[t].x*255); - u.c[1] = static_cast(attr[t].y*255); - u.c[2] = static_cast(attr[t].z*255); - u.c[3] = static_cast(attr[t].w*255); + u.c[0] = static_cast(attr[sem].x*255); + u.c[1] = static_cast(attr[sem].y*255); + u.c[2] = static_cast(attr[sem].z*255); + u.c[3] = static_cast(attr[sem].w*255); *ptr++ = u.f; } else { - const Vector4 &v = (t==0 ? vtx : attr[t]); + const Vector4 &v = (sem==0 ? vtx : attr[sem]); *ptr++ = v.x; if(sz>=2) *ptr++ = v.y; if(sz>=3) *ptr++ = v.z; diff --git a/source/builders/vertexbuilder.h b/source/builders/vertexbuilder.h index 28fb6be2..6c360572 100644 --- a/source/builders/vertexbuilder.h +++ b/source/builders/vertexbuilder.h @@ -81,19 +81,19 @@ public: { normal(Vector3(x, y, z)); } void normal(const Vector3 &n) - { attrib(get_component_type(NORMAL3), mtx*Vector4(n.x, n.y, n.z, 0)); } + { attrib(get_attribute_semantic(NORMAL3), mtx*Vector4(n.x, n.y, n.z, 0)); } void tangent(float x, float y, float z) { tangent(Vector3(x, y, z)); } void tangent(const Vector3 &t) - { attrib(get_component_type(TANGENT3), mtx*Vector4(t.x, t.y, t.z, 0)); } + { attrib(get_attribute_semantic(TANGENT3), mtx*Vector4(t.x, t.y, t.z, 0)); } void binormal(float x, float y, float z) { binormal(Vector3(x, y, z)); } void binormal(const Vector3 &b) - { attrib(get_component_type(BINORMAL3), mtx*Vector4(b.x, b.y, b.z, 0)); } + { attrib(get_attribute_semantic(BINORMAL3), mtx*Vector4(b.x, b.y, b.z, 0)); } void texcoord(float s) { texcoord(s, 0, 0, 1); } @@ -123,7 +123,7 @@ public: { multitexcoord(i, Vector4(s, t, r, q)); } void multitexcoord(unsigned i, const Vector4 &t) - { attrib(get_component_type(TEXCOORD4)+i, t); } + { attrib(get_attribute_semantic(TEXCOORD4)+i, t); } void color(unsigned char r, unsigned char g, unsigned char b) { color(r, g, b, 255); } @@ -138,7 +138,7 @@ public: { color(Color(r, g, b, a)); } void color(const Color &c) - { attrib(get_component_type(COLOR4_FLOAT), Vector4(c.r, c.g, c.b, c.a)); } + { attrib(get_attribute_semantic(COLOR4_FLOAT), Vector4(c.r, c.g, c.b, c.a)); } void attrib(unsigned i, float x) { attrib(i, x, 0, 0, 1); } diff --git a/source/core/mesh.cpp b/source/core/mesh.cpp index dba8a991..3a79f3e5 100644 --- a/source/core/mesh.cpp +++ b/source/core/mesh.cpp @@ -218,13 +218,13 @@ Mesh::Loader::Loader(Mesh &m, bool g): add("winding", &Loader::winding); } -void Mesh::Loader::vertices(const vector &c) +void Mesh::Loader::vertices(const vector &a) { - if(c.empty()) - throw invalid_argument("No vertex components"); + if(a.empty()) + throw invalid_argument("No vertex attributes"); VertexFormat fmt; - for(vector::const_iterator i=c.begin(); i!=c.end(); ++i) + for(vector::const_iterator i=a.begin(); i!=a.end(); ++i) fmt = (fmt, *i); obj.vertices.reset(fmt); load_sub(obj.vertices); diff --git a/source/core/mesh.h b/source/core/mesh.h index 57c87b7e..c325ade6 100644 --- a/source/core/mesh.h +++ b/source/core/mesh.h @@ -32,7 +32,7 @@ public: public: Loader(Mesh &, bool = true); private: - void vertices(const std::vector &); + void vertices(const std::vector &); void batch(PrimitiveType); void winding(FaceWinding); }; diff --git a/source/core/program.cpp b/source/core/program.cpp index fa9a5ded..7fff5859 100644 --- a/source/core/program.cpp +++ b/source/core/program.cpp @@ -171,9 +171,9 @@ void Program::bind_attribute(unsigned index, const string &name) glBindAttribLocation(id, index, name.c_str()); } -void Program::bind_attribute(VertexComponent comp, const string &name) +void Program::bind_attribute(VertexAttribute attr, const string &name) { - bind_attribute(get_component_type(comp), name); + bind_attribute(get_attribute_semantic(attr), name); } void Program::bind_fragment_data(unsigned index, const string &name) diff --git a/source/core/program.h b/source/core/program.h index 05223627..f318c606 100644 --- a/source/core/program.h +++ b/source/core/program.h @@ -126,7 +126,7 @@ public: DEPRECATED const std::vector &get_attached_shaders() const; DEPRECATED void bind_attribute(unsigned, const std::string &); - DEPRECATED void bind_attribute(VertexComponent, const std::string &); + DEPRECATED void bind_attribute(VertexAttribute, const std::string &); DEPRECATED void bind_fragment_data(unsigned, const std::string &); void link(); diff --git a/source/core/vertexarray.cpp b/source/core/vertexarray.cpp index e3aef7a0..0703b1dd 100644 --- a/source/core/vertexarray.cpp +++ b/source/core/vertexarray.cpp @@ -20,7 +20,7 @@ void VertexArray::reset(const VertexFormat &f) { clear(); format = f; - stride = get_stride(format); + stride = format.stride(); } void VertexArray::clear() diff --git a/source/core/vertexformat.cpp b/source/core/vertexformat.cpp index 6c2e2933..05364be8 100644 --- a/source/core/vertexformat.cpp +++ b/source/core/vertexformat.cpp @@ -14,19 +14,19 @@ VertexFormat::VertexFormat(): count(0) { } -VertexFormat::VertexFormat(VertexComponent c): +VertexFormat::VertexFormat(VertexAttribute a): count(1) { - components[0] = c; + attributes[0] = a; } -VertexFormat VertexFormat::operator,(VertexComponent c) const +VertexFormat VertexFormat::operator,(VertexAttribute a) const { - if(count>=MAX_COMPONENTS) + if(count>=MAX_ATTRIBUTES) throw invalid_operation("VertexFormat::operator,"); VertexFormat r = *this; - r.components[r.count++] = c; + r.attributes[r.count++] = a; return r; } @@ -37,8 +37,8 @@ VertexFormat VertexFormat::operator,(unsigned i) const throw invalid_operation("VertexFormat::operator,"); VertexFormat r = *this; - unsigned char *c = &r.components[r.count-1]; - *c = make_indexed_component(static_cast(*c), i); + unsigned char &a = r.attributes[r.count-1]; + a = make_indexed_attribute(static_cast(a), i); return r; } @@ -47,78 +47,79 @@ bool VertexFormat::operator==(const VertexFormat &other) const { if(count!=other.count) return false; - return equal(components, components+count, other.components); + return equal(attributes, attributes+count, other.attributes); } unsigned VertexFormat::stride() const { unsigned s = 0; for(const unsigned char *i=begin(); i!=end(); ++i) - s += get_component_size(*i); + s += get_attribute_size(*i); return s; } -int VertexFormat::offset(VertexComponent comp) const +int VertexFormat::offset(VertexAttribute attr) const { - unsigned type = get_component_type(comp); - unsigned size = get_component_size(comp); + unsigned sem = get_attribute_semantic(attr); + unsigned sz = get_attribute_size(attr); unsigned offs = 0; for(const unsigned char *i=begin(); i!=end(); ++i) { - if(get_component_type(*i)==type) + if(get_attribute_semantic(*i)==sem) { - if(get_component_size(*i)>=size) + if(get_attribute_size(*i)>=sz) return offs; else return -1; } else - offs += get_component_size(*i); + offs += get_attribute_size(*i); } return -1; } -VertexComponent make_indexed_component(VertexComponent comp, unsigned index) + +VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index) { - if(comp>=TEXCOORD1 && comp<=TEXCOORD4) + if(attr>=TEXCOORD1 && attr<=TEXCOORD4) { if(index>=4) - throw out_of_range("make_indexed_component"); + throw out_of_range("make_indexed_attribute"); } - else if(comp>=ATTRIB1 && comp<=ATTRIB4) + else if(attr>=ATTRIB1 && attr<=ATTRIB4) { if(index>=24) - throw out_of_range("make_indexed_component"); + throw out_of_range("make_indexed_attribute"); } else - throw invalid_argument("make_indexed_component"); - return static_cast(comp+index*4); + throw invalid_argument("make_indexed_attribute"); + return static_cast(attr+index*4); } -void operator>>(const LexicalConverter &conv, VertexComponent &c) +void operator>>(const LexicalConverter &conv, VertexAttribute &a) { const string &str = conv.get(); if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4') - c = static_cast(VERTEX2+(str[6]-'2')); + a = static_cast(VERTEX2+(str[6]-'2')); else if(str=="NORMAL3") - c = NORMAL3; + a = NORMAL3; else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT")) - c = static_cast(COLOR3_FLOAT+(str[5]-'3')); + a = static_cast(COLOR3_FLOAT+(str[5]-'3')); else if(str=="COLOR4_UBYTE") - c = COLOR4_UBYTE; + a = COLOR4_UBYTE; else if(str=="TANGENT3") - c = TANGENT3; + a = TANGENT3; else if(str=="BINORMAL3") - c = BINORMAL3; + a = BINORMAL3; else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4') { if(str.size()==9) - c = static_cast(TEXCOORD1+(str[8]-'1')); + a = static_cast(TEXCOORD1+(str[8]-'1')); else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7') - c = static_cast(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4); + a = static_cast(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4); else - throw lexical_error(format("conversion of '%s' to VertexComponent", str)); + throw lexical_error(format("conversion of '%s' to VertexAttribute", str)); } else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_') { @@ -129,12 +130,12 @@ void operator>>(const LexicalConverter &conv, VertexComponent &c) } catch(const lexical_error &) { - throw lexical_error(format("conversion of '%s' to VertexComponent", str)); + throw lexical_error(format("conversion of '%s' to VertexAttribute", str)); } - c = static_cast(ATTRIB1+(str[6]-'1')+n*4); + a = static_cast(ATTRIB1+(str[6]-'1')+n*4); } else - throw lexical_error(format("conversion of '%s' to VertexComponent", str)); + throw lexical_error(format("conversion of '%s' to VertexAttribute", str)); } } // namespace GL diff --git a/source/core/vertexformat.h b/source/core/vertexformat.h index 35612922..3317a015 100644 --- a/source/core/vertexformat.h +++ b/source/core/vertexformat.h @@ -1,6 +1,7 @@ #ifndef MSP_GL_VERTEXFORMAT_H_ #define MSP_GL_VERTEXFORMAT_H_ +#include #include namespace Msp { @@ -9,7 +10,7 @@ namespace GL { /** A single vertex component. Symbolic names are provided for commonly used attributes. These are aliased with with generic attributes, so be careful when picking your attribute indices. */ -enum VertexComponent +enum VertexAttribute { VERTEX2 = 1, VERTEX3, @@ -30,48 +31,59 @@ enum VertexComponent ATTRIB4 }; +DEPRECATED typedef VertexAttribute VertexComponent; + class VertexFormat { private: - enum { MAX_COMPONENTS = 15 }; + enum { MAX_ATTRIBUTES = 15 }; unsigned char count; - unsigned char components[MAX_COMPONENTS]; + unsigned char attributes[MAX_ATTRIBUTES]; public: VertexFormat(); - VertexFormat(VertexComponent); + VertexFormat(VertexAttribute); - VertexFormat operator,(VertexComponent c) const; - VertexFormat operator,(unsigned i) const; + VertexFormat operator,(VertexAttribute) const; + VertexFormat operator,(unsigned) const; bool operator==(const VertexFormat &) const; bool operator!=(const VertexFormat &other) const { return !(*this==other); } bool empty() const { return !count; } - const unsigned char *begin() const { return components; } - const unsigned char *end() const { return components+count; } + const unsigned char *begin() const { return attributes; } + const unsigned char *end() const { return attributes+count; } unsigned stride() const; - int offset(VertexComponent) const; + int offset(VertexAttribute) const; }; -inline VertexFormat operator,(VertexComponent c1, VertexComponent c2) -{ return (VertexFormat(c1), c2); } +inline VertexFormat operator,(VertexAttribute a1, VertexAttribute a2) +{ return (VertexFormat(a1), a2); } + +inline VertexFormat operator,(VertexAttribute a, unsigned i) +{ return (VertexFormat(a), i); } + +VertexAttribute make_indexed_attribute(VertexAttribute, unsigned); + +DEPRECATED inline VertexAttribute make_indexed_component(VertexAttribute a, unsigned i) +{ return make_indexed_attribute(a, i); } -inline VertexFormat operator,(VertexComponent c, unsigned i) -{ return (VertexFormat(c), i); } +inline unsigned get_attribute_semantic(unsigned char a) +{ return a>>2; } -VertexComponent make_indexed_component(VertexComponent, unsigned); +inline unsigned get_attribute_size(unsigned char a) +{ return (a&3)+1; } -inline unsigned get_component_type(unsigned char c) -{ return c>>2; } +DEPRECATED inline unsigned get_component_type(unsigned char c) +{ return get_attribute_semantic(c); } -inline unsigned get_component_size(unsigned char c) -{ return (c&3)+1; } +DEPRECATED inline unsigned get_component_size(unsigned char c) +{ return get_attribute_size(c); } -inline unsigned get_stride(const VertexFormat &f) +DEPRECATED inline unsigned get_stride(const VertexFormat &f) { return f.stride(); } -void operator>>(const LexicalConverter &, VertexComponent &); +void operator>>(const LexicalConverter &, VertexAttribute &); } // namespace GL } // namespace Msp diff --git a/source/core/vertexsetup.cpp b/source/core/vertexsetup.cpp index d50406a1..6a41397a 100644 --- a/source/core/vertexsetup.cpp +++ b/source/core/vertexsetup.cpp @@ -79,12 +79,12 @@ void VertexSetup::refresh() unsigned VertexSetup::get_attribs(const VertexFormat &fmt) { unsigned mask = 0; - for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c) + for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a) { - unsigned t = get_component_type(*c); - if(t>=get_component_type(ATTRIB1)) - t -= get_component_type(ATTRIB1); - mask |= 1<=get_attribute_semantic(ATTRIB1)) + sem -= get_attribute_semantic(ATTRIB1); + mask |= 1< bind_vbuf(!direct, array.get_buffer(), ARRAY_BUFFER); const VertexFormat &fmt = array.get_format(); - unsigned stride = get_stride(fmt)*sizeof(float); + unsigned stride = fmt.stride()*sizeof(float); if(direct) { glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride); @@ -144,30 +144,30 @@ void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding } unsigned offset = 0; - for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c) + for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a) { - unsigned t = get_component_type(*c); - if(t>=get_component_type(ATTRIB1)) - t -= get_component_type(ATTRIB1); - unsigned sz = get_component_size(*c); + unsigned sem = get_attribute_semantic(*a); + if(sem>=get_attribute_semantic(ATTRIB1)) + sem -= get_attribute_semantic(ATTRIB1); + unsigned sz = get_attribute_size(*a); if(direct) { - if(*c==COLOR4_UBYTE) - glVertexArrayAttribFormat(id, t, 4, GL_UNSIGNED_BYTE, true, offset); + if(*a==COLOR4_UBYTE) + glVertexArrayAttribFormat(id, sem, 4, GL_UNSIGNED_BYTE, true, offset); else - glVertexArrayAttribFormat(id, t, sz, GL_FLOAT, false, offset); - glVertexArrayAttribBinding(id, t, binding); - glEnableVertexArrayAttrib(id, t); + glVertexArrayAttribFormat(id, sem, sz, GL_FLOAT, false, offset); + glVertexArrayAttribBinding(id, sem, binding); + glEnableVertexArrayAttrib(id, sem); } else { - if(*c==COLOR4_UBYTE) - glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast(offset)); + if(*a==COLOR4_UBYTE) + glVertexAttribPointer(sem, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast(offset)); else - glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, reinterpret_cast(offset)); + glVertexAttribPointer(sem, sz, GL_FLOAT, false, stride, reinterpret_cast(offset)); if(ARB_instanced_arrays) - glVertexAttribDivisor(t, divisor); - glEnableVertexAttribArray(t); + glVertexAttribDivisor(sem, divisor); + glEnableVertexAttribArray(sem); } offset += sz*sizeof(float); } diff --git a/source/render/instancearray.cpp b/source/render/instancearray.cpp index abd9fcd0..028af3ad 100644 --- a/source/render/instancearray.cpp +++ b/source/render/instancearray.cpp @@ -46,7 +46,7 @@ InstanceArray::InstanceArray(const Object &o): { instance_data = new VertexArray((ATTRIB4,matrix_location, ATTRIB4,matrix_location+1, ATTRIB4,matrix_location+2)); const VertexFormat &fmt = instance_data->get_format(); - matrix_offset = fmt.offset(make_indexed_component(ATTRIB4, matrix_location)); + matrix_offset = fmt.offset(make_indexed_attribute(ATTRIB4, matrix_location)); instance_buffer = new Buffer(ARRAY_BUFFER); instance_data->use_buffer(instance_buffer); -- 2.43.0