From 11a0683469694e2ac9b21da917921ce9f6138cc1 Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Tue, 12 Apr 2016 15:54:55 +0200 Subject: [PATCH] =?utf8?q?Shader::Active=20(=5Fshader=20braucht=20nicht=20?= =?utf8?q?mehr=20=C3=BCbergeben=20werden)=20useShader=20(Model=20->=20Scen?= =?utf8?q?eObject)=20useTexture=20->=20Texture=20Class=20(Model=20->=20Sce?= =?utf8?q?neObject)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Weave/Graphix/Model/IMetaMesh.cpp | 4 +- Weave/Graphix/Model/IMetaMesh.h | 2 +- Weave/Graphix/Model/Model.cpp | 78 ++++++++----------------------- Weave/Graphix/Model/Model.h | 20 ++------ Weave/Graphix/Model/SkyBox.cpp | 4 +- Weave/Graphix/Model/SkyBox.h | 2 +- Weave/Graphix/Shader.cpp | 9 ++++ Weave/Graphix/Shader.h | 4 +- Weave/Graphix/Texture.cpp | 18 ++++++- Weave/Graphix/Texture.h | 6 +-- Weave/Scene/Level.cpp | 4 +- Weave/Scene/SceneObject.cpp | 18 +++---- 12 files changed, 71 insertions(+), 98 deletions(-) diff --git a/Weave/Graphix/Model/IMetaMesh.cpp b/Weave/Graphix/Model/IMetaMesh.cpp index 619380f..1b5fccc 100644 --- a/Weave/Graphix/Model/IMetaMesh.cpp +++ b/Weave/Graphix/Model/IMetaMesh.cpp @@ -146,11 +146,11 @@ void IMetaMesh::unbindShader(Shader* _shader) } } -void IMetaMesh::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat) const +void IMetaMesh::drawModel(Texture* _texture, const mat4& _modelMat) const { for (auto i = models.begin(); i != models.end(); ++i) { - i->first->drawModel(_shader, _texture, _modelMat* i->second); + i->first->drawModel(_texture, _modelMat* i->second); } } diff --git a/Weave/Graphix/Model/IMetaMesh.h b/Weave/Graphix/Model/IMetaMesh.h index b1219fb..e5cdce3 100644 --- a/Weave/Graphix/Model/IMetaMesh.h +++ b/Weave/Graphix/Model/IMetaMesh.h @@ -23,7 +23,7 @@ public: void unbindModel() override; void unbindShader(Shader* shader) override; - void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const override; + void drawModel(Texture* texture, const mat4& modelMat) const override; void drawBBox(const mat4& modelMat, const vec4& color = vec4(0.9f, 0.f, 0.f, 1.f)) const override; diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index c655d75..21f2509 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -133,45 +133,23 @@ void Model::drawWire() const glBindVertexArray(0); } -void Model::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat) const +void Model::drawModel(Texture* _texture, const mat4& _modelMat) const { - //Message::info("Error from before?"); - //Graphix::getGlError(); - useModel(_shader); - //Message::info("IMesh loading Coordinates"); - //Graphix::getGlError(); - useTexture(_texture, _shader); - //Message::info("IMesh loading Texture"); - //Graphix::getGlError(); - useModelMat(_modelMat, _shader); - //Message::info("IMesh loading MMatrix"); - //Graphix::getGlError(); + useModel(_modelMat); + if (Events::isKToggleActive(SDLK_F3)) drawWire(); else drawModel(); - //Message::info("IMesh drawing Elements"); - //Graphix::getGlError(); - //system("pause"); } -void Model::drawWire(Shader* _shader, const mat4& _modelMat) const +void Model::drawWire(const mat4& _modelMat) const { - //Message::info("Error from before?"); - //Graphix::getGlError(); - useModel(_shader); - //Message::info("IMesh loading Coordinates"); - //Graphix::getGlError(); - //useTexture(_texture, _shader); - //Message::info("IMesh loading Texture"); - //Graphix::getGlError(); - useModelMat(_modelMat, _shader); - //Message::info("IMesh loading MMatrix"); - //Graphix::getGlError(); + + useModel(_modelMat); + drawWire(); - //Message::info("IMesh drawing Elements"); - //Graphix::getGlError(); - //system("pause"); + } void Model::drawBBox(const mat4& _modelMat, const vec4& _color) const @@ -193,8 +171,8 @@ void Model::drawBBox(const mat4& _modelMat, const vec4& _color) const vec3 size = (max - min); vec3 center = (max + min) * .5f; - - getBBoxModel()->drawModel(Graphix::shader_BBox, NULL, _modelMat*translate(center)*glm::scale(size)); // + Graphix::shader_BBox->useShader(); + getBBoxModel()->drawModel(nullptr, _modelMat*translate(center)*glm::scale(size)); // } //void Model::getBBcs(vec3& _center, vec3& _size) const @@ -250,11 +228,14 @@ void Model::bindBuffer(const uint &buffer, const uint target, const uint index, } -void Model::useModel(Shader* _shader) const +void Model::useModel(const mat4& _modelMat) const { - _shader->useShader(); + + auto active_shader = Shader::getActive(); + + /* use MODEL*/ uint vao = -1; - auto i = shader_map.find(*_shader); + auto i = shader_map.find(*active_shader); if (i == shader_map.end()) Message::error("Model: Shader wurde nicht an das Model gebunden."); else @@ -262,31 +243,12 @@ void Model::useModel(Shader* _shader) const glBindVertexArray(vao); -} - -void Model::useTexture(Texture* _texture, Shader* _shader) const -{ - if (_texture != nullptr) - { - auto tmp = _shader->getUniformLocation("uColorTexture"); - if (tmp < 0){ - return; - } - int unit = 0; - _texture->bind(unit); - glUniform1i(tmp, unit); - - int tmp2 = _shader->getUniformLocation("material"); - if (tmp2 >= 0) - glUniform4fv(tmp2, 1, value_ptr(_texture->material)); - } -} -void Model::useModelMat(const mat4& _model, Shader* _shader) const -{ - int tmp = _shader->getUniformLocation("uModel"); + /* use ModelMAT*/ + int tmp = active_shader->getUniformLocation("uModel"); if (tmp >= 0) - glUniformMatrix4fv(tmp, 1, GL_FALSE, value_ptr(_model)); + glUniformMatrix4fv(tmp, 1, GL_FALSE, value_ptr(_modelMat)); + } vec3 Model::rotateSize(const vec3& _size, const mat4& _modelMat) diff --git a/Weave/Graphix/Model/Model.h b/Weave/Graphix/Model/Model.h index 1c07c3d..d8f2209 100644 --- a/Weave/Graphix/Model/Model.h +++ b/Weave/Graphix/Model/Model.h @@ -18,25 +18,19 @@ public: /* Binds Model to GL*/ virtual void bindModel(); + virtual void unbindModel(); /* Binds Model to the Shader*/ virtual void bindShader(Shader* shader); - - virtual void unbindModel(); virtual void unbindShader(Shader* shader); /* Draws Model */ - virtual void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const; - virtual void drawWire(Shader* shader, const mat4& modelMat) const; + virtual void drawModel(Texture* texture, const mat4& modelMat) const; + virtual void drawWire(const mat4& modelMat) const; /* Draws a BoundingBox around the Model */ virtual void drawBBox(const mat4& modelMat, const vec4& color = vec4(0.9f, 0.f, 0.f, 1.f)) const; - /* Get min&max from BBox */ -// virtual void getBBmm(vec3& min, vec3& max) const; - /* Get size&position from BBox */ - //void getBBcs(vec3& center, vec3& size) const; - static Model* getBBoxModel(); static Model* getSkyBoxModel(); static Model* newIMetaModel(const std::string& modelpath,bool isConvex=true); @@ -45,12 +39,9 @@ public: static void deleteSkyBoxModel(); static void deleteIMetaModel(const std::string& modelpath); - //virtual float getPDistHit(const vec3& P, const vec3& direction) const; - virtual operator std::string() const; virtual std::string type2str() const = 0; - virtual operator btCollisionShape*() const; virtual void bt_init(bool _convex = true); virtual void bt_setMargin(btScalar margin); @@ -81,9 +72,8 @@ protected: void bindBuffer(const uint &buffer, const uint tartget, const uint index, const uint dim = 3); - virtual void useModel(Shader* shader) const; - virtual void useTexture(Texture* texture, Shader* shader) const; - virtual void useModelMat(const mat4& model, Shader* shader) const; + /* use Model with active Shader*/ + virtual void useModel(const mat4& modelMat) const; virtual void drawModel() const; virtual void drawWire() const; diff --git a/Weave/Graphix/Model/SkyBox.cpp b/Weave/Graphix/Model/SkyBox.cpp index 1e7a038..d932564 100644 --- a/Weave/Graphix/Model/SkyBox.cpp +++ b/Weave/Graphix/Model/SkyBox.cpp @@ -25,7 +25,7 @@ SkyBox::~SkyBox() { } -void SkyBox::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat) const +void SkyBox::drawModel(Texture* _texture, const mat4& _modelMat) const { //_shader->useShader(); //GLint OldCullFaceMode; @@ -35,7 +35,7 @@ void SkyBox::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat //glCullFace(GL_FRONT); glDepthFunc(GL_LEQUAL); //glDisable(GL_DEPTH_TEST); - Model::drawModel(_shader, _texture, _modelMat); + Model::drawModel(_texture, _modelMat); //glEnable(GL_DEPTH_TEST); //glCullFace(OldCullFaceMode); glDepthFunc(OldDepthFuncMode); diff --git a/Weave/Graphix/Model/SkyBox.h b/Weave/Graphix/Model/SkyBox.h index dd1cecb..3371573 100644 --- a/Weave/Graphix/Model/SkyBox.h +++ b/Weave/Graphix/Model/SkyBox.h @@ -10,7 +10,7 @@ public: SkyBox(); ~SkyBox(); - void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const override; + void drawModel(Texture* texture, const mat4& modelMat) const override; //uncommend following lines to use default Loaders //void useModelMat(const mat4& model, Shader* shader) const override; diff --git a/Weave/Graphix/Shader.cpp b/Weave/Graphix/Shader.cpp index b084561..a54b9c0 100644 --- a/Weave/Graphix/Shader.cpp +++ b/Weave/Graphix/Shader.cpp @@ -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 diff --git a/Weave/Graphix/Shader.h b/Weave/Graphix/Shader.h index 027281a..0dd95f1 100644 --- a/Weave/Graphix/Shader.h +++ b/Weave/Graphix/Shader.h @@ -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; }; diff --git a/Weave/Graphix/Texture.cpp b/Weave/Graphix/Texture.cpp index bec14b7..83ac7ee 100644 --- a/Weave/Graphix/Texture.cpp +++ b/Weave/Graphix/Texture.cpp @@ -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 diff --git a/Weave/Graphix/Texture.h b/Weave/Graphix/Texture.h index d884713..d6968a8 100644 --- a/Weave/Graphix/Texture.h +++ b/Weave/Graphix/Texture.h @@ -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 texture_Map; }; \ No newline at end of file diff --git a/Weave/Scene/Level.cpp b/Weave/Scene/Level.cpp index 81ba5c8..49c02a9 100644 --- a/Weave/Scene/Level.cpp +++ b/Weave/Scene/Level.cpp @@ -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); diff --git a/Weave/Scene/SceneObject.cpp b/Weave/Scene/SceneObject.cpp index 53bcc9c..dc0fc4a 100644 --- a/Weave/Scene/SceneObject.cpp +++ b/Weave/Scene/SceneObject.cpp @@ -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); -- 2.47.3