Questionably effective terrain generator

Here’s the code that I wrote/stole from other programs, to randomly generate 3d terrain. When I run it however, the screen appears to have nothing. Any ideas what’s going wrong? (Basic summary @ bottom)


import javax.vecmath.*;
import com.xith3d.scenegraph.*;

import com.xith3d.render.*;
import com.xith3d.render.jogl.*;

public class LandTest {

  private Square[][] tiles;
  private Point3f[][] vertices;
  private final int NUM = 32;
  public static int counter = 0;

  public static void main(String[] args){
    new LandTest();
  }

  public LandTest(){
    initVerts();
    initSquares();

    VirtualUniverse universe = new VirtualUniverse();
    Locale l = new Locale(NUM/2f, 0, NUM/2f);
    View v = new View();
    universe.addLocale(l);
    universe.addView(v);

    BranchGroup scene = new BranchGroup();
    l.addBranchGraph(scene);

    Transform3D rotate = new Transform3D();
    rotate.rotXYZ(0,0,0);
    TransformGroup objRotate = new TransformGroup(rotate);
    scene.addChild(objRotate);

    for(int x = 0; x < NUM; x++){
      for(int y = 0; y < NUM; y++){
        objRotate.addChild(new Shape3D(tiles[x][y].getGeometry(), new Appearance()));
      }
    }

    scene.compile();

    RenderPeer rp = new RenderPeerImpl();
    CanvasPeer cp = rp.makeCanvas(null, 640, 480, 32, false);

    Canvas3D canvas = new Canvas3D();
    canvas.set3DPeer(cp);

    v.addCanvas3D(canvas);
    v.setFrontClipDistance(.5f);
    v.setBackClipDistance(20f);

    Transform3D t = new Transform3D();
    t.lookAt(new Vector3f(0, 5f, 10f),
             new Vector3f(NUM/2f, 0, NUM/2f),
             new Vector3f(0, 1f, 0));
    v.setTransform(t);

    for(int i = 0; i > -1; i++){
      v.renderOnce();
    }

  }

  private void initVerts(){
    vertices = new Point3f[NUM+1][NUM+1];
    for(int x = 0; x <= NUM; x++){
      for(int y = 0; y <= NUM; y++){
        vertices[x][y] = new Point3f(x, (float)(20*Math.random()-10), y);
      }
    }
  }

  private void initSquares(){

    tiles = new Square[NUM][NUM];
    for(int x = 0; x < NUM; x++){
      for(int y = 0; y < NUM; y++){
        tiles[x][y] = new Square( vertices[x][y],
                                  vertices[x+1][y],
                                  vertices[x][y+1],
                                  vertices[x+1][y+1]);
      }
    }
  }
}

class Square {

  private Geometry shape;

  Square(Point3f v1, Point3f v2, Point3f v3, Point3f v4){
    Point3f[] coords = new Point3f[] {
        v1, v2, v3,
        v1, v4, v3
    };
    Color3f[] colors = new Color3f[] {
        new Color3f(0, 1f, 0),
        new Color3f(0, 1f, 0),
        new Color3f(0, 1f, 0),
        new Color3f(0, 1f, 0),
        new Color3f(0, 1f, 0),
        new Color3f(0, 1f, 0)
    };

    TriangleArray array = new TriangleArray(coords.length, GeometryArray.COORDINATES|GeometryArray.COLOR_3);
    array.setCoordinates(0, coords);
    array.setColors(0, colors);
  }

  public Geometry getGeometry(){
    return shape;
  }
}

Ok, so first thing it does: creates a 2^5 +1 x 2^5 + 1 array of Point3fs with definite x and z values. The height of that point is determined randomly at runtime. Then, Square objects are created from those points, constructing a 2^5 x 2^5 array of tiles. Each Square has its own Geometry which is used to create Shape3Ds and they are stored in an unrotated TransformGroup. If anything is rendered, however, I can’t see it at runtime. (The window with its trademark Xith Brown/Black bg appears, but nothing is in it).

