Exception when attempting to use lighted terrain

The code that I use to create my terrain without lighting:

[java]private void createTerrain(String heightMapFile) {

Material mat_terrain = new Material(assetManager,

“Common/MatDefs/Terrain/Terrain.j3md”);

Texture sand = assetManager.loadTexture(

“Textures/Terrain/sand.jpg”);

sand.setWrap(WrapMode.Repeat);

mat_terrain.setTexture(“Tex1”, sand);

mat_terrain.setFloat(“Tex1Scale”, 64f);



AbstractHeightMap heightmap = null;

Texture heightMapImage = assetManager.loadTexture(heightMapFile);



heightMapImage.getImage().setFormat(Format.RGB8);



heightmap = new ImageBasedHeightMap(heightMapImage.getImage());

heightmap.setHeightScale(128);

heightmap.load();



TerrainQuad terrain;

terrain = new TerrainQuad(“terrain”, 129, heightmap.getSize(), heightmap.getHeightMap());

terrain.setMaterial(mat_terrain);



RigidBodyControl terrPhys = new RigidBodyControl(0);

terrain.addControl(terrPhys);



rootNode.attachChild(terrain);

bulletAppState.getPhysicsSpace().add(terrPhys);

}[/java]



Using this code, the terrain shows up correctly.



But when I replace

Material mat_terrain = new Material(assetManager, “Common/MatDefs/Terrain/Terrain.j3md”);

with:

Material mat_terrain = new Material(assetManager, “Common/MatDefs/Terrain/TerrainLighting.j3md”);



I get an exception:

java.lang.IllegalArgumentException: Material parameter is not defined: Tex1

at com.jme3.material.Material.checkSetParam(Material.java:384)

at com.jme3.material.Material.setTextureParam(Material.java:486)

at com.jme3.material.Material.setTexture(Material.java:534)

at Game.createTerrain(Game.java:103)

at Game.initWorld(Game.java:66)

at Game.simpleInitApp(Game.java:43)

at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:231)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:129)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:205)

at java.lang.Thread.run(Thread.java:662)



I guess I’m doing something wrong?

Yeah you try to set a texture that doesn’t exist in that material.

Thanks, I’m now using some code from TerrainTestAdvanced.java, and it’s working now. But it’s behaving strangely: there are huge black patches on the terrain:



http://i.imgur.com/owVls.png



My lighting code:

[java]DirectionalLight sun = new DirectionalLight();

sun.setColor(ColorRGBA.White);

sun.setDirection(new Vector3f(-.9f,-.9f,-.9f).normalizeLocal());

rootNode.addLight(sun);[/java]



The black patches change if I move the camera around.



What my terrain code became:

[java]private void createTerrain(String heightMapPath) {



// TERRAIN TEXTURE material

Material matTerrain = new Material(assetManager, “Common/MatDefs/Terrain/TerrainLighting.j3md”);

matTerrain.setBoolean(“useTriPlanarMapping”, false);

matTerrain.setFloat(“Shininess”, 0.0f);



// ALPHA map (for splat textures)

matTerrain.setTexture(“AlphaMap”, assetManager.loadTexture(“Textures/Terrain/splat/alpha1.png”));

matTerrain.setTexture(“AlphaMap_1”, assetManager.loadTexture(“Textures/Terrain/splat/alpha2.png”));



// HEIGHTMAP image (for the terrain heightmap)

Texture heightMapImage = assetManager.loadTexture(heightMapPath);

heightMapImage.getImage().setFormat(Format.RGB8);



// GRASS texture

Texture grass = assetManager.loadTexture(“Textures/Terrain/splat/grass.jpg”);

grass.setWrap(WrapMode.Repeat);

//matTerrain.setTexture(“DiffuseMap_1”, grass);

//matTerrain.setFloat(“DiffuseMap_1_scale”, grassScale);



// DIRT texture

Texture dirt = assetManager.loadTexture(“Textures/Terrain/splat/dirt.jpg”);

dirt.setWrap(WrapMode.Repeat);

matTerrain.setTexture(“DiffuseMap”, dirt);

matTerrain.setFloat(“DiffuseMap_0_scale”, 1);



// ROCK texture

Texture rock = assetManager.loadTexture(“Textures/Terrain/splat/road.jpg”);

rock.setWrap(WrapMode.Repeat);

//matTerrain.setTexture(“DiffuseMap_2”, rock);

//matTerrain.setFloat(“DiffuseMap_2_scale”, rockScale);



// BRICK texture

Texture brick = assetManager.loadTexture(“Textures/Terrain/BrickWall/BrickWall.jpg”);

brick.setWrap(WrapMode.Repeat);

//matTerrain.setTexture(“DiffuseMap_3”, brick);

//matTerrain.setFloat(“DiffuseMap_3_scale”, rockScale);



// RIVER ROCK texture

Texture riverRock = assetManager.loadTexture(“Textures/Terrain/Pond/Pond.jpg”);

riverRock.setWrap(WrapMode.Repeat);

//matTerrain.setTexture(“DiffuseMap_4”, riverRock);

//matTerrain.setFloat(“DiffuseMap_4_scale”, rockScale);





Texture normalMap0 = assetManager.loadTexture(“Textures/Terrain/splat/grass_normal.jpg”);

normalMap0.setWrap(WrapMode.Repeat);

Texture normalMap1 = assetManager.loadTexture(“Textures/Terrain/splat/dirt_normal.png”);

normalMap1.setWrap(WrapMode.Repeat);

Texture normalMap2 = assetManager.loadTexture(“Textures/Terrain/splat/road_normal.png”);

normalMap2.setWrap(WrapMode.Repeat);

matTerrain.setTexture(“NormalMap”, normalMap0);



// CREATE HEIGHTMAP

AbstractHeightMap heightmap = null;

try {

heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 0.5f);

heightmap.setHeightScale(128);

heightmap.load();

heightmap.smooth(0.9f, 1);



} catch (Exception e) {

e.printStackTrace();

}



TerrainQuad terrain = new TerrainQuad(“terrain”, 65, 513, heightmap.getHeightMap());//, new LodPerspectiveCalculatorFactory(getCamera(), 4)); // add this in to see it use entropy for LOD calculations

TerrainLodControl control = new TerrainLodControl(terrain, getCamera());

control.setLodCalculator( new DistanceLodCalculator(65, 2.7f) ); // patch size, and a multiplier

terrain.addControl(control);

terrain.setMaterial(matTerrain);

terrain.setModelBound(new BoundingBox());

terrain.updateModelBound();

terrain.setLocalTranslation(0, -100, 0);

terrain.setLocalScale(1f, 1f, 1f);



RigidBodyControl terrPhys = new RigidBodyControl(0);

terrain.addControl(terrPhys);



rootNode.attachChild(terrain);

bulletAppState.getPhysicsSpace().add(terrPhys);

}[/java]

Do you have an nVidia graphics card by any chance?

Also what version of jme are you using and where did you get it?

nVidia? Yes, I do. Would this work better on an Intel one? (I might try it on my dad’s computer, he has an Intel one.)



I don’t know the exact version of my copy of JME, but I got it from http://www.jmonkeyengine.com/nightly/ since I prefer using Eclipse instead of the SDK (it keeps crashing and it’s very slow. Don’t know if that’s related to my computer or not.)



EDIT: Found the version in my dowload history: JME 3 2012-02-07

No, nVidia is super fine… but you will get the behavior you see when setting shininess to 0. Set shininess to a non-zero value and your problem should go away.

Yes! It’s gone! Thanks alot!