Start of custom terrain impl

This commit is contained in:
Ziver Koc 2020-04-19 14:06:03 +02:00
parent ea19542171
commit 7950da78d8
5 changed files with 140 additions and 26 deletions

View file

@ -1,26 +1,24 @@
package se.cookery;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.terrain.geomipmap.TerrainLodControl;
import com.jme3.terrain.geomipmap.TerrainQuad;
import se.cookery.core.world.Block;
import se.cookery.core.world.gen.GrassLandWorldGenerator;
import se.cookery.gfx.character.Player;
import se.cookery.gfx.terrain.TerrainMesh;
import se.cookery.gfx.util.GeometryUtil;
import se.cookery.gfx.util.MaterialUtil;
import se.cookery.gfx.util.WireFrameProcessor;
import zutil.log.CompactLogFormatter;
import zutil.log.LogUtil;
public class CookeryClient extends SimpleApplication {
private TerrainQuad terrain;
@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(30);
flyCam.setMoveSpeed(15);
cam.setLocation(new Vector3f(0,20,-10));
cam.lookAt(new Vector3f(0f,0f,0f), Vector3f.UNIT_Z);
@ -34,7 +32,7 @@ public class CookeryClient extends SimpleApplication {
*/
Block block = new GrassLandWorldGenerator().generateBlock(0, 0);
float[] heightMap = new float[Block.BLOCK_SIZE*Block.BLOCK_SIZE];
/*float[] heightMap = new float[Block.BLOCK_SIZE*Block.BLOCK_SIZE];
for (int x=0; x<Block.BLOCK_SIZE; x++) {
for (int y=0; y<Block.BLOCK_SIZE; y++) {
@ -47,15 +45,18 @@ public class CookeryClient extends SimpleApplication {
Material green = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
green.setColor("Color", ColorRGBA.Green);
terrain.setMaterial(green);
rootNode.attachChild(terrain);*/
Geometry terrain = new Geometry("terrain", new TerrainMesh(1, 1, 1, 1));
terrain.setMaterial(MaterialUtil.createMaterial(assetManager, ColorRGBA.Green));
rootNode.attachChild(terrain);
Geometry box = new Geometry("Box", new Box(1,1,1));
Material red = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
red.setColor("Color", ColorRGBA.Red);
box.setMaterial(red);
box.setLocalTranslation(0.5f, 0.5f, 0.5f);
rootNode.attachChild(box);
// Geometry box = GeometryUtil.createBox(assetManager, 1f, 1f, 1f, ColorRGBA.Red);
// box.setLocalTranslation(box.getModelBound().getCenter());
// rootNode.attachChild(box);
Player player = new Player(assetManager);
rootNode.attachChild(player.getGfxNode());
// Wireframe mode
viewPort.addProcessor(new WireFrameProcessor(assetManager));

View file

@ -0,0 +1,28 @@
package se.cookery.gfx.character;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import se.cookery.gfx.util.GeometryUtil;
public class Player {
private Node playerNode = new Node();
public Player(AssetManager assetManager) {
Geometry box = GeometryUtil.createBox(assetManager, 0.5f,0.7f,0.5f, ColorRGBA.Red);
box.setLocalTranslation(0.25f, 0.7f, 0.25f);
playerNode.attachChild(box);
}
public void updatePlayer() {
}
public Spatial getGfxNode() {
return playerNode;
}
}

View file

@ -1,24 +1,78 @@
package se.cookery.gfx.terrain;
import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.scene.Mesh;
import com.jme3.scene.VertexBuffer;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/**
* This terrain generator generates a simple terrain, similar to the basic
* terrain used in my previous tutorials. The difference here is in the shader
* programs, with the use of the "flat" type qualifier when passing the colour
* from vertex shader to fragment shader. This stops the colour being
* interpolated over each triangle, and instead the entire triangle uses the
* colour value from just one of the vertices, the "provoking" vertex. This is
* by default the last specified vertex of the triangle, but it's possible to
* make it use the first vertex instead.
*
* @author Karl
* Class will generate a terrain mesh from a TerrainMap
*/
public class TerrainMesh {
public class TerrainMesh extends Mesh {
private float width;
private float height;
private int widthCount;
private int heightCount;
/**
* Created a terrain Mesh with the given sizes.
*
* @param width is the width of a single square
* @param height is the height of a single square
* @param widthCount the number of squares that make up the whole terrain
* @param heightCount the number of squares that make up the whole terrain
*/
public TerrainMesh(float width, float height, int widthCount, int heightCount){
this.width = 1;
this.height = 1;
updateGeometry();
}
protected void updateGeometry() {
// Setup vertexes
setBuffer(VertexBuffer.Type.Position, 3, new float[]{
// x, y, z
0, 0, 0,
0, 0, height,
width, 0, height,
width, 0, 0,
});
// Setup planes
setBuffer(VertexBuffer.Type.Index, 3, new short[]{
0, 1, 3,
3, 1, 2
});
// Setup texture UV coordinates
setBuffer(VertexBuffer.Type.TexCoord, 2, new float[]{
// u, v
0, 0,
1, 0,
1, 1,
0, 1
});
// Setup light normals
setBuffer(VertexBuffer.Type.Normal, 3, new float[]{
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1
});
updateBound();
setStatic();
}
}

View file

@ -0,0 +1,16 @@
package se.cookery.gfx.util;
import com.jme3.asset.AssetManager;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
public class GeometryUtil {
public static Geometry createBox(AssetManager assetManager, float x, float y, float z, ColorRGBA color) {
Geometry box = new Geometry("Box", new Box(x,y,z));
box.setMaterial(MaterialUtil.createMaterial(assetManager, color));
return box;
}
}

View file

@ -0,0 +1,15 @@
package se.cookery.gfx.util;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
public class MaterialUtil {
public static Material createMaterial(AssetManager assetManager, ColorRGBA color) {
Material basicMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
basicMaterial.setColor("Color", color);
return basicMaterial;
}
}