Skip to main content

Adding light to a IndexedTriangleStrip Array

3 replies [Last post]
laugejepsen
Offline
Joined: 2006-10-04

I'm new to Java3D (have been playing with it for two months), but I have managed to create a small program that can draw the terrain of a landscape using a IndexedTriangleStripArray. It all works and looks great :)

Now, however I would like to add light to it. I found out that I need to calculate the normals of the landscape in order to do this and that that could be done using a NormalGenerator.

The problem is, that even after using the NormalGenerator and adding the light sources it still looks the same.

Could somebody, please point out for me what it is I'm not getting, here? :)

THE CODE FOR THE LANDSCAPE:

private void doTerrain() {

// CREATE THE LANDSCAPE
GeometryInfo thisTerrain = new GeometryInfo(GeometryInfo.TRIANGLE_STRIP_ARRAY);

ArrayList landscapeHeights = doCalculateTerrainHeights();

float[] coordinates = doIndexCoordinates(landscapeHeights);
float[] texCoordinates = doExtractTexCoordinates(coordinates);
int[] indices = doCalculateIndices();
int[] stripCounts = doCalculateStripCounts();

thisTerrain.setCoordinates(coordinates);
thisTerrain.setCoordinateIndices(indices);
thisTerrain.setStripCounts(stripCounts);

thisTerrain.setTextureCoordinateParams(1,2);
thisTerrain.setTextureCoordinates(0, texCoordinates);
thisTerrain.setTextureCoordinateIndices(0, indices);

NormalGenerator ng = new NormalGenerator();
ng.generateNormals(thisTerrain);
GeometryArray result = thisTerrain.getGeometryArray();

Appearance ap = doAppearance();

terrain = new Shape3D(result, ap);
}

THE CODE FOR THE LIGHT:

private void doSetupLighting() {

DirectionalLight light = new DirectionalLight(new Color3f(1.f,1.f,1.f),new Vector3f(-1,-1,0));
light.setCapability(Light.ALLOW_COLOR_WRITE);
light.setInfluencingBounds(new BoundingSphere());
root.addChild(light);

// Create AmbientLight light with color. Add it to scene.
AmbientLight ambientLight = new AmbientLight(new Color3f(.3f,.3f,.3f));
ambientLight.setInfluencingBounds(new BoundingSphere());
root.addChild(ambientLight);
}

The light part is copy-pasted from somewhere else so I have no idea if it even works.

Thanks in advance,
Lauge Jepsen

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
fredden74
Offline
Joined: 2007-03-23

I believe that the problem is here:
ambientLight.setInfluencingBounds(new BoundingSphere());

Your light will be added at the point 0,0,0 and will have a radius of 1. Your landscape is not "touched" by your light.

For my project, I used theses following lines

//for the CENTER, I used the variables:
//midOfX and midOfY are the middle in x and y of the //englobing box of your landscape
//maxZ is the max of z of your points
//the center point is located on the center of top face of //englobing box
//Perhaps, you will have to move the z
Point3d center=new Point3d(midOfX,midOfY,maxZ);

//for the RADIUS, I used the variables:
//dx = delta x = the length in X of your englobing box
//dy, dz for x and z axis
//I want that the light reached each side of my landscape
//so I calculated the distance between my center and the //sides of the englobing box
double radius=Math.sqrt(dz*dz+(dx*dx+dy*dy)/4);

//to finish
ambientLight.setInfluencingBounds(new BoundingSphere(center,radius));

HTH,
Fred

Message was edited by: fredden74

laugejepsen
Offline
Joined: 2006-10-04

Ok, I found out that the light part works fine.

I tested in on a simple sphere and there is no problem there, so it must be something with the Landscape part.

Any help what so ever would be greatly appreciated.

Thanks

olie
Offline
Joined: 2007-03-21

Hey, I'm having a similar problem -- did you ever find the answer?

Also, do you think it's important to re-cast your triangle-strip-array as a shape3d (near the end of your routine)? I just keep an IndexedTriangleStripArray, and it's a rather nice looking bit of terrain.

...Well, except for the no-lighting part, of course ;)

My code is, roughly:

IndexedTriangleStripArray terrain = new IndexedTriangleStripArray (vCount, IndexedTriangleStripArray.COORDINATES, iCount, siCounts);
// Set up coordinates
[snip]
// set up indecies
[snip]
GeometryInfo gi = new GeometryInfo (terrain);
NormalGenerator ng = new NormalGenerator();
ng.generateNormals (gi);

gi.recomputeIndices();

Thanks!