Round viewsphere highlight.

This commit is contained in:
Leonardo Zide 2019-01-19 20:04:08 -08:00
parent e190900778
commit 6077400b3c
5 changed files with 77 additions and 46 deletions

View file

@ -51,6 +51,7 @@ lcContext::lcContext()
mHighlightParams[0] = lcVector4(0.0f, 0.0f, 0.0f, 0.0f); mHighlightParams[0] = lcVector4(0.0f, 0.0f, 0.0f, 0.0f);
mHighlightParams[1] = lcVector4(0.0f, 0.0f, 0.0f, 0.0f); mHighlightParams[1] = lcVector4(0.0f, 0.0f, 0.0f, 0.0f);
mHighlightParams[2] = lcVector4(0.0f, 0.0f, 0.0f, 0.0f); mHighlightParams[2] = lcVector4(0.0f, 0.0f, 0.0f, 0.0f);
mHighlightParams[3] = lcVector4(0.0f, 0.0f, 0.0f, 0.0f);
mColorDirty = false; mColorDirty = false;
mWorldMatrixDirty = false; mWorldMatrixDirty = false;
mViewMatrixDirty = false; mViewMatrixDirty = false;
@ -1178,7 +1179,7 @@ void lcContext::FlushState()
if (mHighlightParamsDirty && Program.HighlightParamsLocation != -1) if (mHighlightParamsDirty && Program.HighlightParamsLocation != -1)
{ {
glUniform4fv(Program.HighlightParamsLocation, 2, mHighlightParams[0]); glUniform4fv(Program.HighlightParamsLocation, 4, mHighlightParams[0]);
mHighlightParamsDirty = false; mHighlightParamsDirty = false;
} }
} }

View file

@ -153,10 +153,12 @@ public:
mColorDirty = true; mColorDirty = true;
} }
void SetHighlightParams(const lcVector4& HighlightMin, const lcVector4& HighlightMax) void SetHighlightParams(const lcVector4& HighlightPosition, const lcVector4& TextColor, const lcVector4& BackgroundColor, const lcVector4& HighlightColor)
{ {
mHighlightParams[0] = HighlightMin; mHighlightParams[0] = HighlightPosition;
mHighlightParams[1] = HighlightMax; mHighlightParams[1] = TextColor;
mHighlightParams[2] = BackgroundColor;
mHighlightParams[3] = HighlightColor;
mHighlightParamsDirty = true; mHighlightParamsDirty = true;
} }
@ -227,7 +229,7 @@ protected:
lcMatrix44 mViewMatrix; lcMatrix44 mViewMatrix;
lcMatrix44 mProjectionMatrix; lcMatrix44 mProjectionMatrix;
lcMatrix44 mViewProjectionMatrix; lcMatrix44 mViewProjectionMatrix;
lcVector4 mHighlightParams[3]; lcVector4 mHighlightParams[4];
bool mColorDirty; bool mColorDirty;
bool mWorldMatrixDirty; bool mWorldMatrixDirty;
bool mViewMatrixDirty; bool mViewMatrixDirty;

View file

