From: Mikko Rasa Date: Mon, 24 Mar 2008 00:10:57 +0000 (+0000) Subject: Support specifying elements in PrimitiveBuilder X-Git-Tag: 0.9~7 X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=0f3ae7cf6aed59f07895dfbef6740be3b666fc20;p=libs%2Fgl.git Support specifying elements in PrimitiveBuilder Add use_textures flag to ObjectPass Add type indicator to uniform* statements of ProgramData --- diff --git a/source/immediate.cpp b/source/immediate.cpp index 8e761f43..8b62d2c4 100644 --- a/source/immediate.cpp +++ b/source/immediate.cpp @@ -18,9 +18,15 @@ Immediate::Immediate(VertexFormat f): void Immediate::end_() { array.apply(); - glDrawArrays(type, 0, array.size()); + glDrawElements(type, indices.size(), UNSIGNED_INT, &indices[0]); array.clear(); + indices.clear(); +} + +void Immediate::element_(unsigned i) +{ + indices.push_back(i); } } // namespace GL diff --git a/source/immediate.h b/source/immediate.h index 7e99272b..c4d8aca5 100644 --- a/source/immediate.h +++ b/source/immediate.h @@ -24,12 +24,14 @@ class Immediate: public PrimitiveBuilder { private: VertexArray array; + std::vector indices; public: Immediate(VertexFormat); private: virtual void begin_() { } virtual void end_(); + virtual void element_(unsigned); }; } // namespace GL diff --git a/source/meshbuilder.cpp b/source/meshbuilder.cpp index cd3be666..025bced1 100644 --- a/source/meshbuilder.cpp +++ b/source/meshbuilder.cpp @@ -14,21 +14,24 @@ namespace GL { MeshBuilder::MeshBuilder(Mesh &m): PrimitiveBuilder(m.vertices), mesh(m), - first(0) + batch(0) { } void MeshBuilder::begin_() { - first=array.size(); + batch=new Batch(type); } void MeshBuilder::end_() { - Batch batch(type); - unsigned last=array.size(); - for(unsigned i=first; iappend(i); } } // namespace GL diff --git a/source/meshbuilder.h b/source/meshbuilder.h index dbeb0f52..c8354b9e 100644 --- a/source/meshbuilder.h +++ b/source/meshbuilder.h @@ -13,19 +13,21 @@ Distributed under the LGPL namespace Msp { namespace GL { +class Batch; class Mesh; class MeshBuilder: public PrimitiveBuilder { private: Mesh &mesh; - unsigned first; + Batch *batch; public: MeshBuilder(Mesh &); private: virtual void begin_(); virtual void end_(); + virtual void element_(unsigned); }; } // namespace GL diff --git a/source/object.cpp b/source/object.cpp index 56b26482..f710145a 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -79,7 +79,7 @@ void Object::setup_render(const ObjectPass &pass) const textures[i]->bind(); } } - else if(main_texture) + else if(main_texture && pass.use_textures) main_texture->bind(); if(material) diff --git a/source/objectpass.cpp b/source/objectpass.cpp index 153186d8..92864b45 100644 --- a/source/objectpass.cpp +++ b/source/objectpass.cpp @@ -29,6 +29,7 @@ ObjectPass::Loader::Loader(ObjectPass &p, Collection &c): coll(c) { add("shader", &Loader::shader); + add("use_textures", &ObjectPass::use_textures); } void ObjectPass::Loader::shader(const string &n) diff --git a/source/objectpass.h b/source/objectpass.h index 59a1dfb4..e8190fa7 100644 --- a/source/objectpass.h +++ b/source/objectpass.h @@ -37,6 +37,7 @@ struct ObjectPass Program *shprog; ProgramData *shdata; + bool use_textures; ObjectPass(); ~ObjectPass(); diff --git a/source/primitivebuilder.cpp b/source/primitivebuilder.cpp index e461cf21..92b2cb0e 100644 --- a/source/primitivebuilder.cpp +++ b/source/primitivebuilder.cpp @@ -38,6 +38,15 @@ void PrimitiveBuilder::end() end_(); } +void PrimitiveBuilder::element(unsigned i) +{ + if(!in_batch) + throw InvalidState("Element specification not between begin() and end()"); + if(i>=array.size()) + throw InvalidParameterValue("Element index out of range"); + element_(i); +} + PrimitiveType PrimitiveBuilder::get_type() const { if(!in_batch) @@ -47,13 +56,13 @@ PrimitiveType PrimitiveBuilder::get_type() const void PrimitiveBuilder::vertex_(float x, float y, float z, float w) { - if(!in_batch) - throw InvalidState("Vertex specification not between begin() and end()"); - builder->texcoord(ts, tt, tr,tq); builder->color(cr, cg, cb, ca); builder->normal(nx, ny, nz); builder->vertex(x, y, z, w); + + if(in_batch) + element_(array.size()-1); } } // namespace GL diff --git a/source/primitivebuilder.h b/source/primitivebuilder.h index cfe0a02c..ee8a8477 100644 --- a/source/primitivebuilder.h +++ b/source/primitivebuilder.h @@ -35,11 +35,13 @@ protected: public: void begin(PrimitiveType); void end(); + void element(unsigned); PrimitiveType get_type() const; protected: virtual void vertex_(float, float, float, float); virtual void begin_() =0; virtual void end_() =0; + virtual void element_(unsigned) =0; }; } // namespace GL diff --git a/source/programdata.cpp b/source/programdata.cpp index f627f2f1..2a8b5970 100644 --- a/source/programdata.cpp +++ b/source/programdata.cpp @@ -95,28 +95,34 @@ ProgramData::Loader::Loader(ProgramData &pd, Program &pr): pdata(pd), prog(pr) { - add("uniform1", &Loader::uniform1); - add("uniform2", &Loader::uniform2); - add("uniform3", &Loader::uniform3); - add("uniform4", &Loader::uniform4); + add("uniform1i", &Loader::uniform1i); + add("uniform1f", &Loader::uniform1f); + add("uniform2f", &Loader::uniform2f); + add("uniform3f", &Loader::uniform3f); + add("uniform4f", &Loader::uniform4f); } -void ProgramData::Loader::uniform1(const string &n, float v) +void ProgramData::Loader::uniform1i(const string &n, int v) { pdata.uniform(prog.get_uniform_location(n), v); } -void ProgramData::Loader::uniform2(const string &n, float v0, float v1) +void ProgramData::Loader::uniform1f(const string &n, float v) +{ + pdata.uniform(prog.get_uniform_location(n), v); +} + +void ProgramData::Loader::uniform2f(const string &n, float v0, float v1) { pdata.uniform(prog.get_uniform_location(n), v0, v1); } -void ProgramData::Loader::uniform3(const string &n, float v0, float v1, float v2) +void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2) { pdata.uniform(prog.get_uniform_location(n), v0, v1, v2); } -void ProgramData::Loader::uniform4(const string &n, float v0, float v1, float v2, float v3) +void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3) { pdata.uniform(prog.get_uniform_location(n), v0, v1, v2, v3); } diff --git a/source/programdata.h b/source/programdata.h index 986bd7b1..4d7938bf 100644 --- a/source/programdata.h +++ b/source/programdata.h @@ -32,10 +32,11 @@ public: public: Loader(ProgramData &, Program &); private: - void uniform1(const std::string &, float); - void uniform2(const std::string &, float, float); - void uniform3(const std::string &, float, float, float); - void uniform4(const std::string &, float, float, float, float); + void uniform1i(const std::string &, int); + void uniform1f(const std::string &, float); + void uniform2f(const std::string &, float, float); + void uniform3f(const std::string &, float, float, float); + void uniform4f(const std::string &, float, float, float, float); }; private: