Skip to content

Instantly share code, notes, and snippets.

@ulmangt
Last active December 26, 2015 01:59
Show Gist options
  • Save ulmangt/7074668 to your computer and use it in GitHub Desktop.
Save ulmangt/7074668 to your computer and use it in GitHub Desktop.
Example of drawing custom OpenGL rendering on a timeline.
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import com.metsci.glimpse.axis.Axis2D;
import com.metsci.glimpse.context.GlimpseBounds;
import com.metsci.glimpse.examples.Example;
import com.metsci.glimpse.layout.GlimpseAxisLayout2D;
import com.metsci.glimpse.painter.base.GlimpseDataPainter2D;
import com.metsci.glimpse.plot.timeline.StackedTimePlot2D;
import com.metsci.glimpse.plot.timeline.data.Epoch;
import com.metsci.glimpse.plot.timeline.layout.TimePlotInfo;
import com.metsci.glimpse.support.color.GlimpseColor;
import com.metsci.glimpse.support.settings.OceanLookAndFeel;
import com.metsci.glimpse.util.units.time.Time;
import com.metsci.glimpse.util.units.time.TimeStamp;
public class TimelineCustomDrawingExample extends CollapsibleTimelinePlotExample
{
public static void main( String[] args ) throws Exception
{
Example example = Example.showWithSwing( new TimelineCustomDrawingExample( ) );
// use the ocean look and feel
example.getCanvas( ).setLookAndFeel( new OceanLookAndFeel( ) );
}
@Override
public StackedTimePlot2D getLayout( )
{
StackedTimePlot2D plot = super.getLayout( );
// get a reference to one of the plots
TimePlotInfo plotInfo = plot.getTimePlot( "speed-plot-1-id" );
// get the GlimpseLayout for the plot (to which painters can be added
// in order to add arbitrary graphics to the plot)
GlimpseAxisLayout2D plotLayout = plotInfo.getLayout( );
// get the timestamp which we'll start drawing our shape at
Epoch epoch = plot.getEpoch( );
TimeStamp baseTime = epoch.getTimeStamp( );
// build the x and y coordinates of the line vertices
final float[] coordsX = new float[5];
final float[] coordsY = new float[5];
coordsX[0] = ( float ) epoch.fromTimeStamp( baseTime.add( Time.fromHours( 1 ) ) );
coordsX[1] = ( float ) epoch.fromTimeStamp( baseTime.add( Time.fromHours( 0.2 ) ) );
coordsX[2] = ( float ) epoch.fromTimeStamp( baseTime.add( Time.fromHours( 0.4 ) ) );
coordsX[3] = ( float ) epoch.fromTimeStamp( baseTime.add( Time.fromHours( 0.6 ) ) );
coordsX[4] = ( float ) epoch.fromTimeStamp( baseTime.add( Time.fromHours( 1 ) ) );
coordsY[0] = 0.0f;
coordsY[1] = 1.0f;
coordsY[2] = 10.0f;
coordsY[3] = 2.0f;
coordsY[4] = 10.0f;
// create a painter for displaying polygonal shapes
GlimpseDataPainter2D customPainter = new GlimpseDataPainter2D( )
{
@Override
public void paintTo( GL2 gl, GlimpseBounds bounds, Axis2D axis )
{
// arbitrary OpenGL rendering can go here
gl.glColor3fv( GlimpseColor.getRed( ), 0 );
gl.glLineWidth( 2.0f );
gl.glBegin( GL.GL_LINE_LOOP );
try
{
for ( int i = 0; i < coordsX.length; i++ )
{
gl.glVertex2d( coordsX[i], coordsY[i] );
}
}
finally
{
gl.glEnd( );
}
}
};
plotLayout.addPainter( customPainter );
return plot;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment