Basic terrain and and a box setup

This commit is contained in:
Ziver Koc 2020-04-03 23:08:07 +02:00
parent d563f539a5
commit ea19542171
2 changed files with 88 additions and 13 deletions

View file

@ -3,18 +3,26 @@ package se.cookery;
import com.jme3.app.SimpleApplication; import com.jme3.app.SimpleApplication;
import com.jme3.material.Material; import com.jme3.material.Material;
import com.jme3.math.ColorRGBA; 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.TerrainLodControl;
import com.jme3.terrain.geomipmap.TerrainQuad; import com.jme3.terrain.geomipmap.TerrainQuad;
import com.jme3.terrain.heightmap.AbstractHeightMap;
import se.cookery.core.world.Block; import se.cookery.core.world.Block;
import se.cookery.core.world.gen.GrassLandWorldGenerator; import se.cookery.core.world.gen.GrassLandWorldGenerator;
import se.cookery.gfx.util.WireFrameProcessor;
import zutil.log.CompactLogFormatter;
import zutil.log.LogUtil;
public class CookeryClient extends SimpleApplication { public class CookeryClient extends SimpleApplication {
private TerrainQuad terrain; private TerrainQuad terrain;
@Override @Override
public void simpleInitApp() { public void simpleInitApp() {
flyCam.setMoveSpeed(50); flyCam.setMoveSpeed(30);
cam.setLocation(new Vector3f(0,20,-10));
cam.lookAt(new Vector3f(0f,0f,0f), Vector3f.UNIT_Z);
/** 3. We have prepared material and heightmap. /** 3. We have prepared material and heightmap.
* Now we create the actual terrain: * Now we create the actual terrain:
@ -24,7 +32,7 @@ public class CookeryClient extends SimpleApplication {
* 3.4) As LOD step scale we supply Vector3f(1,1,1). * 3.4) As LOD step scale we supply Vector3f(1,1,1).
* 3.5) We supply the prepared heightmap itself. * 3.5) We supply the prepared heightmap itself.
*/ */
int patchSize = 65;
Block block = new GrassLandWorldGenerator().generateBlock(0, 0); 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];
@ -33,18 +41,23 @@ public class CookeryClient extends SimpleApplication {
heightMap[Block.BLOCK_SIZE * x + y] = block.getHeight(x, y); heightMap[Block.BLOCK_SIZE * x + y] = block.getHeight(x, y);
} }
} }
heightMap[Block.BLOCK_SIZE*Block.BLOCK_SIZE/2+Block.BLOCK_SIZE/2] = 1;
terrain = new TerrainQuad("Ground", Block.BLOCK_SIZE+1, Block.BLOCK_SIZE+1, heightMap);
terrain = new TerrainQuad("Ground", patchSize, Block.BLOCK_SIZE+1, heightMap); Material green = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
green.setColor("Color", ColorRGBA.Green);
Material mat_terrain = new Material(); terrain.setMaterial(green);
mat_terrain.setColor("Green", ColorRGBA.Green);
/** 4. We give the terrain its material, position & scale it, and attach it. */
terrain.setMaterial(mat_terrain);
rootNode.attachChild(terrain); rootNode.attachChild(terrain);
/** 5. The LOD (level of detail) depends on were the camera is: */ Geometry box = new Geometry("Box", new Box(1,1,1));
TerrainLodControl control = new TerrainLodControl(terrain, getCamera()); Material red = new Material(assetManager,
terrain.addControl(control); "Common/MatDefs/Misc/Unshaded.j3md");
red.setColor("Color", ColorRGBA.Red);
box.setMaterial(red);
box.setLocalTranslation(0.5f, 0.5f, 0.5f);
rootNode.attachChild(box);
// Wireframe mode
viewPort.addProcessor(new WireFrameProcessor(assetManager));
} }
} }

View file

@ -0,0 +1,62 @@
package se.cookery.gfx.util;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.post.SceneProcessor;
import com.jme3.profile.AppProfiler;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.texture.FrameBuffer;
/**
* A processor that will force wireframe material on all objects.
* <br>
* Usage: <pre>viewPort.addProcessor(new WireFrameProcessor(assetManager));</pre>
*/
public class WireFrameProcessor implements SceneProcessor {
RenderManager renderManager;
Material wireMaterial;
public WireFrameProcessor(AssetManager manager) {
wireMaterial = new Material(manager, "/Common/MatDefs/Misc/Unshaded.j3md");
wireMaterial.getAdditionalRenderState().setWireframe(true);
wireMaterial.setColor("Color", ColorRGBA.Blue);
}
@Override
public void initialize(RenderManager rm, ViewPort vp) {
renderManager = rm;
}
@Override
public void reshape(ViewPort vp, int w, int h) { }
@Override
public boolean isInitialized() {
return renderManager != null;
}
@Override
public void preFrame(float tpf) { }
@Override
public void postQueue(RenderQueue rq) {
renderManager.setForcedMaterial(wireMaterial);
}
@Override
public void postFrame(FrameBuffer out) {
renderManager.setForcedMaterial(null);
}
@Override
public void cleanup() {
renderManager.setForcedMaterial(null);
}
@Override
public void setProfiler(AppProfiler profiler) { }
}