@ -11,6 +11,7 @@ lcTexture* lcViewSphere::mTexture;
lcVertexBuffer lcViewSphere::mVertexBuffer; lcVertexBuffer lcViewSphere::mVertexBuffer;
lcIndexBuffer lcViewSphere::mIndexBuffer; lcIndexBuffer lcViewSphere::mIndexBuffer;
const float lcViewSphere::mRadius = 1.0f; const float lcViewSphere::mRadius = 1.0f;
const float lcViewSphere::mHighlightRadius = 0.35f;
const int lcViewSphere::mSubdivisions = 7; const int lcViewSphere::mSubdivisions = 7;
lcViewSphere::lcViewSphere(View* View) lcViewSphere::lcViewSphere(View* View)
@ -179,39 +180,28 @@ void lcViewSphere::Draw()
Context->SetVertexFormatPosition(3); Context->SetVertexFormatPosition(3);
Context->SetIndexBuffer(mIndexBuffer); Context->SetIndexBuffer(mIndexBuffer);
Context->SetHighlightParams(lcVector4(10.0f, 10.0f, 10.0f, 0.0f), lcVector4(-10.0f, -10.0f, -10.0f, 0.0f)); lcVector4 HighlightPosition(0.0f, 0.0f, 0.0f, 0.0f);
Context->DrawIndexedPrimitives(GL_TRIANGLES, mSubdivisions * mSubdivisions * 6 * 6, GL_UNSIGNED_SHORT, 0);
if (mIntersectionFlags.any()) if (mIntersectionFlags.any())
{ {
lcVector4 HighlightMin(-10.0f, -10.0f, -10.0f, 0.0f), HighlightMax(10.0f, 10.0f, 10.0f, 0.0f);
for (int AxisIdx = 0; AxisIdx < 3; AxisIdx++) for (int AxisIdx = 0; AxisIdx < 3; AxisIdx++)
{ {
if (mIntersectionFlags.test(2 * AxisIdx)) if (mIntersectionFlags.test(2 * AxisIdx))
{ HighlightPosition[AxisIdx] = 1.0f;
HighlightMin[AxisIdx] = 0.5f;
HighlightMax[AxisIdx] = 10.0f;
}
else if (mIntersectionFlags.test(2 * AxisIdx + 1)) else if (mIntersectionFlags.test(2 * AxisIdx + 1))
{ HighlightPosition[AxisIdx] = -1.0f;
HighlightMin[AxisIdx] = -10.0f;
HighlightMax[AxisIdx] = -0.5f;
}
} }
Context->SetHighlightParams(HighlightMin, HighlightMax); HighlightPosition = lcVector4(lcNormalize(lcVector3(HighlightPosition)), mHighlightRadius);
for (int FlagIdx = 0; FlagIdx < 6; FlagIdx++)
{
if (mIntersectionFlags.test(FlagIdx))
{
int FaceBase = FlagIdx * (mSubdivisions) * (mSubdivisions) * 6;
Context->DrawIndexedPrimitives(GL_TRIANGLES, mSubdivisions * mSubdivisions * 6, GL_UNSIGNED_SHORT, FaceBase * sizeof(GLushort));
}
}
} }
const lcVector4 TextColor(0.0, 0.0, 0.0, 1.0);
const lcVector4 BackgroundColor(1.0, 1.0, 1.0, 1.0);
const lcVector4 HighlightColor(1.0, 0.0, 0.0, 1.0);
Context->SetHighlightParams(HighlightPosition, TextColor, BackgroundColor, HighlightColor);
Context->DrawIndexedPrimitives(GL_TRIANGLES, mSubdivisions * mSubdivisions * 6 * 6, GL_UNSIGNED_SHORT, 0);
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
glDepthFunc(GL_LEQUAL); glDepthFunc(GL_LEQUAL);
@ -326,15 +316,59 @@ std::bitset<6> lcViewSphere::GetIntersectionFlags(lcVector3& Intersection) const
{ {
Intersection = (StartEnd[0] + (StartEnd[1] - StartEnd[0]) * Distance) / mRadius; Intersection = (StartEnd[0] + (StartEnd[1] - StartEnd[0]) * Distance) / mRadius;
const float Side = 0.5f; auto CheckIntersection = [&]()
for (int AxisIdx = 0; AxisIdx < 3; AxisIdx++)
{ {
if (mIntersection[AxisIdx] > Side) for (int Axis1Idx = 0; Axis1Idx < 6; Axis1Idx++)
IntersectionFlags.set(2 * AxisIdx); {
else if (mIntersection[AxisIdx] < -Side) lcVector3 Point(0.0f, 0.0f, 0.0f);
IntersectionFlags.set(2 * AxisIdx + 1);
} Point[Axis1Idx / 2] = Axis1Idx % 2 ? -1.0f : 1.0f;
if (lcLengthSquared(Point - Intersection) < mHighlightRadius * mHighlightRadius)
{
IntersectionFlags.set(Axis1Idx);
return;
}
for (int Axis2Idx = 0; Axis2Idx < 6; Axis2Idx++)
{
if (Axis1Idx / 2 == Axis2Idx / 2)
continue;
lcVector3 Point(0.0f, 0.0f, 0.0f);
Point[Axis1Idx / 2] = Axis1Idx % 2 ? -0.70710678118f : 0.70710678118f;
Point[Axis2Idx / 2] = Axis2Idx % 2 ? -0.70710678118f : 0.70710678118f;
if (lcLengthSquared(Point - Intersection) < mHighlightRadius * mHighlightRadius)
{
IntersectionFlags.set(Axis1Idx);
IntersectionFlags.set(Axis2Idx);
return;
}
for (int Axis3Idx = 0; Axis3Idx < 6; Axis3Idx++)
{
if (Axis1Idx / 2 == Axis3Idx / 2 || Axis2Idx / 2 == Axis3Idx / 2)
continue;
lcVector3 Point(0.0f, 0.0f, 0.0f);
Point[Axis1Idx / 2] = Axis1Idx % 2 ? -0.57735026919f : 0.57735026919f;
Point[Axis2Idx / 2] = Axis2Idx % 2 ? -0.57735026919f : 0.57735026919f;
Point[Axis3Idx / 2] = Axis3Idx % 2 ? -0.57735026919f : 0.57735026919f;
if (lcLengthSquared(Point - Intersection) < mHighlightRadius * mHighlightRadius)
{
IntersectionFlags.set(Axis1Idx);
IntersectionFlags.set(Axis2Idx);
IntersectionFlags.set(Axis3Idx);
return;
}
}
}
}
};
CheckIntersection();
} }
return IntersectionFlags; return IntersectionFlags;

View file

@ -36,5 +36,6 @@ protected:
static lcVertexBuffer mVertexBuffer; static lcVertexBuffer mVertexBuffer;
static lcIndexBuffer mIndexBuffer; static lcIndexBuffer mIndexBuffer;
static const float mRadius; static const float mRadius;
static const float mHighlightRadius;
static const int mSubdivisions; static const int mSubdivisions;
}; };

View file

@ -1,20 +1,13 @@
LC_PIXEL_INPUT vec3 PixelNormal; LC_PIXEL_INPUT vec3 PixelNormal;
LC_PIXEL_OUTPUT LC_PIXEL_OUTPUT
uniform mediump vec4 HighlightParams[2]; uniform mediump vec4 HighlightParams[4];
uniform samplerCube Texture; uniform samplerCube Texture;
const mediump vec4 TextColor = vec4(0.0, 0.0, 0.0, 1.0);
const mediump vec4 BackgroundColor = vec4(1.0, 1.0, 1.0, 1.0);
const mediump vec4 HighlightColor = vec4(1.0, 0.0, 0.0, 1.0);
void main() void main()
{ {
LC_SHADER_PRECISION float TexelAlpha = textureCube(Texture, PixelNormal).a; LC_SHADER_PRECISION float TexelAlpha = textureCube(Texture, PixelNormal).a;
LC_SHADER_PRECISION float Distance = length(vec3(HighlightParams[0]) - PixelNormal);
if (PixelNormal.x > HighlightParams[0].x && PixelNormal.y > HighlightParams[0].y && PixelNormal.z > HighlightParams[0].z && LC_SHADER_PRECISION float Highlight = step(Distance, HighlightParams[0].w);
PixelNormal.x < HighlightParams[1].x && PixelNormal.y < HighlightParams[1].y && PixelNormal.z < HighlightParams[1].z) gl_FragColor = mix(mix(HighlightParams[2], HighlightParams[3], Highlight), HighlightParams[1], TexelAlpha);
gl_FragColor = mix(HighlightColor, TextColor, TexelAlpha);
else
gl_FragColor = mix(BackgroundColor, TextColor, TexelAlpha);
} }