From ea19542171c7c017b55fc2d3ad2bee22341439d8 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Fri, 3 Apr 2020 23:08:07 +0200 Subject: [PATCH] Basic terrain and and a box setup --- core/src/se/cookery/CookeryClient.java | 39 ++++++++---- .../cookery/gfx/util/WireFrameProcessor.java | 62 +++++++++++++++++++ 2 files changed, 88 insertions(+), 13 deletions(-) create mode 100644 core/src/se/cookery/gfx/util/WireFrameProcessor.java diff --git a/core/src/se/cookery/CookeryClient.java b/core/src/se/cookery/CookeryClient.java index 4a42605..8567160 100644 --- a/core/src/se/cookery/CookeryClient.java +++ b/core/src/se/cookery/CookeryClient.java @@ -3,18 +3,26 @@ 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 com.jme3.terrain.heightmap.AbstractHeightMap; import se.cookery.core.world.Block; 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 { private TerrainQuad terrain; @Override 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. * 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.5) We supply the prepared heightmap itself. */ - int patchSize = 65; + Block block = new GrassLandWorldGenerator().generateBlock(0, 0); 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*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 mat_terrain = new Material(); - mat_terrain.setColor("Green", ColorRGBA.Green); - - /** 4. We give the terrain its material, position & scale it, and attach it. */ - terrain.setMaterial(mat_terrain); + Material green = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); + green.setColor("Color", ColorRGBA.Green); + terrain.setMaterial(green); rootNode.attachChild(terrain); - /** 5. The LOD (level of detail) depends on were the camera is: */ - TerrainLodControl control = new TerrainLodControl(terrain, getCamera()); - terrain.addControl(control); + 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); + + // Wireframe mode + viewPort.addProcessor(new WireFrameProcessor(assetManager)); } } \ No newline at end of file diff --git a/core/src/se/cookery/gfx/util/WireFrameProcessor.java b/core/src/se/cookery/gfx/util/WireFrameProcessor.java new file mode 100644 index 0000000..ec006fa --- /dev/null +++ b/core/src/se/cookery/gfx/util/WireFrameProcessor.java @@ -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. + *
+ * Usage:
viewPort.addProcessor(new WireFrameProcessor(assetManager));
+ */ +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) { } +} \ No newline at end of file