]> git.leopard-lacewing.eu Git - cgue_weave.git/commitdiff
Shader::Active (_shader braucht nicht mehr übergeben werden)
authorPeter Schaefer <schaeferpm@gmail.com>
Tue, 12 Apr 2016 13:54:55 +0000 (15:54 +0200)
committerPeter Schaefer <schaeferpm@gmail.com>
Tue, 12 Apr 2016 13:54:55 +0000 (15:54 +0200)
useShader (Model -> SceneObject)
useTexture -> Texture Class (Model -> SceneObject)

12 files changed:
Weave/Graphix/Model/IMetaMesh.cpp
Weave/Graphix/Model/IMetaMesh.h
Weave/Graphix/Model/Model.cpp
Weave/Graphix/Model/Model.h
Weave/Graphix/Model/SkyBox.cpp
Weave/Graphix/Model/SkyBox.h
Weave/Graphix/Shader.cpp
Weave/Graphix/Shader.h
Weave/Graphix/Texture.cpp
Weave/Graphix/Texture.h
Weave/Scene/Level.cpp
Weave/Scene/SceneObject.cpp

index 619380f91190b7879b7eaa5d2b755224eabb98b4..1b5fccc3a280bce9b6834bd05751411dd509cf1c 100644 (file)
@@ -146,11 +146,11 @@ void IMetaMesh::unbindShader(Shader* _shader)
        }\r
 }\r
 \r
-void IMetaMesh::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat) const\r
+void IMetaMesh::drawModel(Texture* _texture, const mat4& _modelMat) const\r
 {\r
        for (auto i = models.begin(); i != models.end(); ++i)\r
        {\r
-               i->first->drawModel(_shader, _texture, _modelMat* i->second);\r
+               i->first->drawModel(_texture, _modelMat* i->second);\r
        }\r
 }\r
 \r
index b1219fbe648e1fdc78bae708100b6b26302a76fa..e5cdce3214e80c6f44c775878810cb86ada32272 100644 (file)
@@ -23,7 +23,7 @@ public:
        void unbindModel() override;\r
        void unbindShader(Shader* shader) override;\r
 \r
-       void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const override;\r
+       void drawModel(Texture* texture, const mat4& modelMat) const override;\r
 \r
        void drawBBox(const mat4& modelMat, const vec4& color = vec4(0.9f, 0.f, 0.f, 1.f)) const override;\r
 \r
index c655d75fec01046c42fc03ccc6ca967bc9b3f141..21f250936ee25b336373a9f6ee6c02df78bd580c 100644 (file)
@@ -133,45 +133,23 @@ void Model::drawWire() const
        glBindVertexArray(0);\r
 }\r
 \r
-void Model::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat) const\r
+void Model::drawModel(Texture* _texture, const mat4& _modelMat) const\r
 {\r
-       //Message::info("Error from before?");\r
-       //Graphix::getGlError();\r
-       useModel(_shader);\r
-       //Message::info("IMesh loading Coordinates");\r
-       //Graphix::getGlError();\r
-       useTexture(_texture, _shader);\r
-       //Message::info("IMesh loading Texture");\r
-       //Graphix::getGlError();\r
-       useModelMat(_modelMat, _shader);\r
-       //Message::info("IMesh loading MMatrix");\r
-       //Graphix::getGlError();\r
+       useModel(_modelMat);\r
+\r
        if (Events::isKToggleActive(SDLK_F3))\r
                drawWire();\r
        else\r
                drawModel();\r
-       //Message::info("IMesh drawing Elements");\r
-       //Graphix::getGlError();\r
-       //system("pause");\r
 }\r
 \r
-void Model::drawWire(Shader* _shader, const mat4& _modelMat) const\r
+void Model::drawWire(const mat4& _modelMat) const\r
 {\r
-       //Message::info("Error from before?");\r
-       //Graphix::getGlError();\r
-       useModel(_shader);\r
-       //Message::info("IMesh loading Coordinates");\r
-       //Graphix::getGlError();\r
-       //useTexture(_texture, _shader);\r
-       //Message::info("IMesh loading Texture");\r
-       //Graphix::getGlError();\r
-       useModelMat(_modelMat, _shader);\r
-       //Message::info("IMesh loading MMatrix");\r
-       //Graphix::getGlError();\r
+\r
+       useModel(_modelMat);\r
+\r
        drawWire();\r
-       //Message::info("IMesh drawing Elements");\r
-       //Graphix::getGlError();\r
-       //system("pause");\r
+\r
 }\r
 \r
 void Model::drawBBox(const mat4& _modelMat, const vec4& _color) const\r
