diff --git a/src/org/atriaSoft/gale/resource/Resource.java b/src/org/atriaSoft/gale/resource/Resource.java index e783431..8487b9a 100644 --- a/src/org/atriaSoft/gale/resource/Resource.java +++ b/src/org/atriaSoft/gale/resource/Resource.java @@ -8,12 +8,13 @@ import org.atriaSoft.gale.Log; import org.atriaSoft.gale.context.Context; public abstract class Resource { + protected static String NO_NAME_RESOURCE = "---"; protected static int MAXRESOURCELEVEL = 5; private static int idGenerated = 10; protected long uid = -1; //!< unique ID definition protected int count = 1; protected int resourceLevel = MAXRESOURCELEVEL-1; //!< Level of the resource ==> for update priority [0..5] 0 must be update first. - protected String name; //!< name of the resource ... + protected String name = NO_NAME_RESOURCE; //!< name of the resource ... protected boolean resourceHasBeenInit = false; //!< Know if the init function has bben called protected List listType = new ArrayList(); /** diff --git a/src/org/atriaSoft/gale/resource/ResourceManager.java b/src/org/atriaSoft/gale/resource/ResourceManager.java index 5ff92ce..0d53771 100644 --- a/src/org/atriaSoft/gale/resource/ResourceManager.java +++ b/src/org/atriaSoft/gale/resource/ResourceManager.java @@ -161,10 +161,18 @@ public class ResourceManager { public Resource localKeep(String filename) { Log.verbose("KEEP (DEFAULT) : file : '" + filename + "' in " + this.resourceList.size() + " resources"); for (Resource it : this.resourceList) { - if (it != null) { - if (it.getName() == filename) { - return it; - } + if (it == null) { + continue; + } + if (it.getName() == null) { + continue; + } + //Log.verbose("compare : " + filename + " ==???== " + it.getName()); + if (it.getName().contentEquals(Resource.NO_NAME_RESOURCE)) { + continue; + } + if (it.getName().contentEquals(filename)) { + return it; } } return null; diff --git a/src/org/atriaSoft/gale/resource/ResourceTexture.java b/src/org/atriaSoft/gale/resource/ResourceTexture.java index 551c586..3688bdb 100644 --- a/src/org/atriaSoft/gale/resource/ResourceTexture.java +++ b/src/org/atriaSoft/gale/resource/ResourceTexture.java @@ -176,7 +176,7 @@ public class ResourceTexture extends Resource { } GL13.glActiveTexture(textureIdBinding[idTexture]); GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.texId); - if (dataColorSpace == TextureColorMode.rgba) { + if (dataColorSpace == TextureColorMode.rgb) { OpenGL.enable(OpenGL.Flag.flag_cullFace); OpenGL.enable(OpenGL.Flag.flag_back); } @@ -185,7 +185,7 @@ public class ResourceTexture extends Resource { if (this.loaded == false) { return; } - if (dataColorSpace == TextureColorMode.rgba) { + if (dataColorSpace == TextureColorMode.rgb) { OpenGL.disable(OpenGL.Flag.flag_cullFace); OpenGL.disable(OpenGL.Flag.flag_back); } diff --git a/src/org/atriaSoft/gameEngine/Environement.java b/src/org/atriaSoft/gameEngine/Environement.java index 474b78f..6755f8d 100644 --- a/src/org/atriaSoft/gameEngine/Environement.java +++ b/src/org/atriaSoft/gameEngine/Environement.java @@ -17,6 +17,7 @@ import org.atriaSoft.gale.key.KeyType; import org.atriaSoft.gameEngine.camera.Camera; import org.atriaSoft.gameEngine.engines.EngineRender; import org.atriaSoft.gameEngine.engines.EngineAI; +import org.atriaSoft.gameEngine.engines.EngineDynamicMeshs; import org.atriaSoft.gameEngine.engines.EngineLight; import org.atriaSoft.gameEngine.engines.EngineParticle; import org.atriaSoft.gameEngine.engines.EnginePhysics; @@ -26,7 +27,7 @@ public class Environement { public Signal signalPlayTimeChange = new Signal(); private GameStatus propertyStatus = GameStatus.gameStop; // !< the display is running (not in pause) public float propertyRatio = 1.0f; // !< Speed ratio - protected List engine = new ArrayList(); // !< EGE sub engine interface (like physique, rendering, + protected List engines = new ArrayList(); // !< EGE sub engine interface (like physique, rendering, // audio, ...). private List listEntity = new ArrayList(); // !< List of all entity added in the Game List controls = new ArrayList(); @@ -42,6 +43,7 @@ public class Environement { // we add the 4 classical engines (the order is used to the global rendering cycle ... addEngine(new EnginePhysics(this)); addEngine(new EngineAI(this)); + addEngine(new EngineDynamicMeshs(this)); addEngine(new EngineRender(this)); addEngine(new EngineParticle(this)); addEngine(new EngineLight(this)); @@ -81,31 +83,31 @@ public class Environement { return; } // check if not exist - for (Engine it : engine) { + for (Engine it : engines) { if (it.getType().contains(ref.getType())) { it = ref; return; } } // add it at the end ... - engine.add(ref); + engines.add(ref); } public void rmEngine(Engine ref) { - engine.remove(ref); + engines.remove(ref); } public void rmEngine(String type) { - for (Engine it : engine) { + for (Engine it : engines) { if (it.getType().contains(type)) { - engine.remove(it); + engines.remove(it); return; } } } public Engine getEngine(String type) { - for (Engine it : engine) { + for (Engine it : engines) { if (it.getType().contains(type)) { return it; } @@ -115,7 +117,7 @@ public class Environement { } public void engineComponentRemove(Component ref) { - for (Engine it: engine) { + for (Engine it: engines) { if (it.getType().contentEquals(ref.getType())) { it.componentRemove(ref); return; @@ -124,7 +126,7 @@ public class Environement { } public void engineComponentAdd(Component ref) { - for (Engine it: engine) { + for (Engine it: engines) { if (it.getType().contentEquals(ref.getType())) { it.componentAdd(ref); return; @@ -140,7 +142,7 @@ public class Environement { return; } OpenGL.setCameraMatrix(camera.getConvertionMatrix()); - for (Engine it: engine) { + for (Engine it: engines) { //Log.verbose(" render: " + it.getType()); it.render(deltaMilli, camera); } @@ -408,6 +410,8 @@ public class Environement { EventTime event = new EventTime(lastCallTime, lastCallTime-startTime, lastCallTime-lastUpdate, lastCallTime-lastUpdate); for (ControlInterface elem : controls) { elem.periodicCall(event); + }for (Engine engine : engines) { + engine.update((lastCallTime-lastUpdate)/100); } } } diff --git a/src/org/atriaSoft/gameEngine/components/ComponentDynamicMeshs.java b/src/org/atriaSoft/gameEngine/components/ComponentDynamicMeshs.java new file mode 100644 index 0000000..f9444db --- /dev/null +++ b/src/org/atriaSoft/gameEngine/components/ComponentDynamicMeshs.java @@ -0,0 +1,65 @@ +package org.atriaSoft.gameEngine.components; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.atriaSoft.etk.Uri; +import org.atriaSoft.gameEngine.Component; +import org.atriaSoft.gameEngine.engines.EngineDynamicMeshs; +import org.atriaSoft.gameEngine.resource.ResourceStaticMesh; +import org.atriaSoft.gameEngine.resource.ResourceStaticMeshObj; + +public class ComponentDynamicMeshs extends Component { + protected Map meshs = new HashMap(); + + @Override + public String getType() { + // TODO Auto-generated method stub + return EngineDynamicMeshs.ENGINE_NAME; + } + + public ComponentDynamicMeshs() { + + } + + public Set getKeys() { + return meshs.keySet(); + } + + public void setMesh(String name, ResourceStaticMesh mesh) { + this.meshs.put(name, mesh); + } + + public ResourceStaticMesh getMesh(String name) { + return meshs.get(name); + } + + public void bindForRendering(String name) { + ResourceStaticMesh mesh = meshs.get(name); + if (mesh == null) { + return; + } + mesh.bindForRendering(); + } + + public void unBindForRendering(String name) { + ResourceStaticMesh mesh = meshs.get(name); + if (mesh == null) { + return; + } + mesh.unBindForRendering(); + } + + public void render(String name) { + ResourceStaticMesh mesh = meshs.get(name); + if (mesh == null) { + return; + } + mesh.render(); + } + + public void update(float timeStep) {} + + +} diff --git a/src/org/atriaSoft/gameEngine/components/ComponentMap.java b/src/org/atriaSoft/gameEngine/components/ComponentMap.java new file mode 100644 index 0000000..a92d5f8 --- /dev/null +++ b/src/org/atriaSoft/gameEngine/components/ComponentMap.java @@ -0,0 +1,16 @@ +package org.atriaSoft.gameEngine.components; + +import org.atriaSoft.gameEngine.Component; +import org.atriaSoft.gameEngine.engines.EngineMap; + +public class ComponentMap extends Component { + + @Override + public String getType() { + // TODO Auto-generated method stub + return EngineMap.ENGINE_NAME; + } + + public void update(float timeStep) {}; + +} diff --git a/src/org/atriaSoft/gameEngine/components/ComponentMaterials.java b/src/org/atriaSoft/gameEngine/components/ComponentMaterials.java new file mode 100644 index 0000000..573b151 --- /dev/null +++ b/src/org/atriaSoft/gameEngine/components/ComponentMaterials.java @@ -0,0 +1,31 @@ +package org.atriaSoft.gameEngine.components; + +import java.util.HashMap; +import java.util.Map; + +import org.atriaSoft.gameEngine.Component; +import org.atriaSoft.gameEngine.Material; + +public class ComponentMaterials extends Component { + // the material is not a resource, it can change in time... with AI or selection... + private Map materials = new HashMap(); + + public ComponentMaterials(Material material) { + super(); + + } + public ComponentMaterials() { + super(); + } + @Override + public String getType() { + return "materials"; + } + public Material getMaterial(String name) { + return materials.get(name); + } + public void setMaterial(String name, Material material) { + this.materials.put(name, material); + } + +} diff --git a/src/org/atriaSoft/gameEngine/components/ComponentRender.java b/src/org/atriaSoft/gameEngine/components/ComponentRender.java index fb17489..23d290f 100644 --- a/src/org/atriaSoft/gameEngine/components/ComponentRender.java +++ b/src/org/atriaSoft/gameEngine/components/ComponentRender.java @@ -18,4 +18,6 @@ public abstract class ComponentRender extends Component { return this.propertyDebugNormal; } public abstract void render(); + public void update(float timeStep) {}; + } diff --git a/src/org/atriaSoft/gameEngine/components/ComponentRenderTexturedMaterialsDynamicMeshs.java b/src/org/atriaSoft/gameEngine/components/ComponentRenderTexturedMaterialsDynamicMeshs.java new file mode 100644 index 0000000..8aa0eaf --- /dev/null +++ b/src/org/atriaSoft/gameEngine/components/ComponentRenderTexturedMaterialsDynamicMeshs.java @@ -0,0 +1,115 @@ +package org.atriaSoft.gameEngine.components; + +import java.util.Set; + +import org.atriaSoft.etk.Uri; +import org.atriaSoft.etk.math.Matrix4f; +import org.atriaSoft.etk.math.Vector3f; +import org.atriaSoft.gale.backend3d.OpenGL; +import org.atriaSoft.gale.resource.ResourceProgram; +import org.atriaSoft.gameEngine.Component; +import org.atriaSoft.gameEngine.Light; +import org.atriaSoft.gameEngine.Material; +import org.atriaSoft.gameEngine.engines.EngineLight; + +public class ComponentRenderTexturedMaterialsDynamicMeshs extends ComponentRender { + private static final int numberOfLight = 8; + ComponentDynamicMeshs meshs = null; + ComponentTextures textures = null; + ComponentMaterials materials = null; + ComponentPosition position = null; + ResourceProgram program = null; + EngineLight lightEngine; + private int GLMatrixTransformation; + private int GLMatrixProjection; + private int GLMatrixView; + private int GLambientFactor; + private int GLdiffuseFactor; + private int GLspecularFactor; + private int GLshininess; + private GlLightIndex[] GLlights; + + public ComponentRenderTexturedMaterialsDynamicMeshs(Uri vertexShader, Uri fragmentShader, EngineLight lightEngine) { + this.lightEngine = lightEngine; + this.program = ResourceProgram.create(vertexShader, fragmentShader); + if (this.program != null) { + this.GLMatrixTransformation = this.program.getUniform("in_matrixTransformation"); + this.GLMatrixProjection = this.program.getUniform("in_matrixProjection"); + this.GLMatrixView = this.program.getUniform("in_matrixView"); + this.GLambientFactor = this.program.getUniform("in_material.ambientFactor"); + this.GLdiffuseFactor = this.program.getUniform("in_material.diffuseFactor"); + this.GLspecularFactor = this.program.getUniform("in_material.specularFactor"); + this.GLshininess = this.program.getUniform("in_material.shininess"); + this.GLlights = new GlLightIndex[numberOfLight]; + for (int iii=0; iii keys = this.meshs.getKeys(); + + for (int iii=0; iii need update before drawing... + OpenGL.updateAllFlags(); + // Request the draw all the elements: + this.meshs.render(key); + this.textures.unBindForRendering(key); + this.meshs.unBindForRendering(key); + } + this.program.unUse(); + } +} + diff --git a/src/org/atriaSoft/gameEngine/components/ComponentRenderTexturedMaterialsStaticMesh.java b/src/org/atriaSoft/gameEngine/components/ComponentRenderTexturedMaterialsStaticMesh.java index f4d4b8b..bba326e 100644 --- a/src/org/atriaSoft/gameEngine/components/ComponentRenderTexturedMaterialsStaticMesh.java +++ b/src/org/atriaSoft/gameEngine/components/ComponentRenderTexturedMaterialsStaticMesh.java @@ -9,16 +9,6 @@ import org.atriaSoft.gameEngine.Component; import org.atriaSoft.gameEngine.Light; import org.atriaSoft.gameEngine.Material; import org.atriaSoft.gameEngine.engines.EngineLight; -class GlLightIndex { - int GLcolor; - int GLposition; - int GLattenuation; - public GlLightIndex(int gLcolor, int gLposition, int gLattenuation) { - GLcolor = gLcolor; - GLposition = gLposition; - GLattenuation = gLattenuation; - } -} public class ComponentRenderTexturedMaterialsStaticMesh extends ComponentRender { private static final int numberOfLight = 8; ComponentStaticMesh mesh = null; diff --git a/src/org/atriaSoft/gameEngine/components/ComponentRenderTexturedMaterialsStaticMeshs.java b/src/org/atriaSoft/gameEngine/components/ComponentRenderTexturedMaterialsStaticMeshs.java new file mode 100644 index 0000000..df105ec --- /dev/null +++ b/src/org/atriaSoft/gameEngine/components/ComponentRenderTexturedMaterialsStaticMeshs.java @@ -0,0 +1,115 @@ +package org.atriaSoft.gameEngine.components; + +import java.util.Set; + +import org.atriaSoft.etk.Uri; +import org.atriaSoft.etk.math.Matrix4f; +import org.atriaSoft.etk.math.Vector3f; +import org.atriaSoft.gale.backend3d.OpenGL; +import org.atriaSoft.gale.resource.ResourceProgram; +import org.atriaSoft.gameEngine.Component; +import org.atriaSoft.gameEngine.Light; +import org.atriaSoft.gameEngine.Material; +import org.atriaSoft.gameEngine.engines.EngineLight; + +public class ComponentRenderTexturedMaterialsStaticMeshs extends ComponentRender { + private static final int numberOfLight = 8; + ComponentStaticMeshs meshs = null; + ComponentTextures textures = null; + ComponentMaterials materials = null; + ComponentPosition position = null; + ResourceProgram program = null; + EngineLight lightEngine; + private int GLMatrixTransformation; + private int GLMatrixProjection; + private int GLMatrixView; + private int GLambientFactor; + private int GLdiffuseFactor; + private int GLspecularFactor; + private int GLshininess; + private GlLightIndex[] GLlights; + + public ComponentRenderTexturedMaterialsStaticMeshs(Uri vertexShader, Uri fragmentShader, EngineLight lightEngine) { + this.lightEngine = lightEngine; + this.program = ResourceProgram.create(vertexShader, fragmentShader); + if (this.program != null) { + this.GLMatrixTransformation = this.program.getUniform("in_matrixTransformation"); + this.GLMatrixProjection = this.program.getUniform("in_matrixProjection"); + this.GLMatrixView = this.program.getUniform("in_matrixView"); + this.GLambientFactor = this.program.getUniform("in_material.ambientFactor"); + this.GLdiffuseFactor = this.program.getUniform("in_material.diffuseFactor"); + this.GLspecularFactor = this.program.getUniform("in_material.specularFactor"); + this.GLshininess = this.program.getUniform("in_material.shininess"); + this.GLlights = new GlLightIndex[numberOfLight]; + for (int iii=0; iii keys = this.meshs.getKeys(); + + for (int iii=0; iii need update before drawing... + OpenGL.updateAllFlags(); + // Request the draw all the elements: + this.meshs.render(key); + this.textures.unBindForRendering(key); + this.meshs.unBindForRendering(key); + } + this.program.unUse(); + } +} + diff --git a/src/org/atriaSoft/gameEngine/components/ComponentStaticMeshs.java b/src/org/atriaSoft/gameEngine/components/ComponentStaticMeshs.java new file mode 100644 index 0000000..b5351df --- /dev/null +++ b/src/org/atriaSoft/gameEngine/components/ComponentStaticMeshs.java @@ -0,0 +1,67 @@ +package org.atriaSoft.gameEngine.components; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.atriaSoft.etk.Uri; +import org.atriaSoft.gameEngine.Log; +import org.atriaSoft.gameEngine.Component; +import org.atriaSoft.gameEngine.resource.ResourceStaticMesh; +import org.atriaSoft.gameEngine.resource.ResourceStaticMeshObj; + +public class ComponentStaticMeshs extends Component { + private Map meshs = new HashMap(); + + @Override + public String getType() { + // TODO Auto-generated method stub + return "static-meshs"; + } + + public ComponentStaticMeshs(Uri meshUrl) { + // TODO : load Mesh + Log.critical("Can not Load the Mesh for now ... " + meshUrl); + ResourceStaticMeshObj mesh = ResourceStaticMeshObj.create(meshUrl); + setMesh("default", mesh); + } + public ComponentStaticMeshs() { + // nothing to do ... + } + + public Set getKeys() { + return meshs.keySet(); + } + + public void setMesh(String name, ResourceStaticMesh mesh) { + this.meshs.put(name, mesh); + } + + public ResourceStaticMesh getMesh(String name) { + return meshs.get(name); + } + + public void bindForRendering(String name) { + ResourceStaticMesh mesh = meshs.get(name); + if (mesh == null) { + return; + } + mesh.bindForRendering(); + } + + public void unBindForRendering(String name) { + ResourceStaticMesh mesh = meshs.get(name); + if (mesh == null) { + return; + } + mesh.unBindForRendering(); + } + + public void render(String name) { + ResourceStaticMesh mesh = meshs.get(name); + if (mesh == null) { + return; + } + mesh.render(); + } +} diff --git a/src/org/atriaSoft/gameEngine/components/ComponentTextures.java b/src/org/atriaSoft/gameEngine/components/ComponentTextures.java new file mode 100644 index 0000000..0e0bfa6 --- /dev/null +++ b/src/org/atriaSoft/gameEngine/components/ComponentTextures.java @@ -0,0 +1,40 @@ +package org.atriaSoft.gameEngine.components; + +import java.util.HashMap; +import java.util.Map; + +import org.atriaSoft.etk.Uri; +import org.atriaSoft.gale.resource.ResourceTexture; +import org.atriaSoft.gale.test.sample2.Log; +import org.atriaSoft.gameEngine.Component; +import org.atriaSoft.gameEngine.Material; + +public class ComponentTextures extends Component { + private Map textures = new HashMap(); + @Override + public String getType() { + // TODO Auto-generated method stub + return "textures"; + } + public ComponentTextures() { + + } + public void setTexture(String name, Uri textureName) { + ResourceTexture texture = ResourceTexture.createFromPng(textureName); + if (texture == null) { + Log.error("can not instanciate Texture ..."); + return; + } + textures.put(name, texture); + } + + + public void bindForRendering(String name) { + this.textures.get(name).bindForRendering(0); + + } + public void unBindForRendering(String name) { + this.textures.get(name).unBindForRendering(); + } + +} diff --git a/src/org/atriaSoft/gameEngine/components/GlLightIndex.java b/src/org/atriaSoft/gameEngine/components/GlLightIndex.java new file mode 100644 index 0000000..8d4a230 --- /dev/null +++ b/src/org/atriaSoft/gameEngine/components/GlLightIndex.java @@ -0,0 +1,12 @@ +package org.atriaSoft.gameEngine.components; + +public class GlLightIndex { + int GLcolor; + int GLposition; + int GLattenuation; + public GlLightIndex(int gLcolor, int gLposition, int gLattenuation) { + GLcolor = gLcolor; + GLposition = gLposition; + GLattenuation = gLattenuation; + } +} diff --git a/src/org/atriaSoft/gameEngine/engines/EngineAI.java b/src/org/atriaSoft/gameEngine/engines/EngineAI.java index fa60cb4..1a9ef52 100644 --- a/src/org/atriaSoft/gameEngine/engines/EngineAI.java +++ b/src/org/atriaSoft/gameEngine/engines/EngineAI.java @@ -13,7 +13,7 @@ public class EngineAI extends Engine { public static final String ENGINE_NAME = "ia"; private float accumulator = 0; private static float TIME_STEP = 5.0f; - private Vector components; + private Vector components = new Vector(); public EngineAI(Environement env) { super(env); // TODO Auto-generated constructor stub @@ -38,7 +38,7 @@ public class EngineAI extends Engine { accumulator += (float)deltaMili*0.0001f; // While there is enough accumulated time to take one or several physics steps while (accumulator >= TIME_STEP) { - Log.warning("Generate for " + accumulator + " / " + TIME_STEP + " for:" + components.size()); + //Log.warning("AI: Generate for " + accumulator + " / " + TIME_STEP + " for:" + components.size()); // call every object to usdate their constant forces applyed for (ComponentAI it: components) { it.update(TIME_STEP); diff --git a/src/org/atriaSoft/gameEngine/engines/EngineDynamicMeshs.java b/src/org/atriaSoft/gameEngine/engines/EngineDynamicMeshs.java new file mode 100644 index 0000000..70176a4 --- /dev/null +++ b/src/org/atriaSoft/gameEngine/engines/EngineDynamicMeshs.java @@ -0,0 +1,69 @@ +package org.atriaSoft.gameEngine.engines; + +import java.util.Vector; + +import org.atriaSoft.gameEngine.Component; +import org.atriaSoft.gameEngine.Engine; +import org.atriaSoft.gameEngine.Environement; +import org.atriaSoft.gameEngine.camera.Camera; +import org.atriaSoft.gameEngine.components.ComponentDynamicMeshs; + +public class EngineDynamicMeshs extends Engine { + public static final String ENGINE_NAME = "dynamic-meshs"; + private static float TIME_STEP = 5.0f; + private float accumulator = TIME_STEP; + private Vector components = new Vector(); + public EngineDynamicMeshs(Environement env) { + super(env); + // TODO Auto-generated constructor stub + } + + @Override + public void componentRemove(Component ref) { + components.remove(ref); + } + + @Override + public void componentAdd(Component ref) { + if (ref instanceof ComponentDynamicMeshs == true) { + components.add((ComponentDynamicMeshs)ref); + return; + } + } + + @Override + public void update(long deltaMili) { + + //Log.warning("engine update : " + deltaMili + " " + accumulator + " >= " + TIME_STEP); + // Add the time difference in the accumulator + accumulator += (float)deltaMili*0.0001f; + // While there is enough accumulated time to take one or several physics steps + while (accumulator >= TIME_STEP) { + //Log.warning("Generate for " + accumulator + " / " + TIME_STEP + " for:" + components.size()); + // call every object to usdate their constant forces applyed + for (ComponentDynamicMeshs it: components) { + it.update(TIME_STEP); + } + // Decrease the accumulated time + accumulator -= TIME_STEP; + } + + } + + @Override + public void render(long deltaMili, Camera camera) { + // nothing to do ... + } + + @Override + public void renderDebug(long deltaMili, Camera camera) { + // nothing to do ... + } + + @Override + public String getType() { + // TODO Auto-generated method stub + return ENGINE_NAME; + } + +} diff --git a/src/org/atriaSoft/gameEngine/engines/EngineMap.java b/src/org/atriaSoft/gameEngine/engines/EngineMap.java new file mode 100644 index 0000000..04abb2e --- /dev/null +++ b/src/org/atriaSoft/gameEngine/engines/EngineMap.java @@ -0,0 +1,71 @@ +package org.atriaSoft.gameEngine.engines; + +import java.util.Vector; + +import org.atriaSoft.etk.math.Vector3f; +import org.atriaSoft.gameEngine.Component; +import org.atriaSoft.gameEngine.Engine; +import org.atriaSoft.gameEngine.Environement; +import org.atriaSoft.gameEngine.Light; +import org.atriaSoft.gameEngine.Log; +import org.atriaSoft.gameEngine.camera.Camera; +import org.atriaSoft.gameEngine.components.ComponentAI; +import org.atriaSoft.gameEngine.components.ComponentMap; + +public class EngineMap extends Engine { + public static final String ENGINE_NAME = "map"; + private float accumulator = 0; + private static float TIME_STEP = 5.0f; + private Vector components = new Vector(); + public EngineMap(Environement env) { + super(env); + // TODO Auto-generated constructor stub + } + + @Override + public void componentRemove(Component ref) { + components.remove(ref); + } + + @Override + public void componentAdd(Component ref) { + if (ref instanceof ComponentMap == true) { + components.add((ComponentMap)ref); + return; + } + } + + @Override + public void update(long deltaMili) { + // Add the time difference in the accumulator + accumulator += (float)deltaMili*0.0001f; + // While there is enough accumulated time to take one or several physics steps + while (accumulator >= TIME_STEP) { + // Log.warning("MAP: Generate for " + accumulator + " / " + TIME_STEP + " for:" + components.size()); + // call every object to update their constant forces applied + for (ComponentMap it: components) { + it.update(TIME_STEP); + } + // Decrease the accumulated time + accumulator -= TIME_STEP; + } + + } + + @Override + public void render(long deltaMili, Camera camera) { + // nothing to do ... + } + + @Override + public void renderDebug(long deltaMili, Camera camera) { + // nothing to do ... + } + + @Override + public String getType() { + // TODO Auto-generated method stub + return ENGINE_NAME; + } + +} diff --git a/src/org/atriaSoft/gameEngine/engines/EngineRender.java b/src/org/atriaSoft/gameEngine/engines/EngineRender.java index 14e7aaa..49167ea 100644 --- a/src/org/atriaSoft/gameEngine/engines/EngineRender.java +++ b/src/org/atriaSoft/gameEngine/engines/EngineRender.java @@ -7,6 +7,7 @@ import org.atriaSoft.gameEngine.Component; import org.atriaSoft.gameEngine.Engine; import org.atriaSoft.gameEngine.Environement; import org.atriaSoft.gameEngine.camera.Camera; +import org.atriaSoft.gameEngine.components.ComponentAI; import org.atriaSoft.gameEngine.components.ComponentRender; class ResultNearestElement { @@ -16,6 +17,8 @@ class ResultNearestElement { public class EngineRender extends Engine { public static final String ENGINE_NAME = "render"; + private float accumulator = 0; + private static float TIME_STEP = 5.0f; private Vector components = new Vector(); private Vector displayElementOrdered = new Vector(); //private ResourceColored3DObject debugDrawProperty; @@ -39,7 +42,18 @@ public class EngineRender extends Engine { @Override public void update(long deltaMili) { - // TODO Auto-generated method stub + // Add the time difference in the accumulator + accumulator += (float)deltaMili*0.0001f; + // While there is enough accumulated time to take one or several physics steps + while (accumulator >= TIME_STEP) { + // Log.warning("RENDER: Generate for " + accumulator + " / " + TIME_STEP + " for:" + components.size()); + // call every object to usdate their constant forces applyed + for (ComponentRender it: components) { + it.update(TIME_STEP); + } + // Decrease the accumulated time + accumulator -= TIME_STEP; + } } diff --git a/src/org/atriaSoft/gameEngine/map/ComponentDynamicMeshsVoxelMap.java b/src/org/atriaSoft/gameEngine/map/ComponentDynamicMeshsVoxelMap.java new file mode 100644 index 0000000..10b41a8 --- /dev/null +++ b/src/org/atriaSoft/gameEngine/map/ComponentDynamicMeshsVoxelMap.java @@ -0,0 +1,212 @@ +package org.atriaSoft.gameEngine.map; + +import org.atriaSoft.etk.math.Vector2f; +import org.atriaSoft.etk.math.Vector3f; +import org.atriaSoft.etk.math.Vector3i; +import org.atriaSoft.gale.backend3d.OpenGL.RenderMode; +import org.atriaSoft.gale.test.sample2.Log; +import org.atriaSoft.gameEngine.components.ComponentDynamicMeshs; +import org.atriaSoft.gameEngine.resource.ResourceListTexturedMesh; + +public class ComponentDynamicMeshsVoxelMap extends ComponentDynamicMeshs { + private VoxelChunk chunk; + ResourceListTexturedMesh unbreakable = ResourceListTexturedMesh.create(RenderMode.triangle); + ResourceListTexturedMesh stone = ResourceListTexturedMesh.create(RenderMode.triangle); + ResourceListTexturedMesh dirt = ResourceListTexturedMesh.create(RenderMode.triangle); + ResourceListTexturedMesh grass = ResourceListTexturedMesh.create(RenderMode.triangle); + + public ComponentDynamicMeshsVoxelMap(VoxelChunk chunk) { + super(); + this.chunk = chunk; + this.setMesh("unbreakable", unbreakable); + this.setMesh("stone", stone); + this.setMesh("dirt", dirt); + this.setMesh("grass", grass); + } + + private void drawPlane(Vector3i base, int xxx, int yyy, int zzz, int type) { + //Log.warning("Add plane Z : " + (base.x + xxx) + ", " + (base.y + yyy) + ", " + (base.z + zzz)); + Vector3f v1 = new Vector3f(base.x + xxx , base.y + yyy , base.z + zzz); + Vector3f v2 = new Vector3f(base.x + xxx , base.y + yyy+1, base.z + zzz); + Vector3f v3 = new Vector3f(base.x + xxx+1, base.y + yyy+1, base.z + zzz); + Vector3f v4 = new Vector3f(base.x + xxx+1, base.y + yyy , base.z + zzz); + Vector2f t1 = new Vector2f(0, 0); + Vector2f t2 = new Vector2f(0, 1); + Vector2f t3 = new Vector2f(1, 1); + Vector2f t4 = new Vector2f(1, 0); + Vector3f n1 = new Vector3f(0, 0, -1); + if (type == VoxelType.NATIVE_UNBREAKABLE) { + unbreakable.addQuad(v1, v2, v3, v4, t1, t2, t3, t4, n1); + } else if (type == VoxelType.NATIVE_DIRT) { + dirt.addQuad(v1, v2, v3, v4, t1, t2, t3, t4, n1); + } else if (type == VoxelType.NATIVE_STONE) { + stone.addQuad(v1, v2, v3, v4, t1, t2, t3, t4, n1); + } else if (type == VoxelType.NATIVE_GRASS) { + grass.addQuad(v1, v2, v3, v4, t1, t2, t3, t4, n1); + } + } + private void drawPlane_anti(Vector3i base, int xxx, int yyy, int zzz, int type) { + //Log.warning("Add plane Z : " + (base.x + xxx) + ", " + (base.y + yyy) + ", " + (base.z + zzz)); + Vector3f v1 = new Vector3f(base.x + xxx , base.y + yyy , base.z + zzz); + Vector3f v2 = new Vector3f(base.x + xxx , base.y + yyy+1, base.z + zzz); + Vector3f v3 = new Vector3f(base.x + xxx+1, base.y + yyy+1, base.z + zzz); + Vector3f v4 = new Vector3f(base.x + xxx+1, base.y + yyy , base.z + zzz); + Vector2f t1 = new Vector2f(0, 0); + Vector2f t2 = new Vector2f(0, 1); + Vector2f t3 = new Vector2f(1, 1); + Vector2f t4 = new Vector2f(1, 0); + Vector3f n1 = new Vector3f(0, 0, 1); + if (type == VoxelType.NATIVE_UNBREAKABLE) { + unbreakable.addQuad(v1, v4, v3, v2, t1, t4, t3, t2, n1); + } else if (type == VoxelType.NATIVE_DIRT) { + dirt.addQuad(v1, v4, v3, v2, t1, t4, t3, t2, n1); + } else if (type == VoxelType.NATIVE_STONE) { + stone.addQuad(v1, v4, v3, v2, t1, t4, t3, t2, n1); + } else if (type == VoxelType.NATIVE_GRASS) { + grass.addQuad(v1, v4, v3, v2, t1, t4, t3, t2, n1); + } + } + private void drawPlaneVerticalX(Vector3i base, int xxx, int yyy, int zzz, int type) { + //Log.warning("Add plane X : " + (base.x + xxx) + ", " + (base.y + yyy) + ", " + (base.z + zzz)); + Vector3f v1 = new Vector3f(base.x + xxx , base.y + yyy , base.z + zzz); + Vector3f v2 = new Vector3f(base.x + xxx , base.y + yyy , base.z + zzz+1); + Vector3f v3 = new Vector3f(base.x + xxx , base.y + yyy+1, base.z + zzz+1); + Vector3f v4 = new Vector3f(base.x + xxx , base.y + yyy+1, base.z + zzz); + Vector2f t1 = new Vector2f(0, 0); + Vector2f t2 = new Vector2f(0, 1); + Vector2f t3 = new Vector2f(1, 1); + Vector2f t4 = new Vector2f(1, 0); + Vector3f n1 = new Vector3f(-1, 0, 0); + if (type == VoxelType.NATIVE_UNBREAKABLE) { + unbreakable.addQuad(v1, v2, v3, v4, t1, t2, t3, t4, n1); + } else if (type == VoxelType.NATIVE_DIRT) { + dirt.addQuad(v1, v2, v3, v4, t1, t2, t3, t4, n1); + } else if (type == VoxelType.NATIVE_STONE) { + stone.addQuad(v1, v2, v3, v4, t1, t2, t3, t4, n1); + } else if (type == VoxelType.NATIVE_GRASS) { + grass.addQuad(v1, v2, v3, v4, t1, t2, t3, t4, n1); + } + } + private void drawPlaneVerticalX_anti(Vector3i base, int xxx, int yyy, int zzz, int type) { + //Log.warning("Add plane X : " + (base.x + xxx) + ", " + (base.y + yyy) + ", " + (base.z + zzz)); + Vector3f v1 = new Vector3f(base.x + xxx , base.y + yyy , base.z + zzz); + Vector3f v2 = new Vector3f(base.x + xxx , base.y + yyy , base.z + zzz+1); + Vector3f v3 = new Vector3f(base.x + xxx , base.y + yyy+1, base.z + zzz+1); + Vector3f v4 = new Vector3f(base.x + xxx , base.y + yyy+1, base.z + zzz); + Vector2f t1 = new Vector2f(0, 0); + Vector2f t2 = new Vector2f(0, 1); + Vector2f t3 = new Vector2f(1, 1); + Vector2f t4 = new Vector2f(1, 0); + Vector3f n1 = new Vector3f(1, 0, 0); + if (type == VoxelType.NATIVE_UNBREAKABLE) { + unbreakable.addQuad(v1, v4, v3, v2, t1, t4, t3, t2, n1); + } else if (type == VoxelType.NATIVE_DIRT) { + dirt.addQuad(v1, v4, v3, v2, t1, t4, t3, t2, n1); + } else if (type == VoxelType.NATIVE_STONE) { + stone.addQuad(v1, v4, v3, v2, t1, t4, t3, t2, n1); + } else if (type == VoxelType.NATIVE_GRASS) { + grass.addQuad(v1, v4, v3, v2, t1, t4, t3, t2, n1); + } + } + private void drawPlaneVerticalY(Vector3i base, int xxx, int yyy, int zzz, int type) { + //Log.warning("Add plane Y : " + (base.x + xxx) + ", " + (base.y + yyy) + ", " + (base.z + zzz)); + Vector3f v1 = new Vector3f(base.x + xxx , base.y + yyy , base.z + zzz); + Vector3f v2 = new Vector3f(base.x + xxx , base.y + yyy , base.z + zzz+1); + Vector3f v3 = new Vector3f(base.x + xxx+1, base.y + yyy , base.z + zzz+1); + Vector3f v4 = new Vector3f(base.x + xxx+1, base.y + yyy , base.z + zzz); + Vector2f t1 = new Vector2f(0, 0); + Vector2f t2 = new Vector2f(0, 1); + Vector2f t3 = new Vector2f(1, 1); + Vector2f t4 = new Vector2f(1, 0); + Vector3f n1 = new Vector3f(0, 1, 0); + if (type == VoxelType.NATIVE_UNBREAKABLE) { + unbreakable.addQuad(v1, v2, v3, v4, t1, t2, t3, t4, n1); + } else if (type == VoxelType.NATIVE_DIRT) { + dirt.addQuad(v1, v2, v3, v4, t1, t2, t3, t4, n1); + } else if (type == VoxelType.NATIVE_STONE) { + stone.addQuad(v1, v2, v3, v4, t1, t2, t3, t4, n1); + } else if (type == VoxelType.NATIVE_GRASS) { + grass.addQuad(v1, v2, v3, v4, t1, t2, t3, t4, n1); + } + } + private void drawPlaneVerticalY_anti(Vector3i base, int xxx, int yyy, int zzz, int type) { + //Log.warning("Add plane Y : " + (base.x + xxx) + ", " + (base.y + yyy) + ", " + (base.z + zzz)); + Vector3f v1 = new Vector3f(base.x + xxx , base.y + yyy , base.z + zzz); + Vector3f v2 = new Vector3f(base.x + xxx , base.y + yyy , base.z + zzz+1); + Vector3f v3 = new Vector3f(base.x + xxx+1, base.y + yyy , base.z + zzz+1); + Vector3f v4 = new Vector3f(base.x + xxx+1, base.y + yyy , base.z + zzz); + Vector2f t1 = new Vector2f(0, 0); + Vector2f t2 = new Vector2f(0, 1); + Vector2f t3 = new Vector2f(1, 1); + Vector2f t4 = new Vector2f(1, 0); + Vector3f n1 = new Vector3f(0, -1, 0); + if (type == VoxelType.NATIVE_UNBREAKABLE) { + unbreakable.addQuad(v1, v4, v3, v2, t1, t4, t3, t2, n1); + } else if (type == VoxelType.NATIVE_DIRT) { + dirt.addQuad(v1, v4, v3, v2, t1, t4, t3, t2, n1); + } else if (type == VoxelType.NATIVE_STONE) { + stone.addQuad(v1, v4, v3, v2, t1, t4, t3, t2, n1); + } else if (type == VoxelType.NATIVE_GRASS) { + grass.addQuad(v1, v4, v3, v2, t1, t4, t3, t2, n1); + } + } + + @Override + public void update(float timeStep) { + Log.warning("update : " + timeStep); + if (chunk.haveChange() == false) { + return; + } + Log.warning(" ==> YES"); + Voxel[][][] data = chunk.getData(); + unbreakable.clear(); + stone.clear(); + dirt.clear(); + grass.clear(); + for (int xxx=0; xxx < VoxelChunk.VOXEL_CHUNK_SIZE; xxx++) { + for (int yyy=0; yyy < VoxelChunk.VOXEL_CHUNK_SIZE; yyy++) { + for (int zzz=0; zzz < VoxelChunk.VOXEL_CHUNK_SIZE; zzz++) { + Voxel current = data[xxx][yyy][zzz]; + if (current.active == false) { + continue; + } + Voxel bottom = chunk.getVoxel(xxx, yyy, zzz-1); + if (bottom == null || bottom.active == false) { + drawPlane(chunk.getPosition(), xxx, yyy, zzz, current.type); + } + Voxel up = chunk.getVoxel(xxx, yyy, zzz+1); + if (up == null || up.active == false) { + drawPlane_anti(chunk.getPosition(), xxx, yyy, zzz+1, current.type); + } + Voxel left = chunk.getVoxel(xxx-1, yyy, zzz); + if (left == null || left.active == false) { + drawPlaneVerticalX(chunk.getPosition(), xxx, yyy, zzz, current.type); + } + Voxel right = chunk.getVoxel(xxx+1, yyy, zzz); + if (right == null || right.active == false) { + drawPlaneVerticalX_anti(chunk.getPosition(), xxx+1, yyy, zzz, current.type); + } + Voxel front = chunk.getVoxel(xxx, yyy-1, zzz); + if (front == null || front.active == false) { + drawPlaneVerticalY_anti(chunk.getPosition(), xxx, yyy, zzz, current.type); + } + Voxel back = chunk.getVoxel(xxx, yyy+1, zzz); + if (back == null || back.active == false) { + drawPlaneVerticalY(chunk.getPosition(), xxx, yyy+1, zzz, current.type); + } + } + } + } + unbreakable.flush(); + stone.flush(); + dirt.flush(); + grass.flush(); + } + + @Override + public void render(String name) { + //Log.warning("Render : " + name); + super.render(name); + } + +} diff --git a/src/org/atriaSoft/gameEngine/map/ComponentRenderVoxelMesh.java b/src/org/atriaSoft/gameEngine/map/ComponentRenderVoxelMesh.java new file mode 100644 index 0000000..ae2f677 --- /dev/null +++ b/src/org/atriaSoft/gameEngine/map/ComponentRenderVoxelMesh.java @@ -0,0 +1,19 @@ +package org.atriaSoft.gameEngine.map; + +import org.atriaSoft.etk.Uri; +import org.atriaSoft.gameEngine.components.ComponentRenderTexturedMaterialsStaticMeshs; +import org.atriaSoft.gameEngine.engines.EngineLight; + +public class ComponentRenderVoxelMesh extends ComponentRenderTexturedMaterialsStaticMeshs { + + public ComponentRenderVoxelMesh(Uri vertexShader, Uri fragmentShader, EngineLight lightEngine, VoxelChunk chunk) { + super(vertexShader, fragmentShader, lightEngine); + // TODO Auto-generated constructor stub + } + + @Override + public void update(float timeStep) { + + } + +} diff --git a/src/org/atriaSoft/gameEngine/map/MapVoxel.java b/src/org/atriaSoft/gameEngine/map/MapVoxel.java new file mode 100644 index 0000000..811d5b4 --- /dev/null +++ b/src/org/atriaSoft/gameEngine/map/MapVoxel.java @@ -0,0 +1,77 @@ +package org.atriaSoft.gameEngine.map; + +import java.util.ArrayList; +import java.util.List; + +import org.atriaSoft.etk.Uri; +import org.atriaSoft.etk.math.Transform3D; +import org.atriaSoft.etk.math.Vector3i; +import org.atriaSoft.etk.math.Vector3f; +import org.atriaSoft.gameEngine.Entity; +import org.atriaSoft.gameEngine.Environement; +import org.atriaSoft.gameEngine.Light; +import org.atriaSoft.gameEngine.Material; +import org.atriaSoft.gameEngine.components.ComponentLight; +import org.atriaSoft.gameEngine.components.ComponentLightSun; +import org.atriaSoft.gameEngine.components.ComponentMaterials; +import org.atriaSoft.gameEngine.components.ComponentPosition; +import org.atriaSoft.gameEngine.components.ComponentRenderTexturedMaterialsDynamicMeshs; +import org.atriaSoft.gameEngine.components.ComponentRenderTexturedMaterialsStaticMeshs; +import org.atriaSoft.gameEngine.components.ComponentRenderTexturedStaticMesh; +import org.atriaSoft.gameEngine.components.ComponentStaticMesh; +import org.atriaSoft.gameEngine.components.ComponentStaticMeshs; +import org.atriaSoft.gameEngine.components.ComponentTexture; +import org.atriaSoft.gameEngine.components.ComponentTextures; +import org.atriaSoft.gameEngine.engines.EngineLight; +import org.atriaSoft.gameEngine.engines.EngineMap; + +public class MapVoxel extends EngineMap { + //List listOfChunks = new ArrayList(); + ComponentTextures textures; + + public MapVoxel(Environement env){ + super(env); + // for basic test ... after generate dynamic ... + textures = new ComponentTextures(); + } + public void init() { + textures.setTexture("stone", new Uri("DATA", "blocks/stone.png")); + textures.setTexture("grass", new Uri("DATA", "blocks/dirt_podzol_top.png")); + textures.setTexture("dirt", new Uri("DATA", "blocks/dirt.png")); + textures.setTexture("watter", new Uri("DATA", "blocks/water_static.png")); + textures.setTexture("unbreakable", new Uri("DATA", "blocks/stone_diorite.png")); + +// addNewChunk(new Vector3i(-1,-1, 0)); +// addNewChunk(new Vector3i(-1, 0, 0)); +// addNewChunk(new Vector3i(-1, 1, 0)); +// addNewChunk(new Vector3i( 0,-1, 0)); + addNewChunk(new Vector3i( 0, 0, 0)); +// addNewChunk(new Vector3i( 0, 1, 0)); +// addNewChunk(new Vector3i( 1,-1, 0)); +// addNewChunk(new Vector3i( 1, 0, 0)); +// addNewChunk(new Vector3i( 1, 1, 0)); + } + private void addNewChunk(Vector3i position) { + // simple sun to have a global light ... + Entity tmpEntity = new Entity(this.env); + tmpEntity.addComponent(new ComponentPosition(new Transform3D(new Vector3f(position.x,position.y,0)))); + VoxelChunk tmpVoxelChunk = new VoxelChunk(this, position); + tmpEntity.addComponent(tmpVoxelChunk); + ComponentDynamicMeshsVoxelMap mesh = new ComponentDynamicMeshsVoxelMap(tmpVoxelChunk); + tmpEntity.addComponent(mesh); + tmpEntity.addComponent(textures); + ComponentMaterials materials = new ComponentMaterials(); + materials.setMaterial("stone", new Material()); + materials.setMaterial("grass", new Material()); + materials.setMaterial("dirt", new Material()); + materials.setMaterial("watter", new Material()); + materials.setMaterial("unbreakable", new Material()); + tmpEntity.addComponent(materials); + tmpEntity.addComponent(new ComponentRenderTexturedMaterialsDynamicMeshs( + new Uri("DATA", "basicMaterial.vert"), + new Uri("DATA", "basicMaterial.frag"), + (EngineLight)env.getEngine(EngineLight.ENGINE_NAME))); + this.env.addEntity(tmpEntity); + + } +} diff --git a/src/org/atriaSoft/gameEngine/map/Voxel.java b/src/org/atriaSoft/gameEngine/map/Voxel.java new file mode 100644 index 0000000..b29a737 --- /dev/null +++ b/src/org/atriaSoft/gameEngine/map/Voxel.java @@ -0,0 +1,11 @@ +package org.atriaSoft.gameEngine.map; + +public class Voxel { + public int type = VoxelType.NATIVE_UNKNOWN; + public boolean active = true; + public Voxel(int type, boolean active) { + this.type = type; + this.active = active; + } + +} diff --git a/src/org/atriaSoft/gameEngine/map/VoxelChunk.java b/src/org/atriaSoft/gameEngine/map/VoxelChunk.java new file mode 100644 index 0000000..205d1b8 --- /dev/null +++ b/src/org/atriaSoft/gameEngine/map/VoxelChunk.java @@ -0,0 +1,75 @@ +package org.atriaSoft.gameEngine.map; + +import org.atriaSoft.etk.math.Vector2i; +import org.atriaSoft.etk.math.Vector3i; +import org.atriaSoft.gameEngine.components.ComponentMap; + +// This chunk in designed in 2D no upper chunk, and no lower chunk... +public class VoxelChunk extends ComponentMap { + + public final static int VOXEL_CHUNK_SIZE = 32; + private Voxel[][][] data = new Voxel[VOXEL_CHUNK_SIZE][VOXEL_CHUNK_SIZE][VOXEL_CHUNK_SIZE]; + private final Vector3i position; + //private VoxelChunk[] neibours = new VoxelChunk[8]; + private final MapVoxel globalMap; + + private boolean haveChange = false; + + public VoxelChunk(MapVoxel globalMap, Vector3i position) { + this.globalMap = globalMap; + this.position = position; + basicFill(); + haveChange = true; + } + // this is a default fill tho have a usable map ==> only 3 layers ... + private void basicFill() { + for (int xxx=0; xxx < VOXEL_CHUNK_SIZE; xxx++) { + for (int yyy=0; yyy < VOXEL_CHUNK_SIZE; yyy++) { + for (int zzz=0; zzz < VOXEL_CHUNK_SIZE; zzz++) { + if(zzz < 1) { + data[xxx][yyy][zzz] = new Voxel(VoxelType.NATIVE_UNBREAKABLE, true); + } else if(zzz < 8) { + data[xxx][yyy][zzz] = new Voxel(VoxelType.NATIVE_STONE, true); + } else if(zzz < 12) { + data[xxx][yyy][zzz] = new Voxel(VoxelType.NATIVE_DIRT, true); + } else if(zzz < 13) { + data[xxx][yyy][zzz] = new Voxel(VoxelType.NATIVE_GRASS, true); + } else { + data[xxx][yyy][zzz] = new Voxel(VoxelType.NATIVE_UNKNOWN, false); + } + } + } + } + } + + public Voxel[][][] getData() { + return data; + } + public boolean haveChange() { + boolean tmp = haveChange; + haveChange = false; + return tmp; + } + public Voxel getVoxel(int xxx, int yyy, int zzz) { + // TODO Auto-generated method stub + if (xxx < 0) { + return null; + }if (xxx >= VOXEL_CHUNK_SIZE) { + return null; + } + if (yyy < 0) { + return null; + }if (yyy >= VOXEL_CHUNK_SIZE) { + return null; + } + if (zzz < 0) { + return null; + }if (zzz >= VOXEL_CHUNK_SIZE) { + return null; + } + return data[xxx][yyy][zzz]; + } + public Vector3i getPosition() { + return position; + } +} diff --git a/src/org/atriaSoft/gameEngine/map/VoxelType.java b/src/org/atriaSoft/gameEngine/map/VoxelType.java new file mode 100644 index 0000000..d40dab1 --- /dev/null +++ b/src/org/atriaSoft/gameEngine/map/VoxelType.java @@ -0,0 +1,15 @@ +package org.atriaSoft.gameEngine.map; + +public class VoxelType { + public static final int NATIVE_UNKNOWN = 0; + public static final int NATIVE_BASIC_ID = 10000; + public static final int NATIVE_UNBREAKABLE = NATIVE_BASIC_ID; + public static final int NATIVE_DIRT = NATIVE_BASIC_ID + 1; + public static final int NATIVE_GRASS = NATIVE_BASIC_ID + 2; + public static final int NATIVE_STONE = NATIVE_BASIC_ID + 3; + public static final int NATIVE_SAND = NATIVE_BASIC_ID + 4; + public static final int NATIVE_WOOD = NATIVE_BASIC_ID + 101; + public static final int NATIVE_LEAF = NATIVE_BASIC_ID + 102; + public static final int NATIVE_WATTER = NATIVE_BASIC_ID + 201; + public static final int NATIVE_MAGMA = NATIVE_BASIC_ID + 202; +} diff --git a/src/org/atriaSoft/gameEngine/resource/ResourceListTexturedMesh.java b/src/org/atriaSoft/gameEngine/resource/ResourceListTexturedMesh.java new file mode 100644 index 0000000..e9a7e16 --- /dev/null +++ b/src/org/atriaSoft/gameEngine/resource/ResourceListTexturedMesh.java @@ -0,0 +1,111 @@ +package org.atriaSoft.gameEngine.resource; + +import java.util.ArrayList; +import java.util.List; + +import org.atriaSoft.etk.Uri; +import org.atriaSoft.etk.math.Vector3f; +import org.atriaSoft.etk.math.Vector2f; +import org.atriaSoft.gale.Log; +import org.atriaSoft.gale.backend3d.OpenGL.RenderMode; +import org.atriaSoft.gale.resource.Resource; +import org.atriaSoft.gale.resource.ResourceVirtualArrayObject; + + +public class ResourceListTexturedMesh extends ResourceStaticMesh { + protected List vertices = new ArrayList(); + protected List textureCoords = new ArrayList(); + protected List normals = new ArrayList(); + protected List indices = new ArrayList(); + + protected ResourceListTexturedMesh(Uri uriFile) { + super(uriFile); + addResourceType("ResourceListTexturedMesh"); + } + protected ResourceListTexturedMesh(RenderMode mode) { + super(mode); + addResourceType("ResourceListTexturedMesh"); + } + + private float[] toFloatArray(List values) { + float[] out = new float[values.size()]; + for (int iii=0; iii values) { + int[] out = new int[values.size()]; + for (int iii=0; iii values) { + float[] out = new float[values.size()*3]; + for (int iii=0; iii values) { + float[] out = new float[values.size()*2]; + for (int iii=0; iii 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); + } +} diff --git a/src/org/atriaSoft/gameEngine/sample/LoxelEngine/res/basic.frag b/src/org/atriaSoft/gameEngine/sample/LoxelEngine/res/basic.frag new file mode 100644 index 0000000..13d2aa8 --- /dev/null +++ b/src/org/atriaSoft/gameEngine/sample/LoxelEngine/res/basic.frag @@ -0,0 +1,17 @@ +#version 400 core + +#ifdef GL_ES +precision mediump float; +precision mediump int; +#endif + +in vec2 io_textureCoords; + +uniform sampler2D in_textureBase; + +// output: +out vec4 out_Color; + +void main(void) { + out_Color = texture(in_textureBase, io_textureCoords); +} diff --git a/src/org/atriaSoft/gameEngine/sample/LoxelEngine/res/basic.vert b/src/org/atriaSoft/gameEngine/sample/LoxelEngine/res/basic.vert new file mode 100644 index 0000000..c0dcaa7 --- /dev/null +++ b/src/org/atriaSoft/gameEngine/sample/LoxelEngine/res/basic.vert @@ -0,0 +1,21 @@ +#version 400 core + +#ifdef GL_ES +precision mediump float; +precision mediump int; +#endif + +// Input: +in vec3 in_position; +in vec2 in_textureCoords; +uniform mat4 in_matrixTransformation; +uniform mat4 in_matrixProjection; +uniform mat4 in_matrixView; + +// output: +out vec2 io_textureCoords; + +void main(void) { + gl_Position = in_matrixProjection * in_matrixView * in_matrixTransformation * vec4(in_position, 1.0); + io_textureCoords = in_textureCoords; +} diff --git a/src/org/atriaSoft/gameEngine/sample/LoxelEngine/res/basicMaterial.frag b/src/org/atriaSoft/gameEngine/sample/LoxelEngine/res/basicMaterial.frag new file mode 100644 index 0000000..3ca8c7e --- /dev/null +++ b/src/org/atriaSoft/gameEngine/sample/LoxelEngine/res/basicMaterial.frag @@ -0,0 +1,80 @@ +#version 400 core + +#ifdef GL_ES +precision mediump float; +precision mediump int; +#endif + +struct Light { + vec3 color; + vec3 position; + vec3 attenuation; +}; + +struct Material { + vec3 ambientFactor; + vec3 diffuseFactor; + vec3 specularFactor; + float shininess; +}; +const int MAX_LIGHT_NUMBER = 8; + + +in vec2 io_textureCoords; +in vec3 io_surfaceNormal; +in vec3 io_toCameraVector; +in vec3 io_toLightVector[MAX_LIGHT_NUMBER]; +// FOW: Fog Of War result calculation +in float io_fowVisibility; + +// texture properties +uniform sampler2D in_textureBase; +// Material +uniform Material in_material; +// 2 light for suns and other for locals ... +uniform Light in_lights[MAX_LIGHT_NUMBER]; +// global color of the sky ... needed to have a better color for the FOW +//uniform vec3 in_sky_color; +const vec3 in_sky_color = vec3(1.0,1.0,1.0); + +// output: +out vec4 out_Color; + +void main(void) { + // disable transparency elements in the texture ... + // Can be set at the start of the shader ... + vec4 textureColour = texture(in_textureBase, io_textureCoords); + if (textureColour.a < 0.5) { + discard; + } + + vec3 unitNormal = normalize(io_surfaceNormal); + vec3 unitVectorToCamera = normalize(io_toCameraVector); + vec3 totalDiffuse = vec3(0.0); + vec3 totalSpecular = vec3(0.0); + for(int iii=0; iii maybe set an uniform for this + totalDiffuse = max(totalDiffuse, 0.3); + + out_Color = vec4(totalDiffuse,1.0) * textureColour + vec4(totalSpecular, 1.0); + out_Color = mix(vec4(in_sky_color,1.0), out_Color, io_fowVisibility); +} + + + + diff --git a/src/org/atriaSoft/gameEngine/sample/LoxelEngine/res/basicMaterial.vert b/src/org/atriaSoft/gameEngine/sample/LoxelEngine/res/basicMaterial.vert new file mode 100644 index 0000000..c75190f --- /dev/null +++ b/src/org/atriaSoft/gameEngine/sample/LoxelEngine/res/basicMaterial.vert @@ -0,0 +1,59 @@ +#version 400 core + +#ifdef GL_ES +precision mediump float; +precision mediump int; +#endif + +struct Light { + vec3 color; + vec3 position; + vec3 attenuation; +}; +const int MAX_LIGHT_NUMBER = 8; + +// Input: +in vec3 in_position; +in vec3 in_normal; +in vec2 in_textureCoords; +// 2 light for suns and other for locals ... +uniform Light in_lights[MAX_LIGHT_NUMBER]; + +uniform mat4 in_matrixTransformation; +uniform mat4 in_matrixProjection; +uniform mat4 in_matrixView; + +//uniform float in_numberOfRows; +//uniform vec2 in_offset; +const float in_numberOfRows = 1; +const vec2 in_offset = vec2(0.0,0.0); + +// Configuration of the FOV ==> TODO: Set it in parameter uniform ... +const float c_density = 0.007; +const float c_gradient = 1.5; + +// output: +out vec2 io_textureCoords; +out vec3 io_surfaceNormal; +out vec3 io_toCameraVector; +out vec3 io_toLightVector[MAX_LIGHT_NUMBER]; +// FOW: Fog Of War result calculation +out float io_fowVisibility; + +void main(void) { + vec4 worldPosition = in_matrixTransformation * vec4(in_position, 1.0); + vec4 positionRelativeToCam = in_matrixView * worldPosition; + gl_Position = in_matrixProjection * positionRelativeToCam; + io_textureCoords = (in_textureCoords/in_numberOfRows) + in_offset; + + io_surfaceNormal = (in_matrixTransformation * vec4(in_normal, 0.0)).xyz; + for(int iii=0;iii