From c65f705931afc0d7ea13916d219b25143858240a Mon Sep 17 00:00:00 2001 From: uPiscium Date: Thu, 8 Aug 2024 15:12:40 +0900 Subject: [PATCH] Fix screen system bugs. --- impls/screen.cpp | 62 ++++++++++++++++++++++++++++++-------------- includes/screen.hpp | 17 ++++++++---- includes/texture.hpp | 2 +- tests/TGTest.cpp | 43 +++++++++++++++++++++++++++--- 4 files changed, 96 insertions(+), 28 deletions(-) diff --git a/impls/screen.cpp b/impls/screen.cpp index a82c7c8..d7b3785 100644 --- a/impls/screen.cpp +++ b/impls/screen.cpp @@ -1,31 +1,51 @@ #include "../includes/screen.hpp" -#include "../includes/exceptions.hpp" +/* #include "../includes/exceptions.hpp" */ + +#include namespace TerreateGraphics::Core { using namespace TerreateGraphics::Defines; void Screen::AddBuffer() { - if (mTextures.size() >= 32) { - throw Exceptions::ScreenError("Max buffer count reached."); - return; - } + /* if (mTextures.size() >= 32) { */ + /* throw Exceptions::ScreenError("Max buffer count reached."); */ + /* return; */ + /* } */ + + /* GLObject buffer = GLObject(); */ + + /* glBindFramebuffer(GL_FRAMEBUFFER, mFrameBuffer); */ + /* glGenTextures(1, buffer); */ + /* glBindTexture(GL_TEXTURE_2D, buffer); */ + /* glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA, + * GL_FLOAT, */ + /* nullptr); */ + /* glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); */ + /* glFramebufferTexture2D(GL_FRAMEBUFFER, */ + /* GL_COLOR_ATTACHMENT0 + mTextures.size(), + * GL_TEXTURE_2D, */ + /* buffer, 0); */ + /* glBindFramebuffer(GL_FRAMEBUFFER, 0); */ + + /* Texture texture = Texture(buffer, mWidth, mHeight, 4); */ + /* mDrawBuffers.push_back(GL_COLOR_ATTACHMENT0 + mTextures.size()); */ + /* mTextures.push_back(texture); */ + /* glBindTexture(GL_TEXTURE_2D, 0); */ - ID buffer = 0; + GLObject buffer = GLObject(); glBindFramebuffer(GL_FRAMEBUFFER, mFrameBuffer); - glGenTextures(1, &buffer); + glGenTextures(1, buffer); glBindTexture(GL_TEXTURE_2D, buffer); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA, GL_FLOAT, - nullptr); + NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glFramebufferTexture2D(GL_FRAMEBUFFER, - GL_COLOR_ATTACHMENT0 + mTextures.size(), GL_TEXTURE_2D, + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, buffer, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0); - Texture texture = Texture(buffer, mWidth, mHeight, 4); - mDrawBuffers.push_back(GL_COLOR_ATTACHMENT0 + mTextures.size()); - mTextures.push_back(texture); + mTexture = Texture(buffer, mWidth, mHeight, 4); + mDrawBuffers.push_back(GL_COLOR_ATTACHMENT0); glBindTexture(GL_TEXTURE_2D, 0); } @@ -61,27 +81,31 @@ void Screen::Transcript(Screen const &screen) const { void Screen::DrawOnlyBind() const { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mFrameBuffer); - glDrawBuffers(mDrawBuffers.size(), mDrawBuffers.data()); + // glDrawBuffers(mDrawBuffers.size(), mDrawBuffers.data()); } -void Screen::Bind() const { +void Screen::Bind() { + glGetIntegerv(GL_VIEWPORT, mInitialViewPort); glBindFramebuffer(GL_FRAMEBUFFER, mFrameBuffer); - glDrawBuffers(mDrawBuffers.size(), mDrawBuffers.data()); + glViewport(0, 0, mWidth, mHeight); + // glDrawBuffers(mDrawBuffers.size(), mDrawBuffers.data()); } void Screen::Unbind() const { glBindFramebuffer(GL_FRAMEBUFFER, 0); - glDrawBuffer(GL_BACK); + glViewport(mInitialViewPort[0], mInitialViewPort[1], mInitialViewPort[2], + mInitialViewPort[3]); + // glDrawBuffer(GL_BACK); } -void Screen::Fill(Vec const &color) const { +void Screen::Fill(Vec const &color) { this->Bind(); glClearColor(color[0], color[1], color[2], 0.0f); glClear(GL_COLOR_BUFFER_BIT); this->Unbind(); } -void Screen::Clear() const { +void Screen::Clear() { this->Bind(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); this->Unbind(); diff --git a/includes/screen.hpp b/includes/screen.hpp index 634d00a..db531af 100644 --- a/includes/screen.hpp +++ b/includes/screen.hpp @@ -14,7 +14,9 @@ class Screen final : public TerreateObjectBase { GLObject mFrameBuffer = GLObject(); Uint mWidth; Uint mHeight; - Vec mTextures; + Int mInitialViewPort[4]; + // Vec mTextures; + Texture mTexture; Vec mDrawBuffers; // FIXME: Support multi-buffer screen. @@ -47,7 +49,7 @@ class Screen final : public TerreateObjectBase { * @brief: Getter for texture. * @return: texture */ - Texture const &GetTexture() const { return mTextures[0]; } + Texture const &GetTexture() const { return mTexture; } /* * @brief: Transcript to other screen. @@ -67,7 +69,7 @@ class Screen final : public TerreateObjectBase { /* * @brief: Binds the screen. */ - void Bind() const; + void Bind(); /* * @brief: Unbinds the screen. */ @@ -78,11 +80,11 @@ class Screen final : public TerreateObjectBase { * @detail: Color format is (red, green, blue). Each color is float (0 * ~ 1.0). */ - void Fill(Vec const &color) const; + void Fill(Vec const &color); /* * @brief: Clear screen. */ - void Clear() const; + void Clear(); // FIXME: Support multi-buffer screen. /* Texture const &operator[](Index const &index) const { */ @@ -90,6 +92,11 @@ class Screen final : public TerreateObjectBase { /* } */ operator Bool() const override { return mFrameBuffer; } }; + +class CubeScreen : public TerreateObjectBase { +private: + ; +}; } // namespace TerreateGraphics::Core #endif // __TERREATE_GRAPHICS_SCREEN_HPP__ diff --git a/includes/texture.hpp b/includes/texture.hpp index e1607e3..c87bf94 100644 --- a/includes/texture.hpp +++ b/includes/texture.hpp @@ -41,7 +41,7 @@ class Texture final : public TerreateObjectBase { * @param: height: height of texture * @param: channels: number of channels in texture */ - Texture(Uint const &texture, Uint const &width, Uint const &height, + Texture(GLObject const &texture, Uint const &width, Uint const &height, Uint const &channels) : mTexture(texture), mWidth(width), mHeight(height), mChannels(channels) { } diff --git a/tests/TGTest.cpp b/tests/TGTest.cpp index 28ff0de..a7fa5cf 100644 --- a/tests/TGTest.cpp +++ b/tests/TGTest.cpp @@ -1,5 +1,7 @@ #include "../../includes/TerreateGraphics.hpp" +#include + using namespace TerreateGraphics::Core; // using namespace TerreateMath::Utils; @@ -7,6 +9,7 @@ class TestApp : public WindowController { private: Clock mClock; Shader mShader; + Shader mScreenShader; mat4 mTransform; Float mWidth = 1500.0f; Float mHeight = 750.0f; @@ -26,6 +29,8 @@ class TestApp : public WindowController { Buffer mBuffer; BufferDataConstructor mColorDataConstructor; + Screen mScreen; + public: void SizeCallback(Window *window, int const &width, int const &height) override { @@ -62,7 +67,7 @@ class TestApp : public WindowController { } public: - TestApp() { + TestApp() : mScreen(1000, 1000) { mFont = Font("tests/resources/AsebiMin-Light.otf", 200); mText.LoadFont(&mFont); mText.LoadText(L"日本語テスト"); @@ -77,6 +82,13 @@ class TestApp : public WindowController { mShader.Compile(); mShader.Link(); + mScreenShader.AddVertexShaderSource( + Shader::LoadShaderSource("tests/resources/vertex.glsl")); + mScreenShader.AddFragmentShaderSource( + Shader::LoadShaderSource("tests/resources/fragment.glsl")); + mScreenShader.Compile(); + mScreenShader.Link(); + BufferDataConstructor bdc; bdc.AddVertexComponent("iPosition", {{-600.0f, -600.0f, 600.0f}, @@ -125,6 +137,14 @@ class TestApp : public WindowController { "uTransform", mTransform * scale(identity(), vec3(1.0f / mWidth, 1.0f / mHeight, 1.0f / mDepth))); + + mScreenShader.Use(); + mScreenShader.SetInt("uTexture", 0); + mScreenShader.ActiveTexture(TextureTargets::TEX_0); + mScreenShader.SetMat4( + "uTransform", + mTransform * scale(identity(), + vec3(1.0f / 1000.0f, 1.0f / 1000.f, 1.0f / mDepth))); } void OnFrame(Window *window) override { @@ -136,13 +156,30 @@ class TestApp : public WindowController { mat4 model = rotate(identity(), angle, vec3(1, 1, 1)); model = translate(model, vec3(0.0f, 0.0f, -100.0f)); + mScreenShader.Use(); + mScreenShader.SetMat4("uModel", model); + mScreenShader.SetMat4("uNormalTransform", transpose(inverse(model))); + + Texture const &texture = mScreen.GetTexture(); + + mScreen.Fill({0.2, 0.2, 0.2}); + mScreen.Clear(); + mScreen.Bind(); + texture.Bind(); + mBuffer.Draw(DrawMode::TRIANGLES); + texture.Unbind(); + // mText.Render(50, 50, mScreen.GetWidth(), mScreen.GetHeight()); + mScreen.Unbind(); + mScreenShader.Unuse(); + + window->Bind(); mShader.Use(); mShader.SetMat4("uModel", model); mShader.SetMat4("uNormalTransform", transpose(inverse(model))); - mTexture.Bind(); + texture.Bind(); mBuffer.Draw(DrawMode::TRIANGLES); - mTexture.Unbind(); + texture.Unbind(); mShader.Unuse(); mText.Render(50, 50, mWidth, mHeight);