Finishing antialiasing

This commit is contained in:
Vladislav Khorev 2018-04-11 17:16:37 +03:00
parent b2a0901369
commit f70741f6e6
5 changed files with 52 additions and 184 deletions

View File

@ -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()
{

View File

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

View File

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

View File

@ -15,98 +15,9 @@ TMyApplication* Application;
static const float BUFFER_DISTANCE = 50.f;
//#define USE_SHADER_X16
#ifdef USE_SHADER_X16
std::vector<TRenderPair> linesToRenderBoxesX16(const std::vector<std::pair<Vector2f, Vector2f>>& lineArr)
TRenderPair linesToRenderBoxes(const std::vector<std::pair<Vector2f, Vector2f>>& lineArr)
{
std::vector<TRenderPair> 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<std::string>(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<std::string>(i % 16)] = Vector4f(a, b, c, 0);
}
for (auto& pair : lineRenderPairArr)
{
pair.second.RefreshBuffer();
}
return lineRenderPairArr;
}
#else
std::vector<TRenderPair> linesToRenderBoxes(const std::vector<std::pair<Vector2f, Vector2f>>& lineArr)
{
std::vector<TRenderPair> lineRenderPairArr;
lineRenderPairArr.resize(lineArr.size());
TRenderPair pair;
for (size_t i = 0; i < lineArr.size(); i++)
{
@ -114,9 +25,6 @@ std::vector<TRenderPair> linesToRenderBoxes(const std::vector<std::pair<Vector2f
Vector2f startPoint = lineArr[i].first;
Vector2f endPoint = lineArr[i].second;
auto& pair = lineRenderPairArr[i];
Vector2f renderAreaStart{ std::min<float>(startPoint(0), endPoint(0)) - BUFFER_DISTANCE, std::min<float>(startPoint(1), endPoint(1)) - BUFFER_DISTANCE };
Vector2f renderAreEnd{ std::max<float>(startPoint(0), endPoint(0)) + BUFFER_DISTANCE, std::max<float>(startPoint(1), endPoint(1)) + BUFFER_DISTANCE };
@ -129,21 +37,6 @@ std::vector<TRenderPair> linesToRenderBoxes(const std::vector<std::pair<Vector2f
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(renderAreEnd(0), renderAreaStart(1), 0));
/*
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0), startPoint(1), 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0), endPoint(1), 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0), endPoint(1), 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0), startPoint(1), 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0), endPoint(1), 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0), startPoint(1), 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));
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));
@ -162,22 +55,30 @@ std::vector<TRenderPair> linesToRenderBoxes(const std::vector<std::pair<Vector2f
float b = lineNormVec(1);
float c = -(lineNormVec(0)*startPoint(0) + lineNormVec(1)*startPoint(1));
pair.first.Vec4Map["lineParams"] = Vector4f(a, b, c, 0);
pair.first.Vec4Map["startPointEndPoint"] = Vector4f(startPoint(0), startPoint(1), endPoint(0), endPoint(1));
pair.second.Data.Vec3CoordArr["vLineParams"].push_back(Vector3f(a, b, c));
pair.second.Data.Vec3CoordArr["vLineParams"].push_back(Vector3f(a, b, c));
pair.second.Data.Vec3CoordArr["vLineParams"].push_back(Vector3f(a, b, c));
pair.second.Data.Vec3CoordArr["vLineParams"].push_back(Vector3f(a, b, c));
pair.second.Data.Vec3CoordArr["vLineParams"].push_back(Vector3f(a, b, c));
pair.second.Data.Vec3CoordArr["vLineParams"].push_back(Vector3f(a, b, c));
pair.second.Data.Vec4CoordArr["vStartPointEndPoint"].push_back(Vector4f(startPoint(0), startPoint(1), endPoint(0), endPoint(1)));
pair.second.Data.Vec4CoordArr["vStartPointEndPoint"].push_back(Vector4f(startPoint(0), startPoint(1), endPoint(0), endPoint(1)));
pair.second.Data.Vec4CoordArr["vStartPointEndPoint"].push_back(Vector4f(startPoint(0), startPoint(1), endPoint(0), endPoint(1)));
pair.second.Data.Vec4CoordArr["vStartPointEndPoint"].push_back(Vector4f(startPoint(0), startPoint(1), endPoint(0), endPoint(1)));
pair.second.Data.Vec4CoordArr["vStartPointEndPoint"].push_back(Vector4f(startPoint(0), startPoint(1), endPoint(0), endPoint(1)));
pair.second.Data.Vec4CoordArr["vStartPointEndPoint"].push_back(Vector4f(startPoint(0), startPoint(1), endPoint(0), endPoint(1)));
}
pair.second.RefreshBuffer();
return pair;
}
return lineRenderPairArr;
}
#endif
void TMyApplication::InnerInit()
{
@ -201,8 +102,8 @@ void TMyApplication::InnerInit()
}
srand (static_cast<size_t>(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<std::pair<Vector2f, Vector2f>> 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();

View File

@ -33,7 +33,7 @@ protected:
bool Inited;
public:
std::vector<TRenderPair> lineRenderPairArr;
TRenderPair lineRenderPair;
bool Loaded;