Update shader

This commit is contained in:
Vladislav Khorev 2018-04-11 16:40:22 +03:00
parent 75b76fc236
commit b2a0901369
3 changed files with 75 additions and 13 deletions

View File

@ -3,14 +3,46 @@ varying vec4 color;
varying vec3 position; varying vec3 position;
uniform vec4 lineParams; uniform vec4 lineParams;
uniform vec4 startPointEndPoint;
void main() void main()
{ {
float a = lineParams.x; float a = lineParams.x;
float b = lineParams.y; float b = lineParams.y;
float c = lineParams.z; float c = lineParams.z;
float distance = abs(a*position.x + b*position.y + c);
gl_FragColor = vec4(color.rgb, color.a*clamp(1.2 / (distance + 1.0) - 0.5, 0.0, 1.0)); float Xa = startPointEndPoint.r;
float Ya = startPointEndPoint.g;
float Xb = startPointEndPoint.b;
float Yb = startPointEndPoint.a;
float distance;
float distanceToA = (Xa - position.x)*(Xb - Xa) + (Ya - position.y)*(Yb - Ya);
float distanceToB = (Xb - position.x)*(Xb - Xa) + (Yb - position.y)*(Yb - Ya);
if (distanceToA * distanceToB > 0)
{
float len = sqrt((Xb - Xa)*(Xb - Xa) + (Yb - Ya)*(Yb - Ya));
distance = max(abs(distanceToA/len), abs(distanceToB/len));
}
else
{
distance = abs(a*position.x + b*position.y + c);
}
float t = clamp(1.2 / (distance + 1.0) - 0.5, 0.0, 1.0);
gl_FragColor = vec4(color.rgb, color.a*t);
//gl_FragColor = vec4(color.rgb*clamp(1.2 / (distance + 1.0) - 0.5, 0.0, 1.0), 1.0);
} }

View File

@ -42,5 +42,6 @@ void main()
distance = min(distance, abs(lineParams15.x*position.x + lineParams15.y*position.y + lineParams15.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, 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

@ -11,7 +11,11 @@
TMyApplication* Application; TMyApplication* Application;
#define USE_SHADER_X16 //static const float BUFFER_DISTANCE = 30.f;
static const float BUFFER_DISTANCE = 50.f;
//#define USE_SHADER_X16
#ifdef USE_SHADER_X16 #ifdef USE_SHADER_X16
@ -19,6 +23,8 @@ TMyApplication* Application;
std::vector<TRenderPair> linesToRenderBoxesX16(const std::vector<std::pair<Vector2f, Vector2f>>& lineArr) std::vector<TRenderPair> linesToRenderBoxesX16(const std::vector<std::pair<Vector2f, Vector2f>>& lineArr)
{ {
std::vector<TRenderPair> lineRenderPairArr; std::vector<TRenderPair> lineRenderPairArr;
size_t boxCount = lineArr.size() / 16; size_t boxCount = lineArr.size() / 16;
@ -53,12 +59,12 @@ std::vector<TRenderPair> linesToRenderBoxesX16(const std::vector<std::pair<Vecto
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)- BUFFER_DISTANCE, startPoint(1) - BUFFER_DISTANCE, 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(startPoint(0) - BUFFER_DISTANCE, endPoint(1)+ BUFFER_DISTANCE, 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) + BUFFER_DISTANCE, endPoint(1) + BUFFER_DISTANCE, 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) - BUFFER_DISTANCE, startPoint(1) - BUFFER_DISTANCE, 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) + BUFFER_DISTANCE, endPoint(1) + BUFFER_DISTANCE, 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0), startPoint(1), 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));
@ -111,13 +117,32 @@ std::vector<TRenderPair> linesToRenderBoxes(const std::vector<std::pair<Vector2f
auto& pair = lineRenderPairArr[i]; 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 };
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(renderAreaStart(0), renderAreaStart(1), 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(renderAreaStart(0), renderAreEnd(1), 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(renderAreEnd(0), renderAreEnd(1), 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(renderAreaStart(0), renderAreaStart(1), 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(renderAreEnd(0), renderAreEnd(1), 0));
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), 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(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(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(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), endPoint(1), 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0), startPoint(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)); pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
@ -138,10 +163,14 @@ std::vector<TRenderPair> linesToRenderBoxes(const std::vector<std::pair<Vector2f
float c = -(lineNormVec(0)*startPoint(0) + lineNormVec(1)*startPoint(1)); float c = -(lineNormVec(0)*startPoint(0) + lineNormVec(1)*startPoint(1));
pair.first.Vec4Map["lineParams"] = Vector4f(a, b, c, 0); pair.first.Vec4Map["lineParams"] = Vector4f(a, b, c, 0);
pair.first.Vec4Map["startPointEndPoint"] = Vector4f(startPoint(0), startPoint(1), endPoint(0), endPoint(1));
pair.second.RefreshBuffer(); pair.second.RefreshBuffer();
} }
return lineRenderPairArr; return lineRenderPairArr;
} }
@ -193,9 +222,9 @@ void TMyApplication::InnerInit()
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
lineArr.push_back({ {10.f + i * 20.f, 10.f}, { 20.f + i * 20.f, 400.f } }); lineArr.push_back({ {50.f + i * 20.f, 50.f}, { 60.f + i * 20.f, 440.f } });
lineArr.push_back({ { 20.f + i * 20.f, 400.f },{ 30.f + i * 20.f, 10.f } }); lineArr.push_back({ { 60.f + i * 20.f, 440.f },{ 70.f + i * 20.f, 50.f } });
}; };