From 2466c913aa7cac4ef83d7158305ea220f946b3ee Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Sun, 24 Apr 2016 21:28:18 +0200 Subject: [PATCH] =?utf8?q?dBuffer=20Object=20hinzugef=C3=BCgt=20Texture=20?= =?utf8?q?vereinfacht?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit aktuell wird der dBuffer + Bloom gezeichnet :) --- Weave/Graphix/Model/Model.cpp | 6 +- Weave/Graphix/Shader.cpp | 8 +- Weave/Graphix/Shader.h | 4 +- Weave/Graphix/Textures.h | 3 +- Weave/Graphix/Textures/Texture.cpp | 25 +++++- Weave/Graphix/Textures/Texture.h | 8 +- Weave/Graphix/Textures/dBufferObject.cpp | 97 ++++++++++++++++++++++++ Weave/Graphix/Textures/dBufferObject.h | 34 +++++++++ Weave/Graphix/Textures/fBufferObject.cpp | 5 +- Weave/Graphix/Textures/fBufferObject.h | 6 +- Weave/Graphix/Textures/tImage.cpp | 17 +---- Weave/Scene/Scene.cpp | 15 ++-- Weave/Weave.vcxproj | 2 + Weave/Weave.vcxproj.filters | 6 ++ 14 files changed, 200 insertions(+), 36 deletions(-) create mode 100644 Weave/Graphix/Textures/dBufferObject.cpp create mode 100644 Weave/Graphix/Textures/dBufferObject.h diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index dd07243..8289d91 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -135,9 +135,9 @@ void Model::drawModel(const mat4& _modelMat, drawType _type, const vec4& _color) switch (_type) { - default: - case dr_Model: - drawModel(); + default: + case dr_Model: + drawModel(); break; case dr_WireC: diff --git a/Weave/Graphix/Shader.cpp b/Weave/Graphix/Shader.cpp index 1af65e0..d7e7d1d 100644 --- a/Weave/Graphix/Shader.cpp +++ b/Weave/Graphix/Shader.cpp @@ -6,6 +6,8 @@ using namespace std; +#define SHADER_NUM 7 + Shader::Shader(string _shader1, string _shader2) { GLuint handleS1 = loadShader("../shader/"+_shader1); @@ -79,7 +81,7 @@ void Shader::bindBuffer(const uint &_buffer, const uint _target, const string& _ int ind = glGetAttribLocation(handle, _name.c_str()); if (ind == (uint)-1) { - Message::error("Model: BindBuffer wird ausgelassen da index fuer [" + _name + "] fehlerhaft ist."); + //Message::error("Model: BindBuffer wird ausgelassen da index fuer [" + _name + "] fehlerhaft ist."); return; } @@ -238,7 +240,7 @@ void Shader::cleanup() { if (initShader) { - for (int i = 1; i < 6; i++) + for (int i = 1; i < SHADER_NUM; i++) delete shList[i]; } @@ -252,4 +254,4 @@ const Shader* Shader::getShader(ShaderTarget _target) bool Shader::initShader = false; -const Shader* Shader::shList[6]; +const Shader* Shader::shList[SHADER_NUM]; diff --git a/Weave/Graphix/Shader.h b/Weave/Graphix/Shader.h index 77ebd46..9970b16 100644 --- a/Weave/Graphix/Shader.h +++ b/Weave/Graphix/Shader.h @@ -9,7 +9,7 @@ using std::string; typedef unsigned int uint; -enum ShaderTarget { +enum ShaderTarget { SH_ACTIVE = 0, SH_BASIC = 1, SH_BASICTEXTURE = 2, @@ -17,7 +17,7 @@ enum ShaderTarget { SH_BLUR = 4, SH_BLEND = 5, SH_SHADOWDIR = 6 -}; +}; //Don't forget to resize the shList Array at the begining of shader.cpp! class Shader { diff --git a/Weave/Graphix/Textures.h b/Weave/Graphix/Textures.h index 9b981cd..0684d64 100644 --- a/Weave/Graphix/Textures.h +++ b/Weave/Graphix/Textures.h @@ -3,4 +3,5 @@ #include "Textures/Texture.h" //#include "Textures\tImage.h" #include "Textures\fBufferObject.h" -#include "Textures\rBufferObject.h" \ No newline at end of file +#include "Textures\rBufferObject.h" +#include "Textures\dBufferObject.h" \ No newline at end of file diff --git a/Weave/Graphix/Textures/Texture.cpp b/Weave/Graphix/Textures/Texture.cpp index 32703c4..6c6b6b6 100644 --- a/Weave/Graphix/Textures/Texture.cpp +++ b/Weave/Graphix/Textures/Texture.cpp @@ -10,10 +10,31 @@ using std::string; using std::unordered_map; -Texture::Texture() - : texture_internal(GL_RGB16F), texture_format(GL_RGB),texture_target(GL_TEXTURE_2D),texture_type(GL_UNSIGNED_BYTE) +Texture::Texture(texTarget _target) + : texture_target(GL_TEXTURE_2D) { glGenTextures(1, &handle); + + switch (_target) + { + default: + case texT_COLORBUFFER: + texture_internal = GL_RGB16F; + texture_format = GL_RGB; + texture_type = GL_UNSIGNED_BYTE; + break; + case texT_IMAGE: + texture_internal = GL_RGBA8; + texture_format = GL_BGRA; + texture_type = GL_UNSIGNED_BYTE; + break; + case texT_DEPTHBUFFER: + texture_internal = GL_DEPTH_COMPONENT; + texture_format = GL_DEPTH_COMPONENT; + texture_type = GL_FLOAT; + break; + } + } Texture::~Texture() diff --git a/Weave/Graphix/Textures/Texture.h b/Weave/Graphix/Textures/Texture.h index ff15a82..997e6bb 100644 --- a/Weave/Graphix/Textures/Texture.h +++ b/Weave/Graphix/Textures/Texture.h @@ -7,13 +7,19 @@ class tImage; +enum texTarget { + texT_COLORBUFFER, + texT_DEPTHBUFFER, + texT_IMAGE +}; + class Texture { public: static Texture* newTImage(const std::string& path, const vec4& material); static void deleteTImage(const unsigned int handle); - Texture(); + Texture(texTarget target = texT_COLORBUFFER); ~Texture(); virtual void bindTexture(unsigned int width = 0, unsigned int height = 0); diff --git a/Weave/Graphix/Textures/dBufferObject.cpp b/Weave/Graphix/Textures/dBufferObject.cpp new file mode 100644 index 0000000..9623602 --- /dev/null +++ b/Weave/Graphix/Textures/dBufferObject.cpp @@ -0,0 +1,97 @@ +#include "dBufferObject.h" + +#include +#include "../Shader.h" +#include "../../Message.h" + +#include "Texture.h" +#include "rBufferObject.h" + + +dBufferObject::dBufferObject() : fBufferObject(0, false) +{ + //buffer_target = GL_FRAMEBUFFER; + + /*gen Buffer*/ + //glGenFramebuffers(1, &handle); + + + /*gen Texture*/ + textures = new Texture(texT_DEPTHBUFFER); +} + + +dBufferObject::~dBufferObject() +{ +// delete textures; + + //if (rBO != nullptr) + // delete rBO; + + glDeleteFramebuffers(1, &handle); +} + +//unsigned int dBufferObject::getDim() const +//{ +// return dim; +//} +// +//Texture* dBufferObject::getTexture(unsigned int i) const +//{ +// return &textures[i]; +//} + +void dBufferObject::bindBuffer(unsigned int _width, unsigned int _height) +{ + + glBindFramebuffer(buffer_target, handle); + + /*Bind Texture Buffers (RENDER)*/ + textures->bindTexture(_width, _height); + glFramebufferTexture2D(buffer_target, GL_DEPTH_ATTACHMENT, textures->getTTarget(), *textures, 0); + + glDrawBuffer(GL_NONE); + glReadBuffer(GL_NONE); + if (glCheckFramebufferStatus(buffer_target) != GL_FRAMEBUFFER_COMPLETE) + Message::error("dBufferObject: Framebuffer not complete!"); + +} + +//void dBufferObject::unbindBuffer() +//{ +// for (unsigned int i = 0; i < dim; i++) +// { +// textures[i].unbindTexture(); +// } +// +// if (rBO != nullptr) +// rBO->unbindBuffer(); +// +// glDeleteFramebuffers(1, &handle); +// glGenFramebuffers(1, &handle); +// +//} +// +//void dBufferObject::useBuffer() const +//{ +// glBindFramebuffer(buffer_target, handle); +// +//} +// +//void dBufferObject::clearBuffer() const +//{ +// glBindFramebuffer(buffer_target, 0); +//} +// +//void dBufferObject::updateSize(unsigned int _width, unsigned int _height) +//{ +// +// for (unsigned int i = 0; i < dim; i++) +// { +// textures[i].updateSize(_width, _height); +// +// } +// +// if (rBO != nullptr) +// rBO->updateSize(_width, _height); +//} diff --git a/Weave/Graphix/Textures/dBufferObject.h b/Weave/Graphix/Textures/dBufferObject.h new file mode 100644 index 0000000..8e764dd --- /dev/null +++ b/Weave/Graphix/Textures/dBufferObject.h @@ -0,0 +1,34 @@ +#pragma once + +#include "fBufferObject.h" + +class Texture; +//class rBufferObject; + +class dBufferObject + : public fBufferObject +{ +public: + dBufferObject(); + virtual ~dBufferObject(); + + virtual void bindBuffer(unsigned int width = 0, unsigned int height = 0) override; +// virtual void unbindBuffer() override; + +// virtual void useBuffer() const override; + +// virtual void clearBuffer() const override; + +// void updateSize(unsigned int _width, unsigned int _height); + +// unsigned int getDim() const; +// Texture* getTexture(unsigned int i = 0) const; + +protected: +// unsigned int dim; +// Texture* textures = nullptr; + +// rBufferObject* rBO = nullptr; + +}; + diff --git a/Weave/Graphix/Textures/fBufferObject.cpp b/Weave/Graphix/Textures/fBufferObject.cpp index ebef232..67a8ae3 100644 --- a/Weave/Graphix/Textures/fBufferObject.cpp +++ b/Weave/Graphix/Textures/fBufferObject.cpp @@ -18,7 +18,8 @@ fBufferObject::fBufferObject(unsigned int _dim, bool _rbo) /*gen Texture*/ - textures = new Texture[_dim]; + if(_dim > 0) + textures = new Texture[_dim]; if (_rbo) rBO = new rBufferObject; @@ -50,7 +51,7 @@ void fBufferObject::bindBuffer(unsigned int _width, unsigned int _height) glBindFramebuffer(buffer_target, handle); - /*Bind Texture Buffers*/ + /*Bind Texture Buffers (COLOR)*/ unsigned int* attachments = new unsigned int[dim]; for (unsigned int i = 0; i < dim; i++) { diff --git a/Weave/Graphix/Textures/fBufferObject.h b/Weave/Graphix/Textures/fBufferObject.h index f8fbc04..0f62429 100644 --- a/Weave/Graphix/Textures/fBufferObject.h +++ b/Weave/Graphix/Textures/fBufferObject.h @@ -19,10 +19,10 @@ public: virtual void clearBuffer() const override; - void updateSize(unsigned int _width, unsigned int _height); + virtual void updateSize(unsigned int _width, unsigned int _height) override; - unsigned int getDim() const; - Texture* getTexture(unsigned int i = 0) const; + virtual unsigned int getDim() const; + virtual Texture* getTexture(unsigned int i = 0) const; protected: unsigned int dim; diff --git a/Weave/Graphix/Textures/tImage.cpp b/Weave/Graphix/Textures/tImage.cpp index 3e6a9dd..6f4614b 100644 --- a/Weave/Graphix/Textures/tImage.cpp +++ b/Weave/Graphix/Textures/tImage.cpp @@ -9,7 +9,7 @@ using std::string; -tImage::tImage(const string& _path, const vec4& _material) : path(_path), material(_material) +tImage::tImage(const string& _path, const vec4& _material) : Texture(texT_IMAGE), path(_path), material(_material) { auto path_type = _path.substr(_path.find_last_of("_") + 1); path_type = path_type.substr(0, path_type.find_first_of(".")); @@ -18,11 +18,7 @@ tImage::tImage(const string& _path, const vec4& _material) : path(_path), materi { texture_target = GL_TEXTURE_CUBE_MAP; } - else if (path_type == "2D") - { - texture_target = GL_TEXTURE_2D; - } - else + else if(path_type != "2D") { Message::info("Texture : Undefined Type, was set to default (2D)"); } @@ -33,10 +29,6 @@ tImage::tImage(const string& _path, const vec4& _material) : path(_path), materi Message::error("Texture: Couldn't detect texture file format."); } - texture_internal = GL_RGBA8; - texture_format = GL_BGRA; - texture_type = GL_UNSIGNED_BYTE; - loadImage(path); } @@ -58,10 +50,7 @@ void tImage::useTexture(unsigned int _unit) const { /* bind Texture*/ - glActiveTexture(GL_TEXTURE0 + _unit); - glBindTexture(texture_target, handle); - - Shader::getShader()->setUniformLocation("uColorTexture", _unit); + Texture::useTexture(_unit); /* bind Material*/ Shader::getShader()->setUniformLocation("material", material); diff --git a/Weave/Scene/Scene.cpp b/Weave/Scene/Scene.cpp index d89358d..0ecfe89 100644 --- a/Weave/Scene/Scene.cpp +++ b/Weave/Scene/Scene.cpp @@ -18,6 +18,8 @@ #include +#define NELEMS(x) (sizeof(x) / sizeof((x)[0])) + using std::list; using std::set; @@ -68,7 +70,7 @@ lookat(_lookat) blurPingPong[i]->bindBuffer(Graphix::getWindowWidth(), Graphix::getWindowHeight()); } - shadowdir = new fBufferObject(); + shadowdir = new dBufferObject(); shadowdir->bindBuffer(Graphix::getWindowWidth(), Graphix::getWindowHeight()); postRender = new fBufferObject(2, false); @@ -218,7 +220,7 @@ void Scene::draw() const //Directional Light Shadow shadowdir->useBuffer(); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glClear(GL_DEPTH_BUFFER_BIT); Shader::getShader(SH_SHADOWDIR)->useShader(); drawSceneObjects(); @@ -290,7 +292,8 @@ void Scene::draw() const render->clearBuffer(); //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //SceneObjects.front()->gTexture()->useTexture(); - render->getTexture(0)->useTexture(); + shadowdir->getTexture()->useTexture(); + //render->getTexture(0)->useTexture(); blurPingPong[!horizontal]->getTexture()->useTexture(1); Model::getPlaneModel()->drawModel(mat4(1.f)); } @@ -355,8 +358,8 @@ void Scene::setView(unsigned int x, unsigned int y, unsigned int width, unsigned void Scene::bindShader() { /*SceneObject*/ - ShaderTarget shader[] = {SH_BASIC,SH_BASICTEXTURE,SH_LIGHTING }; - for (int s = 0; s < 3; s++) + ShaderTarget shader[] = {SH_BASIC,SH_BASICTEXTURE,SH_LIGHTING, SH_SHADOWDIR}; + for (int s = 0; s < NELEMS(shader); s++) { for (auto i = SceneObjects.cbegin(); i != SceneObjects.cend(); ++i) (*i)->bindShader(Shader::getShader(shader[s])); @@ -369,4 +372,6 @@ void Scene::bindShader() for (auto i = SceneObjects.cbegin(); i != SceneObjects.cend(); ++i) (*i)->bindShaderCollision(Shader::getShader(SH_BASIC)); + + } \ No newline at end of file diff --git a/Weave/Weave.vcxproj b/Weave/Weave.vcxproj index c8cb897..82b6bd4 100644 --- a/Weave/Weave.vcxproj +++ b/Weave/Weave.vcxproj @@ -106,6 +106,7 @@ + @@ -142,6 +143,7 @@ + diff --git a/Weave/Weave.vcxproj.filters b/Weave/Weave.vcxproj.filters index 90fa55c..e079ff4 100644 --- a/Weave/Weave.vcxproj.filters +++ b/Weave/Weave.vcxproj.filters @@ -102,6 +102,9 @@ Source Files + + Source Files + @@ -206,5 +209,8 @@ Header Files + + Header Files + \ No newline at end of file -- 2.47.3