I have had problems creating terrains with and without texture :(. Judging by the low responses to this problem, I am getting the feeling that may be there is not an efficient or working way in xith to do this :-/. hope i am wrong.

/me has a heightmap terrain generation demo, not sure if it’s of any help though

JCD, might be worth looking at this demo as I am not getting anywhere fast. Where can i find the demo?

Hmm… it works better if I use a QuadArray instead of a TriangleArray. Now I can actually see something rendered. I’ll show it once I get something concrete.

[quote]JCD, might be worth looking at this demo as I am not getting anywhere fast. Where can i find the demo?
[/quote]
Lemmie tweak it and hopefully release it tonight

w00t!!

http://www.geocities.com/silver_klyph/demos.html

Could someone help me in jar’ing a program in Xith3d? The GSG isn’t very helpful, no offense to whoever wrote it, I just have never jar’ed up a program before and Sun’s guide isn’t that great either. Just listing the files that need to be loaded would be very helpful. I can only provide screenshots of my programs now, but I’d like to eventually webstart something non-trivial.

The GSG isn’t about jar packaging, that’s why it can’t help you in this case. What is so bad about the Sun tutorial? You can use “jar cf yourjar.jar file1.class file2.class dir1 dir2” to create a jar file with the name “yourjar.jar” including the files “file1.class”, “file2.class” and the directories “dir1” and “dir2”. Most IDEs can handle this automatically for you.

I meant neither was helpful because I wasn’t sure what files to JAR up. Even after I put all the xith .jar files into my own jar file, it can’t find com.xith3d.scenegraph.Node, do I have to jar the entire src directory and subdirectories?

Here is my plain project example. Create the following files and folders. This will apply to linux, just edit .bat files to linux scripts.

And modify a reference to your jdk path in script files. I always prefer an explicit reference so that know for sure what version is being used.

You always can unzip xith3d.jar file to classes folder and then jar it with your own files. Result is a single .jar file with all necessary files. I would prefer to use a separate xith3d.jar file however, just reference it with -classpath argument in a runner script.

c:\myapp\compile.bat
c:\myapp\jar.bat
c:\myapp\go.bat
c:\myapp\classes
c:\myapp\lib
c:\myapp\src<all .java files with appropriate package subfolders>
c:\myapp\src\META-INF\MANIFEST.MF

MANIFEST.MF


Implementation-Title: MyAppTitle
Implementation-Version: 1.0.0 (2003-01-01)
Implementation-Vendor: MyCompany Ltd.
Implementation-URL: http://www.myserver.com

compile.bat


c:\j2sdk1.4.2\bin\javac.exe -sourcepath ./src -d ./classes ./src/*.java
xcopy /Y .\src\META-INF\*.* .\classes\META-INF\
pause

jar.bat


SET MF=./classes/META-INF/MANIFEST.MF
c:\j2sdk1.4.2\bin\jar.exe cvfm ./lib/myjar.jar %MF% -C ./classes .
pause

go.bat


SET CP=./lib/myjar.jar;<put here other .jar references>
c:\j2sdk1.4.2\bin\java.exe -cp %CP% MyMainClass
pause

[quote]I meant neither was helpful because I wasn’t sure what files to JAR up. Even after I put all the xith .jar files into my own jar file, it can’t find com.xith3d.scenegraph.Node, do I have to jar the entire src directory and subdirectories?
[/quote]
Creating a Webstart app:

First you have to package the files you created (without the libs you need!). After that you have to wrap the native libs in jars (because Webstart needs them as jars) as shown in the GSG. You have to reference all the jars you need in the Webstart jnlp file and specify a main class. If you look close at the jnlp example in the GSG, you can see the directory structure in which all the jars are placed. Next step is to sign the files and you are ready to go.

[quote]Could someone help me in jar’ing a program in Xith3d? The GSG isn’t very helpful, no offense to whoever wrote it, I just have never jar’ed up a program before and Sun’s guide isn’t that great either. Just listing the files that need to be loaded would be very helpful. I can only provide screenshots of my programs now, but I’d like to eventually webstart something non-trivial.
[/quote]
If it is the JWS tutorial you are referring to then I wrote it. Can you elaborate on what isn’t helpful? Using that guide I’ve made some non-trivial JWS Xith3D apps.

I trust you are aware that a jar file is simply a zip file? Just create a zip file and rename the extension to .jar - or as Jens suggested use sun’s handy “jar” tool.

EDIT: Oh, and the best way to do it (that I have found) is use an Ant build script, an example of which is in the GSG CVS. This has the advantages in that it’s platform independant, IDE independant and dead simple.

Will.