From f70741f6e6c0831dc02cafd8b75f45cdddb3494a Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Wed, 11 Apr 2018 17:16:37 +0300 Subject: [PATCH] Finishing antialiasing --- assets/ms_fragment.txt | 8 +- assets/ms_fragment_x16.txt | 47 ----------- assets/ms_vertex.txt | 13 ++- game/main_code.cpp | 166 ++++++++----------------------------- game/main_code.h | 2 +- 5 files changed, 52 insertions(+), 184 deletions(-) delete mode 100755 assets/ms_fragment_x16.txt diff --git a/assets/ms_fragment.txt b/assets/ms_fragment.txt index 1ff2249..0dbed3b 100755 --- a/assets/ms_fragment.txt +++ b/assets/ms_fragment.txt @@ -2,8 +2,12 @@ precision mediump float; varying vec4 color; varying vec3 position; -uniform vec4 lineParams; -uniform vec4 startPointEndPoint; +//uniform vec4 lineParams; +//uniform vec4 startPointEndPoint; + +varying vec3 lineParams; +varying vec4 startPointEndPoint; + void main() { diff --git a/assets/ms_fragment_x16.txt b/assets/ms_fragment_x16.txt deleted file mode 100755 index e7d3efe..0000000 --- a/assets/ms_fragment_x16.txt +++ /dev/null @@ -1,47 +0,0 @@ -precision mediump float; -varying vec4 color; -varying vec3 position; - -uniform vec4 lineParams0; -uniform vec4 lineParams1; -uniform vec4 lineParams2; -uniform vec4 lineParams3; -uniform vec4 lineParams4; -uniform vec4 lineParams5; -uniform vec4 lineParams6; -uniform vec4 lineParams7; -uniform vec4 lineParams8; -uniform vec4 lineParams9; -uniform vec4 lineParams10; -uniform vec4 lineParams11; -uniform vec4 lineParams12; -uniform vec4 lineParams13; -uniform vec4 lineParams14; -uniform vec4 lineParams15; - -void main() -{ - float distance = abs(lineParams0.x*position.x + lineParams0.y*position.y + lineParams0.z); - - - distance = min(distance, abs(lineParams1.x*position.x + lineParams1.y*position.y + lineParams1.z)); - - distance = min(distance, abs(lineParams2.x*position.x + lineParams2.y*position.y + lineParams2.z)); - distance = min(distance, abs(lineParams3.x*position.x + lineParams3.y*position.y + lineParams3.z)); - distance = min(distance, abs(lineParams4.x*position.x + lineParams4.y*position.y + lineParams4.z)); - distance = min(distance, abs(lineParams5.x*position.x + lineParams5.y*position.y + lineParams5.z)); - distance = min(distance, abs(lineParams6.x*position.x + lineParams6.y*position.y + lineParams6.z)); - distance = min(distance, abs(lineParams7.x*position.x + lineParams7.y*position.y + lineParams7.z)); - distance = min(distance, abs(lineParams8.x*position.x + lineParams8.y*position.y + lineParams8.z)); - distance = min(distance, abs(lineParams9.x*position.x + lineParams9.y*position.y + lineParams9.z)); - distance = min(distance, abs(lineParams10.x*position.x + lineParams10.y*position.y + lineParams10.z)); - distance = min(distance, abs(lineParams11.x*position.x + lineParams11.y*position.y + lineParams11.z)); - distance = min(distance, abs(lineParams12.x*position.x + lineParams12.y*position.y + lineParams12.z)); - distance = min(distance, abs(lineParams13.x*position.x + lineParams13.y*position.y + lineParams13.z)); - distance = min(distance, abs(lineParams14.x*position.x + lineParams14.y*position.y + lineParams14.z)); - distance = min(distance, abs(lineParams15.x*position.x + lineParams15.y*position.y + lineParams15.z)); - - - //gl_FragColor = vec4(color.rgb, color.a*clamp(1.2 / (distance + 1.0) - 0.5, 0.0, 1.0)); - gl_FragColor = vec4(color.rgb*clamp(1.2 / (distance + 1.0) - 0.5, 0.0, 1.0), 1.0); -} \ No newline at end of file diff --git a/assets/ms_vertex.txt b/assets/ms_vertex.txt index f0da8c3..188278f 100755 --- a/assets/ms_vertex.txt +++ b/assets/ms_vertex.txt @@ -1,13 +1,22 @@ attribute vec3 vPosition; attribute vec4 vColor; -varying vec4 color; +attribute vec3 vLineParams; +attribute vec4 vStartPointEndPoint; + varying vec3 position; +varying vec4 color; +varying vec3 lineParams; +varying vec4 startPointEndPoint; + uniform mat4 ProjectionMatrix; void main() { gl_Position = ProjectionMatrix * vec4(vPosition.xyz, 1.0); - color = vColor; + position = vPosition; + color = vColor; + lineParams = vLineParams; + startPointEndPoint = vStartPointEndPoint; } \ No newline at end of file diff --git a/game/main_code.cpp b/game/main_code.cpp index fe3242a..c0f307f 100644 --- a/game/main_code.cpp +++ b/game/main_code.cpp @@ -15,98 +15,9 @@ TMyApplication* Application; static const float BUFFER_DISTANCE = 50.f; -//#define USE_SHADER_X16 - - -#ifdef USE_SHADER_X16 - - -std::vector linesToRenderBoxesX16(const std::vector>& lineArr) +TRenderPair linesToRenderBoxes(const std::vector>& lineArr) { - - - std::vector lineRenderPairArr; - - size_t boxCount = lineArr.size() / 16; - - if (lineArr.size() % 16 > 0) - { - boxCount++; - } - - lineRenderPairArr.resize(boxCount); - - //Set some initial values: - for (size_t i = 0; i < lineRenderPairArr.size(); i++) - { - - for (size_t j = 0; j < 16; j++) - { - lineRenderPairArr[i].first.Vec4Map["lineParams" + boost::lexical_cast(j)] = Vector4f(0, 0, 9999, 0); - } - } - - for (size_t i = 0; i < lineArr.size(); i++) - { - - size_t boxIndex = i / 16; - - Vector2f startPoint = lineArr[i].first; - Vector2f endPoint = lineArr[i].second; - - - auto& pair = lineRenderPairArr[boxIndex]; - - - - pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0)- BUFFER_DISTANCE, startPoint(1) - BUFFER_DISTANCE, 0)); - pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0) - BUFFER_DISTANCE, endPoint(1)+ BUFFER_DISTANCE, 0)); - pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0) + BUFFER_DISTANCE, endPoint(1) + BUFFER_DISTANCE, 0)); - pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0) - BUFFER_DISTANCE, startPoint(1) - BUFFER_DISTANCE, 0)); - pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0) + BUFFER_DISTANCE, endPoint(1) + BUFFER_DISTANCE, 0)); - pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0) + BUFFER_DISTANCE, startPoint(1) - BUFFER_DISTANCE, 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(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(1, 0, 0, 1)); - pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); - - Vector2f lineVec = endPoint - startPoint; - Vector2f lineNormVec = { -lineVec(1), lineVec(0) }; - - lineNormVec.normalize(); - - //float distance = abs(normVec.x*(position.x - startPoint.x) + normVec.y*(position.y - startPoint.y)); - //distance = a*x + b*y + c, here we calculate a, b,c and pass them to shader - float a = lineNormVec(0); - float b = lineNormVec(1); - float c = -(lineNormVec(0)*startPoint(0) + lineNormVec(1)*startPoint(1)); - - - pair.first.Vec4Map["lineParams" + boost::lexical_cast(i % 16)] = Vector4f(a, b, c, 0); - - } - - for (auto& pair : lineRenderPairArr) - { - pair.second.RefreshBuffer(); - } - - - return lineRenderPairArr; -} - - -#else - - -std::vector linesToRenderBoxes(const std::vector>& lineArr) -{ - std::vector lineRenderPairArr; - - lineRenderPairArr.resize(lineArr.size()); + TRenderPair pair; for (size_t i = 0; i < lineArr.size(); i++) { @@ -114,9 +25,6 @@ std::vector linesToRenderBoxes(const std::vector(startPoint(0), endPoint(0)) - BUFFER_DISTANCE, std::min(startPoint(1), endPoint(1)) - BUFFER_DISTANCE }; Vector2f renderAreEnd{ std::max(startPoint(0), endPoint(0)) + BUFFER_DISTANCE, std::max(startPoint(1), endPoint(1)) + BUFFER_DISTANCE }; @@ -129,21 +37,6 @@ std::vector linesToRenderBoxes(const std::vector linesToRenderBoxes(const std::vector(time(NULL))); - ResourceManager->ShaderManager.AddShader("DefaultShader", "shader1vertex.txt", "shader1fragment.txt"); - ResourceManager->ShaderManager.AddShader("ColorShader", "color_vertex.txt", "color_fragment.txt"); + //ResourceManager->ShaderManager.AddShader("DefaultShader", "shader1vertex.txt", "shader1fragment.txt"); + //ResourceManager->ShaderManager.AddShader("ColorShader", "color_vertex.txt", "color_fragment.txt"); #ifdef USE_SHADER_X16 ResourceManager->ShaderManager.AddShader("AntialiasingShader", "ms_vertex.txt", "ms_fragment_x16.txt"); @@ -213,11 +114,14 @@ void TMyApplication::InnerInit() - Renderer->PushShader("DefaultShader"); + //Renderer->PushShader("DefaultShader"); ResourceManager->TexList.AddTexture("console_bkg.bmp"); + Renderer->PushShader("AntialiasingShader"); + + std::vector> lineArr; for (int i = 0; i < 10; i++) @@ -228,14 +132,8 @@ void TMyApplication::InnerInit() }; -#ifdef USE_SHADER_X16 - lineRenderPairArr = linesToRenderBoxesX16(lineArr); -#else - lineRenderPairArr = linesToRenderBoxes(lineArr); - -#endif - + lineRenderPair = linesToRenderBoxes(lineArr); Renderer->SetOrthoProjection(); @@ -296,11 +194,15 @@ void TMyApplication::InnerDraw() glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); CheckGlError(""); - for (auto& pair : lineRenderPairArr) - { - TRenderParamsSetter params(pair.first); - Renderer->DrawTriangleList(pair.second); - } + + EnableVertexAttribArray("vLineParams"); + EnableVertexAttribArray("vStartPointEndPoint"); + + TRenderParamsSetter params(lineRenderPair.first); + Renderer->DrawTriangleList(lineRenderPair.second); + + DisableVertexAttribArray("vStartPointEndPoint"); + DisableVertexAttribArray("vLineParams"); Renderer->PopShader(); diff --git a/game/main_code.h b/game/main_code.h index 4608369..542643a 100644 --- a/game/main_code.h +++ b/game/main_code.h @@ -33,7 +33,7 @@ protected: bool Inited; public: - std::vector lineRenderPairArr; + TRenderPair lineRenderPair; bool Loaded;