From ed4a0178da21e5b6f8f96212e441937a1d318769 Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Dev Date: Sat, 2 Dec 2017 15:25:02 +0500 Subject: [PATCH] SSAA 4X demo --- assets/SSAA_4X.frag | 18 +++ assets/SSAA_4X.vertex | 11 ++ assets/color_fragment.txt | 7 ++ assets/color_vertex.txt | 11 ++ game/gamecode.cpp | 254 +++++++++++++++++++++++--------------- game/main_code.cpp | 24 +++- 6 files changed, 223 insertions(+), 102 deletions(-) create mode 100755 assets/SSAA_4X.frag create mode 100755 assets/SSAA_4X.vertex create mode 100755 assets/color_fragment.txt create mode 100755 assets/color_vertex.txt diff --git a/assets/SSAA_4X.frag b/assets/SSAA_4X.frag new file mode 100755 index 0000000..62db00e --- /dev/null +++ b/assets/SSAA_4X.frag @@ -0,0 +1,18 @@ +precision mediump float; + +uniform sampler2D Texture; +uniform vec2 samplesOffset[4]; + +varying vec2 texCoord; + +void main() +{ + vec3 color = vec3(0, 0, 0); + for (int i = 0; i < 4; ++i) + { + color += texture2D(Texture, texCoord + samplesOffset[i]).rgb; + } + color /= 4; + + gl_FragColor = vec4(texture2D(Texture, texCoord ), 1.0); +} \ No newline at end of file diff --git a/assets/SSAA_4X.vertex b/assets/SSAA_4X.vertex new file mode 100755 index 0000000..8b5aa61 --- /dev/null +++ b/assets/SSAA_4X.vertex @@ -0,0 +1,11 @@ +attribute vec3 vPosition; +attribute vec2 vTexCoord; +varying vec2 texCoord; + +uniform mat4 ProjectionMatrix; + +void main() +{ + gl_Position = ProjectionMatrix * vec4(vPosition.xyz, 1.0); + texCoord = vTexCoord; +} \ No newline at end of file diff --git a/assets/color_fragment.txt b/assets/color_fragment.txt new file mode 100755 index 0000000..145e880 --- /dev/null +++ b/assets/color_fragment.txt @@ -0,0 +1,7 @@ +precision mediump float; +varying vec4 color; + +void main() +{ + gl_FragColor = color; +} \ No newline at end of file diff --git a/assets/color_vertex.txt b/assets/color_vertex.txt new file mode 100755 index 0000000..158514e --- /dev/null +++ b/assets/color_vertex.txt @@ -0,0 +1,11 @@ +attribute vec3 vPosition; +attribute vec4 vColor; +varying vec4 color; + +uniform mat4 ProjectionMatrix; + +void main() +{ + gl_Position = ProjectionMatrix * vec4(vPosition.xyz, 1.0); + color = vColor; +} \ No newline at end of file diff --git a/game/gamecode.cpp b/game/gamecode.cpp index eb3d70d..c350179 100644 --- a/game/gamecode.cpp +++ b/game/gamecode.cpp @@ -426,6 +426,8 @@ void TBall::Update(size_t dt) //=========================================== //=========================================== +TRenderPair pair; +TRenderPair rect; TGameLevel::TGameLevel() { @@ -446,6 +448,44 @@ TGameLevel::TGameLevel() BonusFloorPosY = 0.f; + pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 0, 0)); + pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 320, 0)); + pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(480, 320, 0)); + pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 0, 0)); + pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 320, 0)); + pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(480, 0, 0)); + + pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); + pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); + pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 1, 0, 1)); + pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 0, 1, 1)); + pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 0, 1, 1)); + pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 1, 0, 1)); + + pair.first.ShaderName = "ColorShader"; + + pair.second.RefreshBuffer(); + + + + + rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 0, 0)); + rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 320, 0)); + rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(480, 320, 0)); + rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(480, 320, 0)); + rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(480, 0, 0)); + rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 0, 0)); + + rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(0, 0)); + rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(0, 1)); + rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(1, 1)); + rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(1, 1)); + rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(1, 0)); + rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(0, 0)); + + rect.first.ShaderName = "ColorShader"; + + rect.second.RefreshBuffer(); } TGameLevel::~TGameLevel() @@ -735,10 +775,9 @@ void TGameLevel::Draw() bool mustShowButtons = ((LevelState == CONST_LEVELSTATE_PAUSE) || (LevelState == CONST_LEVELSTATE_GO_TO_PAUSE)); - bool pause = (mustShowButtons || (LevelState == CONST_LEVELSTATE_FINISHED)); - - bool renderBufferReady = RenderBufferReady; + + bool pause = true || (mustShowButtons || (LevelState == CONST_LEVELSTATE_FINISHED)); if (pause && renderBufferReady) { @@ -764,89 +803,96 @@ void TGameLevel::Draw() Renderer->SwitchToFrameBuffer("LevelBuffer"); Renderer->SetProjectionMatrix(480.f, 320.f); - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClearColor(0.0f, 0.0f, 1.0f, 1.0f); glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); CheckGlError(); } - glBindTexture(GL_TEXTURE_2D,ResourceManager->TexList[BkgTexture]); - Renderer->DrawRect(Vector2f(0, 0), Vector2f(480.f, 320.f)); - - std::list::iterator iBall; - - Renderer->PushShader("BrickShader"); - - - for (int i=0; iTexList[BkgTexture]); + //Renderer->DrawRect(Vector2f(0, 0), Vector2f(480.f, 320.f)); + // + //std::list::iterator iBall; + // + //Renderer->PushShader("BrickShader"); + // + // + //for (int i=0; i>::iterator colorBlockIterator; - for (colorBlockIterator = BlockInstansingList.ColorBlockList.begin(); colorBlockIterator != BlockInstansingList.ColorBlockList.end(); ++colorBlockIterator) - { - RenderUniform4fv("BrickColor", colorBlockIterator->first.first.data()); - glBindTexture(GL_TEXTURE_2D,ResourceManager->TexList[colorBlockIterator->first.second]); - - Renderer->DrawTriangleList(colorBlockIterator->second); - } - - std::list::iterator iBonus; - - for (iBonus = BonusFallingList.begin(); iBonus != BonusFallingList.end(); ++iBonus) - { - iBonus->Draw(); - } - - DrawBallInstancingList(); - - Renderer->PopShader(); - - - glBindTexture(GL_TEXTURE_2D,ResourceManager->TexList[CONST_REFLECTOR_TEXTURE]); - Renderer->DrawRect(Vector2f(-128.f, -16.f)+ReflectorPos, Vector2f(128.f, 16.f)+ReflectorPos); - - - const Vector2f wallUpPos1(240.f-256.f, 320.f-64.f); - const Vector2f wallUpPos2(240.f+256.f, 320.f); - glBindTexture(GL_TEXTURE_2D,ResourceManager->TexList[CONST_WALL_UP_TEXTURE]); - Renderer->DrawRect(wallUpPos1, wallUpPos2); - - const Vector2f wallLeftPos1(0.f, 320.f - 512.f); - const Vector2f wallLeftPos2(32.f, 320.f); - glBindTexture(GL_TEXTURE_2D,ResourceManager->TexList[CONST_WALL_LEFT_TEXTURE]); - Renderer->DrawRect(wallLeftPos1, wallLeftPos2); - - const Vector2f wallRightPos1(480.f-32.f, 320.f - 512.f); - const Vector2f wallRightPos2(480.f, 320.f); - glBindTexture(GL_TEXTURE_2D,ResourceManager->TexList[CONST_WALL_RIGHT_TEXTURE]); - Renderer->DrawRect(wallRightPos1, wallRightPos2); - + // std::list>::iterator colorBlockIterator; + // for (colorBlockIterator = BlockInstansingList.ColorBlockList.begin(); colorBlockIterator != BlockInstansingList.ColorBlockList.end(); ++colorBlockIterator) + // { + // RenderUniform4fv("BrickColor", colorBlockIterator->first.first.data()); + // glBindTexture(GL_TEXTURE_2D,ResourceManager->TexList[colorBlockIterator->first.second]); + // + // Renderer->DrawTriangleList(colorBlockIterator->second); + // } + // + // std::list::iterator iBonus; + // + // for (iBonus = BonusFallingList.begin(); iBonus != BonusFallingList.end(); ++iBonus) + // { + // iBonus->Draw(); + // } + // + // DrawBallInstancingList(); + // + // Renderer->PopShader(); + // + // + // glBindTexture(GL_TEXTURE_2D,ResourceManager->TexList[CONST_REFLECTOR_TEXTURE]); + //Renderer->DrawRect(Vector2f(-128.f, -16.f)+ReflectorPos, Vector2f(128.f, 16.f)+ReflectorPos); + // + // + // const Vector2f wallUpPos1(240.f-256.f, 320.f-64.f); + // const Vector2f wallUpPos2(240.f+256.f, 320.f); + //glBindTexture(GL_TEXTURE_2D,ResourceManager->TexList[CONST_WALL_UP_TEXTURE]); + //Renderer->DrawRect(wallUpPos1, wallUpPos2); + // + //const Vector2f wallLeftPos1(0.f, 320.f - 512.f); + //const Vector2f wallLeftPos2(32.f, 320.f); + //glBindTexture(GL_TEXTURE_2D,ResourceManager->TexList[CONST_WALL_LEFT_TEXTURE]); + //Renderer->DrawRect(wallLeftPos1, wallLeftPos2); + // + //const Vector2f wallRightPos1(480.f-32.f, 320.f - 512.f); + //const Vector2f wallRightPos2(480.f, 320.f); + //glBindTexture(GL_TEXTURE_2D,ResourceManager->TexList[CONST_WALL_RIGHT_TEXTURE]); + //Renderer->DrawRect(wallRightPos1, wallRightPos2); + // + + //if (BonusFloorTimer>0.f) + //{ + // + // const Vector2f wallDownPos(240.f, BonusFloorPosY); + // + // glBindTexture(GL_TEXTURE_2D,ResourceManager->TexList[CONST_WALL_BONUS_TEXTURE]); + // + // Renderer->DrawRect(Vector2f(-256.f, -16.f)+wallDownPos, Vector2f(256.f, 16.f)+wallDownPos); + // } + // + // + // if (!pause) + // { + // RenderUniform1f("Transparency", 1.f); + // glBindTexture(GL_TEXTURE_2D,ResourceManager->TexList[CONST_BACK_BTN_TEXTURE]); + // const Vector2f BackBtnPos(240.f, 320.f - 32.f - 20.f); + // Renderer->DrawRect(Vector2f(-128.f, -32.f)+BackBtnPos, Vector2f(128.f, 32.f)+BackBtnPos); + // } - if (BonusFloorTimer>0.f) { - - const Vector2f wallDownPos(240.f, BonusFloorPosY); - - glBindTexture(GL_TEXTURE_2D,ResourceManager->TexList[CONST_WALL_BONUS_TEXTURE]); - - Renderer->DrawRect(Vector2f(-256.f, -16.f)+wallDownPos, Vector2f(256.f, 16.f)+wallDownPos); - } - - - if (!pause) - { - RenderUniform1f("Transparency", 1.f); - glBindTexture(GL_TEXTURE_2D,ResourceManager->TexList[CONST_BACK_BTN_TEXTURE]); - const Vector2f BackBtnPos(240.f, 320.f - 32.f - 20.f); - Renderer->DrawRect(Vector2f(-128.f, -32.f)+BackBtnPos, Vector2f(128.f, 32.f)+BackBtnPos); - } - + TRenderParamsSetter params(pair.first); + Renderer->DrawTriangleList(pair.second); + } + + CheckGlError(); + if (pause && !renderBufferReady) { @@ -905,29 +951,37 @@ void TGameLevel::DrawBallInstancingList() void TGameLevel::DrawBuffer() { *SE::Console << "TGameLevel::DrawBuffer"; - Renderer->PushShader("FrameShader"); - float brightness; - if (CONST_LEVELSTATE_GO_TO_PAUSE) - { - brightness = 0.3f + 0.7f * StateTimer / CONST_PAUSE_APPEAR_TIME; - } - else - { - brightness = 0.3f; - } - - RenderUniform1f("Brightness", brightness); - glBindTexture(GL_TEXTURE_2D,ResourceManager->FrameManager.GetFrameTexture("LevelBuffer")); - - //Matrix switched to identity - //Vector2f RectPos = Vector2f(-1, -1); - //Vector2f RectSize = Vector2f(2, 2); - Vector2f RectPos = Vector2f(240.f, 160.f); - Vector2f RectSize = Vector2f(240.f, 160.f); + // Renderer->PushShader("FrameShader"); + // float brightness; + // if (CONST_LEVELSTATE_GO_TO_PAUSE) + // { + // brightness = 0.3f + 0.7f * StateTimer / CONST_PAUSE_APPEAR_TIME; + // } + // else + // { + // brightness = 0.3f; + // } + // + // RenderUniform1f("Brightness", brightness); + // glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("LevelBuffer")); + // + ////Matrix switched to identity + // //Vector2f RectPos = Vector2f(-1, -1); + // //Vector2f RectSize = Vector2f(2, 2); + //Vector2f RectPos = Vector2f(240.f, 160.f); + //Vector2f RectSize = Vector2f(240.f, 160.f); - Renderer->DrawRect(RectPos-RectSize, RectPos+RectSize); + // Renderer->DrawRect(RectPos-RectSize, RectPos+RectSize); - Renderer->PopShader(); + // Renderer->PopShader(); + + + + { + TRenderParamsSetter params(rect.first); + glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("LevelBuffer")); + Renderer->DrawTriangleList(rect.second); + } CheckGlError(); } diff --git a/game/main_code.cpp b/game/main_code.cpp index 62be883..33f4cf0 100644 --- a/game/main_code.cpp +++ b/game/main_code.cpp @@ -80,6 +80,8 @@ void TMyApplication::InnerInit() ResourceManager->ShaderManager.AddShader("DefaultShader", "shader1vertex.txt", "shader1fragment.txt"); ResourceManager->ShaderManager.AddShader("FrameShader", "frameshader_vertex.txt", "frameshader_fragment.txt"); ResourceManager->ShaderManager.AddShader("BrickShader", "brickshader_vertex.txt", "brickshader_fragment.txt"); + ResourceManager->ShaderManager.AddShader("ColorShader", "color_vertex.txt", "color_fragment.txt"); + ResourceManager->ShaderManager.AddShader("SSAA_4X", "SSAA_4X.vertex", "SSAA_4X.frag"); Renderer->PushShader("DefaultShader"); ResourceManager->TexList.AddTexture(CONST_LOADING_BACKGROUND_BLACK + ".png", CONST_LOADING_BACKGROUND_BLACK); @@ -88,7 +90,7 @@ void TMyApplication::InnerInit() ResourceManager->TexList.AddTexture("console_bkg.bmp"); - ResourceManager->FrameManager.AddFrameRenderBuffer("LevelBuffer", 512, 512); + ResourceManager->FrameManager.AddFrameRenderBuffer("LevelBuffer", 480 * 2, 320 * 2); OnDrawSignal.connect(boost::bind(&TGameLoading::Draw, boost::ref(GameLoading))); Inited = true; @@ -96,7 +98,25 @@ void TMyApplication::InnerInit() Renderer->SetOrthoProjection(); Renderer->SetFullScreenViewport(); - + + Renderer->PushShader("SSAA_4X"); + + const float cos30 = sqrt(3) / 2; + const float sin30 = 0.5f; + const float sampleRadiusX = 0.75 / 480; + const float sampleRadiusY = 0.75 / 320; + + Vector2f offset; + offset = Vector2f(cos30 * sampleRadiusX, sin30 * sampleRadiusY); + RenderUniform2fv("samplesOffset[0]", &offset[0]); + offset = Vector2f(-sin30 * sampleRadiusX, cos30 * sampleRadiusY); + RenderUniform2fv("samplesOffset[1]", &offset[0]); + offset = Vector2f(-cos30 * sampleRadiusX, -sin30 * sampleRadiusY); + RenderUniform2fv("samplesOffset[2]", &offset[0]); + offset = Vector2f(sin30 * sampleRadiusX, -cos30 * sampleRadiusY); + RenderUniform2fv("samplesOffset[3]", &offset[0]); + + Renderer->PopShader(); } void TMyApplication::InnerDeinit()