@@ -193,8 +171,8 @@ void Model::drawBBox(const mat4& _modelMat, const vec4& _color) const
 \r
        vec3 size = (max - min);\r
        vec3 center = (max + min) * .5f;\r
-\r
-       getBBoxModel()->drawModel(Graphix::shader_BBox, NULL, _modelMat*translate(center)*glm::scale(size)); //\r
+       Graphix::shader_BBox->useShader();\r
+       getBBoxModel()->drawModel(nullptr, _modelMat*translate(center)*glm::scale(size)); //\r
 }\r
 \r
 //void Model::getBBcs(vec3& _center, vec3& _size) const\r
@@ -250,11 +228,14 @@ void Model::bindBuffer(const uint &buffer, const uint target, const uint index,
 }\r
 \r
 \r
-void Model::useModel(Shader* _shader) const\r
+void Model::useModel(const mat4& _modelMat) const\r
 {\r
-       _shader->useShader();\r
+\r
+       auto active_shader = Shader::getActive();\r
+\r
+       /* use MODEL*/\r
        uint vao = -1;\r
-       auto i = shader_map.find(*_shader);\r
+       auto i = shader_map.find(*active_shader);\r
        if (i == shader_map.end())\r
                Message::error("Model: Shader wurde nicht an das Model gebunden.");\r
        else\r
@@ -262,31 +243,12 @@ void Model::useModel(Shader* _shader) const
 \r
        glBindVertexArray(vao);\r
 \r
-}\r
-\r
-void Model::useTexture(Texture* _texture, Shader* _shader) const\r
-{\r
-       if (_texture != nullptr)\r
-       {\r
-               auto tmp = _shader->getUniformLocation("uColorTexture");\r
-               if (tmp < 0){\r
-                       return;\r
-               }\r
-               int unit = 0;\r
-               _texture->bind(unit);\r
-               glUniform1i(tmp, unit);\r
-\r
-               int tmp2 = _shader->getUniformLocation("material");\r
-               if (tmp2 >= 0)\r
-                       glUniform4fv(tmp2, 1, value_ptr(_texture->material));\r
-       }\r
-}\r
 \r
-void Model::useModelMat(const mat4& _model, Shader* _shader) const\r
-{\r
-       int tmp = _shader->getUniformLocation("uModel");\r
+       /* use ModelMAT*/\r
+       int tmp = active_shader->getUniformLocation("uModel");\r
        if (tmp >= 0)\r
-               glUniformMatrix4fv(tmp, 1, GL_FALSE, value_ptr(_model));\r
+               glUniformMatrix4fv(tmp, 1, GL_FALSE, value_ptr(_modelMat));\r
+\r
 }\r
 \r
 vec3 Model::rotateSize(const vec3& _size, const mat4& _modelMat)\r
index 1c07c3d53226f20f8e09e6ab39b1a04d5b63aab0..d8f2209f016450479d0c0e4fca3d0e122bfceecd 100644 (file)
@@ -18,25 +18,19 @@ public:
 \r
        /* Binds Model to GL*/\r
        virtual void bindModel();\r
+       virtual void unbindModel();\r
 \r
        /* Binds Model to the Shader*/\r
        virtual void bindShader(Shader* shader);\r
-\r
-       virtual void unbindModel();\r
        virtual void unbindShader(Shader* shader);\r
 \r
        /* Draws Model */\r
-       virtual void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const;\r
-       virtual void drawWire(Shader* shader, const mat4& modelMat) const;\r
+       virtual void drawModel(Texture* texture, const mat4& modelMat) const;\r
+       virtual void drawWire(const mat4& modelMat) const;\r
 \r
        /* Draws a BoundingBox around the Model */\r
        virtual void drawBBox(const mat4& modelMat, const vec4& color = vec4(0.9f, 0.f, 0.f, 1.f)) const;\r
 \r
