From 97d091ac037a2106afff1db5f6bb8ddfcf75fbdf Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Wed, 6 May 2015 17:31:47 +0200 Subject: [PATCH] redesigned new/delete for models added colors for BBox --- Weave/Graphix/Graphix.cpp | 1 + Weave/Graphix/Model.h | 7 ++- Weave/Graphix/Model/BBox.cpp | 11 +++++ Weave/Graphix/Model/BBox.h | 3 +- Weave/Graphix/Model/IMesh.cpp | 13 ++++-- Weave/Graphix/Model/IMesh.h | 19 +++----- Weave/Graphix/Model/IMetaMesh.cpp | 12 +++-- Weave/Graphix/Model/IMetaMesh.h | 13 +++--- Weave/Graphix/Model/Model.cpp | 77 +++++++++++++++++++++++++------ Weave/Graphix/Model/Model.h | 18 ++++++-- Weave/Graphix/Model/SkyBox.cpp | 6 ++- Weave/Graphix/Model/SkyBox.h | 10 ++-- Weave/Scene/EventBox.cpp | 4 +- Weave/Scene/EventBox.h | 1 - Weave/Scene/SceneObject.cpp | 16 +++++-- Weave/Scene/SceneObject.h | 1 + Weave/Scene/Sky.cpp | 5 +- Weave/Weave.vcxproj | 16 +++---- Weave/Weave.vcxproj.filters | 44 +++++++++--------- 19 files changed, 181 insertions(+), 96 deletions(-) diff --git a/Weave/Graphix/Graphix.cpp b/Weave/Graphix/Graphix.cpp index be1e6ee..f6dfb75 100644 --- a/Weave/Graphix/Graphix.cpp +++ b/Weave/Graphix/Graphix.cpp @@ -97,6 +97,7 @@ void Graphix::clear() void Graphix::cleanup() { delete shader_BBox; + SDL_SetWindowGrab(sdl_window, SDL_FALSE); SDL_GL_DeleteContext(sdl_glcontext); diff --git a/Weave/Graphix/Model.h b/Weave/Graphix/Model.h index 601c461..d786d9a 100644 --- a/Weave/Graphix/Model.h +++ b/Weave/Graphix/Model.h @@ -1,7 +1,6 @@ #pragma once #include "Model\Model.h" -#include "Model\IMetaMesh.h" -#include "Model\SkyBox.h" -#include "Model\BBox.h" - +//#include "Model\IMetaMesh.h" +//#include "Model\SkyBox.h" +//#include "Model\BBox.h" diff --git a/Weave/Graphix/Model/BBox.cpp b/Weave/Graphix/Model/BBox.cpp index 796866f..70a7478 100644 --- a/Weave/Graphix/Model/BBox.cpp +++ b/Weave/Graphix/Model/BBox.cpp @@ -19,8 +19,13 @@ BBox::BBox() BBox::~BBox() { + glDeleteBuffers(1, &vertexBuffer); + //glDeleteBuffers(1, &indexBuffer); + //glDeleteBuffers(1, &normalBuffer); + //glDeleteBuffers(1, &uvBuffer); } + void BBox::drawModel() const { uint indexL1[4]{ 0, 1, 2, 3 }, @@ -33,3 +38,9 @@ void BBox::drawModel() const glDrawElements(GL_LINES, 8, GL_UNSIGNED_INT, indexS1); glBindVertexArray(0); } + + +BBox::operator std::string() const +{ + return "BBox"; +} diff --git a/Weave/Graphix/Model/BBox.h b/Weave/Graphix/Model/BBox.h index c56c518..35f8d92 100644 --- a/Weave/Graphix/Model/BBox.h +++ b/Weave/Graphix/Model/BBox.h @@ -7,7 +7,8 @@ public: BBox(); ~BBox(); - void drawModel() const; + void drawModel() const override; + operator std::string() const override; }; diff --git a/Weave/Graphix/Model/IMesh.cpp b/Weave/Graphix/Model/IMesh.cpp index 75942ee..4a6b3e5 100644 --- a/Weave/Graphix/Model/IMesh.cpp +++ b/Weave/Graphix/Model/IMesh.cpp @@ -11,9 +11,9 @@ #include "../Graphix.h" +using std::string; - -IMesh::IMesh(const string& _modelpath, uint _mindex) +IMesh::IMesh(const string& _modelpath, uint _mindex) : modelpath(_modelpath) { float *vertex = nullptr, *normals = nullptr, *uvs = nullptr; @@ -32,7 +32,7 @@ IMesh::IMesh(const string& _modelpath, uint _mindex) } -IMesh::IMesh(const aiMesh* _mesh, const mat4& _transformation) : modelMat(_transformation) +IMesh::IMesh(const aiMesh* _mesh, const mat4& _transformation) : modelMat(_transformation), modelpath("IMesh") { float *vertex = nullptr, *normals = nullptr, *uvs = nullptr; @@ -155,4 +155,9 @@ void IMesh::checkColliding(float& _overlap, float& _inside, const Model* _model, return; -} \ No newline at end of file +} + +IMesh::operator string() const +{ + return "IMesh"; +} diff --git a/Weave/Graphix/Model/IMesh.h b/Weave/Graphix/Model/IMesh.h index c75f7ad..14e7353 100644 --- a/Weave/Graphix/Model/IMesh.h +++ b/Weave/Graphix/Model/IMesh.h @@ -5,8 +5,6 @@ #include "Model.h" -using std::string; - class Shader; class Texture; struct aiMesh; @@ -14,12 +12,12 @@ struct aiMesh; class IMesh : public Model { public: - IMesh(const string& modelpath, uint index=0); + IMesh(const std::string& modelpath, uint index=0); IMesh(const aiMesh* mesh, const mat4& transformation); virtual ~IMesh(); - void useModelMat(const mat4& model, Shader* shader) const; + void useModelMat(const mat4& model, Shader* shader) const override; //void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const; @@ -28,18 +26,15 @@ public: /* calls Colliding to check if self collides with model*/ virtual void checkColliding(float& overlap, float& inside, const Model* model, const mat4& modelMatThis, const mat4& modelMatOther) const; -protected: + operator std::string() const override; +protected: + std::string modelpath; mat4 modelMat; // Mesh Speichern? - bool import(const string& modelpath, uint& numvertices, uint& numfaces, float*& vertex, float*& uvs, float*& normals, uint*& index , uint mindex = 0) const; + bool import(const std::string& modelpath, uint& numvertices, uint& numfaces, float*& vertex, float*& uvs, float*& normals, uint*& index , uint mindex = 0) const; bool import(const aiMesh* mesh, uint& numvertices, uint& numfaces, float*& vertex, float*& uvs, float*& normals, uint*& index) const; - - - - -}; - +}; \ No newline at end of file diff --git a/Weave/Graphix/Model/IMetaMesh.cpp b/Weave/Graphix/Model/IMetaMesh.cpp index 9348c87..aadddaf 100644 --- a/Weave/Graphix/Model/IMetaMesh.cpp +++ b/Weave/Graphix/Model/IMetaMesh.cpp @@ -8,6 +8,7 @@ #include "../Shader.h" #include "../../Message.h" +using std::string; IMetaMesh::IMetaMesh(const string& _modelpath) { @@ -85,14 +86,14 @@ void IMetaMesh::drawModel(Shader* _shader, Texture* _texture, const mat4& _model } -void IMetaMesh::drawBBox(Shader* _shader, const mat4& _modelMat) const +void IMetaMesh::drawBBox(const mat4& _modelMat,const vec4& _color) const { - Model::drawBBox(_shader, _modelMat); + Model::drawBBox(_modelMat, _color); if (models.size() > 1) { for (auto i = models.begin(); i != models.end(); ++i) { - (*i)->drawBBox(_shader, _modelMat); + (*i)->drawBBox(_modelMat, _color*0.8f); } } @@ -117,3 +118,8 @@ void IMetaMesh::checkColliding(float& _overlap, float& _inside, const Model* _mo } } } + +IMetaMesh::operator string() const +{ + return modelpath; +} \ No newline at end of file diff --git a/Weave/Graphix/Model/IMetaMesh.h b/Weave/Graphix/Model/IMetaMesh.h index 5d2b3cb..13f763f 100644 --- a/Weave/Graphix/Model/IMetaMesh.h +++ b/Weave/Graphix/Model/IMetaMesh.h @@ -6,8 +6,6 @@ #include #include "../GLM.h" -using std::string; - class IMesh; class Shader; class Texture; @@ -15,20 +13,23 @@ class Texture; class IMetaMesh : public Model { public: - IMetaMesh(const string& modelpath); + IMetaMesh(const std::string& modelpath); ~IMetaMesh(); - void bindShader(Shader* shader); + void bindShader(Shader* shader) override; - void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const; + void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const override; - void drawBBox(Shader* shader, const mat4& modelMat) const; + void drawBBox(const mat4& modelMat, const vec4& color = vec4(0.9f, 0.f, 0.f, 1.f)) const override; /* calls Colliding to check if self collides with model*/ void checkColliding(float& overlap, float& inside, const Model* model, const mat4& modelMatThis, const mat4& modelMatOther) const; + operator std::string() const override; + protected: + std::string modelpath; std::list models; }; diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index 6b270a1..88db18a 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -9,6 +9,8 @@ #include "../Graphix.h" #include "BBox.h" +#include "SkyBox.h" +#include "IMetaMesh.h" #include "../../Events.h" @@ -100,22 +102,17 @@ void Model::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat) //system("pause"); } -void Model::drawBBox(Shader* _shader, const mat4& _modelMat) const +void Model::drawBBox(const mat4& _modelMat, const vec4& _color) const { - if (BoundingBox == nullptr) - { - BoundingBox = new BBox(); - //BBoxShader = new Shader("basic_FS.hlsl", "basic_VS.hlsl"); - BoundingBox->bindShader(Graphix::shader_BBox); - - //int tmp = Graphix::shader_BBox->getUniformLocation("uFragmentColor"); - //if (tmp >= 0) - // glUniform4fv(Graphix::shader_BBox->getHandle(), 1, value_ptr(vec4(.3f, .9f, .9f, 1.f))); - } - // BoundingBox->bindShader(_shader); vec3 BBsiz, BBpos; getBBsp(BBsiz, BBpos); - BoundingBox->drawModel(Graphix::shader_BBox, (Texture*)nullptr, _modelMat*translate(BBpos)*glm::scale(BBsiz*2.f)); + Graphix::getGlError(); + Graphix::shader_BBox->useShader(); + int tmp = Graphix::shader_BBox->getUniformLocation("uFragmentColor"); + if (tmp >= 0) + glUniform4fv(tmp, 1, value_ptr(_color)); + Graphix::getGlError(); + getBBoxModel()->drawModel(Graphix::shader_BBox, NULL, _modelMat*translate(BBpos)*glm::scale(BBsiz*2.f)); } void Model::updateBB(const vec3& _min, const vec3& _max) @@ -362,6 +359,55 @@ Model* Model::getBBoxModel() return BoundingBox; } +Model* Model::getSkyBoxModel() +{ + if (SkyBoxModel == nullptr) + { + SkyBoxModel = new SkyBox(); + //SkyBoxModel->bindShader(Graphix::shader_); + } + return SkyBoxModel; +} + +Model* Model::newIMetaModel(const string& _modelpath) +{ + Model*& ptr = IMetaModel[_modelpath]; + if (ptr == nullptr) + { + ptr = new IMetaMesh(_modelpath); + //ptr->bindShader(Graphix::shader_BBox); + } + ++IMetaModel_count[_modelpath]; + return ptr; +} + +void Model::deleteBBoxModel() +{ + delete BoundingBox; +} + +void Model::deleteSkyBoxModel() +{ + delete SkyBoxModel; +} + + +void Model::deleteIMetaModel(const string& _modelpath) +{ + if (_modelpath == "SkyBox" || _modelpath == "BBox" || _modelpath == "IMesh") + { + Message::warning("deleteIMetaModel: dont use deleteIMetaModel with " + _modelpath); + return; + } + + uint& count = IMetaModel_count[_modelpath]; + --count; + + if (!count) + delete IMetaModel[_modelpath]; + +} + float Model::getPDistHit(const vec3& _P, const vec3& _direction) const { @@ -376,5 +422,6 @@ float Model::getPDistHit(const vec3& _P, const vec3& _direction) const } Model* Model::BoundingBox = nullptr; -Shader* Model::BBoxShader = nullptr; -bool Model::exBBox = false; \ No newline at end of file +Model* Model::SkyBoxModel = nullptr; +std::unordered_map Model::IMetaModel; +std::unordered_map Model::IMetaModel_count; \ No newline at end of file diff --git a/Weave/Graphix/Model/Model.h b/Weave/Graphix/Model/Model.h index 283ea1b..e18bc9e 100644 --- a/Weave/Graphix/Model/Model.h +++ b/Weave/Graphix/Model/Model.h @@ -2,6 +2,7 @@ #include "../GLM.h" #include +#include class Shader; class Texture; @@ -21,7 +22,7 @@ public: virtual void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const; /* Draws a BoundingBox around the Model */ - virtual void drawBBox(Shader* shader, const mat4& modelMat) const; + 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; @@ -33,9 +34,17 @@ public: void checkColliding(float& overlap, float& inside, const Model* model, const mat4& modelMatThis, const mat4& modelMatOther) const; static Model* getBBoxModel(); + static Model* getSkyBoxModel(); + static Model* newIMetaModel(const std::string& modelpath); + + static void deleteBBoxModel(); + 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 = 0; + protected: uint numvertices, numfaces; uint vertexBuffer, indexBuffer, normalBuffer, uvBuffer; @@ -55,10 +64,9 @@ protected: virtual void drawModel() const; static Model* BoundingBox; - static Shader* BBoxShader; - static bool exBBox; - - + static Model* SkyBoxModel; + static std::unordered_map IMetaModel; + static std::unordered_map IMetaModel_count; static vec3 rotateSize(const vec3& size, const mat4& modelMat); static void checkCollideByAxis(float& overlap, float& inside, uint dim, const float* posA, const float* posB, const float* sizeA, const float* sizeB); diff --git a/Weave/Graphix/Model/SkyBox.cpp b/Weave/Graphix/Model/SkyBox.cpp index 9c37e0c..c91970c 100644 --- a/Weave/Graphix/Model/SkyBox.cpp +++ b/Weave/Graphix/Model/SkyBox.cpp @@ -40,7 +40,11 @@ void SkyBox::useTexture(Texture* _texture, Shader* _shader) const { } -void SkyBox::drawBBox(Shader* _shader, const mat4& _modelMat) const +void SkyBox::drawBBox(const mat4& _modelMat, const vec4& _color) const { } +SkyBox::operator std::string() const +{ + return "SkyBox"; +} \ No newline at end of file diff --git a/Weave/Graphix/Model/SkyBox.h b/Weave/Graphix/Model/SkyBox.h index 5bf1552..1b56702 100644 --- a/Weave/Graphix/Model/SkyBox.h +++ b/Weave/Graphix/Model/SkyBox.h @@ -9,13 +9,15 @@ public: SkyBox(); ~SkyBox(); - void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const; + void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const override; //uncommend following lines to use default Loaders - void useModelMat(const mat4& model, Shader* shader) const; - void useTexture(Texture* texture, Shader* shader) const; + void useModelMat(const mat4& model, Shader* shader) const override; + void useTexture(Texture* texture, Shader* shader) const override; - void drawBBox(Shader* shader, const mat4& modelMat) const; + void drawBBox(const mat4& modelMat, const vec4& color = vec4(0.f)) const override; + + operator std::string() const override; }; diff --git a/Weave/Scene/EventBox.cpp b/Weave/Scene/EventBox.cpp index 2abe6a8..420d66c 100644 --- a/Weave/Scene/EventBox.cpp +++ b/Weave/Scene/EventBox.cpp @@ -27,7 +27,7 @@ void EventBox::update(float _deltaT) void EventBox::draw() const { if (Events::isKToggleActive(SDLK_F6)) - model->drawBBox(shader, modelMat); + model->drawBBox(modelMat,vec4(.3f,.9f,.9f,1.f)); } void EventBox::collides(SceneObject* _other) @@ -52,4 +52,4 @@ void EventBox::collides(SceneObject* _other) } } -} \ No newline at end of file +} diff --git a/Weave/Scene/EventBox.h b/Weave/Scene/EventBox.h index 6be1a34..d7e5d68 100644 --- a/Weave/Scene/EventBox.h +++ b/Weave/Scene/EventBox.h @@ -22,4 +22,3 @@ protected: uint eb_type; }; - diff --git a/Weave/Scene/SceneObject.cpp b/Weave/Scene/SceneObject.cpp index d407948..8caf26f 100644 --- a/Weave/Scene/SceneObject.cpp +++ b/Weave/Scene/SceneObject.cpp @@ -29,7 +29,8 @@ collision_ignore(false), texture(nullptr), ySpeed(0), yStatic(true), -ignore(false) +ignore(false), +newModel(true) { //Message::info("Error from befor?"); //Graphix::getGlError(); @@ -39,7 +40,8 @@ ignore(false) //modelID = _shader->getUniformLocation("modelMat"); if (_modelpath != "") { - model = new IMetaMesh(_modelpath); + model = Model::newIMetaModel(_modelpath); + model->bindShader(shader); } if (texturepath != "") @@ -47,7 +49,7 @@ ignore(false) //Message::info("Creating SkyBox Shader"); //Graphix::getGlError(); - model->bindShader(shader); + //Message::info("Binding Shader"); //Graphix::getGlError(); //Message::info("Done"); @@ -63,7 +65,8 @@ collision_ignore(false), texture(nullptr), ySpeed(0), yStatic(true), -ignore(false) +ignore(false), +newModel(false) { //Message::info("Error from befor?"); //Graphix::getGlError(); @@ -86,6 +89,8 @@ ignore(false) SceneObject::~SceneObject() { delete texture; + if (newModel) + Model::deleteIMetaModel(*model); } float SceneObject::getFloor() const @@ -96,6 +101,7 @@ float SceneObject::getFloor() const void SceneObject::update(float deltaT) { + //Fallen if (!yStatic) { vec3 pos = getPosition(); @@ -133,7 +139,7 @@ void SceneObject::draw() const model->drawModel(shader, texture, modelMat); if (Events::isKToggleActive(SDLK_F6)) - model->drawBBox(shader, modelMat); + model->drawBBox(modelMat); } void SceneObject::checkCollision(SceneObject* _first, SceneObject* _second) diff --git a/Weave/Scene/SceneObject.h b/Weave/Scene/SceneObject.h index 46a8c21..f129077 100644 --- a/Weave/Scene/SceneObject.h +++ b/Weave/Scene/SceneObject.h @@ -74,6 +74,7 @@ protected: Shader* shader; Texture* texture; Model* model; + bool newModel; Scene* mainScene; diff --git a/Weave/Scene/Sky.cpp b/Weave/Scene/Sky.cpp index cedb4cd..b71400c 100644 --- a/Weave/Scene/Sky.cpp +++ b/Weave/Scene/Sky.cpp @@ -4,9 +4,8 @@ #include "../Graphix/Model.h" -Sky::Sky() : SceneObject(new Shader("skybox_VS.hlsl","skybox_color_FS.hlsl"), mat4(1.f), vec4(1.f), new SkyBox(), "") -{ - +Sky::Sky() : SceneObject(new Shader("skybox_VS.hlsl","skybox_color_FS.hlsl"), mat4(1.f), vec4(1.f), Model::getSkyBoxModel(), "") +{ } diff --git a/Weave/Weave.vcxproj b/Weave/Weave.vcxproj index 6ca484d..968c17c 100644 --- a/Weave/Weave.vcxproj +++ b/Weave/Weave.vcxproj @@ -92,49 +92,49 @@ - - + + + + - - - - + + + + - - diff --git a/Weave/Weave.vcxproj.filters b/Weave/Weave.vcxproj.filters index 10ea336..509b1b4 100644 --- a/Weave/Weave.vcxproj.filters +++ b/Weave/Weave.vcxproj.filters @@ -45,34 +45,34 @@ Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files @@ -107,40 +107,40 @@ Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files -- 2.47.3