X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fmesh.cpp;h=981a59e8002c46585f498887da1c32f00429b7ce;hb=f19366d32cc29287a2730cfba90893e407754081;hp=492f416d1adeccb28e2abf3febfbe874a679abeb;hpb=f1244e29afd2a36aafc2373d485457b4cb0411ff;p=libs%2Fgl.git diff --git a/source/core/mesh.cpp b/source/core/mesh.cpp index 492f416d..981a59e8 100644 --- a/source/core/mesh.cpp +++ b/source/core/mesh.cpp @@ -1,42 +1,31 @@ -#include -#include -#include -#include #include "buffer.h" #include "error.h" #include "mesh.h" #include "renderer.h" #include "resourcemanager.h" -#include "vertexsetup.h" using namespace std; namespace Msp { namespace GL { -Mesh::Mesh(ResourceManager *rm) +Mesh::Mesh(ResourceManager *rm): + vbuf(0), + ibuf(0), + dirty(0), + disallow_rendering(false), + face_winding(NON_MANIFOLD) { - init(rm); + if(rm) + set_manager(rm); } -Mesh::Mesh(const VertexFormat &f, ResourceManager *rm) +Mesh::Mesh(const VertexFormat &f, ResourceManager *rm): + Mesh(rm) { - init(rm); storage(f); } -void Mesh::init(ResourceManager *rm) -{ - vbuf = 0; - ibuf = 0; - dirty = 0; - disallow_rendering = false; - winding = 0; - - if(rm) - set_manager(rm); -} - Mesh::~Mesh() { set_manager(0); @@ -89,7 +78,6 @@ void Mesh::check_buffers(unsigned mask) ibuf = new Buffer; if(!batches.empty()) batches.front().change_buffer(ibuf); - vtx_setup.set_index_buffer(*ibuf); dirty |= INDEX_BUFFER; #ifdef DEBUG @@ -97,6 +85,9 @@ void Mesh::check_buffers(unsigned mask) vbuf->set_debug_name(debug_name+" [IBO]"); #endif } + + if(!batches.empty()) + vtx_setup.set_index_buffer(*ibuf, batches.front().get_index_type()); } } @@ -105,7 +96,7 @@ unsigned Mesh::get_n_vertices() const return vertices.size(); } -float *Mesh::modify_vertex(unsigned i) +char *Mesh::modify_vertex(unsigned i) { if(vertices.get_format().empty()) throw invalid_operation("Mesh::modify_vertex"); @@ -127,7 +118,7 @@ void Mesh::add_batch(const Batch &b) bool reallocate = (batches.size()==batches.capacity()); if(reallocate) { - for(vector::iterator i=batches.end(); i!=batches.begin(); ) + for(auto i=batches.end(); i!=batches.begin(); ) (--i)->use_buffer(0); } @@ -136,22 +127,35 @@ void Mesh::add_batch(const Batch &b) if(reallocate) { prev = 0; - for(vector::iterator i=batches.begin(); i!=batches.end(); ++i) + for(Batch &a: batches) { - i->use_buffer(ibuf, prev); - prev = &*i; + a.use_buffer(ibuf, prev); + prev = &a; } } else batches.back().use_buffer(ibuf, prev); } + DataType existing_type = batches.front().get_index_type(); + DataType added_type = batches.back().get_index_type(); + if(existing_type!=added_type) + { + if(get_type_size(existing_type)>get_type_size(added_type)) + batches.back().set_index_type(existing_type); + else + { + for(Batch &a: batches) + a.set_index_type(added_type); + } + } + check_buffers(INDEX_BUFFER); } -void Mesh::set_winding(const WindingTest *w) +void Mesh::set_winding(FaceWinding w) { - winding = w; + face_winding = w; } void Mesh::draw(Renderer &renderer) const @@ -183,17 +187,17 @@ void Mesh::draw(Renderer &renderer, const VertexSetup *vs, unsigned count) const resize_buffers(); renderer.set_vertex_setup(vs ? vs : &vtx_setup); - renderer.set_winding_test(winding); + renderer.set_front_face(face_winding); if(!count) { - for(vector::const_iterator i=batches.begin(); i!=batches.end(); ++i) - renderer.draw(*i); + for(const Batch &b: batches) + renderer.draw(b); } else { - for(vector::const_iterator i=batches.begin(); i!=batches.end(); ++i) - renderer.draw_instanced(*i, count); + for(const Batch &b: batches) + renderer.draw_instanced(b, count); } } @@ -211,9 +215,9 @@ Resource::AsyncLoader *Mesh::load(IO::Seekable &io, const Resources *) return new AsyncLoader(*this, io); } -UInt64 Mesh::get_data_size() const +uint64_t Mesh::get_data_size() const { - UInt64 size = 0; + uint64_t size = 0; if(vbuf) size += vbuf->get_size(); if(ibuf) @@ -256,17 +260,17 @@ Mesh::Loader::Loader(Mesh &m, bool g): add("storage", &Loader::storage); add("vertices", &Loader::vertices); add("vertices", &Loader::vertices_with_format); - add("winding", &Loader::winding); + add("winding", &Mesh::face_winding); } -void Mesh::Loader::storage(const vector &a) +void Mesh::Loader::storage(const vector &attrs) { - if(a.empty()) + if(attrs.empty()) throw invalid_argument("No vertex attributes"); VertexFormat fmt; - for(vector::const_iterator i=a.begin(); i!=a.end(); ++i) - fmt = (fmt, *i); + for(VertexAttribute a: attrs) + fmt = (fmt, a); obj.storage(fmt); } @@ -290,14 +294,6 @@ void Mesh::Loader::batch(PrimitiveType p) obj.add_batch(btc); } -void Mesh::Loader::winding(FaceWinding w) -{ - if(w==CLOCKWISE) - obj.winding = &WindingTest::clockwise(); - else if(w==COUNTERCLOCKWISE) - obj.winding = &WindingTest::counterclockwise(); -} - Mesh::AsyncLoader::AsyncLoader(Mesh &m, IO::Seekable &i): mesh(m),