What should I use to draw a bunch of connected lines?

So in OpenGL I’d use: glBegin(GL_LINES) or glBegin(GL_POINTS) and throw in all my vertices I want for a long connection of lines. What should I use in Java3D to achieve the same effect? I can’t seem to find a good resource to help me on this when I search. I want to be able to modify the length, so add/remove vertices at any time, and I’d also like to be able to add separate points that would show up as dots. So I want to be able to draw:

 __ . _ ___ . _ . _____

Anyone know what I can use for that?

degenerate triangles?

I have no idea what that is…
I found the IndexedLineArray and LineArray classes but I’m not sure what to do with them. I’m not sure if they can help me either. The geometry package doesn’t seem to have anything useful.

Hi,

Here are some snipets of code from a simulation I did.

For a updateable geometry I used this(the numVerts is the maximum allowed for the applicaction):

    tris = new TriangleArray(numVerts, GeometryArray.COORDINATES | GeometryArray.NORMALS | GeometryArray.BY_REFERENCE);
    tris.setCapability(GeometryArray.ALLOW_REF_DATA_WRITE);
    tris.setCapability(GeometryArray.ALLOW_REF_DATA_READ);
    tris.setCapability(GeometryArray.ALLOW_COUNT_READ);
    tris.setCapability(GeometryArray.ALLOW_COUNT_WRITE);

For the appearance in lines this:

      app.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_LINE, PolygonAttributes.CULL_BACK, 0));

Last, for the update, (using the GeometryUpdater interface):

  public void updateData(Geometry geo){
    TriangleArray tris = (TriangleArray)geo;
    float[] coords = tris.getCoordRefFloat();
    float[] norms = tris.getNormalRefFloat();
    int validVerts = 0; 
     //Upadte your vertex/normal/texcoord..
     //Now you can have a new number of  vertices..
     //set it to the geometry.
    tris.setValidVertexCount(validVerts);
  }

This method is called from the behaviors that receives the input.

SInce you want to show the lines and the vertices, you can have two Shape3D objects sharing the same geometry but using the line appearance y point appearance(look in the javadocs for the points, I also use CULL_NONE for them)

Rafael .-

OK I looked into the code you gave me and I feel like I have a better understanding of my options now. However I’m having some trouble with the LineStripArray class.


points[length] = new Point3d(x,y,z);
length++;
if (length < 2)
	return;
		
lines = new LineStripArray(points.length, GeometryArray.COORDINATES, stripLengths);
lines.setCoordinates(0, points);

gives me this:


Exception in thread "main" java.lang.NullPointerException
at javax.media.j3d.GeometryArrayRetained.setCoordinates(GeometryArrayRetained.java:4033)
at javax.media.j3d.GeometryArray.setCoordinates(GeometryArray.java:1401)

stripLengths looks like this:
stripLengths = new int[] {100};
and points looks like this:
points = new Point3d[100];

so why am I getting a NullPointerException? This only happens after the scene starts and I try to recreate “lines” as you see in the first block of code. I’ve found 2 examples online of people creating new LineStripArray’s during animation for this same effect and aparantly it works for them. What am I missing?

EDIT: Also, if I don’t re-initialize lines, I can call the setCoordinate() method successfully before the application starts rendering, but if I call it afterward I get the NullPointerException.

You can’t update the geometry afer it goes live (the rendering begins).
The only way to change it is by setting the correct capabilities.(ALLOW_COORD_READ/WRITE look
for them in the docs).

From some discussions here I know hat the only safe place to update geometry is in
a GeomtryUpdater called in a Behavior.

Rafael.-

To update at run time you need to do geometry by reference. (And yes you need to set all the right properties.)

The updater actually isnt ina behavior, its gets called back at avery specific time when its safe to make the chnage.

OK here’s what I have


