From: Peter Schaefer Date: Sat, 9 May 2015 11:53:58 +0000 (+0200) Subject: fixed new/delete for textures X-Git-Url: https://git.leopard-lacewing.eu/?a=commitdiff_plain;h=d61b9b7d04c229ae57f68c56782de71270a704fd;p=cgue_weave.git fixed new/delete for textures removed badhack for textures --- diff --git a/Weave/Game.cpp b/Weave/Game.cpp index dd53c20..0a6af33 100644 --- a/Weave/Game.cpp +++ b/Weave/Game.cpp @@ -66,7 +66,7 @@ Game::Game() : playing(true) //current_world->addObject(new SceneObject(shader1, glm::mat4(1.0f), "Player.dae", "model_player.png")); //current_world->addObject(new SceneObject(shader1, translate(vec3(-3.f, .4f, 0.f))*scale(vec3(3.f)), "cow/cow.dae", "model_cow_2D.jpg")); current_world->addObject(new SceneObject(shader1, translate(vec3(-3.f, .4f, 0.f))*scale(vec3(.32f)), vec4(3.0f, 0.5f, 0.4f, 1.5f), "duck.dae", "model_duck_2D.png")); - current_world->addObject(new SceneObject(shader1, translate(vec3(-5.f, .4f, 0.f))*scale(vec3(.32f)), vec4(3.0f, 3.f, 0.4f, 1.5f), "duck.dae", "model_cow_2D.jpg")); + current_world->addObject(new SceneObject(shader1, translate(vec3(-5.f, .4f, 0.f))*scale(vec3(.32f)), vec4(3.0f, 3.f, 0.4f, 1.5f), "duck.dae", "model_duck_2D.png")); current_world->addObject(new EventBox(translate(vec3(3.f, .4f, 0.f)),EB_LOSTZONE)); current_world->addObject(new EventBox(translate(vec3(3.f, .4f, -15.f)), EB_WINZONE)); diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index a47d7dd..b447307 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -115,6 +115,7 @@ void Model::drawBBox(const mat4& _modelMat, const vec4& _color) const getBBoxModel()->drawModel(Graphix::shader_BBox, NULL, _modelMat*translate(BBpos)*glm::scale(BBsiz*2.f)); } + void Model::updateBB(const vec3& _min, const vec3& _max) { BBmin = _min; @@ -174,7 +175,6 @@ void Model::getBBsp(vec3& _size, vec3& _position) const } - void Model::genBuffer(uint &buffer, uint size, void* value) { glGenBuffers(1, &buffer); @@ -198,6 +198,7 @@ void Model::bindBuffer(const uint &buffer, const uint index, const uint dim) glBindBuffer(GL_ARRAY_BUFFER, 0); } + void Model::useModel(Shader* _shader) const { _shader->useShader(); @@ -237,6 +238,7 @@ void Model::useModelMat(const mat4& _model, Shader* _shader) const glUniformMatrix4fv(tmp, 1, GL_FALSE, value_ptr(_model)); } + Overlap Model::isColliding(const Model* _modelThis, const Model* _modelOther, const mat4& _modelMatThis, const mat4& _modelMatOther) { return _modelThis->checkColS2O(_modelMatThis, _modelOther, _modelMatOther); @@ -339,6 +341,7 @@ void Model::checkCollideByAxis(float& _overlap, float& _inside, uint dim, const _inside = -1; } + vec3 Model::rotateSize(const vec3& _size, const mat4& _modelMat) { vec3 Size = vec3(0.f); @@ -358,6 +361,7 @@ void Model::scaleVec3(vec3& _vec, const vec3& _scale) _vec[i] *= _scale[i]; } + Model* Model::getBBoxModel() { if (BoundingBox == nullptr) @@ -402,7 +406,6 @@ void Model::deleteSkyBoxModel() SkyBoxModel = nullptr; } - void Model::deleteIMetaModel(const string& _modelpath) { if (_modelpath == "SkyBox" || _modelpath == "BBox" || _modelpath == "IMesh") @@ -424,6 +427,7 @@ void Model::deleteIMetaModel(const string& _modelpath) } + float Model::getPDistHit(const vec3& _P, const vec3& _direction) const { diff --git a/Weave/Graphix/Model/Model.h b/Weave/Graphix/Model/Model.h index 5d9f581..02b2076 100644 --- a/Weave/Graphix/Model/Model.h +++ b/Weave/Graphix/Model/Model.h @@ -13,8 +13,6 @@ typedef unsigned int uint; class Model { public: - Model(); - virtual ~Model(); /* Binds Model to the Model*/ virtual void bindShader(Shader* shader); @@ -52,6 +50,9 @@ public: virtual operator std::string() const = 0; protected: + Model(); + virtual ~Model(); + uint numvertices, numfaces; uint vertexBuffer, indexBuffer, normalBuffer, uvBuffer; diff --git a/Weave/Graphix/Texture.cpp b/Weave/Graphix/Texture.cpp index 7454dc1..23e3782 100644 --- a/Weave/Graphix/Texture.cpp +++ b/Weave/Graphix/Texture.cpp @@ -8,6 +8,9 @@ #include "Graphix.h" using std::string; +using std::unordered_map; + + Texture::Texture(const string& _path, const vec4& _material) : path(_path), handle(-1), TEXTURE_TYPE(-1), material(_material) { @@ -15,10 +18,12 @@ Texture::Texture(const string& _path, const vec4& _material) : path(_path), hand //Graphix::getGlError(); //Message::info("Do some Stuff"); - auto i = texture_map.find(_path); - if (i != texture_map.end()) + + texHandle& t_ptr = texture_Map[_path]; + + if (t_ptr.handle >= 0) { - handle = i->second; + handle = t_ptr.handle; return; } @@ -88,7 +93,7 @@ Texture::Texture(const string& _path, const vec4& _material) : path(_path), hand glTexParameteri(TEXTURE_TYPE, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); } - texture_map[_path] = handle; + t_ptr.handle = handle; //Graphix::getGlError(); //Message::info("Done"); @@ -96,10 +101,42 @@ Texture::Texture(const string& _path, const vec4& _material) : path(_path), hand Texture::~Texture() { - texture_map.erase(path); glDeleteTextures(1, &handle); } +Texture* Texture::newTexture(const string& _path, const vec4& _material) +{ + texHandle& t_ptr = texture_Map[_path]; + + if (t_ptr.ptr == nullptr) + { + t_ptr.ptr = new Texture(_path, _material); + } + + ++t_ptr.count; + + return t_ptr.ptr; +} + +void Texture::deleteTexture(const string& _path) +{ + texHandle& t_ptr = texture_Map[_path]; + + if (t_ptr.ptr == nullptr) + { + Message::warning("deleteTexture: " + _path + " doesn't exist."); + return; + } + + --t_ptr.count; + + if (!t_ptr.count) + { + delete t_ptr.ptr; + texture_Map.erase(_path); + } +} + void Texture::bind(int unit) { glActiveTexture(GL_TEXTURE0 + unit); @@ -111,10 +148,4 @@ Texture::operator string() const return path; } -void Texture::clean() -{ - //RALLLY BAD HACK!!!! No Texture will be freed!!!! - texture_map.clear(); -} - -str2int_map Texture::texture_map; \ No newline at end of file +unordered_map Texture::texture_Map; \ No newline at end of file diff --git a/Weave/Graphix/Texture.h b/Weave/Graphix/Texture.h index 313920a..f0eaa10 100644 --- a/Weave/Graphix/Texture.h +++ b/Weave/Graphix/Texture.h @@ -1,36 +1,36 @@ #pragma once -#include #include "GLM.h" - +#include #include -using std::unordered_map; - -using std::string; - -typedef unordered_map str2int_map; - class Texture { public: - Texture(const string& path, const vec4& material); - ~Texture(); + static Texture* newTexture(const std::string& path, const vec4& material); + static void deleteTexture(const std::string& path); void bind(int unit); - operator string() const; + operator std::string() const; vec4 material; - //RALLLY BAD HACK!!!! No Texture will be freed!!!! - static void clean(); - private: + Texture(const std::string& path, const vec4& material); + ~Texture(); + unsigned int handle; - string path; + std::string path; unsigned int TEXTURE_TYPE; - static str2int_map texture_map; + typedef struct texStruct + { + int handle=-1; + unsigned int count=0; + Texture* ptr=nullptr; + } texHandle; + + static std::unordered_map texture_Map; }; \ No newline at end of file diff --git a/Weave/Scene/SceneObject.cpp b/Weave/Scene/SceneObject.cpp index 7ffd156..4784e5b 100644 --- a/Weave/Scene/SceneObject.cpp +++ b/Weave/Scene/SceneObject.cpp @@ -45,7 +45,7 @@ newModel(true) } if (texturepath != "") - texture = new Texture(texturepath, _material); + texture = Texture::newTexture(texturepath, _material); //Message::info("Creating SkyBox Shader"); //Graphix::getGlError(); @@ -75,7 +75,7 @@ newModel(false) //Graphix::getGlError(); //modelID = _shader->getUniformLocation("modelMat"); if (texturepath != "") - texture = new Texture(texturepath, _material); + texture = Texture::newTexture(texturepath, _material); //Message::info("Creating SkyBox Shader"); //Graphix::getGlError(); @@ -88,9 +88,10 @@ newModel(false) SceneObject::~SceneObject() { - delete texture; - if (newModel) - Model::deleteIMetaModel(*model); + if (texture!=nullptr) + Texture::deleteTexture(*texture); + if (newModel) + Model::deleteIMetaModel(*model); } float SceneObject::getFloor() const