Skip to content

Commit

Permalink
improve associations and lines rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
mi-sts committed May 19, 2024
1 parent 70e3e4f commit f18687f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class LinesLayerRenderer(
companion object {
private const val UseCommonColorUniformName = "uUseCommonColor"

private const val DefaultLocalVerticesPositionsDivider = 600f
private const val DefaultLocalVerticesPositionsDivider = 400f
private const val HighlightingWidthMultiplier = 2.5f
private const val NonOpaqueLineWidthFactor = 1f
private const val NonOpaqueMaxLineWidth = 4f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package solve.rendering.engine.core.renderers

import org.joml.Vector2f
import org.joml.Vector3f
import org.joml.Vector4f
import solve.constants.ShadersPointAssociationFragmentPath
import solve.constants.ShadersPointAssociationVertexPath
import solve.rendering.engine.Window
Expand All @@ -16,6 +17,7 @@ import solve.rendering.engine.utils.times
import solve.rendering.engine.utils.toFloatVector
import solve.rendering.engine.utils.toVector2i
import solve.scene.view.association.AssociationManager
import kotlin.math.min

class PointAssociationsRenderer(
window: Window,
Expand All @@ -39,7 +41,7 @@ class PointAssociationsRenderer(
override fun createNewBatch(zIndex: Int): RenderBatch {
val shaderAttributesTypes = listOf(
ShaderAttributeType.FLOAT2,
ShaderAttributeType.FLOAT3
ShaderAttributeType.FLOAT4
)

return RenderBatch(
Expand All @@ -59,8 +61,6 @@ class PointAssociationsRenderer(

associationConnections.forEach { associationConnection ->
associationConnection.associationLines.forEach { associationLine ->
val batch = getAvailableBatch(null, 0)

val lineStartShaderPosition = getFramePixelShaderPosition(
associationLine.firstKeypointFrameIndex,
associationLine.firstKeypoint.coordinate.toVector2i().toFloatVector()
Expand All @@ -72,40 +72,89 @@ class PointAssociationsRenderer(

val lineVector = lineFinishShaderPosition - lineStartShaderPosition
val normalVector = Vector2f(-lineVector.y, lineVector.x).normalize()
val linePoints = listOf(lineStartShaderPosition, lineFinishShaderPosition)

val firstKeypointColor =
associationLine.firstKeypoint.layerSettings.getColor(associationLine.firstKeypoint)
val secondKeypointColor =
associationLine.secondKeypoint.layerSettings.getColor(associationLine.secondKeypoint)
val lineColor = firstKeypointColor.interpolate(secondKeypointColor, 0.5)

val lineColorVector = Vector3f(
val lineColorVector = Vector4f(
lineColor.red.toFloat(),
lineColor.green.toFloat(),
lineColor.blue.toFloat()
lineColor.blue.toFloat(),
1f
)

linePoints.forEachIndexed { sideIndex, linePoint ->
val pointToVertexVector = Vector2f(normalVector) * AssociationLineWidth / window.camera.zoom /
val zeroAlphaLineColorVector = Vector4f(lineColorVector).also { it.w = 0f }

drawLineRectVertices(
lineStartShaderPosition,
lineFinishShaderPosition,
AssociationLineWidth,
lineColorVector,
lineColorVector,
normalVector
)
val nonOpaqueLineRectWidth = min(
AssociationLineWidth * NonOpaqueLineWidthFactor,
NonOpaqueMaxLineWidth
)
val nonOpaqueRectNormalOffset = Vector2f(normalVector) *
(AssociationLineWidth + nonOpaqueLineRectWidth) / 2f / window.camera.zoom /
DefaultLocalVerticesPositionsDivider

val upperVertexPosition = linePoint + pointToVertexVector
val bottomVertexPosition = linePoint - pointToVertexVector
val firstVertexPosition = if (sideIndex == 0) upperVertexPosition else bottomVertexPosition
val secondVertexPosition = if (sideIndex == 0) bottomVertexPosition else upperVertexPosition
batch.pushVector2f(firstVertexPosition)
batch.pushVector3f(lineColorVector)
batch.pushVector2f(secondVertexPosition)
batch.pushVector3f(lineColorVector)
}
drawLineRectVertices(
lineStartShaderPosition + nonOpaqueRectNormalOffset,
lineFinishShaderPosition + nonOpaqueRectNormalOffset,
nonOpaqueLineRectWidth,
lineColorVector,
zeroAlphaLineColorVector,
normalVector
)
drawLineRectVertices(
lineStartShaderPosition - nonOpaqueRectNormalOffset,
lineFinishShaderPosition - nonOpaqueRectNormalOffset,
nonOpaqueLineRectWidth,
zeroAlphaLineColorVector,
lineColorVector,
normalVector
)
}

}
}

private fun drawLineRectVertices(
rectCenterStartPoint: Vector2f,
rectCenterFinishPoint: Vector2f,
rectWidth: Float,
bottomVerticesColor: Vector4f,
upperVerticesColor: Vector4f,
normalVector: Vector2f
) {
val batch = getAvailableBatch(null, 0)
val centerLinePoints = listOf(rectCenterStartPoint, rectCenterFinishPoint)
centerLinePoints.forEachIndexed { sideIndex, linePoint ->
val pointToVertexVector = Vector2f(normalVector) * (rectWidth / 2f) / window.camera.zoom /
DefaultLocalVerticesPositionsDivider

val upperVertexPosition = linePoint + pointToVertexVector
val bottomVertexPosition = linePoint - pointToVertexVector
val firstVertexPosition = if (sideIndex == 0) upperVertexPosition else bottomVertexPosition
val secondVertexPosition = if (sideIndex == 0) bottomVertexPosition else upperVertexPosition
val firstVertexColor = if (sideIndex == 0) upperVerticesColor else bottomVerticesColor
val secondVertexColor = if (sideIndex == 0) bottomVerticesColor else upperVerticesColor
batch.pushVector2f(firstVertexPosition)
batch.pushVector4f(firstVertexColor)
batch.pushVector2f(secondVertexPosition)
batch.pushVector4f(secondVertexColor)
}
}

companion object {
private const val DefaultLocalVerticesPositionsDivider = 800f

private const val AssociationLineWidth = 3.0f
private const val AssociationLineWidth = 3f
private const val NonOpaqueLineWidthFactor = 1f
private const val NonOpaqueMaxLineWidth = 3f
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#version 330 core

in vec3 fColor;
in vec4 fColor;

out vec4 color;

void main()
{
color = vec4(fColor, 1.0);
color = fColor;
float alphaSquare = color.w * color.w;
color.x *= alphaSquare;
color.y *= alphaSquare;
color.z *= alphaSquare;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#version 330 core

layout (location=0) in vec2 aPos;
layout (location=1) in vec3 aColor;
layout (location=1) in vec4 aColor;

uniform mat4 uProjection;

out vec3 fColor;
out vec4 fColor;

void main()
{
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/engine/shaders/landmark/line/line.frag
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ out vec4 color;
void main()
{
vec4 premulitpliedColor = fColor;
float alpha = fColor.w;
premulitpliedColor.x *= alpha;
premulitpliedColor.y *= alpha;
premulitpliedColor.z *= alpha;
float alphaSquare = fColor.w * fColor.w;
premulitpliedColor.x *= alphaSquare;
premulitpliedColor.y *= alphaSquare;
premulitpliedColor.z *= alphaSquare;
color = premulitpliedColor;
}
7 changes: 6 additions & 1 deletion src/main/resources/engine/shaders/landmark/point/point.frag
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ void main()
if (localRadius < 0.6)
color = vec4(fColor, 1);
else
color = vec4(fColor, (1 - localRadius) * (1 - localRadius));
color = vec4(fColor, pow((1 - localRadius), 1.8));

float alpha = color.w;
color.x *= alpha;
color.y *= alpha;
color.z *= alpha;
}

0 comments on commit f18687f

Please sign in to comment.