Skip to content

Commit

Permalink
Fix small visual issue edges would start from node center. Now they s…
Browse files Browse the repository at this point in the history
…tart from node border.
  • Loading branch information
eduramiba committed Jun 12, 2024
1 parent 4d1bd2c commit c5afb8b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ attribute vec2 targetPosition;
attribute float size;//It's the weight
attribute vec4 sourceColor;
attribute vec4 elementColor;
attribute float sourceSize;
attribute float targetSize;

varying vec4 fragColor;
Expand All @@ -42,9 +43,10 @@ void main() {
vec2 sideVector = vec2(-directionNormalized.y, directionNormalized.x) * thickness * 0.5;
vec2 arrowHeight = directionNormalized * thickness * ARROW_HEIGHT * 2.0;

vec2 lineEnd = direction - directionNormalized * targetSize;
vec2 lineStart = directionNormalized * sourceSize;
vec2 lineLength = (direction - lineStart) - directionNormalized * targetSize;

vec2 edgeVert = lineEnd * vert.x + sideVector * vert.y + arrowHeight * vert.z;
vec2 edgeVert = lineStart + lineLength * vert.x + sideVector * vert.y + arrowHeight * vert.z;

gl_Position = mvp * vec4(edgeVert + position, 0.0, 1.0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ attribute float size;//It's the weight
attribute vec4 sourceColor;
attribute vec4 targetColor;
attribute vec4 elementColor;
attribute float sourceSize;
attribute float targetSize;

varying vec4 fragColor;

Expand All @@ -40,8 +42,10 @@ void main() {

vec2 sideVector = vec2(-directionNormalized.y, directionNormalized.x) * thickness * 0.5;

vec2 lineEnd = direction;
vec2 edgeVert = lineEnd * vert.x + sideVector * vert.y;
vec2 lineStart = directionNormalized * sourceSize;
vec2 lineLength = (direction - lineStart) - directionNormalized * targetSize;

vec2 edgeVert = lineStart + lineLength * vert.x + sideVector * vert.y;

gl_Position = mvp * vec4(edgeVert + position, 0.0, 1.0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class EdgeLineModelDirected {
public static final int POSITION_TARGET_FLOATS = 2;
public static final int SOURCE_COLOR_FLOATS = 1;
public static final int COLOR_FLOATS = 1;
public static final int SOURCE_SIZE_FLOATS = 1;
public static final int TARGET_SIZE_FLOATS = 1;
public static final int SIZE_FLOATS = 1;

Expand All @@ -28,6 +29,7 @@ public class EdgeLineModelDirected {
+ POSITION_TARGET_FLOATS
+ SOURCE_COLOR_FLOATS
+ COLOR_FLOATS
+ SOURCE_SIZE_FLOATS
+ TARGET_SIZE_FLOATS
+ SIZE_FLOATS;

Expand Down Expand Up @@ -67,6 +69,7 @@ private void initProgram(GL2ES2 gl) {
.addAttribLocation(ATTRIB_NAME_SIZE, SHADER_SIZE_LOCATION)
.addAttribLocation(ATTRIB_NAME_SOURCE_COLOR, SHADER_SOURCE_COLOR_LOCATION)
.addAttribLocation(ATTRIB_NAME_COLOR, SHADER_COLOR_LOCATION)
.addAttribLocation(ATTRIB_NAME_SOURCE_SIZE, SHADER_SOURCE_SIZE_LOCATION)
.addAttribLocation(ATTRIB_NAME_TARGET_SIZE, SHADER_TARGET_SIZE_LOCATION)
.init(gl);

Expand All @@ -84,6 +87,7 @@ private void initProgram(GL2ES2 gl) {
.addAttribLocation(ATTRIB_NAME_SIZE, SHADER_SIZE_LOCATION)
.addAttribLocation(ATTRIB_NAME_SOURCE_COLOR, SHADER_SOURCE_COLOR_LOCATION)
.addAttribLocation(ATTRIB_NAME_COLOR, SHADER_COLOR_LOCATION)
.addAttribLocation(ATTRIB_NAME_SOURCE_SIZE, SHADER_SOURCE_SIZE_LOCATION)
.addAttribLocation(ATTRIB_NAME_TARGET_SIZE, SHADER_TARGET_SIZE_LOCATION)
.init(gl);

Expand All @@ -101,6 +105,7 @@ private void initProgram(GL2ES2 gl) {
.addAttribLocation(ATTRIB_NAME_SIZE, SHADER_SIZE_LOCATION)
.addAttribLocation(ATTRIB_NAME_SOURCE_COLOR, SHADER_SOURCE_COLOR_LOCATION)
.addAttribLocation(ATTRIB_NAME_COLOR, SHADER_COLOR_LOCATION)
.addAttribLocation(ATTRIB_NAME_SOURCE_SIZE, SHADER_SOURCE_SIZE_LOCATION)
.addAttribLocation(ATTRIB_NAME_TARGET_SIZE, SHADER_TARGET_SIZE_LOCATION)
.init(gl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ public class EdgeLineModelUndirected {
public static final int TARGET_COLOR_FLOATS = SOURCE_COLOR_FLOATS;
public static final int COLOR_FLOATS = 1;
public static final int SIZE_FLOATS = 1;
public static final int SOURCE_SIZE_FLOATS = 1;
public static final int TARGET_SIZE_FLOATS = 1;

public static final int TOTAL_ATTRIBUTES_FLOATS
= POSITION_SOURCE_FLOATS
+ POSITION_TARGET_LOCATION
+ SOURCE_COLOR_FLOATS
+ TARGET_COLOR_FLOATS
+ COLOR_FLOATS
+ SIZE_FLOATS;
+ SIZE_FLOATS
+ SOURCE_SIZE_FLOATS
+ TARGET_SIZE_FLOATS;

private static final int VERTEX_PER_TRIANGLE = 3;

Expand Down Expand Up @@ -69,6 +73,8 @@ private void initProgram(GL2ES2 gl) {
.addAttribLocation(ATTRIB_NAME_SOURCE_COLOR, SHADER_SOURCE_COLOR_LOCATION)
.addAttribLocation(ATTRIB_NAME_TARGET_COLOR, SHADER_TARGET_COLOR_LOCATION)
.addAttribLocation(ATTRIB_NAME_COLOR, SHADER_COLOR_LOCATION)
.addAttribLocation(ATTRIB_NAME_SOURCE_SIZE, SHADER_SOURCE_SIZE_LOCATION)
.addAttribLocation(ATTRIB_NAME_TARGET_SIZE, SHADER_TARGET_SIZE_LOCATION)
.init(gl);

programWithSelectionSelected = new GLShaderProgram(SHADERS_ROOT, SHADERS_EDGE_LINE_SOURCE_WITH_SELECTION_SELECTED, SHADERS_EDGE_LINE_SOURCE)
Expand All @@ -86,6 +92,8 @@ private void initProgram(GL2ES2 gl) {
.addAttribLocation(ATTRIB_NAME_SOURCE_COLOR, SHADER_SOURCE_COLOR_LOCATION)
.addAttribLocation(ATTRIB_NAME_TARGET_COLOR, SHADER_TARGET_COLOR_LOCATION)
.addAttribLocation(ATTRIB_NAME_COLOR, SHADER_COLOR_LOCATION)
.addAttribLocation(ATTRIB_NAME_SOURCE_SIZE, SHADER_SOURCE_SIZE_LOCATION)
.addAttribLocation(ATTRIB_NAME_TARGET_SIZE, SHADER_TARGET_SIZE_LOCATION)
.init(gl);

programWithSelectionUnselected = new GLShaderProgram(SHADERS_ROOT, SHADERS_EDGE_LINE_SOURCE_WITH_SELECTION_UNSELECTED, SHADERS_EDGE_LINE_SOURCE)
Expand All @@ -103,6 +111,8 @@ private void initProgram(GL2ES2 gl) {
.addAttribLocation(ATTRIB_NAME_SOURCE_COLOR, SHADER_SOURCE_COLOR_LOCATION)
.addAttribLocation(ATTRIB_NAME_TARGET_COLOR, SHADER_TARGET_COLOR_LOCATION)
.addAttribLocation(ATTRIB_NAME_COLOR, SHADER_COLOR_LOCATION)
.addAttribLocation(ATTRIB_NAME_SOURCE_SIZE, SHADER_SOURCE_SIZE_LOCATION)
.addAttribLocation(ATTRIB_NAME_TARGET_SIZE, SHADER_TARGET_SIZE_LOCATION)
.init(gl);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ protected void fillUndirectedEdgeAttributesDataWithoutSelection(final float[] bu
fillUndirectedEdgeAttributesDataBase(buffer, edge, index);

buffer[index + 7] = Float.intBitsToFloat(edge.getRGBA());//Color

//Source and target size:
buffer[index + 8] = edge.getSource().size();
buffer[index + 9] = edge.getTarget().size();
}

protected void fillUndirectedEdgeAttributesDataWithSelection(final float[] buffer, final Edge edge, final int index, final boolean selected) {
Expand Down Expand Up @@ -606,6 +610,10 @@ protected void fillUndirectedEdgeAttributesDataWithSelection(final float[] buffe
} else {
buffer[index + 7] = Float.intBitsToFloat(edge.getRGBA());//Color
}

//Source and target size:
buffer[index + 8] = edge.getSource().size();
buffer[index + 9] = edge.getTarget().size();
}

protected void fillDirectedEdgeAttributesDataBase(final float[] buffer, final Edge edge, final int index) {
Expand Down Expand Up @@ -638,8 +646,9 @@ protected void fillDirectedEdgeAttributesDataWithoutSelection(final float[] buff
//Color:
buffer[index + 6] = Float.intBitsToFloat(edge.getRGBA());//Color

//Target size:
buffer[index + 7] = edge.getTarget().size();
//Source and target size:
buffer[index + 7] = edge.getSource().size();
buffer[index + 8] = edge.getTarget().size();
}

protected void fillDirectedEdgeAttributesDataWithSelection(final float[] buffer, final Edge edge, final int index, final boolean selected) {
Expand Down Expand Up @@ -678,8 +687,9 @@ protected void fillDirectedEdgeAttributesDataWithSelection(final float[] buffer,
buffer[index + 6] = Float.intBitsToFloat(edge.getRGBA());//Color
}

//Target size:
buffer[index + 7] = target.size();
//Source and target size:
buffer[index + 7] = source.size();
buffer[index + 8] = target.size();
}

private UndirectedEdgesVAO undirectedEdgesVAO;
Expand Down Expand Up @@ -820,6 +830,12 @@ protected void configure(GL2ES2 gl) {
offset += EdgeLineModelUndirected.TARGET_COLOR_FLOATS * Float.BYTES;

gl.glVertexAttribPointer(SHADER_COLOR_LOCATION, EdgeLineModelUndirected.COLOR_FLOATS * Float.BYTES, GL_UNSIGNED_BYTE, false, stride, offset);
offset += EdgeLineModelUndirected.COLOR_FLOATS * Float.BYTES;

gl.glVertexAttribPointer(SHADER_SOURCE_SIZE_LOCATION, EdgeLineModelDirected.SOURCE_SIZE_FLOATS, GL_FLOAT, false, stride, offset);
offset += EdgeLineModelDirected.SOURCE_SIZE_FLOATS * Float.BYTES;

gl.glVertexAttribPointer(SHADER_TARGET_SIZE_LOCATION, EdgeLineModelDirected.TARGET_SIZE_FLOATS, GL_FLOAT, false, stride, offset);
}
attributesBuffer.unbind(gl);
}
Expand All @@ -833,7 +849,9 @@ protected int[] getUsedAttributeLocations() {
SHADER_SIZE_LOCATION,
SHADER_SOURCE_COLOR_LOCATION,
SHADER_TARGET_COLOR_LOCATION,
SHADER_COLOR_LOCATION
SHADER_COLOR_LOCATION,
SHADER_SOURCE_SIZE_LOCATION,
SHADER_TARGET_SIZE_LOCATION
};
}

Expand All @@ -846,7 +864,9 @@ protected int[] getInstancedAttributeLocations() {
SHADER_SIZE_LOCATION,
SHADER_SOURCE_COLOR_LOCATION,
SHADER_TARGET_COLOR_LOCATION,
SHADER_COLOR_LOCATION
SHADER_COLOR_LOCATION,
SHADER_SOURCE_SIZE_LOCATION,
SHADER_TARGET_SIZE_LOCATION
};
} else {
return null;
Expand Down Expand Up @@ -891,6 +911,9 @@ protected void configure(GL2ES2 gl) {
gl.glVertexAttribPointer(SHADER_COLOR_LOCATION, EdgeLineModelDirected.COLOR_FLOATS * Float.BYTES, GL_UNSIGNED_BYTE, false, stride, offset);
offset += EdgeLineModelDirected.COLOR_FLOATS * Float.BYTES;

gl.glVertexAttribPointer(SHADER_SOURCE_SIZE_LOCATION, EdgeLineModelDirected.SOURCE_SIZE_FLOATS, GL_FLOAT, false, stride, offset);
offset += EdgeLineModelDirected.SOURCE_SIZE_FLOATS * Float.BYTES;

gl.glVertexAttribPointer(SHADER_TARGET_SIZE_LOCATION, EdgeLineModelDirected.TARGET_SIZE_FLOATS, GL_FLOAT, false, stride, offset);
}
attributesBuffer.unbind(gl);
Expand All @@ -905,6 +928,7 @@ protected int[] getUsedAttributeLocations() {
SHADER_SIZE_LOCATION,
SHADER_SOURCE_COLOR_LOCATION,
SHADER_COLOR_LOCATION,
SHADER_SOURCE_SIZE_LOCATION,
SHADER_TARGET_SIZE_LOCATION
};
}
Expand All @@ -918,6 +942,7 @@ protected int[] getInstancedAttributeLocations() {
SHADER_SIZE_LOCATION,
SHADER_SOURCE_COLOR_LOCATION,
SHADER_COLOR_LOCATION,
SHADER_SOURCE_SIZE_LOCATION,
SHADER_TARGET_SIZE_LOCATION
};
} else {
Expand Down

0 comments on commit c5afb8b

Please sign in to comment.