Skip to content

Commit

Permalink
Merge pull request #128 from uPiscium/develop
Browse files Browse the repository at this point in the history
Fix screen system bugs.
  • Loading branch information
uPiscium authored Aug 8, 2024
2 parents 91c3f43 + 6dfe620 commit 2dea368
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 28 deletions.
62 changes: 43 additions & 19 deletions impls/screen.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,51 @@
#include "../includes/screen.hpp"
#include "../includes/exceptions.hpp"
/* #include "../includes/exceptions.hpp" */

#include <iostream>

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);
}

Expand Down Expand Up @@ -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<Float> const &color) const {
void Screen::Fill(Vec<Float> 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();
Expand Down
17 changes: 12 additions & 5 deletions includes/screen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class Screen final : public TerreateObjectBase {
GLObject mFrameBuffer = GLObject();
Uint mWidth;
Uint mHeight;
Vec<Texture> mTextures;
Int mInitialViewPort[4];
// Vec<Texture> mTextures;
Texture mTexture;
Vec<GLenum> mDrawBuffers;

// FIXME: Support multi-buffer screen.
Expand Down Expand Up @@ -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.
Expand All @@ -67,7 +69,7 @@ class Screen final : public TerreateObjectBase {
/*
* @brief: Binds the screen.
*/
void Bind() const;
void Bind();
/*
* @brief: Unbinds the screen.
*/
Expand All @@ -78,18 +80,23 @@ class Screen final : public TerreateObjectBase {
* @detail: Color format is (red, green, blue). Each color is float (0
* ~ 1.0).
*/
void Fill(Vec<Float> const &color) const;
void Fill(Vec<Float> const &color);
/*
* @brief: Clear screen.
*/
void Clear() const;
void Clear();

// FIXME: Support multi-buffer screen.
/* Texture const &operator[](Index const &index) const { */
/* return mTextures[index]; */
/* } */
operator Bool() const override { return mFrameBuffer; }
};

class CubeScreen : public TerreateObjectBase {
private:
;
};
} // namespace TerreateGraphics::Core

#endif // __TERREATE_GRAPHICS_SCREEN_HPP__
2 changes: 1 addition & 1 deletion includes/texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
Expand Down
43 changes: 40 additions & 3 deletions tests/TGTest.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include "../../includes/TerreateGraphics.hpp"

#include <iostream>

using namespace TerreateGraphics::Core;
// using namespace TerreateMath::Utils;

class TestApp : public WindowController {
private:
Clock mClock;
Shader mShader;
Shader mScreenShader;
mat4 mTransform;
Float mWidth = 1500.0f;
Float mHeight = 750.0f;
Expand All @@ -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 {
Expand Down Expand Up @@ -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"日本語テスト");
Expand All @@ -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},
Expand Down Expand Up @@ -125,6 +137,14 @@ class TestApp : public WindowController {
"uTransform",
mTransform * scale(identity<mat4>(),
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<mat4>(),
vec3(1.0f / 1000.0f, 1.0f / 1000.f, 1.0f / mDepth)));
}

void OnFrame(Window *window) override {
Expand All @@ -136,13 +156,30 @@ class TestApp : public WindowController {
mat4 model = rotate(identity<mat4>(), 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);
Expand Down

0 comments on commit 2dea368

Please sign in to comment.