stripLengths = new int[] {100};
		lines = new LineStripArray(100, GeometryArray.COORDINATES | GeometryArray.NORMALS | GeometryArray.BY_REFERENCE, stripLengths);
		lines.setCapability(GeometryArray.ALLOW_REF_DATA_WRITE);
		lines.setCapability(GeometryArray.ALLOW_REF_DATA_READ);
		lines.setCapability(GeometryArray.ALLOW_COUNT_READ);
		lines.setCapability(GeometryArray.ALLOW_COUNT_WRITE);
		lines.setCapability(LineArray.ALLOW_COORDINATE_WRITE);
		
		setNode(new Shape3D(lines, getAppearance()));
	    getAppearance().setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_LINE, PolygonAttributes.CULL_BACK, 0));
	    
	    //addPoint(0, 0, 0);
		//addPoint(1, 1, 1);
	    points[0] = new Point3d(0, 0, 0);
	    points[1] = new Point3d(1, 1, 1);


	public void addPoint(double x, double y, double z) {
		points[length] = new Point3d(x,y,z);
		length++;
		
		if (length < 2)
			return;
		
		lines.updateData(this);
	}
	
	public void updateData(Geometry geo){
		LineStripArray lsa = (LineStripArray)geo;
		lsa.setCoordinates(0, points);
	}

But I still get:
Exception in thread “main” java.lang.IllegalStateException: GeometryArray: cannot directly access data in BY_REFERENCE mode
when I call the addPoint() method once running. What am I doing wrong?

Looks liek yo uare trying to call the updater directly from main.

You cannot. You can only update the data on a callback in an updater.

RTFM

http://download.java.net/media/java3d/javadoc/1.3.2/javax/media/j3d/GeometryUpdater.html

Yeah I already read that. I also found an example doing what I’m doing.

I am NOT calling the method that you just said I can’t. I’m calling the GeometryArray.updateData(GeometryUpdater) method. Since that accesses something different I thought that’s where it goes in the background to do it in a safe way.

In that code, lines = new LineStripArray, and this implements GeometryUpdater.

Edit: Here’s the full exception:


Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: GeometryArray: cannot directly access data in BY_REFERENCE mode
	at javax.media.j3d.GeometryArray.setCoordinates(GeometryArray.java:1399)
	at org.opensourcephysics.display3d.java3d.ElementTrail.updateData(ElementTrail.java:127)
	at javax.media.j3d.GeometryArrayRetained.updateData(GeometryArrayRetained.java:6266)
	at javax.media.j3d.GeometryArray.updateData(GeometryArray.java:950)
	at org.opensourcephysics.display3d.java3d.ElementTrail.addPoint(ElementTrail.java:122)

Sicne you didnt include that critical information in the code you posted, it seemed reasonable that you werent doing it.

Taking a closer look it loosk to me like you are trying to chnage the references (the ISA) not the data itself, which would be referred to BY the references.

Just for clarification,


lines = new LineStripArray...

was in the original code. LineStripArray doesn’t have the method you accused me of using, so there was no need for clarification.

im sorry, that line you qoted appears to be totally unrelated to any of the issues at hand.

You did not show us you were using GeometryArray.updateData() method ergo I assumed you were not.
Long distance mind reading is not a specilty of mine.

Now knwoing that you are, your problem looks stroingly to be that you are trying to change the geometry object rather then change the point data refernced through the geometry object.

TO quote the docs:


By Reference: A new set of methods in Java 3D version 1.2 allows data to be accessed by reference, 
directly from the user's arrays. To use this feature, set the BY_REFERENCE bit in the vertexFormat field 
of the constructor for this GeometryArray. In this mode, the various set methods for coordinates, normals, 
colors, and texture coordinates are not used. Instead, new methods are used to set a reference to user-supplied 
coordinate, color, normal, and texture coordinate arrays

RTFM: http://hermes.hud.ac.uk/info/manuals/java3d/javax/media/j3d/GeometryArray.html

You cannot chnage the geometry, you change the arrays the geometry refernces.