Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animation #3

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class MainGLFW {
private static final boolean DISABLE_INSTANCED_RENDERING = false;
private static final boolean DISABLE_VAOS = false;

private static final boolean DEBUG = false;
private static final boolean DEBUG = true;
private static final boolean USE_OPENGL_ES = false;

private static final int WIDTH = 1024;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package org.gephi.viz.engine;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

import jdk.nashorn.internal.runtime.options.Option;
import org.gephi.graph.api.GraphModel;
import org.gephi.graph.api.Rect2D;
import org.gephi.viz.engine.pipeline.RenderingLayer;
Expand Down Expand Up @@ -90,6 +85,36 @@ public class VizEngine<R extends RenderingTarget, I> {
private final InstanceContent instanceContent;
private final AbstractLookup lookup;

//Timer
private float globalTime;
private Optional<Float> selectedTime = Optional.empty();

public float getGlobalTime() {
return globalTime;
}

public void setGlobalTime(float globalTime) {
this.globalTime = globalTime;
}

public Optional<Float >getSelectedTime() {
return selectedTime;
}

// Update only if empty
// Dev comments : I don't know if it make sense to take what's currently in globalTime ?
public void setSelectedTime() {
if(!this.selectedTime.isPresent()) {
this.selectedTime = Optional.of(globalTime);
}
}

// Reset selected time
public void unsetSelectedTime() {
this.selectedTime = Optional.empty();
}


public VizEngine(GraphModel graphModel, R renderingTarget) {
this.graphModel = Objects.requireNonNull(graphModel, "graphModel mandatory");
this.instanceContent = new InstanceContent();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.gephi.viz.engine.status;

import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import org.gephi.graph.api.Edge;
import org.gephi.graph.api.Node;
import org.gephi.viz.engine.util.TimeUtils;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @author Eduardo Ramos
*/
public class TimeUtils {

public static long getTimeMillis() {
return System.nanoTime() / 1_000_000;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Constants {
public static final String ATTRIB_NAME_SOURCE_SIZE = "sourceSize";
public static final String ATTRIB_NAME_TARGET_SIZE = "targetSize";


public static final int SHADER_VERT_LOCATION = 0;
public static final int SHADER_POSITION_LOCATION = 1;
public static final int SHADER_COLOR_LOCATION = 2;
Expand All @@ -42,6 +43,9 @@ public class Constants {
public static final String UNIFORM_NAME_BACKGROUND_COLOR = "backgroundColor";
public static final String UNIFORM_NAME_COLOR_LIGHTEN_FACTOR = "colorLightenFactor";

public static final String UNIFORM_NAME_GLOBAL_TIME = "fGlobalTime";
public static final String UNIFORM_NAME_GLOBAL_SELECTED_START_TIME = "fSelectedStartGlobalTime";
public static final String UNIFORM_NAME_IS_SELECTION_ON = "bIsSelectionOn";
//Rendering order:
public static final int RENDERING_ORDER_NODES = 100;
public static final int RENDERING_ORDER_EDGES = 50;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
precision lowp float;
#endif

uniform float fGlobalTime;

varying lowp vec4 fragColor;

void main() {
gl_FragColor = fragColor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ uniform float minWeight;
uniform float weightDifferenceDivisor;
uniform float edgeScaleMin;
uniform float edgeScaleMax;
uniform float fGlobalTime;
uniform float fSelectedStartGlobalTime;
uniform bool bIsSelectionOn;

attribute vec3 vert;
attribute vec2 position;
Expand Down Expand Up @@ -45,8 +48,11 @@ void main() {
}
color = color / 255.0;

float smoothTransition = smoothstep(0.000,.5,.0001+pow(fGlobalTime - fSelectedStartGlobalTime,.5));

color.rgb = min(colorBias + color.rgb * colorMultiplier, 1.0);
color.rgb = mix(color.rgb, backgroundColor.rgb, colorLightenFactor);

color.rgb = mix(color.rgb, backgroundColor.rgb, colorLightenFactor*smoothTransition);

fragColor = color;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
precision lowp float;
#endif

uniform float fGlobalTime;

varying lowp vec4 fragColor;

void main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ uniform float minWeight;
uniform float weightDifferenceDivisor;
uniform float edgeScaleMin;
uniform float edgeScaleMax;
uniform float fGlobalTime;
uniform float fSelectedStartGlobalTime;
uniform bool bIsSelectionOn;

attribute vec2 vert;
attribute vec2 position;
Expand Down Expand Up @@ -42,8 +45,11 @@ void main() {
}
color = color / 255.0;

float smoothTransition = smoothstep(0.001,.005,pow(fGlobalTime - fSelectedStartGlobalTime,.5));

color.rgb = colorBias + color.rgb * colorMultiplier;
color.rgb = mix(color.rgb, backgroundColor.rgb, colorLightenFactor);

color.rgb = mix(color.rgb, backgroundColor.rgb, colorLightenFactor*smoothTransition);

fragColor = color;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
precision lowp float;
#endif

uniform float fGlobalTime;

varying vec4 fragColor;

void main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
uniform mat4 mvp;
uniform vec4 backgroundColor;
uniform float colorLightenFactor;
uniform float fGlobalTime;
uniform float fSelectedStartGlobalTime;
uniform bool bIsSelectionOn;

attribute vec2 vert;
attribute vec2 position;
Expand All @@ -13,14 +16,16 @@ attribute float size;

varying vec4 fragColor;

void main() {
void main() {
vec2 instancePosition = size * vert + position;
gl_Position = mvp * vec4(instancePosition, 0.0, 1.0);

//bgra -> rgba because Java color is argb big-endian
vec4 color = elementColor.bgra / 255.0;
color.rgb = colorBias + color.rgb * colorMultiplier;
color.rgb = mix(color.rgb, backgroundColor.rgb, colorLightenFactor);

float smoothTransition = smoothstep(0.000,.5,.0001+pow(fGlobalTime - fSelectedStartGlobalTime,.5));
color.rgb = mix(color.rgb, backgroundColor.rgb, colorLightenFactor*smoothTransition);

fragColor = color;
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
package org.gephi.viz.engine.lwjgl;

import org.gephi.viz.engine.VizEngine;
import org.gephi.viz.engine.status.GraphSelection;
import org.gephi.viz.engine.util.TimeUtils;
import org.gephi.viz.engine.util.gl.OpenGLOptions;
import org.lwjgl.glfw.GLFW;
import static org.lwjgl.glfw.GLFW.glfwMakeContextCurrent;
import static org.lwjgl.glfw.GLFW.glfwPollEvents;
import static org.lwjgl.glfw.GLFW.glfwSetWindowCloseCallback;
import static org.lwjgl.glfw.GLFW.glfwSetWindowSizeCallback;
import static org.lwjgl.glfw.GLFW.glfwSwapBuffers;
import static org.lwjgl.glfw.GLFW.glfwSwapInterval;
import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_RENDERER;
import static org.lwjgl.opengl.GL11.GL_VENDOR;
import static org.lwjgl.opengl.GL11.GL_VERSION;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glClearColor;
import static org.lwjgl.opengl.GL11.glDisable;
import static org.lwjgl.opengl.GL11.glGetString;
import static org.lwjgl.opengl.GL11.glViewport;
import org.lwjgl.opengl.GLCapabilities;
import org.lwjgl.opengl.GLUtil;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;

/**
*
* @author Eduardo Ramos
Expand Down Expand Up @@ -133,6 +121,19 @@ public void loop() {
glClear(GL_COLOR_BUFFER_BIT); // clear the framebuffer

updateFPS();

// fGlobalTime for all program of the engine, using glfwGetTime retrive time since started;
engine.setGlobalTime((float) glfwGetTime());

// selectedTime for all program of the engine.
GraphSelection selection = engine.getLookup().lookup(GraphSelection.class);

// A selection is currently active
if(selection.getSelectedNodesCount() > 0){
engine.setSelectedTime();
} else {
engine.unsetSelectedTime();
}
engine.display();

glfwSwapBuffers(windowHandle); // swap the color buffers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.gephi.viz.engine.lwjgl.models;

import org.gephi.viz.engine.util.gl.Constants;
import static org.gephi.viz.engine.util.gl.Constants.*;
import org.gephi.viz.engine.util.NumberUtils;
import org.gephi.viz.engine.lwjgl.util.gl.GLShaderProgram;
import org.gephi.viz.engine.util.NumberUtils;
import org.gephi.viz.engine.util.gl.Constants;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL31;
import org.lwjgl.opengl.GL42;

import java.util.Optional;

import static org.gephi.viz.engine.util.gl.Constants.*;

/**
*
* @author Eduardo Ramos
Expand Down Expand Up @@ -63,6 +66,9 @@ private void initProgram() {
.addUniformName(UNIFORM_NAME_EDGE_SCALE_MAX)
.addUniformName(UNIFORM_NAME_MIN_WEIGHT)
.addUniformName(UNIFORM_NAME_WEIGHT_DIFFERENCE_DIVISOR)
.addUniformName(UNIFORM_NAME_GLOBAL_TIME)
.addUniformName(UNIFORM_NAME_GLOBAL_SELECTED_START_TIME)
.addUniformName(UNIFORM_NAME_IS_SELECTION_ON)
.addAttribLocation(ATTRIB_NAME_VERT, SHADER_VERT_LOCATION)
.addAttribLocation(ATTRIB_NAME_POSITION, SHADER_POSITION_LOCATION)
.addAttribLocation(ATTRIB_NAME_POSITION_TARGET, SHADER_POSITION_TARGET_LOCATION)
Expand All @@ -84,8 +90,8 @@ public void drawArraysMultipleInstance(int drawBatchCount) {
GL20.glDrawArrays(GL20.GL_TRIANGLES, 0, VERTEX_COUNT * drawBatchCount);
}

public void drawInstanced(float[] mvpFloats, float[] backgroundColorFloats, float colorLightenFactor, int instanceCount, int instancesOffset, float scale, float minWeight, float maxWeight) {
useProgram(mvpFloats, backgroundColorFloats, colorLightenFactor, scale, minWeight, maxWeight);
public void drawInstanced(float[] mvpFloats, float[] backgroundColorFloats, float colorLightenFactor, int instanceCount, int instancesOffset, float scale, float minWeight, float maxWeight, float globalTime, Optional<Float> selectedTime) {
useProgram(mvpFloats, backgroundColorFloats, colorLightenFactor, scale, minWeight, maxWeight, globalTime, selectedTime);
if (instancesOffset > 0) {
GL42.glDrawArraysInstancedBaseInstance(GL20.GL_TRIANGLES, 0, VERTEX_COUNT, instanceCount, instancesOffset);
} else {
Expand All @@ -94,22 +100,30 @@ public void drawInstanced(float[] mvpFloats, float[] backgroundColorFloats, floa
stopUsingProgram();
}

public void useProgram(float[] mvpFloats, float[] backgroundColorFloats, float colorLightenFactor, float scale, float minWeight, float maxWeight) {
public void useProgram(float[] mvpFloats, float[] backgroundColorFloats, float colorLightenFactor, float scale, float minWeight, float maxWeight, float globalTime, Optional<Float> selectedTime) {
program.use();
prepareProgramData(mvpFloats, backgroundColorFloats, colorLightenFactor, scale, minWeight, maxWeight);
prepareProgramData(mvpFloats, backgroundColorFloats, colorLightenFactor, scale, minWeight, maxWeight, globalTime, selectedTime);
}

public void stopUsingProgram() {
program.stopUsing();
}

private void prepareProgramData(float[] mvpFloats, float[] backgroundColorFloats, float colorLightenFactor, float scale, float minWeight, float maxWeight) {
private void prepareProgramData(float[] mvpFloats, float[] backgroundColorFloats, float colorLightenFactor, float scale, float minWeight, float maxWeight, float globalTime, Optional<Float> selectedTime) {
GL20.glUniformMatrix4fv(program.getUniformLocation(UNIFORM_NAME_MODEL_VIEW_PROJECTION), false, mvpFloats);
GL20.glUniform4fv(program.getUniformLocation(UNIFORM_NAME_BACKGROUND_COLOR), backgroundColorFloats);
GL20.glUniform1f(program.getUniformLocation(UNIFORM_NAME_COLOR_LIGHTEN_FACTOR), colorLightenFactor);
GL20.glUniform1f(program.getUniformLocation(UNIFORM_NAME_EDGE_SCALE_MIN), EDGE_SCALE_MIN * scale);
GL20.glUniform1f(program.getUniformLocation(UNIFORM_NAME_EDGE_SCALE_MAX), EDGE_SCALE_MAX * scale);
GL20.glUniform1f(program.getUniformLocation(UNIFORM_NAME_MIN_WEIGHT), minWeight);
GL20.glUniform1f(program.getUniformLocation(UNIFORM_NAME_GLOBAL_TIME), globalTime);
if(selectedTime.isPresent()){
GL20.glUniform1f(program.getUniformLocation(UNIFORM_NAME_GLOBAL_SELECTED_START_TIME), selectedTime.get());
GL20.glUniform1f(program.getUniformLocation(UNIFORM_NAME_IS_SELECTION_ON), selectedTime.get());
} else {
GL20.glUniform1f(program.getUniformLocation(UNIFORM_NAME_GLOBAL_SELECTED_START_TIME), 0);
GL20.glUniform1f(program.getUniformLocation(UNIFORM_NAME_IS_SELECTION_ON), 0);
}

if (NumberUtils.equalsEpsilon(minWeight, maxWeight, 1e-3f)) {
GL20.glUniform1f(program.getUniformLocation(UNIFORM_NAME_WEIGHT_DIFFERENCE_DIVISOR), 1);
Expand Down
Loading