From 5ef79287060f4e6cef540e4465e8cb15edbef812 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 26 May 2014 16:42:57 -0430 Subject: [PATCH] Refactored rendering to use modelbatch everywhere. --- src/ve/ucv/ciens/ccg/nxtar/NxtARCore.java | 6 +- .../entities/MarkerTestEntityCreator.java | 97 ++++------- .../shaders/SingleLightPerPixelShader.java | 34 ++-- ...nalityWrapper.java => ActionResolver.java} | 2 +- .../ciens/ccg/nxtar/states/InGameState.java | 89 +++++----- .../ccg/nxtar/systems/AnimationSystem.java | 37 ++++ .../systems/CollisionDetectionSystem.java | 37 ++++ .../nxtar/systems/MarkerRenderingSystem.java | 83 +++++---- .../ModelBatchMarkerRenderingSystem.java | 162 ------------------ .../nxtar/systems/ObjectRenderingSystem.java | 70 ++++---- 10 files changed, 268 insertions(+), 349 deletions(-) rename src/ve/ucv/ciens/ccg/nxtar/interfaces/{AndroidFunctionalityWrapper.java => ActionResolver.java} (94%) create mode 100644 src/ve/ucv/ciens/ccg/nxtar/systems/AnimationSystem.java create mode 100644 src/ve/ucv/ciens/ccg/nxtar/systems/CollisionDetectionSystem.java delete mode 100644 src/ve/ucv/ciens/ccg/nxtar/systems/ModelBatchMarkerRenderingSystem.java diff --git a/src/ve/ucv/ciens/ccg/nxtar/NxtARCore.java b/src/ve/ucv/ciens/ccg/nxtar/NxtARCore.java index 62ec6ce..315e89d 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/NxtARCore.java +++ b/src/ve/ucv/ciens/ccg/nxtar/NxtARCore.java @@ -17,7 +17,7 @@ package ve.ucv.ciens.ccg.nxtar; import ve.ucv.ciens.ccg.nxtar.interfaces.ImageProcessor; import ve.ucv.ciens.ccg.nxtar.interfaces.ApplicationEventsListener; -import ve.ucv.ciens.ccg.nxtar.interfaces.AndroidFunctionalityWrapper; +import ve.ucv.ciens.ccg.nxtar.interfaces.ActionResolver; import ve.ucv.ciens.ccg.nxtar.network.RobotControlThread; import ve.ucv.ciens.ccg.nxtar.network.SensorReportThread; import ve.ucv.ciens.ccg.nxtar.network.ServiceDiscoveryThread; @@ -121,7 +121,7 @@ public class NxtARCore extends Game implements ApplicationEventsListener{ /** *

Wrapper around the Operating System methods.

*/ - private AndroidFunctionalityWrapper osFunction; + private ActionResolver osFunction; // Networking related fields. /** @@ -206,7 +206,7 @@ public class NxtARCore extends Game implements ApplicationEventsListener{ // Check if the concrete application implements all required interfaces. try{ - this.osFunction = (AndroidFunctionalityWrapper)concreteApp; + this.osFunction = (ActionResolver)concreteApp; }catch(ClassCastException cc){ Gdx.app.debug(TAG, CLASS_NAME + ".Main() :: concreteApp does not implement the Toaster interface. Toasting disabled."); this.osFunction = null; diff --git a/src/ve/ucv/ciens/ccg/nxtar/entities/MarkerTestEntityCreator.java b/src/ve/ucv/ciens/ccg/nxtar/entities/MarkerTestEntityCreator.java index 7af7904..c6cfbd7 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/entities/MarkerTestEntityCreator.java +++ b/src/ve/ucv/ciens/ccg/nxtar/entities/MarkerTestEntityCreator.java @@ -15,32 +15,27 @@ */ package ve.ucv.ciens.ccg.nxtar.entities; -import ve.ucv.ciens.ccg.nxtar.components.CustomShaderComponent; import ve.ucv.ciens.ccg.nxtar.components.EnvironmentComponent; import ve.ucv.ciens.ccg.nxtar.components.GeometryComponent; import ve.ucv.ciens.ccg.nxtar.components.MarkerCodeComponent; -import ve.ucv.ciens.ccg.nxtar.components.MeshComponent; import ve.ucv.ciens.ccg.nxtar.components.ModelComponent; import ve.ucv.ciens.ccg.nxtar.components.ShaderComponent; -import ve.ucv.ciens.ccg.nxtar.exceptions.ShaderFailedToLoadException; -import ve.ucv.ciens.ccg.nxtar.graphics.shaders.CustomShaderBase; import ve.ucv.ciens.ccg.nxtar.graphics.shaders.SingleLightPerPixelShader; -import ve.ucv.ciens.ccg.nxtar.graphics.shaders.SingleLightPhongShader; import com.artemis.Entity; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; -import com.badlogic.gdx.graphics.GL20; -import com.badlogic.gdx.graphics.Mesh; import com.badlogic.gdx.graphics.VertexAttribute; import com.badlogic.gdx.graphics.VertexAttributes; import com.badlogic.gdx.graphics.VertexAttributes.Usage; import com.badlogic.gdx.graphics.g3d.Environment; +import com.badlogic.gdx.graphics.g3d.Material; import com.badlogic.gdx.graphics.g3d.Model; import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; +import com.badlogic.gdx.graphics.g3d.attributes.FloatAttribute; import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight; import com.badlogic.gdx.graphics.g3d.loader.G3dModelLoader; -import com.badlogic.gdx.graphics.g3d.utils.MeshBuilder; +import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; import com.badlogic.gdx.math.Matrix3; import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.utils.JsonReader; @@ -49,101 +44,71 @@ public class MarkerTestEntityCreator extends EntityCreatorBase { private static final String TAG = "MARKER_TEST_ENTITY_CREATOR"; private static final String CLASS_NAME = MarkerTestEntityCreator.class.getSimpleName(); - private Mesh sphereMesh; - private Mesh boxMesh; private Model bombModel; - private CustomShaderBase phongShader; + private Model boxModel; private SingleLightPerPixelShader ppShader; - private Mesh bombMesh; @Override public void createAllEntities() { - MeshBuilder builder; - Entity bomb, sphere, box, bombModelBatch; + ModelBuilder builder; + Entity bomb1, box, bomb2; G3dModelLoader loader; Environment environment; + Material material; // Create mesh. Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Creating the meshes."); - builder = new MeshBuilder(); - - builder.begin(new VertexAttributes(new VertexAttribute(Usage.Position, 3, "a_position"), new VertexAttribute(Usage.Normal, 3, "a_normal"), new VertexAttribute(Usage.Color, 4, "a_color")), GL20.GL_TRIANGLES);{ - builder.setColor(1.0f, 1.0f, 1.0f, 1.0f); - builder.sphere(1.0f, 1.0f, 1.0f, 10, 10); - }sphereMesh = builder.end(); - - builder.begin(new VertexAttributes(new VertexAttribute(Usage.Position, 3, "a_position"), new VertexAttribute(Usage.Normal, 3, "a_normal"), new VertexAttribute(Usage.Color, 4, "a_color")), GL20.GL_TRIANGLES);{ - builder.setColor(1.0f, 1.0f, 1.0f, 1.0f); - builder.box(0.5f, 0.5f, 6.0f); - }boxMesh = builder.end(); loader = new G3dModelLoader(new JsonReader()); bombModel = loader.loadModel(Gdx.files.internal("models/Bomb_test_2.g3dj")); - bombMesh = bombModel.meshes.get(0); - Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): " + bombMesh.getVertexAttributes().toString()); + material = new Material(new FloatAttribute(FloatAttribute.Shininess, 50.0f), new ColorAttribute(ColorAttribute.Diffuse, 1.0f, 1.0f, 1.0f, 1.0f), new ColorAttribute(ColorAttribute.Specular, 1.0f, 1.0f, 1.0f, 1.0f)); - // Load the phong shader. - Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Loading the phong shader."); - try{ - phongShader = new SingleLightPhongShader().loadShader(); - }catch(ShaderFailedToLoadException se){ - Gdx.app.error(TAG, CLASS_NAME + ".InGameState(): " + se.getMessage()); - Gdx.app.exit(); - } + builder = new ModelBuilder(); + boxModel = builder.createBox(0.5f, 0.5f, 6.0f, material, new VertexAttributes(new VertexAttribute(Usage.Position, 3, "a_position"), new VertexAttribute(Usage.Normal, 3, "a_normal"), new VertexAttribute(Usage.Color, 4, "a_color")).getMask()); + // Load the shader. ppShader = new SingleLightPerPixelShader(); ppShader.init(); environment = new Environment(); environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.3f, 0.3f, 0.3f, 1.0f)); - environment.add(new DirectionalLight().set(new Color(1, 1, 1, 1), new Vector3(-2, -2, -2))); + environment.add(new DirectionalLight().set(new Color(1, 1, 1, 1), new Vector3(1, 0, 0.5f))); // Create the entities. Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Creating the enitites."); - bomb = world.createEntity(); - bomb.addComponent(new GeometryComponent(new Vector3(0.0f, 0.0f, 0.0f), new Matrix3().idt(), new Vector3(1.0f, 1.0f, 1.0f))); - bomb.addComponent(new MeshComponent(bombMesh)); - bomb.addComponent(new CustomShaderComponent(phongShader)); - bomb.addComponent(new MarkerCodeComponent(1023)); + bomb1 = world.createEntity(); + bomb1.addComponent(new GeometryComponent(new Vector3(0.0f, 0.0f, 0.0f), new Matrix3().idt(), new Vector3(1.0f, 1.0f, 1.0f))); + bomb1.addComponent(new ModelComponent(bombModel)); + bomb1.addComponent(new EnvironmentComponent(environment)); + bomb1.addComponent(new ShaderComponent(ppShader)); + bomb1.addComponent(new MarkerCodeComponent(1023)); - bombModelBatch = world.createEntity(); - bombModelBatch.addComponent(new GeometryComponent(new Vector3(0.0f, 0.0f, 0.0f), new Matrix3().idt(), new Vector3(1.0f, 1.0f, 1.0f))); - bombModelBatch.addComponent(new ModelComponent(bombModel)); - bombModelBatch.addComponent(new EnvironmentComponent(environment)); - bombModelBatch.addComponent(new MarkerCodeComponent(89)); - bombModelBatch.addComponent(new ShaderComponent(ppShader)); - - sphere = world.createEntity(); - sphere.addComponent(new GeometryComponent(new Vector3(0.0f, 0.0f, 0.0f), new Matrix3().idt(), new Vector3(1.0f, 1.0f, 1.0f))); - sphere.addComponent(new MeshComponent(sphereMesh)); - sphere.addComponent(new CustomShaderComponent(phongShader)); - sphere.addComponent(new MarkerCodeComponent(10)); + bomb2 = world.createEntity(); + bomb2.addComponent(new GeometryComponent(new Vector3(0.0f, 0.0f, 0.0f), new Matrix3().idt(), new Vector3(1.0f, 1.0f, 1.0f))); + bomb2.addComponent(new ModelComponent(bombModel)); + bomb2.addComponent(new EnvironmentComponent(environment)); + bomb2.addComponent(new MarkerCodeComponent(89)); + bomb2.addComponent(new ShaderComponent(ppShader)); box = world.createEntity(); box.addComponent(new GeometryComponent(new Vector3(-1.0f, 0.0f, 0.0f), new Matrix3().idt(), new Vector3(1.0f, 1.0f, 1.0f))); - box.addComponent(new MeshComponent(boxMesh)); - box.addComponent(new CustomShaderComponent(phongShader)); + box.addComponent(new ModelComponent(boxModel)); + box.addComponent(new ShaderComponent(ppShader)); + box.addComponent(new EnvironmentComponent(environment)); // Add the entities to the world. Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Adding entities to the world."); //sphere.addToWorld(); - bomb.addToWorld(); - bombModelBatch.addToWorld(); - sphere.addToWorld(); + bomb1.addToWorld(); + bomb2.addToWorld(); box.addToWorld(); } @Override public void dispose() { - if(phongShader != null && phongShader.getShaderProgram() != null) - phongShader.getShaderProgram().dispose(); - - if(sphereMesh != null) - sphereMesh.dispose(); - - if(boxMesh != null) - boxMesh.dispose(); + if(boxModel != null) + boxModel.dispose(); if(bombModel != null) bombModel.dispose(); diff --git a/src/ve/ucv/ciens/ccg/nxtar/graphics/shaders/SingleLightPerPixelShader.java b/src/ve/ucv/ciens/ccg/nxtar/graphics/shaders/SingleLightPerPixelShader.java index d8f924b..f10fb1c 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/graphics/shaders/SingleLightPerPixelShader.java +++ b/src/ve/ucv/ciens/ccg/nxtar/graphics/shaders/SingleLightPerPixelShader.java @@ -25,18 +25,18 @@ import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; import com.badlogic.gdx.graphics.g3d.attributes.FloatAttribute; import com.badlogic.gdx.graphics.g3d.utils.RenderContext; import com.badlogic.gdx.graphics.glutils.ShaderProgram; +import com.badlogic.gdx.math.Matrix4; import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.utils.GdxRuntimeException; public class SingleLightPerPixelShader implements Shader{ - private static final String TAG = "SINGLE_LIGHT_PER_PIXEL_SHADER"; - private static final String CLASS_NAME = SingleLightPerPixelShader.class.getSimpleName(); private static final String VERTEX_SHADER_PATH = "shaders/directionalPerPixelSingleLight/directionalPerPixel_vert.glsl"; private static final String FRAGMENT_SHADER_PATH = "shaders/directionalPerPixelSingleLight/directionalPerPixel_frag.glsl"; private ShaderProgram program; private Camera camera; private RenderContext context; + private Matrix4 normalMatrix; // Uniform locations. private int u_geomTrans; @@ -48,6 +48,7 @@ public class SingleLightPerPixelShader implements Shader{ private int u_shiny; private int u_cameraPos; private int u_materialDiffuse; + private int u_normalMatrix; public SingleLightPerPixelShader(){ program = null; @@ -57,12 +58,13 @@ public class SingleLightPerPixelShader implements Shader{ @Override public void init() throws GdxRuntimeException{ + normalMatrix = new Matrix4().idt(); + // Compile the shader. program = new ShaderProgram(Gdx.files.internal(VERTEX_SHADER_PATH), Gdx.files.internal(FRAGMENT_SHADER_PATH)); - if(!program.isCompiled()){ - Gdx.app.log(TAG, CLASS_NAME + ".init(): Shader failed to compile."); + + if(!program.isCompiled()) throw new GdxRuntimeException(program.getLog()); - } // Cache uniform locations. u_projTrans = program.getUniformLocation("u_projTrans"); @@ -74,6 +76,7 @@ public class SingleLightPerPixelShader implements Shader{ u_shiny = program.getUniformLocation("u_shiny"); u_cameraPos = program.getUniformLocation("u_cameraPos"); u_materialDiffuse = program.getUniformLocation("u_materialDiffuse"); + u_normalMatrix = program.getUniformLocation("u_normalMatrix"); } @Override @@ -92,12 +95,16 @@ public class SingleLightPerPixelShader implements Shader{ // Check for all needed lighting and material attributes. if(renderable.environment.directionalLights.size < 1) return false; + if(!renderable.environment.has(ColorAttribute.AmbientLight)) return false; + if(!renderable.material.has(ColorAttribute.Diffuse)) return false; + if(!renderable.material.has(ColorAttribute.Specular)) return false; + if(!renderable.material.has(FloatAttribute.Shininess)) return false; @@ -109,7 +116,7 @@ public class SingleLightPerPixelShader implements Shader{ if(this.camera != null || this.context != null) throw new GdxRuntimeException("Called begin twice before calling end."); - this.camera = camera; + this.camera = camera; this.context = context; program.begin(); @@ -125,15 +132,16 @@ public class SingleLightPerPixelShader implements Shader{ @Override public void render(Renderable renderable){ // Get material colors. - Vector3 lightPosition = renderable.environment.directionalLights.get(0).direction; - Color diffuseLightColor = renderable.environment.directionalLights.get(0).color; - Color diffuseColor = ((ColorAttribute)renderable.material.get(ColorAttribute.Diffuse)).color; - Color specularColor = ((ColorAttribute)renderable.material.get(ColorAttribute.Specular)).color; - Color ambientColor = ((ColorAttribute)renderable.environment.get(ColorAttribute.AmbientLight)).color; - float shininess = ((FloatAttribute)renderable.material.get(FloatAttribute.Shininess)).value; + Vector3 lightPosition = renderable.environment.directionalLights.get(0).direction; + Color diffuseLightColor = renderable.environment.directionalLights.get(0).color; + Color diffuseColor = ((ColorAttribute)renderable.material.get(ColorAttribute.Diffuse)).color; + Color specularColor = ((ColorAttribute)renderable.material.get(ColorAttribute.Specular)).color; + Color ambientColor = ((ColorAttribute)renderable.environment.get(ColorAttribute.AmbientLight)).color; + float shininess = ((FloatAttribute)renderable.material.get(FloatAttribute.Shininess)).value; // Set model dependant uniforms. program.setUniformMatrix(u_geomTrans, renderable.worldTransform); + program.setUniformMatrix(u_normalMatrix, normalMatrix.idt().mul(renderable.worldTransform).inv().tra()); program.setUniformf(u_lightPos, lightPosition); program.setUniformf(u_lightDiffuse, diffuseLightColor); program.setUniformf(u_materialDiffuse, diffuseColor); @@ -149,7 +157,7 @@ public class SingleLightPerPixelShader implements Shader{ public void end(){ program.end(); - this.camera = null; + this.camera = null; this.context = null; } diff --git a/src/ve/ucv/ciens/ccg/nxtar/interfaces/AndroidFunctionalityWrapper.java b/src/ve/ucv/ciens/ccg/nxtar/interfaces/ActionResolver.java similarity index 94% rename from src/ve/ucv/ciens/ccg/nxtar/interfaces/AndroidFunctionalityWrapper.java rename to src/ve/ucv/ciens/ccg/nxtar/interfaces/ActionResolver.java index d233374..13f874f 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/interfaces/AndroidFunctionalityWrapper.java +++ b/src/ve/ucv/ciens/ccg/nxtar/interfaces/ActionResolver.java @@ -15,7 +15,7 @@ */ package ve.ucv.ciens.ccg.nxtar.interfaces; -public interface AndroidFunctionalityWrapper{ +public interface ActionResolver{ public void showShortToast(String msg); public void showLongToast(String msg); public void enableMulticast(); diff --git a/src/ve/ucv/ciens/ccg/nxtar/states/InGameState.java b/src/ve/ucv/ciens/ccg/nxtar/states/InGameState.java index 0c7b66b..ad4c163 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/states/InGameState.java +++ b/src/ve/ucv/ciens/ccg/nxtar/states/InGameState.java @@ -29,7 +29,6 @@ import ve.ucv.ciens.ccg.nxtar.network.monitors.MotorEventQueue; import ve.ucv.ciens.ccg.nxtar.network.monitors.VideoFrameMonitor; import ve.ucv.ciens.ccg.nxtar.systems.MarkerPositioningSystem; import ve.ucv.ciens.ccg.nxtar.systems.MarkerRenderingSystem; -import ve.ucv.ciens.ccg.nxtar.systems.ModelBatchMarkerRenderingSystem; import ve.ucv.ciens.ccg.nxtar.systems.ObjectRenderingSystem; import ve.ucv.ciens.ccg.nxtar.utils.ProjectConstants; @@ -48,6 +47,7 @@ import com.badlogic.gdx.graphics.Texture.TextureFilter; import com.badlogic.gdx.graphics.Texture.TextureWrap; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.graphics.g3d.ModelBatch; import com.badlogic.gdx.graphics.glutils.FrameBuffer; import com.badlogic.gdx.graphics.glutils.ShaderProgram; import com.badlogic.gdx.math.Matrix4; @@ -69,49 +69,52 @@ public class InGameState extends BaseState{ private static final float SHINYNESS = 50.0f; // Background related fields. - private float uScaling[]; - protected Sprite background; - private Texture backgroundTexture; - private ShaderProgram backgroundShader; + private float uScaling[]; + protected Sprite background; + private Texture backgroundTexture; + private ShaderProgram backgroundShader; // 3D rendering fields. - private Matrix4 projectionMatrix; - private FrameBuffer frameBuffer; - private Sprite frameBufferSprite; + private ModelBatch modelBatch; + private Matrix4 projectionMatrix; + private FrameBuffer frameBuffer; + private Sprite frameBufferSprite; - // Game objects. - private World gameWorld; - private EntityCreatorBase entityCreator; + // Game world objects. + private World gameWorld; + private EntityCreatorBase entityCreator; + private MarkerRenderingSystem markerRenderingSystem; + private ObjectRenderingSystem objectRenderingSystem; // Cameras. - private OrthographicCamera unitaryOrthoCamera; - private OrthographicCamera pixelPerfectOrthoCamera; - private CustomPerspectiveCamera perspectiveCamera; + private OrthographicCamera unitaryOrthoCamera; + private OrthographicCamera pixelPerfectOrthoCamera; + private CustomPerspectiveCamera perspectiveCamera; // Video stream graphics. - private Texture videoFrameTexture; - private Sprite renderableVideoFrame; - private Pixmap videoFrame; + private Texture videoFrameTexture; + private Sprite renderableVideoFrame; + private Pixmap videoFrame; // Interface buttons. - private Texture buttonTexture; - private Texture buttonTexture2; - private Sprite motorA; - private Sprite motorB; - private Sprite motorC; - private Sprite motorD; - private Sprite headA; - private Sprite headB; - private Sprite headC; + private Texture buttonTexture; + private Texture buttonTexture2; + private Sprite motorA; + private Sprite motorB; + private Sprite motorC; + private Sprite motorD; + private Sprite headA; + private Sprite headB; + private Sprite headC; // Button touch helper fields. - private boolean[] motorButtonsTouched; - private int[] motorButtonsPointers; - private boolean[] motorGamepadButtonPressed; + private boolean[] motorButtonsTouched; + private int[] motorButtonsPointers; + private boolean[] motorGamepadButtonPressed; // Monitors. - private VideoFrameMonitor frameMonitor; - private MotorEventQueue queue; + private VideoFrameMonitor frameMonitor; + private MotorEventQueue queue; public InGameState(final NxtARCore core){ this.core = core; @@ -179,6 +182,7 @@ public class InGameState extends BaseState{ uScaling[1] = Gdx.graphics.getHeight() > Gdx.graphics.getWidth() ? 16.0f : 9.0f; // Set up the 3D rendering. + modelBatch = new ModelBatch(); projectionMatrix = new Matrix4().idt(); frameBuffer = null; perspectiveCamera = null; @@ -191,9 +195,10 @@ public class InGameState extends BaseState{ entityCreator.setWorld(gameWorld); entityCreator.createAllEntities(); gameWorld.setSystem(new MarkerPositioningSystem()); - gameWorld.setSystem(new MarkerRenderingSystem(), true); - gameWorld.setSystem(new ModelBatchMarkerRenderingSystem(), true); - gameWorld.setSystem(new ObjectRenderingSystem(), true); + markerRenderingSystem = new MarkerRenderingSystem(modelBatch); + objectRenderingSystem = new ObjectRenderingSystem(modelBatch); + gameWorld.setSystem(markerRenderingSystem, true); + gameWorld.setSystem(objectRenderingSystem, true); gameWorld.initialize(); } @@ -306,14 +311,13 @@ public class InGameState extends BaseState{ RenderParameters.setEyePosition(perspectiveCamera.position); // Call rendering systems. - gameWorld.getSystem(MarkerRenderingSystem.class).setMarkerData(data); - gameWorld.getSystem(MarkerRenderingSystem.class).process(); + markerRenderingSystem.begin(perspectiveCamera, data); + markerRenderingSystem.process(); + markerRenderingSystem.end(); - gameWorld.getSystem(ModelBatchMarkerRenderingSystem.class).begin(perspectiveCamera, data); - gameWorld.getSystem(ModelBatchMarkerRenderingSystem.class).process(); - gameWorld.getSystem(ModelBatchMarkerRenderingSystem.class).end(); - - gameWorld.getSystem(ObjectRenderingSystem.class).process(); + objectRenderingSystem.begin(perspectiveCamera); + objectRenderingSystem.process(); + objectRenderingSystem.end(); }frameBuffer.end(); // Set the frame buffer object texture to a renderable sprite. @@ -382,7 +386,8 @@ public class InGameState extends BaseState{ @Override public void dispose(){ - gameWorld.getSystem(ModelBatchMarkerRenderingSystem.class).dispose(); + if(modelBatch != null) + modelBatch.dispose(); if(entityCreator != null) entityCreator.dispose(); diff --git a/src/ve/ucv/ciens/ccg/nxtar/systems/AnimationSystem.java b/src/ve/ucv/ciens/ccg/nxtar/systems/AnimationSystem.java new file mode 100644 index 0000000..445d0e6 --- /dev/null +++ b/src/ve/ucv/ciens/ccg/nxtar/systems/AnimationSystem.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2014 Miguel Angel Astor Romero + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package ve.ucv.ciens.ccg.nxtar.systems; + +import ve.ucv.ciens.ccg.nxtar.components.ModelComponent; + +import com.artemis.Aspect; +import com.artemis.Entity; +import com.artemis.systems.EntityProcessingSystem; + +public class AnimationSystem extends EntityProcessingSystem { + + @SuppressWarnings("unchecked") + public AnimationSystem(){ + super(Aspect.getAspectForAll(ModelComponent.class)); + } + + @Override + protected void process(Entity e) { + // TODO Auto-generated method stub + + } + +} diff --git a/src/ve/ucv/ciens/ccg/nxtar/systems/CollisionDetectionSystem.java b/src/ve/ucv/ciens/ccg/nxtar/systems/CollisionDetectionSystem.java new file mode 100644 index 0000000..c53eaaa --- /dev/null +++ b/src/ve/ucv/ciens/ccg/nxtar/systems/CollisionDetectionSystem.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2014 Miguel Angel Astor Romero + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package ve.ucv.ciens.ccg.nxtar.systems; + +import ve.ucv.ciens.ccg.nxtar.components.ModelComponent; + +import com.artemis.Aspect; +import com.artemis.Entity; +import com.artemis.systems.EntityProcessingSystem; + +public class CollisionDetectionSystem extends EntityProcessingSystem { + + @SuppressWarnings("unchecked") + public CollisionDetectionSystem(){ + super(Aspect.getAspectForAll(ModelComponent.class)); + } + + @Override + protected void process(Entity e) { + // TODO Auto-generated method stub + + } + +} diff --git a/src/ve/ucv/ciens/ccg/nxtar/systems/MarkerRenderingSystem.java b/src/ve/ucv/ciens/ccg/nxtar/systems/MarkerRenderingSystem.java index f1e0081..d2058bc 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/systems/MarkerRenderingSystem.java +++ b/src/ve/ucv/ciens/ccg/nxtar/systems/MarkerRenderingSystem.java @@ -15,11 +15,11 @@ */ package ve.ucv.ciens.ccg.nxtar.systems; -import ve.ucv.ciens.ccg.nxtar.components.CustomShaderComponent; +import ve.ucv.ciens.ccg.nxtar.components.EnvironmentComponent; import ve.ucv.ciens.ccg.nxtar.components.GeometryComponent; import ve.ucv.ciens.ccg.nxtar.components.MarkerCodeComponent; -import ve.ucv.ciens.ccg.nxtar.components.MeshComponent; -import ve.ucv.ciens.ccg.nxtar.graphics.RenderParameters; +import ve.ucv.ciens.ccg.nxtar.components.ModelComponent; +import ve.ucv.ciens.ccg.nxtar.components.ShaderComponent; import ve.ucv.ciens.ccg.nxtar.interfaces.ImageProcessor.MarkerData; import ve.ucv.ciens.ccg.nxtar.utils.ProjectConstants; @@ -29,16 +29,18 @@ import com.artemis.Entity; import com.artemis.annotations.Mapper; import com.artemis.systems.EntityProcessingSystem; import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.PerspectiveCamera; +import com.badlogic.gdx.graphics.g3d.ModelBatch; import com.badlogic.gdx.math.Matrix4; public class MarkerRenderingSystem extends EntityProcessingSystem { @Mapper ComponentMapper markerMapper; @Mapper ComponentMapper geometryMapper; - @Mapper ComponentMapper shaderMapper; - @Mapper ComponentMapper meshMapper; + @Mapper ComponentMapper modelMapper; + @Mapper ComponentMapper environmentMapper; + @Mapper ComponentMapper shaderMapper; - private static final String TAG = "MARKER_RENDERING_SYSTEM"; + private static final String TAG = "MODEL_BATCH_MARKER_RENDERING_SYSTEM"; private static final String CLASS_NAME = MarkerRenderingSystem.class.getSimpleName(); /** @@ -56,43 +58,59 @@ public class MarkerRenderingSystem extends EntityProcessingSystem { */ private Matrix4 scalingMatrix; - /** - *

The total transformation to be applied to an entity.

- */ - private Matrix4 combinedTransformationMatrix; + private MarkerData markers; - MarkerData markers; + private PerspectiveCamera camera; + + private ModelBatch batch; @SuppressWarnings("unchecked") - public MarkerRenderingSystem(){ - super(Aspect.getAspectForAll(MarkerCodeComponent.class, GeometryComponent.class, CustomShaderComponent.class, MeshComponent.class)); + public MarkerRenderingSystem(ModelBatch batch){ + super(Aspect.getAspectForAll(MarkerCodeComponent.class, GeometryComponent.class, ShaderComponent.class, EnvironmentComponent.class, ModelComponent.class)); - markers = null; - translationMatrix = new Matrix4().setToTranslation(0.0f, 0.0f, 0.0f); - rotationMatrix = new Matrix4().idt(); - scalingMatrix = new Matrix4().setToScaling(0.0f, 0.0f, 0.0f); - combinedTransformationMatrix = new Matrix4(); + markers = null; + camera = null; + this.batch = batch; + translationMatrix = new Matrix4().setToTranslation(0.0f, 0.0f, 0.0f); + rotationMatrix = new Matrix4().idt(); + scalingMatrix = new Matrix4().setToScaling(0.0f, 0.0f, 0.0f); } - public void setMarkerData(MarkerData markers){ + public void begin(PerspectiveCamera camera, MarkerData markers) throws RuntimeException{ + if(this.camera != null) + throw new RuntimeException("Begin called twice without calling end."); + + if(this.markers != null) + throw new RuntimeException("Begin called twice without calling end."); + this.markers = markers; + this.camera = camera; + batch.begin(camera); + } + + public void end(){ + batch.end(); + camera = null; + markers = null; } @Override protected void process(Entity e) { MarkerCodeComponent marker; GeometryComponent geometry; - CustomShaderComponent shaderComp; - MeshComponent meshComp; + EnvironmentComponent environment; + ModelComponent model; + ShaderComponent shader; - if(markers == null) + if(markers == null || camera == null) return; Gdx.app.log(TAG, CLASS_NAME + ".process(): Getting components."); - marker = markerMapper.get(e); - geometry = geometryMapper.get(e); - shaderComp = shaderMapper.get(e); - meshComp = meshMapper.get(e); + marker = markerMapper.get(e); + geometry = geometryMapper.get(e); + model = modelMapper.get(e); + environment = environmentMapper.get(e); + shader = shaderMapper.get(e); Gdx.app.log(TAG, CLASS_NAME + ".process(): Processing markers."); for(int i = 0; i < ProjectConstants.MAXIMUM_NUMBER_OF_MARKERS; i++){ @@ -123,14 +141,13 @@ public class MarkerRenderingSystem extends EntityProcessingSystem { rotationMatrix.val[Matrix4.M33] = 1; scalingMatrix.setToScaling(geometry.scaling); - combinedTransformationMatrix.idt().mul(translationMatrix).mul(rotationMatrix).mul(scalingMatrix); - RenderParameters.setTransformationMatrix(combinedTransformationMatrix); + + // Apply the geometric transformations to the model. + model.instance.transform.idt().mul(translationMatrix).mul(rotationMatrix).mul(scalingMatrix); + model.instance.calculateTransforms(); // Render the marker; - shaderComp.shader.getShaderProgram().begin();{ - shaderComp.shader.setUniforms(); - meshComp.model.render(shaderComp.shader.getShaderProgram(), GL20.GL_TRIANGLES); - }shaderComp.shader.getShaderProgram().end(); + batch.render(model.instance, environment.environment, shader.shader); } }else{ Gdx.app.log(TAG, CLASS_NAME + ".process(): Skipping marker number " + Integer.toString(i) + "."); diff --git a/src/ve/ucv/ciens/ccg/nxtar/systems/ModelBatchMarkerRenderingSystem.java b/src/ve/ucv/ciens/ccg/nxtar/systems/ModelBatchMarkerRenderingSystem.java deleted file mode 100644 index 0fb4c12..0000000 --- a/src/ve/ucv/ciens/ccg/nxtar/systems/ModelBatchMarkerRenderingSystem.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright (C) 2014 Miguel Angel Astor Romero - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package ve.ucv.ciens.ccg.nxtar.systems; - -import ve.ucv.ciens.ccg.nxtar.components.EnvironmentComponent; -import ve.ucv.ciens.ccg.nxtar.components.GeometryComponent; -import ve.ucv.ciens.ccg.nxtar.components.MarkerCodeComponent; -import ve.ucv.ciens.ccg.nxtar.components.ModelComponent; -import ve.ucv.ciens.ccg.nxtar.components.ShaderComponent; -import ve.ucv.ciens.ccg.nxtar.interfaces.ImageProcessor.MarkerData; -import ve.ucv.ciens.ccg.nxtar.utils.ProjectConstants; - -import com.artemis.Aspect; -import com.artemis.ComponentMapper; -import com.artemis.Entity; -import com.artemis.annotations.Mapper; -import com.artemis.systems.EntityProcessingSystem; -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.PerspectiveCamera; -import com.badlogic.gdx.graphics.g3d.ModelBatch; -import com.badlogic.gdx.math.Matrix4; - -public class ModelBatchMarkerRenderingSystem extends EntityProcessingSystem { - @Mapper ComponentMapper markerMapper; - @Mapper ComponentMapper geometryMapper; - @Mapper ComponentMapper modelMapper; - @Mapper ComponentMapper environmentMapper; - @Mapper ComponentMapper shaderMapper; - - private static final String TAG = "MODEL_BATCH_MARKER_RENDERING_SYSTEM"; - private static final String CLASS_NAME = ModelBatchMarkerRenderingSystem.class.getSimpleName(); - - /** - *

A matrix representing 3D translations.

- */ - private Matrix4 translationMatrix; - - /** - *

A matrix representing 3D rotations.

- */ - private Matrix4 rotationMatrix; - - /** - *

A matrix representing 3D scalings.

- */ - private Matrix4 scalingMatrix; - - private MarkerData markers; - - private PerspectiveCamera camera; - - private ModelBatch batch; - - @SuppressWarnings("unchecked") - public ModelBatchMarkerRenderingSystem(){ - super(Aspect.getAspectForAll(MarkerCodeComponent.class, GeometryComponent.class, ShaderComponent.class, EnvironmentComponent.class, ModelComponent.class)); - - markers = null; - camera = null; - batch = new ModelBatch(); - translationMatrix = new Matrix4().setToTranslation(0.0f, 0.0f, 0.0f); - rotationMatrix = new Matrix4().idt(); - scalingMatrix = new Matrix4().setToScaling(0.0f, 0.0f, 0.0f); - } - - public void dispose(){ - batch.dispose(); - } - - public void begin(PerspectiveCamera camera, MarkerData markers) throws RuntimeException{ - if(this.camera != null) - throw new RuntimeException("Begin called twice without calling end."); - - if(this.markers != null) - throw new RuntimeException("Begin called twice without calling end."); - - this.markers = markers; - this.camera = camera; - batch.begin(camera); - } - - public void end(){ - batch.end(); - camera = null; - markers = null; - } - - @Override - protected void process(Entity e) { - MarkerCodeComponent marker; - GeometryComponent geometry; - EnvironmentComponent environment; - ModelComponent model; - ShaderComponent shader; - - if(markers == null || camera == null) - return; - - Gdx.app.log(TAG, CLASS_NAME + ".process(): Getting components."); - marker = markerMapper.get(e); - geometry = geometryMapper.get(e); - model = modelMapper.get(e); - environment = environmentMapper.get(e); - shader = shaderMapper.get(e); - - Gdx.app.log(TAG, CLASS_NAME + ".process(): Processing markers."); - for(int i = 0; i < ProjectConstants.MAXIMUM_NUMBER_OF_MARKERS; i++){ - if(markers.markerCodes[i] != 1){ - if(markers.markerCodes[i] == marker.code){ - Gdx.app.log(TAG, CLASS_NAME + ".process(): Rendering marker code " + Integer.toString(markers.markerCodes[i]) + "."); - // Set the geometric transformations. - translationMatrix.setToTranslation(geometry.position); - - rotationMatrix.val[Matrix4.M00] = geometry.rotation.val[0]; - rotationMatrix.val[Matrix4.M10] = geometry.rotation.val[1]; - rotationMatrix.val[Matrix4.M20] = geometry.rotation.val[2]; - rotationMatrix.val[Matrix4.M30] = 0; - - rotationMatrix.val[Matrix4.M01] = geometry.rotation.val[3]; - rotationMatrix.val[Matrix4.M11] = geometry.rotation.val[4]; - rotationMatrix.val[Matrix4.M21] = geometry.rotation.val[5]; - rotationMatrix.val[Matrix4.M31] = 0; - - rotationMatrix.val[Matrix4.M02] = geometry.rotation.val[6]; - rotationMatrix.val[Matrix4.M12] = geometry.rotation.val[7]; - rotationMatrix.val[Matrix4.M22] = geometry.rotation.val[8]; - rotationMatrix.val[Matrix4.M32] = 0; - - rotationMatrix.val[Matrix4.M03] = 0; - rotationMatrix.val[Matrix4.M13] = 0; - rotationMatrix.val[Matrix4.M23] = 0; - rotationMatrix.val[Matrix4.M33] = 1; - - scalingMatrix.setToScaling(geometry.scaling); - - // Apply the geometric transformations to the model. - model.instance.transform.idt().mul(translationMatrix).mul(rotationMatrix).mul(scalingMatrix); - model.instance.calculateTransforms(); - - // Render the marker; - batch.render(model.instance, environment.environment, shader.shader); - } - }else{ - Gdx.app.log(TAG, CLASS_NAME + ".process(): Skipping marker number " + Integer.toString(i) + "."); - } - } - } - -} diff --git a/src/ve/ucv/ciens/ccg/nxtar/systems/ObjectRenderingSystem.java b/src/ve/ucv/ciens/ccg/nxtar/systems/ObjectRenderingSystem.java index 011fde5..63a7d2b 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/systems/ObjectRenderingSystem.java +++ b/src/ve/ucv/ciens/ccg/nxtar/systems/ObjectRenderingSystem.java @@ -15,18 +15,19 @@ */ package ve.ucv.ciens.ccg.nxtar.systems; +import ve.ucv.ciens.ccg.nxtar.components.EnvironmentComponent; import ve.ucv.ciens.ccg.nxtar.components.GeometryComponent; import ve.ucv.ciens.ccg.nxtar.components.MarkerCodeComponent; -import ve.ucv.ciens.ccg.nxtar.components.MeshComponent; -import ve.ucv.ciens.ccg.nxtar.components.CustomShaderComponent; -import ve.ucv.ciens.ccg.nxtar.graphics.RenderParameters; +import ve.ucv.ciens.ccg.nxtar.components.ModelComponent; +import ve.ucv.ciens.ccg.nxtar.components.ShaderComponent; import com.artemis.Aspect; import com.artemis.ComponentMapper; import com.artemis.Entity; import com.artemis.annotations.Mapper; import com.artemis.systems.EntityProcessingSystem; -import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.PerspectiveCamera; +import com.badlogic.gdx.graphics.g3d.ModelBatch; import com.badlogic.gdx.math.Matrix4; /** @@ -34,9 +35,10 @@ import com.badlogic.gdx.math.Matrix4; * entities to be rendered must have a geometry, shader and mesh component associated.

*/ public class ObjectRenderingSystem extends EntityProcessingSystem { - @Mapper ComponentMapper geometryMapper; - @Mapper ComponentMapper shaderMapper; - @Mapper ComponentMapper modelMapper; + @Mapper ComponentMapper geometryMapper; + @Mapper ComponentMapper shaderMapper; + @Mapper ComponentMapper modelMapper; + @Mapper ComponentMapper environmentMapper; /** *

A matrix representing 3D translations.

@@ -53,19 +55,32 @@ public class ObjectRenderingSystem extends EntityProcessingSystem { */ private Matrix4 scalingMatrix; - /** - *

The total transformation to be applied to an entity.

- */ - private Matrix4 combinedTransformationMatrix; + private PerspectiveCamera camera; + + private ModelBatch batch; @SuppressWarnings("unchecked") - public ObjectRenderingSystem() { - super(Aspect.getAspectForAll(GeometryComponent.class, CustomShaderComponent.class, MeshComponent.class).exclude(MarkerCodeComponent.class)); + public ObjectRenderingSystem(ModelBatch batch) { + super(Aspect.getAspectForAll(GeometryComponent.class, ShaderComponent.class, ModelComponent.class, EnvironmentComponent.class).exclude(MarkerCodeComponent.class)); + camera = null; + this.batch = batch; translationMatrix = new Matrix4().setToTranslation(0.0f, 0.0f, 0.0f); - rotationMatrix = new Matrix4().idt(); - scalingMatrix = new Matrix4().setToScaling(0.0f, 0.0f, 0.0f); - combinedTransformationMatrix = new Matrix4(); + rotationMatrix = new Matrix4().idt(); + scalingMatrix = new Matrix4().setToScaling(0.0f, 0.0f, 0.0f); + } + + public void begin(PerspectiveCamera camera) throws RuntimeException{ + if(this.camera != null) + throw new RuntimeException("Begin called twice without calling end."); + + this.camera = camera; + batch.begin(camera); + } + + public void end(){ + batch.end(); + camera = null; } /** @@ -76,28 +91,25 @@ public class ObjectRenderingSystem extends EntityProcessingSystem { */ @Override protected void process(Entity e) { - GeometryComponent geometryComponent; - CustomShaderComponent customShaderComponent; - MeshComponent meshComponent; + EnvironmentComponent environment; + GeometryComponent geometryComponent; + ShaderComponent shaderComponent; + ModelComponent modelComponent; // Get the necessary components. geometryComponent = geometryMapper.get(e); - meshComponent = modelMapper.get(e); - customShaderComponent = shaderMapper.get(e); + modelComponent = modelMapper.get(e); + shaderComponent = shaderMapper.get(e); + environment = environmentMapper.get(e); // Calculate the geometric transformation for this entity. translationMatrix.setToTranslation(geometryComponent.position); rotationMatrix.set(geometryComponent.rotation); scalingMatrix.setToScaling(geometryComponent.scaling); - combinedTransformationMatrix.idt().mul(translationMatrix).mul(rotationMatrix).mul(scalingMatrix); - - // Set up the global rendering parameters for this frame. - RenderParameters.setTransformationMatrix(combinedTransformationMatrix); + modelComponent.instance.transform.idt().mul(translationMatrix).mul(rotationMatrix).mul(scalingMatrix); + modelComponent.instance.calculateTransforms(); // Render this entity. - customShaderComponent.shader.getShaderProgram().begin();{ - customShaderComponent.shader.setUniforms(); - meshComponent.model.render(customShaderComponent.shader.getShaderProgram(), GL20.GL_TRIANGLES); - }customShaderComponent.shader.getShaderProgram().end(); + batch.render(modelComponent.instance, environment.environment, shaderComponent.shader); } }