-       /* Get min&max from BBox */\r
-//     virtual void getBBmm(vec3& min, vec3& max) const;\r
-       /* Get size&position from BBox */\r
-       //void getBBcs(vec3& center, vec3& size) const;\r
-\r
        static Model* getBBoxModel();\r
        static Model* getSkyBoxModel();\r
        static Model* newIMetaModel(const std::string& modelpath,bool isConvex=true);\r
@@ -45,12 +39,9 @@ public:
        static void deleteSkyBoxModel();\r
        static void deleteIMetaModel(const std::string& modelpath);\r
 \r
-       //virtual float getPDistHit(const vec3& P, const vec3& direction) const;\r
-\r
        virtual operator std::string() const;\r
        virtual std::string type2str() const = 0;\r
 \r
-       \r
        virtual operator btCollisionShape*() const;\r
        virtual void bt_init(bool _convex = true);\r
        virtual void bt_setMargin(btScalar margin);\r
@@ -81,9 +72,8 @@ protected:
        void bindBuffer(const uint &buffer, const uint tartget, const uint index, const uint dim = 3);\r
        \r
 \r
-       virtual void useModel(Shader* shader) const;\r
-       virtual void useTexture(Texture* texture, Shader* shader) const; \r
-       virtual void useModelMat(const mat4& model, Shader* shader) const;\r
+       /* use Model with active Shader*/\r
+       virtual void useModel(const mat4& modelMat) const;\r
        virtual void drawModel() const;\r
        virtual void drawWire() const;\r
 \r
index 1e7a03811ffd0731b4acf42eb6f4d9b31e9dfac4..d93256416f96ecb051f7a90b0b5c3b70dadb1405 100644 (file)
@@ -25,7 +25,7 @@ SkyBox::~SkyBox()
 {\r
 }\r
 \r
-void SkyBox::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat) const\r
+void SkyBox::drawModel(Texture* _texture, const mat4& _modelMat) const\r
 {\r
        //_shader->useShader();\r
        //GLint OldCullFaceMode;\r
@@ -35,7 +35,7 @@ void SkyBox::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat
        //glCullFace(GL_FRONT);\r
        glDepthFunc(GL_LEQUAL);\r
        //glDisable(GL_DEPTH_TEST);\r
-       Model::drawModel(_shader, _texture, _modelMat);\r
+       Model::drawModel(_texture, _modelMat);\r
        //glEnable(GL_DEPTH_TEST);\r
        //glCullFace(OldCullFaceMode);\r
        glDepthFunc(OldDepthFuncMode);\r
index dd1cecbe8dfd2e11fd66927c101307d33cc58a3a..33715732c78f9aa22a2171a457c8efa120a7f4af 100644 (file)
@@ -10,7 +10,7 @@ public:
        SkyBox();\r
        ~SkyBox();\r
 \r
-       void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const override;\r
+       void drawModel(Texture* texture, const mat4& modelMat) const override;\r
 \r
        //uncommend following lines to use default Loaders\r
        //void useModelMat(const mat4& model, Shader* shader) const override;\r
index b084561a10c6489c3267a4602f766f0ffc897bf4..a54b9c0f4c1f71048cde0ef00a27202cdfd53202 100644 (file)
@@ -27,9 +27,15 @@ Shader::~Shader()
 
 void Shader::useShader() const
 {
+       active = this;
        glUseProgram(handle);
 }
 
+Shader const* Shader::getActive()
+{
+       return active;
+}
+
 int Shader::getAttribLocation(const string& name) const
 {
        int ind = glGetAttribLocation(handle, name.c_str());
@@ -141,3 +147,6 @@ GLuint Shader::loadProgram(GLuint _shader1, GLuint _shader2){
        }
        return programHandle;
 }
+
+
+Shader const * Shader::active = nullptr;
\ No newline at end of file
index 027281aff7d851eeaa49122be3495b3e18816c29..0dd95f1f3f0f707c2a52298db1b72a8d143f55ba 100644 (file)
@@ -15,6 +15,8 @@ public:
 
        void useShader() const;
 
+       static Shader const* getActive();
+
        int getAttribLocation(const string& name) const;
        int getUniformLocation(const string& name) const;
        uint getHandle() const;
@@ -27,6 +29,6 @@ private:
        uint loadProgram(uint _shader1, uint _shader2);
 
 
-
+       static Shader const * active;
 };
 
index bec14b7a8bb505e1313d1efbef6f28e161a5ec34..83ac7eecfa630cc2ed6423c4f5b037966d1577d0 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "../Message.h"
 #include "Graphix.h"
+#include "Shader.h"
 
 using std::string;
 using std::unordered_map;
@@ -136,10 +137,25 @@ void Texture::deleteTexture(const string& _path)
        }
 }
 
-void Texture::bind(int unit)
+void Texture::useTexture() const
 {
+
+       /* bind Texture*/
+       auto tmp = Shader::getActive()->getUniformLocation("uColorTexture");
+       if (tmp < 0) {
+               return;
+       }
+
+       int unit = 0;
        glActiveTexture(GL_TEXTURE0 + unit);
        glBindTexture(TEXTURE_TYPE, handle);
+       glUniform1i(tmp, unit);
+
+       /* bind Material*/
+       int tmp2 = Shader::getActive()->getUniformLocation("material");
+       if (tmp2 >= 0)
+               glUniform4fv(tmp2, 1, value_ptr(material));
+       
 }
 
 Texture::operator string() const
index d8847131417372d44a45127cdcefeb24d80c466b..d6968a8fb40bbd0028d576f99f1fc56177b3974f 100644 (file)
@@ -11,12 +11,10 @@ public:
        static Texture* newTexture(const std::string& path, const vec4& material);
        static void deleteTexture(const std::string& path);
 
-       void bind(int unit);
+       virtual void useTexture() const;
 
        operator std::string() const;
 
-       vec4 material;
-
 private:
        Texture(const std::string& path, const vec4& material);
        ~Texture();
@@ -32,5 +30,7 @@ private:
                Texture* ptr=nullptr;
        } texHandle;
 
+       vec4 material;
+
        static std::unordered_map<std::string, texHandle> texture_Map;
 };
\ No newline at end of file
index 81ba5c8ac3b511db2b3b5fc446d1367d55f6fe93..49c02a93de5c03d03b6f4bfd7d4d7e00c04e1403 100644 (file)
@@ -12,8 +12,8 @@ Level::Level(Shader* _shader, std::string _modelpath, std::string _texturepath)
        collide_group = COL_LEVEL;
        collide_with = COL_MARVIN | COL_ENEMY;
 
-       model->bindModel();
-       model->bindShader(_shader);
+       //model->bindModel();
+       //model->bindShader(shader);
 
        collision = model;
        collision->bt_init(false);
index 53bcc9ce6165c4e89b69f9ec91f2ce8df1adf6de..dc0fc4ae7e1ad6d8e5eee99187153505bb51d4c1 100644 (file)
@@ -158,7 +158,7 @@ move_delta(0)
                collision = Model::getBBoxModel();
        }
 
-//     model->bindModel();
+       model->bindModel();
        model->bindShader(shader);
        //Message::info("Binding Shader");
        //Graphix::getGlError();
@@ -229,18 +229,12 @@ void SceneObject::setMargin(btScalar _margin)
 
 void SceneObject::draw() const
 {
-       //      useModel(shader);
-
-       //      useTexture(texture, shader);
-
-       //      useMMatrix(modelMat,shader);
-
-       //      glUniform1i(shader->getUniformLocation ("inv"), 1);
-
-       //      drawModel();
+       
+       /* Draw Object*/
+       shader->useShader();
+       texture->useTexture();
+       model->drawModel(texture, modelMat);
 
-       //model->drawWire(shader, modelMat);    //BUG
-       model->drawModel(shader, texture, modelMat);
        if (Events::isKToggleActive(SDLK_F6) && collision != nullptr)
        {
                collision->drawBBox(modelMat);