From 9a905a5b2bacf6676b06599b0365ce00529dae6d Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Thu, 14 Apr 2016 19:21:10 +0200 Subject: [PATCH] more BufferClasses --- Weave/Graphix/Model/Model.cpp | 13 +++ Weave/Graphix/Model/Model.h | 2 + Weave/Graphix/Model/Plane.h | 4 +- Weave/Graphix/Shader.cpp | 2 +- Weave/Graphix/Textures.h | 3 +- Weave/Graphix/Textures/BufferObject.cpp | 18 ++++ Weave/Graphix/Textures/BufferObject.h | 29 ++++++ Weave/Graphix/Textures/Texture.cpp | 17 ++-- Weave/Graphix/Textures/Texture.h | 8 +- Weave/Graphix/Textures/fBufferObject.cpp | 114 +++++++++++++++++++++++ Weave/Graphix/Textures/fBufferObject.h | 34 +++++++ Weave/Graphix/Textures/rBufferObject.cpp | 53 +++++++++++ Weave/Graphix/Textures/rBufferObject.h | 25 +++++ Weave/Graphix/Textures/tBuffer.cpp | 66 ------------- Weave/Graphix/Textures/tBuffer.h | 28 ------ Weave/Graphix/Textures/tImage.cpp | 9 +- Weave/Graphix/Textures/tImage.h | 4 +- Weave/Scene/Scene.cpp | 18 +++- Weave/Scene/Scene.h | 4 + Weave/Weave.vcxproj | 7 +- Weave/Weave.vcxproj.filters | 13 ++- shader/basicTexture_FS.hlsl | 4 +- shader/lightingTexture_FS.hlsl | 2 + 23 files changed, 357 insertions(+), 120 deletions(-) create mode 100644 Weave/Graphix/Textures/BufferObject.cpp create mode 100644 Weave/Graphix/Textures/BufferObject.h create mode 100644 Weave/Graphix/Textures/fBufferObject.cpp create mode 100644 Weave/Graphix/Textures/fBufferObject.h create mode 100644 Weave/Graphix/Textures/rBufferObject.cpp create mode 100644 Weave/Graphix/Textures/rBufferObject.h delete mode 100644 Weave/Graphix/Textures/tBuffer.cpp delete mode 100644 Weave/Graphix/Textures/tBuffer.h diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index c819389..4a28612 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -15,6 +15,7 @@ #include "BBox.h" #include "SkyBox.h" #include "IMetaMesh.h" +#include "Plane.h" #include "../../Events.h" @@ -237,6 +238,17 @@ Model* Model::getBBoxModel() return BoundingBox; } +Model* Model::getPlaneModel() +{ + if (PlaneModel == nullptr) + { + PlaneModel = new Plane(); + PlaneModel->bindModel(); + PlaneModel->bindShader(Shader::gBasicTexture()); + } + return PlaneModel; +} + Model* Model::getSkyBoxModel() { if (SkyBoxModel == nullptr) @@ -409,6 +421,7 @@ void Model::bt_init(bool _isConvex) Model* Model::BoundingBox = nullptr; +Model* Model::PlaneModel = nullptr; Model* Model::SkyBoxModel = nullptr; std::unordered_map Model::IMetaModel; std::unordered_map Model::IMetaModel_count; diff --git a/Weave/Graphix/Model/Model.h b/Weave/Graphix/Model/Model.h index c14756b..eb201f1 100644 --- a/Weave/Graphix/Model/Model.h +++ b/Weave/Graphix/Model/Model.h @@ -31,6 +31,7 @@ public: virtual void drawBBox(const mat4& modelMat, const vec4& color = vec4(0.9f, 0.f, 0.f, 1.f)) const; static Model* getBBoxModel(); + static Model* getPlaneModel(); static Model* getSkyBoxModel(); static Model* newIMetaModel(const std::string& modelpath,bool isConvex=true); @@ -71,6 +72,7 @@ protected: static Model* BoundingBox; static Model* SkyBoxModel; + static Model* PlaneModel; static std::unordered_map IMetaModel; static std::unordered_map IMetaModel_count; diff --git a/Weave/Graphix/Model/Plane.h b/Weave/Graphix/Model/Plane.h index 90121fb..483c74b 100644 --- a/Weave/Graphix/Model/Plane.h +++ b/Weave/Graphix/Model/Plane.h @@ -9,10 +9,12 @@ public: Plane(); ~Plane(); - void drawModel() const override; void bt_init(bool isConvex = true) override; std::string type2str() const override; + +protected: + void drawModel() const override; }; diff --git a/Weave/Graphix/Shader.cpp b/Weave/Graphix/Shader.cpp index 68bd179..2a29b5e 100644 --- a/Weave/Graphix/Shader.cpp +++ b/Weave/Graphix/Shader.cpp @@ -200,7 +200,7 @@ GLuint Shader::loadProgram(GLuint _shader1, GLuint _shader2){ glAttachShader(programHandle, _shader1); glAttachShader(programHandle, _shader2); - glBindFragDataLocation(programHandle, 0, "fragColor"); + //glBindFragDataLocation(programHandle, 0, "fragColor"); glLinkProgram(programHandle); diff --git a/Weave/Graphix/Textures.h b/Weave/Graphix/Textures.h index 45f5bc0..9b981cd 100644 --- a/Weave/Graphix/Textures.h +++ b/Weave/Graphix/Textures.h @@ -2,4 +2,5 @@ #include "Textures/Texture.h" //#include "Textures\tImage.h" -#include "Textures\tBuffer.h" +#include "Textures\fBufferObject.h" +#include "Textures\rBufferObject.h" \ No newline at end of file diff --git a/Weave/Graphix/Textures/BufferObject.cpp b/Weave/Graphix/Textures/BufferObject.cpp new file mode 100644 index 0000000..f9ca1a7 --- /dev/null +++ b/Weave/Graphix/Textures/BufferObject.cpp @@ -0,0 +1,18 @@ +#include "BufferObject.h" + +#include + + +BufferObject::BufferObject() +{ +} + + +BufferObject::~BufferObject() +{ +} + +BufferObject::operator unsigned int() const +{ + return handle; +} \ No newline at end of file diff --git a/Weave/Graphix/Textures/BufferObject.h b/Weave/Graphix/Textures/BufferObject.h new file mode 100644 index 0000000..99843cf --- /dev/null +++ b/Weave/Graphix/Textures/BufferObject.h @@ -0,0 +1,29 @@ +#pragma once +class BufferObject +{ +public: +// BufferObject() {}; +// virtual ~BufferObject() = 0; + + virtual void bindBuffer(unsigned int width = 0, unsigned int height = 0) = 0; + virtual void unbindBuffer() = 0; + virtual void useBuffer() const = 0; + virtual void clearBuffer() const = 0; + + virtual void updateSize(unsigned int _width, unsigned int _height) = 0; + + virtual operator unsigned int() const + { + return handle; + } + + virtual unsigned int getTarget() const + { + return buffer_target; + } + +protected: + unsigned int handle; + unsigned int buffer_target; +}; + diff --git a/Weave/Graphix/Textures/Texture.cpp b/Weave/Graphix/Textures/Texture.cpp index c0cf076..2b7fc58 100644 --- a/Weave/Graphix/Textures/Texture.cpp +++ b/Weave/Graphix/Textures/Texture.cpp @@ -65,8 +65,14 @@ Texture::operator unsigned int() const return handle; } -void Texture::bindTexture() +void Texture::bindTexture(unsigned int _width, unsigned int _height) { + if (_width != 0 && _height != 0) + { + width = _width; + height = _height; + } + glBindTexture(texture_target, handle); glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); @@ -85,19 +91,18 @@ void Texture::unbindTexture() glGenTextures(1, &handle); } -void Texture::useTexture() const +void Texture::useTexture(unsigned int _unit) const { /* bind Texture*/ - int unit = 0; - glActiveTexture(GL_TEXTURE0 + unit); + glActiveTexture(GL_TEXTURE0 + _unit); glBindTexture(texture_target, handle); } void Texture::updateSize(unsigned int _width, unsigned int _height) { - width = _width; - height = _height; + if (_width == 0 || _height == 0) + return; glBindTexture(texture_target, handle); glTexImage2D(texture_target, 0, texture_internal, width, height, 0, texture_format, texture_type, data); diff --git a/Weave/Graphix/Textures/Texture.h b/Weave/Graphix/Textures/Texture.h index 3fbc945..5da8a95 100644 --- a/Weave/Graphix/Textures/Texture.h +++ b/Weave/Graphix/Textures/Texture.h @@ -16,11 +16,11 @@ public: Texture(); ~Texture(); - virtual void bindTexture(); + virtual void bindTexture(unsigned int width = 0, unsigned int height = 0); virtual void unbindTexture(); - virtual void useTexture() const; + virtual void useTexture(unsigned int i=0) const; - virtual void updateSize(unsigned int width, unsigned int height); + virtual void updateSize(unsigned int width = 0, unsigned int height = 0); virtual operator unsigned int() const; @@ -42,7 +42,7 @@ protected: typedef struct texStruct { unsigned int count=0; - tImage* ptr=nullptr; + tImage* ptr=NULL; } texHandle; static std::unordered_map texture_Map; diff --git a/Weave/Graphix/Textures/fBufferObject.cpp b/Weave/Graphix/Textures/fBufferObject.cpp new file mode 100644 index 0000000..8ab4a66 --- /dev/null +++ b/Weave/Graphix/Textures/fBufferObject.cpp @@ -0,0 +1,114 @@ +#include "fBufferObject.h" + +#include +#include "../Shader.h" +#include "../../Message.h" + +#include "Texture.h" +#include "rBufferObject.h" + + +fBufferObject::fBufferObject(unsigned int _dim, bool _rbo) + : dim(_dim) +{ + buffer_target = GL_FRAMEBUFFER; + + /*gen Buffer*/ + glGenFramebuffers(1, &handle); + + + /*gen Texture*/ + textures = new Texture[_dim]; + + if (_rbo) + rBO = new rBufferObject; +} + + +fBufferObject::~fBufferObject() +{ + delete textures; + + if (rBO != nullptr) + delete rBO; + + glDeleteFramebuffers(1, &handle); +} + +unsigned int fBufferObject::getDim() const +{ + return dim; +} + +Texture* fBufferObject::getTexture(unsigned int i) const +{ + return &textures[i]; +} + +void fBufferObject::bindBuffer(unsigned int _width, unsigned int _height) +{ + + glBindFramebuffer(buffer_target, handle); + + /*Bind Texture Buffers*/ + unsigned int* attachments = new unsigned int[dim]; + for (unsigned int i = 0; i < dim; i++) + { + textures[i].bindTexture(_width, _height); + glFramebufferTexture2D(buffer_target, GL_COLOR_ATTACHMENT0 + i, textures[i].getTTarget(), textures[i], 0); + attachments[i] = GL_COLOR_ATTACHMENT0 + i; + } + + /*Bind Render Buffers*/ + if (rBO != nullptr) + { + rBO->bindBuffer(_width, _height); + glFramebufferRenderbuffer(buffer_target, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, *rBO); + } + + + //glDrawBuffers(dim, attachments); + if (glCheckFramebufferStatus(buffer_target) != GL_FRAMEBUFFER_COMPLETE) + Message::error("fBufferObject: Framebuffer not complete!"); + + delete attachments; +} + +void fBufferObject::unbindBuffer() +{ + for (unsigned int i = 0; i < dim; i++) + { + textures[i].unbindTexture(); + } + + if (rBO != nullptr) + rBO->unbindBuffer(); + + glDeleteFramebuffers(1, &handle); + glGenFramebuffers(1, &handle); + +} + +void fBufferObject::useBuffer() const +{ + glBindFramebuffer(buffer_target, handle); + +} + +void fBufferObject::clearBuffer() const +{ + glBindFramebuffer(buffer_target, 0); +} + +void fBufferObject::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/fBufferObject.h b/Weave/Graphix/Textures/fBufferObject.h new file mode 100644 index 0000000..f8fbc04 --- /dev/null +++ b/Weave/Graphix/Textures/fBufferObject.h @@ -0,0 +1,34 @@ +#pragma once + +#include "BufferObject.h" + +class Texture; +class rBufferObject; + +class fBufferObject + : public BufferObject +{ +public: + fBufferObject(unsigned int dim=1, bool rbo = true); + virtual ~fBufferObject(); + + 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/rBufferObject.cpp b/Weave/Graphix/Textures/rBufferObject.cpp new file mode 100644 index 0000000..e96ffdf --- /dev/null +++ b/Weave/Graphix/Textures/rBufferObject.cpp @@ -0,0 +1,53 @@ +#include "rBufferObject.h" + +#include +#include "../Shader.h" +#include "../../Message.h" + + +rBufferObject::rBufferObject() +{ + buffer_target = GL_RENDERBUFFER; + + /*gen Buffer*/ + glGenRenderbuffers(1, &handle); +} + + +rBufferObject::~rBufferObject() +{ + glDeleteRenderbuffers(1, &handle); +} + +void rBufferObject::bindBuffer(unsigned int _width, unsigned int _height) +{ + if (_width != 0 && _height != 0) + { + width = _width; + height = _height; + } + glBindRenderbuffer(buffer_target, handle); + glRenderbufferStorage(buffer_target, GL_DEPTH_COMPONENT, width, height); +} + +void rBufferObject::unbindBuffer() +{ + glDeleteRenderbuffers(1, &handle); + glGenRenderbuffers(1, &handle); +} + +void rBufferObject::useBuffer() const +{ + glBindRenderbuffer(buffer_target, handle); + +} + +void rBufferObject::clearBuffer() const +{ + glBindRenderbuffer(buffer_target, 0); +} + +void rBufferObject::updateSize(unsigned int _width, unsigned int _height) +{ + bindBuffer(_width, _height); +} diff --git a/Weave/Graphix/Textures/rBufferObject.h b/Weave/Graphix/Textures/rBufferObject.h new file mode 100644 index 0000000..31f07c2 --- /dev/null +++ b/Weave/Graphix/Textures/rBufferObject.h @@ -0,0 +1,25 @@ +#pragma once + +#include "BufferObject.h" + +class rBufferObject + : public BufferObject +{ +public: + rBufferObject(); + virtual ~rBufferObject(); + + 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) override; + +protected: + unsigned int width=0, height = 0; + +}; + diff --git a/Weave/Graphix/Textures/tBuffer.cpp b/Weave/Graphix/Textures/tBuffer.cpp deleted file mode 100644 index b76f95f..0000000 --- a/Weave/Graphix/Textures/tBuffer.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include "tBuffer.h" - -#include -#include "../Shader.h" - - -tBuffer::tBuffer(unsigned int _dim) -{ - buffer_target = GL_FRAMEBUFFER; - - /*gen Buffer*/ - glGenBuffers(1, &handle); - - - /*gen Texture*/ - textures = new Texture[_dim]; -} - - -tBuffer::~tBuffer() -{ - delete textures; - glDeleteBuffers(1, &handle); -} - -tBuffer::operator unsigned int() const -{ - return handle; -} - -unsigned int tBuffer::getDim() const -{ - return dim; -} - -Texture* tBuffer::getTexture(unsigned int i) const -{ - return &textures[i]; -} - -void tBuffer::bindBuffer() -{ - glBindFramebuffer(buffer_target, handle); - for (unsigned int i = 0; i < dim; i++) - { - textures[i].bindTexture(); - glFramebufferTexture2D(buffer_target, GL_COLOR_ATTACHMENT0 + i, textures[i].getTTarget(), textures[i], 0); - } - -} - -void tBuffer::unbindBuffer() -{ - for (unsigned int i = 0; i < dim; i++) - { - textures[i].unbindTexture(); - } - glDeleteBuffers(1, &handle); - glGenBuffers(1, &handle); - -} - -void tBuffer::useBuffer() const -{ - glBindFramebuffer(buffer_target, handle); -} \ No newline at end of file diff --git a/Weave/Graphix/Textures/tBuffer.h b/Weave/Graphix/Textures/tBuffer.h deleted file mode 100644 index 60b5d03..0000000 --- a/Weave/Graphix/Textures/tBuffer.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include "Texture.h" - -class tBuffer -{ -public: - tBuffer(unsigned int dim=1); - virtual ~tBuffer(); - - virtual void bindBuffer(); - virtual void unbindBuffer(); - virtual void useBuffer() const; - - operator unsigned int() const; - - unsigned int getDim() const; - Texture* getTexture(unsigned int i = 0) const; - - unsigned int dim; - Texture* textures = nullptr; -protected: - unsigned int handle; - unsigned int buffer_target; - - - -}; - diff --git a/Weave/Graphix/Textures/tImage.cpp b/Weave/Graphix/Textures/tImage.cpp index 74e004e..9b0dea0 100644 --- a/Weave/Graphix/Textures/tImage.cpp +++ b/Weave/Graphix/Textures/tImage.cpp @@ -46,7 +46,7 @@ tImage::~tImage() FreeImage_Unload((FIBITMAP*)data); } -void tImage::bindTexture() +void tImage::bindTexture(unsigned int _width, unsigned int _height) { Texture::bindTexture(); @@ -54,15 +54,14 @@ void tImage::bindTexture() } -void tImage::useTexture() const +void tImage::useTexture(unsigned int _unit) const { /* bind Texture*/ - int unit = 0; - glActiveTexture(GL_TEXTURE0 + unit); + glActiveTexture(GL_TEXTURE0 + _unit); glBindTexture(texture_target, handle); - Shader::gActive()->setUniformLocation("uColorTexture", unit); + Shader::gActive()->setUniformLocation("uColorTexture", _unit); /* bind Material*/ Shader::gActive()->setUniformLocation("material", material); diff --git a/Weave/Graphix/Textures/tImage.h b/Weave/Graphix/Textures/tImage.h index cbb19c4..de5ab57 100644 --- a/Weave/Graphix/Textures/tImage.h +++ b/Weave/Graphix/Textures/tImage.h @@ -13,8 +13,8 @@ public: tImage(const std::string& path, const vec4& material); virtual ~tImage(); - virtual void bindTexture() override; - virtual void useTexture() const override; + virtual void bindTexture(unsigned int width = 0, unsigned int height = 0) override; + virtual void useTexture(unsigned int i=0) const override; operator std::string() const; diff --git a/Weave/Scene/Scene.cpp b/Weave/Scene/Scene.cpp index dd8dcb1..b8e6633 100644 --- a/Weave/Scene/Scene.cpp +++ b/Weave/Scene/Scene.cpp @@ -8,6 +8,8 @@ #include "../Graphix/Shader.h" #include "../Graphix/ViewPort.h" +#include "../Graphix/Textures.h" +#include "../Graphix/Model.h" #include "../Message.h" #include "../Events.h" @@ -54,6 +56,14 @@ lookat(_lookat) bt_dynamics_world->setGravity(btVector3(0, YFALL_SPEED, 0)); + + blur = new fBufferObject(2); + render = new fBufferObject(1); + + blur->bindBuffer(Graphix::getWindowWidth(), Graphix::getWindowHeight()); + render->bindBuffer(Graphix::getWindowWidth(), Graphix::getWindowHeight()); + + render->clearBuffer(); } @@ -163,6 +173,7 @@ void Scene::update(float deltaT) void Scene::draw() const { + //render->useBuffer(); viewPort.useViewPort(); //Skybox @@ -177,11 +188,16 @@ void Scene::draw() const } SkyBox.draw(); - for (auto i = SceneObjects.cbegin(); i != SceneObjects.cend(); ++i) { (*i)->draw(); } + + //render->clearBuffer(); + //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + //Shader::gBasicTexture()->useShader(); + //render->getTexture(0)->useTexture(0); + //Model::getPlaneModel()->drawModel(mat4(1.f)); } diff --git a/Weave/Scene/Scene.h b/Weave/Scene/Scene.h index 551af96..5db976f 100644 --- a/Weave/Scene/Scene.h +++ b/Weave/Scene/Scene.h @@ -15,6 +15,7 @@ class SceneObject; class ViewPort; class Sky; class Shader; +class fBufferObject; class Scene { @@ -61,4 +62,7 @@ protected: btSequentialImpulseConstraintSolver* bt_solver; btCollisionWorld* bt_collision_world; btDiscreteDynamicsWorld* bt_dynamics_world; + + fBufferObject* render; + fBufferObject* blur; }; diff --git a/Weave/Weave.vcxproj b/Weave/Weave.vcxproj index 9f707bf..c8cb897 100644 --- a/Weave/Weave.vcxproj +++ b/Weave/Weave.vcxproj @@ -106,7 +106,8 @@ - + + @@ -123,6 +124,7 @@ + @@ -140,7 +142,8 @@ - + + diff --git a/Weave/Weave.vcxproj.filters b/Weave/Weave.vcxproj.filters index 540faaf..90fa55c 100644 --- a/Weave/Weave.vcxproj.filters +++ b/Weave/Weave.vcxproj.filters @@ -96,7 +96,10 @@ Source Files - + + Source Files + + Source Files @@ -194,7 +197,13 @@ Header Files - + + Header Files + + + Header Files + + Header Files diff --git a/shader/basicTexture_FS.hlsl b/shader/basicTexture_FS.hlsl index 132e13f..db0e01d 100644 --- a/shader/basicTexture_FS.hlsl +++ b/shader/basicTexture_FS.hlsl @@ -1,8 +1,10 @@ //Fragment Shader -#version 330 +#version 330 core //in worldNormal; in vec2 fUVs; +layout(location = 0) out vec4 gl_FragColor; + uniform sampler2D uColorTexture; void main() diff --git a/shader/lightingTexture_FS.hlsl b/shader/lightingTexture_FS.hlsl index 501ee92..4b7f56a 100644 --- a/shader/lightingTexture_FS.hlsl +++ b/shader/lightingTexture_FS.hlsl @@ -5,6 +5,8 @@ in vec2 fUVs; in vec3 PointLightPosition1, DirectionalLightDirection1; +layout (location = 0) out vec4 gl_FragColor; + uniform sampler2D uColorTexture; uniform vec4 material; //vec4 in the form (ambient, point, directional, glossyness); so far it was (1, 1, 1, 3) -- 2.47.3