package org.atriaSoft.gameEngine.sample.LoxelEngine; import org.atriaSoft.etk.Color; import org.atriaSoft.etk.Uri; import org.atriaSoft.etk.math.Matrix4f; import org.atriaSoft.etk.math.Quaternion; import org.atriaSoft.etk.math.Transform3D; import org.atriaSoft.etk.math.Vector2f; import org.atriaSoft.etk.math.Vector3f; import org.atriaSoft.gale.Application; import org.atriaSoft.gale.backend3d.OpenGL; import org.atriaSoft.gale.backend3d.OpenGL.Flag; import org.atriaSoft.gale.context.Context; import org.atriaSoft.gameEngine.ControlCameraSimple; import org.atriaSoft.gameEngine.Entity; import org.atriaSoft.gameEngine.Environement; import org.atriaSoft.gameEngine.GameStatus; import org.atriaSoft.gameEngine.Light; import org.atriaSoft.gameEngine.Material; import org.atriaSoft.gameEngine.camera.Camera; import org.atriaSoft.gameEngine.components.ComponentLight; import org.atriaSoft.gameEngine.components.ComponentLightSun; import org.atriaSoft.gameEngine.components.ComponentMaterial; import org.atriaSoft.gameEngine.components.ComponentPosition; import org.atriaSoft.gameEngine.components.ComponentRenderColoredStaticMesh; import org.atriaSoft.gameEngine.components.ComponentRenderTexturedMaterialsStaticMesh; import org.atriaSoft.gameEngine.components.ComponentRenderTexturedStaticMesh; import org.atriaSoft.gameEngine.components.ComponentStaticMesh; import org.atriaSoft.gameEngine.components.ComponentTexture; import org.atriaSoft.gameEngine.engines.EngineLight; import org.atriaSoft.gameEngine.map.MapVoxel; import org.atriaSoft.gameEngine.tools.MeshGenerator; import org.atriaSoft.gale.key.KeyKeyboard; import org.atriaSoft.gale.key.KeySpecial; import org.atriaSoft.gale.key.KeyStatus; import org.atriaSoft.gale.key.KeyType; public class LoxelApplication extends Application { private Environement env; private ComponentPosition objectPosition; private Quaternion basicRotation = Quaternion.identity(); private Quaternion basicRotation2 = Quaternion.identity(); private boolean creationDone; private ControlCameraSimple simpleControl; private Material materialCube; private ComponentPosition lightPosition; private float angleLight = 0; private MapVoxel map; public LoxelApplication(){ creationDone = false; } @Override public void onCreate(Context _context) { env = new Environement(); this.canDraw = true; setSize(new Vector2f(800, 600)); setTitle("Low Poly sample"); map = new MapVoxel(this.env); this.env.addEngine(map); map.init(); // simple sun to have a global light ... Entity sun = new Entity(this.env); sun.addComponent(new ComponentPosition(new Transform3D(new Vector3f(1000,1000,1000)))); sun.addComponent(new ComponentLightSun(new Light(new Vector3f(0.4f,0.4f,0.4f), new Vector3f(0,0,0), new Vector3f(0.8f,0,0)))); env.addEntity(sun); // add a cube to show where in the light ... Entity localLight = new Entity(this.env); lightPosition = new ComponentPosition(new Transform3D(new Vector3f(-10,-10,0))); localLight.addComponent(lightPosition); localLight.addComponent(new ComponentStaticMesh(new Uri("RES", "cube.obj"))); localLight.addComponent(new ComponentTexture(new Uri("RES", "grass.png"))); localLight.addComponent(new ComponentLight(new Light(new Vector3f(0,1,0), new Vector3f(0,0,0), new Vector3f(0.8f,0.03f,0.002f)))); localLight.addComponent(new ComponentRenderTexturedStaticMesh( new Uri("DATA", "basic.vert"), new Uri("DATA", "basic.frag"))); env.addEntity(localLight); Entity gird = new Entity(this.env); gird.addComponent(new ComponentPosition(new Transform3D(new Vector3f(0,0,0)))); gird.addComponent(new ComponentStaticMesh(MeshGenerator.createGrid(5))); gird.addComponent(new ComponentRenderColoredStaticMesh( new Uri("DATA_EGE", "wireColor.vert"), new Uri("DATA_EGE", "wireColor.frag"))); env.addEntity(gird); Entity basicTree = new Entity(this.env); objectPosition = new ComponentPosition(new Transform3D(new Vector3f(0,0,0))); basicTree.addComponent(objectPosition); materialCube = new Material(); basicTree.addComponent(new ComponentMaterial(materialCube)); basicTree.addComponent(new ComponentStaticMesh(new Uri("RES", "cube.obj"))); basicTree.addComponent(new ComponentTexture(new Uri("RES", "grass.png"))); basicTree.addComponent(new ComponentRenderTexturedMaterialsStaticMesh( new Uri("DATA", "basicMaterial.vert"), new Uri("DATA", "basicMaterial.frag"), (EngineLight)env.getEngine(EngineLight.ENGINE_NAME))); env.addEntity(basicTree); Camera mainView = new Camera(); env.addCamera("default", mainView); mainView.setPitch((float)Math.PI*-0.25f); mainView.setPosition(new Vector3f(0,-5,5)); this.simpleControl = new ControlCameraSimple(mainView); env.addControlInterface(simpleControl); // start the engine. env.setPropertyStatus(GameStatus.gameStart); basicRotation.setEulerAngles(new Vector3f(0.005f,0.005f,0.01f)); basicRotation2.setEulerAngles(new Vector3f(0.003f,0.01f,0.001f)); // ready to let Gale & Ege manage the display Log.info("==> Init APPL (END)"); creationDone = true; } @Override public void onRegenerateDisplay(Context context) { //Log.verbose("Regenerate Gale Application"); if (this.creationDone == false) { return; } //materialCube.setAmbientFactor(new Vector3f(1.0f,1.0f,1.0f)); // apply a little rotation to show the element move //objectPosition.getTransform().applyRotation(basicRotation); //objectPosition.getTransform().applyRotation(basicRotation2); angleLight += 0.01; lightPosition.getTransform().getPosition().x = (float)Math.cos(angleLight) * 7.0f; lightPosition.getTransform().getPosition().y = (float)Math.sin(angleLight) * 7.0f; env.periodicCall(); markDrawingIsNeeded(); } @Override public void onDraw(Context _context) { //Log.info("==> appl Draw ..."); Vector2f size = getSize(); if (this.creationDone == false) { OpenGL.setViewPort(new Vector2f(0,0), size); Color bgColor = new Color(0.8f, 0.5f, 0.5f, 1.0f); OpenGL.clearColor(bgColor); return; } // Store openGl context. OpenGL.push(); // set projection matrix: Matrix4f tmpProjection = Matrix4f.createMatrixPerspective(3.14f*0.5f, getAspectRatio(), 0.1f, 50000); OpenGL.setMatrix(tmpProjection); // set the basic openGL view port: (Draw in all the windows...) OpenGL.setViewPort(new Vector2f(0,0), size); // clear background Color bgColor = new Color(0.0f, 1.0f, 0.0f, 1.0f); OpenGL.clearColor(bgColor); // real clear request: OpenGL.clear(OpenGL.ClearFlag.clearFlag_colorBuffer); OpenGL.clear(OpenGL.ClearFlag.clearFlag_depthBuffer); OpenGL.enable(Flag.flag_depthTest); env.render(20, "default"); // Restore context of matrix OpenGL.pop(); } @Override public void onPointer(KeySpecial special, KeyType type, int pointerID, Vector2f pos, KeyStatus state) { env.onPointer(special, type, pointerID, pos, state); } @Override public void onKeyboard(KeySpecial special, KeyKeyboard type, Character value, KeyStatus state) { env.onKeyboard(special, type, value, state); } }