Commit 10c0fe6c authored by Ant01n3's avatar Ant01n3

Changed the dependancy on glutils.

Added the needed sources in the org.graphstream.glutil package. They may
disappear, since this is only used for testing actually.

Also added an angle of view for boids.
parent f3069c57
......@@ -34,6 +34,7 @@ import java.util.Iterator;
import java.util.LinkedList;
import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.AbstractGraph;
import org.graphstream.graph.implementations.AdjacencyListNode;
import org.miv.pherd.Particle;
import org.miv.pherd.geom.Point3;
......@@ -46,7 +47,7 @@ public class Boid extends AdjacencyListNode {
protected Forces forces;
public Boid(Graph graph, String id) {
super(graph, id);
super((AbstractGraph)graph, id);
particle = new BoidParticle((Context) graph);
species = ((Context) graph).getDefaultSpecies();
forces = getDefaultForces();
......@@ -76,6 +77,7 @@ public class Boid extends AdjacencyListNode {
return new Forces.BasicForces();
}
@Override
protected void attributeChanged(String sourceId, long timeId,
String attribute, AttributeChangeEvent event, Object oldValue,
Object newValue) {
......@@ -157,6 +159,7 @@ public class Boid extends AdjacencyListNode {
this.ctx = ctx;
}
@Override
public void move(int time) {
contacts = 0;
mySpeciesContacts = 0;
......@@ -197,10 +200,12 @@ public class Boid extends AdjacencyListNode {
moved = true;
}
@Override
public void inserted() {
}
@Override
public void removed() {
}
......
......@@ -51,7 +51,15 @@ public class BoidSpecies extends HashMap<String, Boid> {
/**
* The distance at which a boid is seen.
*/
public double viewZone = 0.2f;
public double viewZone = 0.3f;
/**
* The boid angle of view, [-1..1]. This is the cosine of the angle between the boid
* direction and the direction toward another boid. -1 Means 360 degrees, all is visible.
* 0 means 180 degrees only boids in front are visible, 0.5 means 90 degrees,
* 0.25 means 45 degrees.
*/
public double angleOfView = -1;
/**
* The boid speed at each step. This is the factor by which the speedFactor
......@@ -77,7 +85,7 @@ public class BoidSpecies extends HashMap<String, Boid> {
* divided by the number of boids seen. This factor is the importance of
* this direction in the boid direction.
*/
public double directionFactor = 0.075f;
public double directionFactor = 0.1f;
/**
* How much other visible boids attract a boid. The barycenter of the
......@@ -85,7 +93,7 @@ public class BoidSpecies extends HashMap<String, Boid> {
* boid an this barycenter. This factor is the importance of this vector in
* the boid direction.
*/
public double attractionFactor = 0.1f;
public double attractionFactor = 0.2f;
/**
* All the visible boids repulse the boid. The repulsion vector is the sum
......@@ -99,7 +107,7 @@ public class BoidSpecies extends HashMap<String, Boid> {
* The inertia is the importance of the boid previous direction in the boid
* direction.
*/
public double inertia = 1.2f;
public double inertia = 1.1f;
/**
* Factor for repulsion on boids of other species. The fear that this
......@@ -121,7 +129,7 @@ public class BoidSpecies extends HashMap<String, Boid> {
* The width of the particle in the GUI if any.
*/
public int width = 4;
//protected DemographicManager demographicManager;
// Constructors
......
......@@ -35,6 +35,10 @@ import java.util.HashMap;
import org.graphstream.graph.Graph;
import org.graphstream.graph.NodeFactory;
import org.graphstream.graph.implementations.AdjacencyListGraph;
import org.graphstream.ui.spriteManager.Sprite;
import org.graphstream.ui.spriteManager.SpriteManager;
import org.graphstream.ui.swingViewer.Viewer;
import org.graphstream.ui.swingViewer.util.Camera;
import org.miv.pherd.ParticleBox;
import org.miv.pherd.ntree.Anchor;
import org.miv.pherd.ntree.CellSpace;
......@@ -182,9 +186,11 @@ public class Context extends AdjacencyListGraph {
*/
public Context() {
super("boids-context");
nodeFactory = new BoidFactory();
setNodeFactory(new BoidFactory());
//nodeFactory = new BoidFactory();
boidSpecies = new HashMap<String, BoidSpecies>();
boidSpecies.put("default", new BoidSpecies(this, "default"));
//boidSpecies.put("default", new BoidSpecies(this, "default"));
getOrCreateSpecies("default");
space = new OctreeCellSpace(new Anchor(-2, -2, -2), new Anchor(2, 2, 2));
pbox = new ParticleBox(maxParticlesPerCell, space, new BoidCellData());
}
......@@ -338,6 +344,7 @@ public class Context extends AdjacencyListGraph {
}
}
@Override
public void stepBegins(double step) {
pbox.step();
}
......@@ -353,6 +360,7 @@ public class Context extends AdjacencyListGraph {
}
}
@Override
protected void attributeChanged(String sourceId, long timeId,
String attribute, AttributeChangeEvent event, Object oldValue,
Object newValue) {
......@@ -403,12 +411,19 @@ public class Context extends AdjacencyListGraph {
public static void main(String... args) {
Context ctx = new Context();
BoidSpecies species = ctx.getDefaultSpecies();
species.angleOfView = -1;
ctx.addAttribute("ui.quality");
ctx.addAttribute("ui.antialias");
ctx.display(false);
ctx.addAttribute("ui.stylesheet", "node { size: 4px; } edge { fill-color: grey; }");
Viewer viewer = ctx.display(false);
Camera cam = viewer.getDefaultView().getCamera();
cam.setGraphViewport(-2, -2, 2, 2);
for (int i = 0; i < 100; i++)
for (int i = 0; i < species.count; i++)
ctx.addNode(String.format("boid%03d", i));
while (true) {
......
......@@ -259,10 +259,43 @@ public abstract class Forces {
* The point to consider.
* @return A true if point is visible.
*/
protected boolean isVisible(BoidParticle source, Point3 point) {
/* protected boolean isVisible(BoidParticle source, Point3 point) {
double d = point.distance(source.getPosition());
return (d <= source.getBoid().getSpecies().viewZone);
}
*/
protected boolean isVisible(BoidParticle source, Point3 point) {
// Check both the distance and angle of view according to the direction
// of the source.
BoidSpecies species = source.getBoid().getSpecies();
Point3 pos = source.getPosition();
double d = pos.distance(point);
// At good distance.
if(d <= species.viewZone) {
// If there is an angle of view.
if(species.angleOfView > -1) {
Vector3 dir = new Vector3(source.dir);
Vector3 light = new Vector3(point.x - pos.x, point.y - pos.y, point.z - pos.z);//(pos.x - point.x, pos.y - point.y, pos.z - point.z);
dir.normalize();
light.normalize();
double angle = dir.dotProduct(light);
// In the field of view.
if(angle > species.angleOfView)
return true;
} else {
return true;
}
}
// Not in view.
return false;
}
protected void actionWithNeighboor(BoidParticle p1, BoidParticle p2,
Vector3 rep) {
......
......@@ -9,6 +9,8 @@ import javax.swing.JFrame;
import javax.swing.Timer;
import org.graphstream.boids.Context;
import org.graphstream.ui.swingViewer.Viewer;
import org.graphstream.ui.swingViewer.util.Camera;
public class GUI extends JFrame implements ActionListener
{
......@@ -77,4 +79,21 @@ public class GUI extends JFrame implements ActionListener
viewer.display();
}
}
public static void main(String args[]) {
Context ctx = new Context();
GUI gui = new GUI(ctx);
for (int i = 0; i < 100; i++)
ctx.addNode(String.format("boid%03d", i));
while (true) {
ctx.stepBegins(0);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}
}
\ No newline at end of file
......@@ -7,7 +7,7 @@ import java.util.LinkedList;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import org.miv.glutil.geom.Geometry;
import org.graphstream.glutil.geom.Geometry;
import org.miv.pherd.geom.Point3;
import org.miv.pherd.geom.Vector3;
......@@ -102,12 +102,10 @@ public class GraphicParticle implements Geometry {
positions.addFirst(oldPos);
}
public void delete(GL gl) {
public void delete(GL2 gl) {
}
public void display(GL tgl) {
GL2 gl = tgl.getGL2();
public void display(GL2 gl) {
double x = pos.x;
double y = pos.y;
double z = pos.z;
......
......@@ -11,10 +11,10 @@ import javax.media.opengl.glu.GLU;
import javax.swing.JPanel;
import org.graphstream.boids.Context;
import org.miv.glutil.Buffer;
import org.miv.glutil.BufferListener;
import org.miv.glutil.SwingBuffer;
import org.miv.glutil.geom.Cube;
import org.graphstream.glutil.Buffer;
import org.graphstream.glutil.BufferListener;
import org.graphstream.glutil.SwingBuffer;
import org.graphstream.glutil.geom.Cube;
import org.miv.pherd.ParticleBoxListener;
import org.miv.pherd.ParticleBoxListenerProxy;
import org.miv.pherd.geom.Point3;
......@@ -141,7 +141,7 @@ public class ParticleViewer extends JPanel implements ParticleBoxListener,
public void displayParticles() {
Iterator<? extends GraphicParticle> i = particles.values().iterator();
GL gl = buffer.getGl();
GL2 gl = (GL2)buffer.getGl();
while (i.hasNext()) {
GraphicParticle p = i.next();
......
package org.graphstream.glutil;
import java.io.IOException;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
/**
* Represents a pixel buffer.
*
* <p>
* A buffer is an addressable set of pixels associated with an output where
* OpenGL rendering can occur. The output makes the link between the abstract
* OGL frame buffer and a toolkit like AWT, Swing, QTJambi or SWT for example.
* </p>
*
* <p>
* The pixel buffer can be associated with a surface in the graphical toolkit
* that is part of a complete UI (the CANVAS mode), or be a window of its own
* (the WINDOW mode) or even be full screen (the FULLSCREEN mode).
* </p>
*
* @author Antoine Dutot
* @since 20040423
*/
public interface Buffer
{
// Constants
/**
* Output modes. These are the way the link is done between the GL and a
* toolkit output.
*/
public static enum OutputMode
{
/**
* Output to a canvas component.
*/
CANVAS,
/**
* Output to a canvas, this canvas will not capture any event.
*/
CANVAS_NO_EVENTS,
/**
* Output to a top-level window.
*/
WINDOW,
/**
* Output to a full-screen window.
*/
FULLSCREEN
}
/**
* Image formats supported by the {@link #dumpImage(String, DumpImageFormat, boolean)}
* method.
*/
public static enum DumpImageFormat
{
PNG,
JPG,
BMP
}
// Accessors
/**
* JOGL GL instance.
* @return A graphic context and a way to make calls to it.
*/
GL2 getGl();
/**
* JOGL GLU instance.
* @return A graphic context and a way to make calls to it.
*/
GLU getGlu();
/**
* Buffer width in pixels.
* @return The exact width of the buffer (not of a possible window around).
*/
int getWidth();
/**
* Buffer height in pixels.
* @return The exact height of the buffer (not of a possible window around).
*/
int getHeight();
/**
* Size of the memory used by this buffer in bytes.
* @return Sum of all the buffers bytes used.
*/
int getMemUsed();
/**
* Format of this buffer.
* @return A description of this buffer.
*/
BufferFormat getFormat();
/**
* Number of registered listeners.
* @return Count of listeners.
*/
int getBufferListenerCount();
/**
* I-th listener.
* @return A buffer listener.
*/
BufferListener getBufferListener( int i );
/**
* Return the instance of graphical component that contains the GL canvas
* where graphics are rendered. As a Buffer can be implemented using several
* graphical toolkits (e.g. Swing, SWT or QTJambi) the returned object is
* not typed. This method returns null if the underlying toolkit does not
* allow to embed the GL buffer into another component.
* @return The instance of graphical component that contains the GL canvas
* where graphics are rendered, or null if unavailable.
*/
Object getComponent();
// Commands
/**
* Make the associated GL context current and launch a display on all
* BufferListeners.
*/
void display();
/**
* Change the buffer dimensions (width,height).
*/
void setSize( int width, int height );
/**
* Register a listener.
* @param listener The listener to register.
*/
void addBufferListener( BufferListener listener );
/**
* Remove a registered listener.
* @param listener The listener to remove.
*/
void removeBufferListener( BufferListener listener );
/**
* Output the frame buffer contents to a file.
* @param fileName The file name.
* @param format The file format (See ).
* @param useAlpha
* @throws IOException
*/
void dumpImage( String fileName, DumpImageFormat format, boolean useAlpha )
throws IOException;
}
package org.graphstream.glutil;
import java.util.*;
import java.awt.image.*;
import javax.media.opengl.*;
/**
* Helper class to implement buffers.
*
* <p>
* It mainly implements the handling of buffer listeners.
* </p>
*
* @author Antoine Dutot
* @since 2007
*/
abstract class BufferBase implements Buffer, GLEventListener
{
// Attributes
/**
* List of listeners.
*/
protected List<BufferListener> listeners = new ArrayList<BufferListener>();
/**
* Format of this buffer.
*/
protected BufferFormat format;
protected boolean inited = false;
// Access
/**
* Format of this buffer.
*/
public BufferFormat getFormat()
{
return format;
}
/**
* Number of registered listeners.
*/
public int getBufferListenerCount()
{
return listeners.size();
}
/**
* I-th listener.
*/
public BufferListener getBufferListener( int i )
{
return listeners.get( i );
}
/**
* Dump the buffer to an image.
* @param useAlpha true if the image must contain an alpha channel.
*/
public abstract BufferedImage getImage( boolean useAlpha );
// Commands
/**
* Make the associated GL context current and launch a display on all
* BufferListeners.
*/
public abstract void display();
/**
* Set the buffer dimensions (<code>width</code>,<code>height</code>).
*/
public abstract void setSize( int width, int height );
/**
* Register a listener.
* @param listener The listener to register.
*/
public void addBufferListener( BufferListener listener )
{
listeners.add( listener );
}
/**
* Remove a registered listener.
* @param listener The listener to remove.
*/
public void removeBufferListener( BufferListener listener )
{
int i = listeners.indexOf( listener );
if( i >= 0 )
listeners.remove( i );
}
// GLEventListener
public void display( GLAutoDrawable drawable )
{
// if( inited )
// {
for( BufferListener l : listeners )
l.display( this );
// }
}
public void displayChanged( GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged )
{
// Ignored!
}
public void init( GLAutoDrawable drawable )
{
inited = true;
for( BufferListener l : listeners )
l.init( this );
}
public void reshape( GLAutoDrawable drawable, int x, int y, int width, int height )
{
for( BufferListener l : listeners )
l.reshape( this, x, y, width, height );
}
// Utility
/**
* Utility method to check that, at the point of calling it, no error is
* stored in the OpenGL machine.
* @param msg A prefix to print before an eventual error, usually indicating
* from where the error originates.
*/
public void checkGlError( String msg )
{
GL gl = getGl();
int error = gl.glGetError();
if( msg == null )
msg = "";
if( error != 0 )
{
switch( error )
{
case GL.GL_INVALID_VALUE:
System.err.printf( "Invalid value [%s] (error code %d)%n", msg, error );
break;
case GL.GL_INVALID_ENUM:
System.err.printf( "Invalid enum [%s] (error code %d)%n", msg, error );
break;
// case GL.GL_INVALID_FRAMEBUFFER_OPERATION_EXT:
// System.err.printf( "Invalid framebuffer operation ext [%s] (error code %d)%n", msg, error );
// break;
case GL.GL_INVALID_OPERATION:
System.err.printf( "Invalid operation [%s] (error code %d)%n", msg, error );
break;
default:
System.err.printf( "Unknown error [%s] (error code %d)%n", msg, error );
break;
}
}
}
}
package org.graphstream.glutil;
/**
* Various settings for a {@link org.miv.glutil.Buffer}.
*
* @author Antoine Dutot
* @since 20040704
*/
public class BufferFormat
{
// Attributes
/**
* Is alpha channel (&gt;0) enabled and on how many bits?.
*/
protected int alphabits = 8;
/**
* Size of a Z-buffer component in bits.
*/
protected int depthbits = 16;
/**
* Size of a red component of the frame buffer in bits.
*/
protected int redbits = 8;
/**
* Size of a green component of the frame buffer in bits.
*/
protected int greenbits = 8;
/**
* Size of a blue component of the frame buffer in bits.