import com.jme.app.SimpleGame;
import com.jme.renderer.ColorRGBA;
import com.jmex.terrain.TerrainBlock;
import com.jmex.terrain.util.ImageBasedHeightMap;
import com.jmex.terrain.util.ProceduralTextureGenerator;
import com.jme.math.Vector3f;
import com.jme.bounding.BoundingBox;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jme.image.Texture;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
public class Engine extends SimpleGame {
public static void main(String[] args) {
Engine app = new Engine();
app.start();
}
protected void simpleInitGame() {
cam.setParallelProjection(true);
float aspect = (float) display.getWidth() / display.getHeight();
cam.setFrustum( -100, 1000, -50 * aspect, 50 * aspect, -50, 50 );
cam.setLocation(new Vector3f(0, 0,0));
cam.setDirection(new Vector3f(-1, -1, -1));
cam.update();
display.getRenderer().setBackgroundColor(ColorRGBA.white);
complexTerrain();
saveImageFile();
//System.exit(0);
}
public void saveImageFile()
{
int width = 1024; int height = 768;
IntBuffer pixelBuf = ByteBuffer.allocateDirect((width*height)<<2).order(ByteOrder.nativeOrder()).asIntBuffer();
GL11.glReadPixels(0, 0, width, height, GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE, pixelBuf);
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
img.setRGB (x, y, pixelBuf.get((height - y - 1) * width + x));
}
}
System.out.println(img.toString());
String filename = "map//MapPattern_Test.jpg";
try
{
System.out.println("Schreibe Pattern Datei: "+filename);
OutputStream out = new BufferedOutputStream(new FileOutputStream(filename));
ImageIO.write(img, "jpeg", out);
}
catch(IOException e)
{
System.out.println("Fehler beim schreiben der Pattern-Datei: "+filename);
}
}
/**
*
* Läd ein Bild (jpeg) als Buffered Image und konvertiert es anschließend in Byte Code
* @param Path
* @return
*/
byte[] loadImageFile(String Path)
{
String formatName = "jpeg"; //gif...
File imageFile = new File (Path);
BufferedImage image = null;
try {
System.out.println("Wird eingelesen...");
image = ImageIO.read(imageFile);
}
catch (IOException e) {
System.out.println("Fehler beim Einlesen der Datei \""+Path+"\"");
}
System.out.println("File \""+Path+"\" eingelesen");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedImage smallImage = image.getSubimage(0, 0, 50, 50);
// BufferedImage smallImage = image;
try
{
ImageIO.write(smallImage, formatName, baos);
}
catch (IOException ex)
{
Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Fehler beim Konvertieren der Bilddatei!");
}
byte[] imageData = baos.toByteArray();
return imageData;
}
private void complexTerrain() {
// This grayscale image will be our terrain
// Create an image height map based on the gray scale of our image.
ImageBasedHeightMap ib = new ImageBasedHeightMap(
new ImageIcon(loadImageFile("TestLandscape.jpg")).getImage()
);
// Create a terrain block from the image's grey scale
TerrainBlock tb = new TerrainBlock("image icon", ib.getSize(),
new Vector3f(.5f, .05f, .5f), ib.getHeightMap(),
new Vector3f(0, 0, 0), false);
// Create an object to generate textured terrain from the
// image based height map.
ProceduralTextureGenerator pg = new ProceduralTextureGenerator(ib);
// Look like water from height 0-60 with the strongest
// "water look" at 30
pg.addTexture(new ImageIcon(loadImageFile("textures"+File.separator+"textures"+File.separator+"wl_wasser_01.jpg")), 0, 30, 60);
// Look like dirt from height 40-120 with the strongest
// "dirt look" at 80
pg.addTexture(new ImageIcon(loadImageFile("textures"+File.separator+"textures"+File.separator+"wl_wiese1_00.jpg")), 40, 80, 120); // Look like highest (pure white) from height 110-256
// with the strongest "white look" at 130
pg.addTexture(new ImageIcon(loadImageFile("textures"+File.separator+"textures"+File.separator+"wl_wiese4_00.jpg")), 110, 130, 256);
// Tell pg to create a texture from the ImageIcon's it has recieved.
pg.createTexture(256);
TextureState ts = display.getRenderer().createTextureState();
// Load the texture and assign it.
ts.setTexture(
TextureManager.loadTexture(
pg.getImageIcon().getImage(),
Texture.MM_LINEAR_LINEAR,
Texture.FM_LINEAR,
true));
tb.setRenderState(ts);
// Give the terrain a bounding box
tb.setModelBound(new BoundingBox());
tb.updateModelBound();
// Move the terrain in front of the camera
// tb.setLocalTranslation(new Vector3f(0, 0, 75));
tb.setLocalTranslation(new Vector3f(0, 0, 0));
// Attach the terrain to our rootNode.
rootNode.attachChild(tb);
}
}