Hi,
Im trying to set up shading for my terrain by using a modified version of a previously posted GLSL terrain shader - but it will not work. The loading of the terrain is no problem but the terrain is completely black, no light, no textures no nothing…
Does anyone know why?>
Here is the code snippet in question:
import com.jme.app.SimpleGame;
import com.jme.image.Texture;
import com.jme.input.NodeHandler;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Torus;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.CullState;
import com.jme.scene.state.GLSLShaderObjectsState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jmex.terrain.TerrainPage;
import com.jmex.terrain.util.ImageBasedHeightMap;
import java.awt.Image;
import java.net.URL;
import javax.swing.ImageIcon;
// Original code by: BlackBlueGL
public class TestShaders extends SimpleGame
{
public static void main(String[] args)
{
TestShaders test = new TestShaders();
test.start();
}
private Image heightmapImage;
private ImageBasedHeightMap heightMap;
private ClassLoader loader = getClass().getClassLoader();
private TerrainPage terrain;
private URL heightmapPath;
private Box shadedGLSLTorus;
private Torus outlineTorus;
private DisplaySystem display;
private GLSLShaderObjectsState shader;
private CullState cullBackFace;
private TextureState ts;
private AlphaState as;
String groundTexture = "jmetest/data/texture/terrain/nicegrass.jpg";
String detailTexture = "jmetest/data/texture/terrain/darkrock.jpg";
String alphaTexture = "jmetest/data/texture/terrain/strigoi.jpg.jpg";
String normalTexture = "jmetest/data/texture/terrain/deadgrass.jpg";
protected void simpleInitGame()
{
display = DisplaySystem.getDisplaySystem();
shader = display.getRenderer().createGLSLShaderObjectsState();
display.setTitle("GLSL Shader Test Envoriment");
display.getRenderer().setBackgroundColor(new ColorRGBA(0.15f, 0.15f, 0.15f, 1.0f));
cam.setLocation(new Vector3f(0, 0, 45));
cam.update();
input = new NodeHandler(rootNode, 10, 2);
lightState.detachAll();
initShader("jmetest/data/texture/terrain/strigoi.jpg", new Vector3f(5, 1.0f, 6), 0.001f);
rootNode.attachChild(terrain);
rootNode.updateRenderState();
}
public void setShadingOnSpatial( Spatial spatial ) {
spatial.setRenderState( cullBackFace );
spatial.setRenderState( shader );
spatial.setRenderState( ts );
spatial.setRenderQueueMode( Renderer.QUEUE_TRANSPARENT );
spatial.setLightCombineMode( LightState.OFF );
spatial.setRenderState( as );
spatial.updateRenderState();
}
public void initShader(String path, Vector3f terrainScale, float mapScale)
{
GLSLShaderObjectsState glsl = display.getRenderer().createGLSLShaderObjectsState();
glsl.load(loader.getResource("jmetest/data/texture/terrain/shader/pixelShader.glsl"),
loader.getResource("jmetest/data/texture/terrain/fragmentShader.glsl"));
createRenderStates();
glsl.setUniform("DiffuseColor", 1.0f, 0.25f, 0.0f);
glsl.setUniform("groundTex", 0);
glsl.setUniform("grassMap", 1);
glsl.setUniform("splatMap", 2);
glsl.setUniform("normalMap", 3);
glsl.setUniform("LightPosition", 2000.0f , 2000.0f , 1000.0f);
glsl.setUniform("SkyColor", 0.9882f , 0.9609f , 0.8164f, 1.0f);
glsl.setUniform("HazeColor", 0.49f , 0.495f , 0.49f, 1.0f);
glsl.setUniform("lightFactor", 2.0f);
glsl.setUniform("tile", 32);
glsl.setEnabled(true);
CullState cs = display.getRenderer().createCullState();
cs.setCullMode(CullState.CS_BACK);
cs.setEnabled(true);
//Get the heightmap from an image
heightmapPath = loader.getResource(path);
heightmapImage = new ImageIcon(heightmapPath).getImage();
//Create the heightmap from the generated Image
heightMap = new ImageBasedHeightMap(heightmapImage);
//Create the terrainpage
terrain = new TerrainPage("Terrain", 33, heightMap.getSize(), terrainScale, heightMap.getHeightMap(), false);
terrain.setLocalTranslation(-100, -400, -100);
shadedGLSLTorus = new Box("My Box",new Vector3f(0,0,0),new Vector3f(1,1,1));
//terrain.setRenderState(glsl);
//terrain.setRenderState(cs);
setShadingOnSpatial(terrain);
}
protected void createRenderStates()
{
Texture t;
ts = display.getRenderer().createTextureState();
{
ts.setEnabled(true);
}
t = TextureManager.loadTexture(
getClass().getClassLoader()
.getResource(groundTexture ),
Texture.MM_NEAREST_LINEAR, Texture.FM_LINEAR);
{
t.setWrap(Texture.WM_WRAP_S_WRAP_T);
ts.setTexture(t, 0);
}
t = TextureManager.loadTexture(
getClass().getClassLoader()
.getResource(detailTexture ),
Texture.MM_NEAREST_LINEAR, Texture.FM_LINEAR);
{
t.setWrap(Texture.WM_WRAP_S_WRAP_T);
ts.setTexture(t, 1);
}
t = TextureManager.loadTexture(
getClass().getClassLoader()
.getResource(alphaTexture ),
Texture.MM_NEAREST_LINEAR, Texture.FM_LINEAR);
{
t.setWrap(Texture.WM_WRAP_S_WRAP_T);
ts.setTexture(t, 2);
}
t = TextureManager.loadTexture(
getClass().getClassLoader()
.getResource(normalTexture ),
Texture.MM_NEAREST_LINEAR, Texture.FM_LINEAR);
{
t.setWrap(Texture.WM_WRAP_S_WRAP_T);
ts.setTexture(t, 3);
}
as = display.getRenderer().createAlphaState();
as.setBlendEnabled( true );
as.setSrcFunction( AlphaState.SB_SRC_ALPHA );
as.setDstFunction( AlphaState.DB_ONE_MINUS_SRC_ALPHA );
as.setEnabled( true );
}
}
Original code was by BlackBlueGL but I cant get it too work for me :( Help!