12 Commits

22 changed files with 1087 additions and 525 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -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.ImageProcessor;
import ve.ucv.ciens.ccg.nxtar.interfaces.ApplicationEventsListener; 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.RobotControlThread;
import ve.ucv.ciens.ccg.nxtar.network.SensorReportThread; import ve.ucv.ciens.ccg.nxtar.network.SensorReportThread;
import ve.ucv.ciens.ccg.nxtar.network.ServiceDiscoveryThread; import ve.ucv.ciens.ccg.nxtar.network.ServiceDiscoveryThread;
@@ -121,7 +121,7 @@ public class NxtARCore extends Game implements ApplicationEventsListener{
/** /**
* <p>Wrapper around the Operating System methods.</p> * <p>Wrapper around the Operating System methods.</p>
*/ */
private AndroidFunctionalityWrapper osFunction; private ActionResolver osFunction;
// Networking related fields. // Networking related fields.
/** /**
@@ -206,7 +206,7 @@ public class NxtARCore extends Game implements ApplicationEventsListener{
// Check if the concrete application implements all required interfaces. // Check if the concrete application implements all required interfaces.
try{ try{
this.osFunction = (AndroidFunctionalityWrapper)concreteApp; this.osFunction = (ActionResolver)concreteApp;
}catch(ClassCastException cc){ }catch(ClassCastException cc){
Gdx.app.debug(TAG, CLASS_NAME + ".Main() :: concreteApp does not implement the Toaster interface. Toasting disabled."); Gdx.app.debug(TAG, CLASS_NAME + ".Main() :: concreteApp does not implement the Toaster interface. Toasting disabled.");
this.osFunction = null; this.osFunction = null;

View File

@@ -0,0 +1,58 @@
/*
* 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.components;
import java.util.LinkedList;
import java.util.List;
import com.artemis.Component;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.utils.AnimationController;
public class AnimationComponent extends Component {
public AnimationController controller;
public List<String> animationsIds;
public int current;
public int next;
public boolean loop;
public AnimationComponent(ModelInstance instance) throws IllegalArgumentException{
this(instance, -1, false);
}
public AnimationComponent(ModelInstance instance, int next) throws IllegalArgumentException{
this(instance, next, false);
}
public AnimationComponent(ModelInstance instance, int next, boolean loop) throws IllegalArgumentException{
if(instance == null)
throw new IllegalArgumentException("Instance is null.");
else if(next < 0)
throw new IllegalArgumentException("Next is less than 0.");
else if(next > instance.animations.size)
throw new IllegalArgumentException("Next is greater than the number of animations for this model.");
controller = new AnimationController(instance);
animationsIds = new LinkedList<String>();
current = -1;
this.next = next;
this.loop = loop;
for(int i = 0; i < instance.animations.size; i++){
animationsIds.add(instance.animations.get(i).id);
}
}
}

View File

@@ -0,0 +1,31 @@
/*
* 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.components;
import ve.ucv.ciens.ccg.nxtar.graphics.shaders.CustomShaderBase;
import com.artemis.Component;
public class CustomShaderComponent extends Component {
public CustomShaderBase shader;
public CustomShaderComponent(CustomShaderBase shader) throws IllegalArgumentException{
if(shader == null)
throw new IllegalArgumentException("Shader cannot be null.");
this.shader = shader;
}
}

View File

@@ -0,0 +1,30 @@
/*
* 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.components;
import com.artemis.Component;
import com.badlogic.gdx.graphics.g3d.Environment;
public class EnvironmentComponent extends Component {
public Environment environment;
public EnvironmentComponent(Environment environment) throws IllegalArgumentException{
if(environment == null)
throw new IllegalArgumentException("Environment is null.");
this.environment = environment;
}
}

View File

@@ -0,0 +1,31 @@
/*
* 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.components;
import com.artemis.Component;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
public class ModelComponent extends Component {
public ModelInstance instance;
public ModelComponent(Model model) throws IllegalArgumentException{
if(model == null)
throw new IllegalArgumentException("Model is null.");
this.instance = new ModelInstance(model);
}
}

View File

@@ -15,16 +15,15 @@
*/ */
package ve.ucv.ciens.ccg.nxtar.components; package ve.ucv.ciens.ccg.nxtar.components;
import ve.ucv.ciens.ccg.nxtar.graphics.shaders.CustomShaderBase;
import com.artemis.Component; import com.artemis.Component;
import com.badlogic.gdx.graphics.g3d.Shader;
public class ShaderComponent extends Component { public class ShaderComponent extends Component{
public CustomShaderBase shader; public Shader shader;
public ShaderComponent(CustomShaderBase shader) throws IllegalArgumentException{ public ShaderComponent(Shader shader) throws IllegalArgumentException{
if(shader == null) if(shader == null)
throw new IllegalArgumentException("Shader cannot be null."); throw new IllegalArgumentException("Shader is null.");
this.shader = shader; this.shader = shader;
} }

View File

@@ -15,79 +15,113 @@
*/ */
package ve.ucv.ciens.ccg.nxtar.entities; package ve.ucv.ciens.ccg.nxtar.entities;
import ve.ucv.ciens.ccg.nxtar.components.AnimationComponent;
import ve.ucv.ciens.ccg.nxtar.components.EnvironmentComponent;
import ve.ucv.ciens.ccg.nxtar.components.GeometryComponent; import ve.ucv.ciens.ccg.nxtar.components.GeometryComponent;
import ve.ucv.ciens.ccg.nxtar.components.MarkerCodeComponent; 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.components.ShaderComponent;
import ve.ucv.ciens.ccg.nxtar.exceptions.ShaderFailedToLoadException; import ve.ucv.ciens.ccg.nxtar.graphics.shaders.SingleLightPerPixelShader;
import ve.ucv.ciens.ccg.nxtar.graphics.shaders.CustomShaderBase;
import ve.ucv.ciens.ccg.nxtar.graphics.shaders.SingleLightPhongShader;
import com.artemis.Entity; import com.artemis.Entity;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute; import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes; import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.VertexAttributes.Usage; import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.utils.MeshBuilder; 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.ModelBuilder;
import com.badlogic.gdx.math.Matrix3; import com.badlogic.gdx.math.Matrix3;
import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.JsonReader;
public class MarkerTestEntityCreator extends EntityCreatorBase { public class MarkerTestEntityCreator extends EntityCreatorBase {
private static final String TAG = "MARKER_TEST_ENTITY_CREATOR"; private static final String TAG = "MARKER_TEST_ENTITY_CREATOR";
private static final String CLASS_NAME = MarkerTestEntityCreator.class.getSimpleName(); private static final String CLASS_NAME = MarkerTestEntityCreator.class.getSimpleName();
private Mesh markerMesh; private Model bombModel;
private CustomShaderBase phongShader; private Model animatedModel;
private Model boxModel;
private SingleLightPerPixelShader ppShader;
@Override @Override
public void createAllEntities() { public void createAllEntities() {
MeshBuilder builder; ModelBuilder builder;
Matrix3 identity = new Matrix3().idt(); Entity bomb, box, anim;
Entity marker; G3dModelLoader loader;
Environment environment;
Material material;
// Create mesh. // Create mesh.
Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Creating the meshes."); 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);
Vector3 v00 = new Vector3(-0.5f, -0.5f, 0.0f);
Vector3 v10 = new Vector3(-0.5f, 0.5f, 0.0f);
Vector3 v11 = new Vector3( 0.5f, 0.5f, 0.0f);
Vector3 v01 = new Vector3( 0.5f, -0.5f, 0.0f);
Vector3 n = new Vector3(0.0f, 1.0f, 0.0f);
builder.patch(v00, v10, v11, v01, n, 10, 10);
}markerMesh = builder.end();
// Load the phong shader. loader = new G3dModelLoader(new JsonReader());
Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Loading the phong shader.");
try{ bombModel = loader.loadModel(Gdx.files.internal("models/Bomb_test_2.g3dj"));
phongShader = new SingleLightPhongShader().loadShader(); animatedModel = loader.loadModel(Gdx.files.internal("models/cube.g3dj"));
}catch(ShaderFailedToLoadException se){
Gdx.app.error(TAG, CLASS_NAME + ".InGameState(): " + se.getMessage()); 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));
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(1, 0, 0.5f)));
// Create the entities. // Create the entities.
Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Creating the enitites."); Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Creating the enitites.");
marker = world.createEntity(); bomb = world.createEntity();
marker.addComponent(new GeometryComponent(new Vector3(0.0f, 0.0f, 0.0f), identity, new Vector3(1.0f, 1.0f, 1.0f))); bomb.addComponent(new GeometryComponent(new Vector3(0.0f, 0.0f, 0.0f), new Matrix3().idt(), new Vector3(1.0f, 1.0f, 1.0f)));
marker.addComponent(new MeshComponent(markerMesh)); bomb.addComponent(new ModelComponent(bombModel));
marker.addComponent(new ShaderComponent(phongShader)); bomb.addComponent(new EnvironmentComponent(environment));
marker.addComponent(new MarkerCodeComponent(213)); bomb.addComponent(new ShaderComponent(ppShader));
bomb.addComponent(new MarkerCodeComponent(1023));
anim = world.createEntity();
anim.addComponent(new GeometryComponent(new Vector3(0.0f, 0.0f, 0.0f), new Matrix3().idt(), new Vector3(0.25f, 0.25f, -0.25f)));
anim.addComponent(new ModelComponent(animatedModel));
anim.addComponent(new AnimationComponent(anim.getComponent(ModelComponent.class).instance, 0, true));
anim.addComponent(new EnvironmentComponent(environment));
anim.addComponent(new MarkerCodeComponent(89));
anim.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 ModelComponent(boxModel));
box.addComponent(new ShaderComponent(ppShader));
box.addComponent(new EnvironmentComponent(environment));
// Add the entities to the world. // Add the entities to the world.
Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Adding entities to the world."); Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Adding entities to the world.");
marker.addToWorld(); //sphere.addToWorld();
bomb.addToWorld();
anim.addToWorld();
box.addToWorld();
} }
@Override @Override
public void dispose() { public void dispose() {
if(phongShader != null && phongShader.getShaderProgram() != null) if(boxModel != null)
phongShader.getShaderProgram().dispose(); boxModel.dispose();
if(markerMesh != null) if(animatedModel != null)
markerMesh.dispose(); animatedModel.dispose();
if(bombModel != null)
bombModel.dispose();
if(ppShader != null)
ppShader.dispose();
} }
} }

View File

@@ -17,7 +17,7 @@ package ve.ucv.ciens.ccg.nxtar.entities;
import ve.ucv.ciens.ccg.nxtar.components.GeometryComponent; import ve.ucv.ciens.ccg.nxtar.components.GeometryComponent;
import ve.ucv.ciens.ccg.nxtar.components.MeshComponent; import ve.ucv.ciens.ccg.nxtar.components.MeshComponent;
import ve.ucv.ciens.ccg.nxtar.components.ShaderComponent; import ve.ucv.ciens.ccg.nxtar.components.CustomShaderComponent;
import ve.ucv.ciens.ccg.nxtar.exceptions.ShaderFailedToLoadException; 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.CustomShaderBase;
import ve.ucv.ciens.ccg.nxtar.graphics.shaders.SingleLightPhongShader; import ve.ucv.ciens.ccg.nxtar.graphics.shaders.SingleLightPhongShader;
@@ -89,22 +89,22 @@ public class TestGameEntityCreator extends EntityCreatorBase {
sphere = world.createEntity(); sphere = world.createEntity();
sphere.addComponent(new GeometryComponent(new Vector3(0.5f, 0.5f, 0.0f), identity, new Vector3(1.0f, 1.0f, 1.0f))); sphere.addComponent(new GeometryComponent(new Vector3(0.5f, 0.5f, 0.0f), identity, new Vector3(1.0f, 1.0f, 1.0f)));
sphere.addComponent(new MeshComponent(sphereMesh)); sphere.addComponent(new MeshComponent(sphereMesh));
sphere.addComponent(new ShaderComponent(singleLightPhongShader)); sphere.addComponent(new CustomShaderComponent(singleLightPhongShader));
cube = world.createEntity(); cube = world.createEntity();
cube.addComponent(new GeometryComponent(new Vector3(-0.5f, -0.5f, 0.0f), identity, new Vector3(1.0f, 1.0f, 1.0f))); cube.addComponent(new GeometryComponent(new Vector3(-0.5f, -0.5f, 0.0f), identity, new Vector3(1.0f, 1.0f, 1.0f)));
cube.addComponent(new MeshComponent(cubeMesh)); cube.addComponent(new MeshComponent(cubeMesh));
cube.addComponent(new ShaderComponent(singleLightPhongShader)); cube.addComponent(new CustomShaderComponent(singleLightPhongShader));
capsule1 = world.createEntity(); capsule1 = world.createEntity();
capsule1.addComponent(new GeometryComponent(new Vector3(-0.5f, 0.5f, 0.0f), identity, new Vector3(1.5f, 1.0f, 1.0f))); capsule1.addComponent(new GeometryComponent(new Vector3(-0.5f, 0.5f, 0.0f), identity, new Vector3(1.5f, 1.0f, 1.0f)));
capsule1.addComponent(new MeshComponent(capsuleMesh)); capsule1.addComponent(new MeshComponent(capsuleMesh));
capsule1.addComponent(new ShaderComponent(singleLightPhongShader)); capsule1.addComponent(new CustomShaderComponent(singleLightPhongShader));
capsule2 = world.createEntity(); capsule2 = world.createEntity();
capsule2.addComponent(new GeometryComponent(new Vector3(0.5f, -0.5f, 0.0f), identity, new Vector3(1.0f, 1.5f, 1.0f))); capsule2.addComponent(new GeometryComponent(new Vector3(0.5f, -0.5f, 0.0f), identity, new Vector3(1.0f, 1.5f, 1.0f)));
capsule2.addComponent(new MeshComponent(capsuleMesh)); capsule2.addComponent(new MeshComponent(capsuleMesh));
capsule2.addComponent(new ShaderComponent(singleLightPhongShader)); capsule2.addComponent(new CustomShaderComponent(singleLightPhongShader));
// Add the entities to the world. // Add the entities to the world.
Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Adding entities to the world."); Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Adding entities to the world.");

View File

@@ -0,0 +1,230 @@
/*
* 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.graphics.shaders;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.g3d.Renderable;
import com.badlogic.gdx.graphics.g3d.Shader;
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 int MAX_NUM_BONES = 4;
private static final Matrix4 IDENTITY = new Matrix4();
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 static final String INCLUDE_SKINNING = "#define SKINNING\n";
private ShaderProgram skinningProgram;
private ShaderProgram baseProgram;
private Camera camera;
private RenderContext context;
private Matrix4 normalMatrix;
// Uniform locations.
private int[] u_geomTrans;
private int[] u_projTrans;
private int[] u_lightPos;
private int[] u_lightDiffuse;
private int[] u_specular;
private int[] u_ambient;
private int[] u_shiny;
private int[] u_cameraPos;
private int[] u_materialDiffuse;
private int[] u_normalMatrix;
private int[] u_bones;
public SingleLightPerPixelShader(){
skinningProgram = null;
baseProgram = null;
camera = null;
context = null;
}
@Override
public void init() throws GdxRuntimeException{
normalMatrix = new Matrix4().idt();
u_bones = new int[MAX_NUM_BONES];
// Compile the shader.
String vertexCode = Gdx.files.internal(VERTEX_SHADER_PATH).readString();
String fragmentCode = Gdx.files.internal(FRAGMENT_SHADER_PATH).readString();
skinningProgram = new ShaderProgram(INCLUDE_SKINNING + vertexCode, fragmentCode);
baseProgram = new ShaderProgram(vertexCode, fragmentCode);
if(!skinningProgram.isCompiled())
throw new GdxRuntimeException(skinningProgram.getLog());
if(!baseProgram.isCompiled())
throw new GdxRuntimeException(skinningProgram.getLog());
// Create uniform locations.
u_projTrans = new int[2];
u_geomTrans = new int[2];
u_lightPos = new int[2];
u_lightDiffuse = new int[2];
u_specular = new int[2];
u_ambient = new int[2];
u_shiny = new int[2];
u_cameraPos = new int[2];
u_materialDiffuse = new int[2];
u_normalMatrix = new int[2];
// Cache uniform locations.
u_projTrans [0] = skinningProgram.getUniformLocation("u_projTrans");
u_geomTrans [0] = skinningProgram.getUniformLocation("u_geomTrans");
u_lightPos [0] = skinningProgram.getUniformLocation("u_lightPos");
u_lightDiffuse [0] = skinningProgram.getUniformLocation("u_lightDiffuse");
u_specular [0] = skinningProgram.getUniformLocation("u_specular");
u_ambient [0] = skinningProgram.getUniformLocation("u_ambient");
u_shiny [0] = skinningProgram.getUniformLocation("u_shiny");
u_cameraPos [0] = skinningProgram.getUniformLocation("u_cameraPos");
u_materialDiffuse [0] = skinningProgram.getUniformLocation("u_materialDiffuse");
u_normalMatrix [0] = skinningProgram.getUniformLocation("u_normalMatrix");
u_projTrans [1] = baseProgram.getUniformLocation("u_projTrans");
u_geomTrans [1] = baseProgram.getUniformLocation("u_geomTrans");
u_lightPos [1] = baseProgram.getUniformLocation("u_lightPos");
u_lightDiffuse [1] = baseProgram.getUniformLocation("u_lightDiffuse");
u_specular [1] = baseProgram.getUniformLocation("u_specular");
u_ambient [1] = baseProgram.getUniformLocation("u_ambient");
u_shiny [1] = baseProgram.getUniformLocation("u_shiny");
u_cameraPos [1] = baseProgram.getUniformLocation("u_cameraPos");
u_materialDiffuse [1] = baseProgram.getUniformLocation("u_materialDiffuse");
u_normalMatrix [1] = baseProgram.getUniformLocation("u_normalMatrix");
for(int i = 0; i < MAX_NUM_BONES; i++){
u_bones[i] = skinningProgram.getUniformLocation("u_bone" + Integer.toString(i));
}
}
@Override
public void dispose(){
if(skinningProgram != null) skinningProgram.dispose();
if(baseProgram != null) baseProgram.dispose();
}
@Override
public int compareTo(Shader other){
return 0;
}
@Override
public boolean canRender(Renderable renderable){
// 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;
return true;
}
@Override
public void begin(Camera camera, RenderContext context) throws GdxRuntimeException{
if(this.camera != null || this.context != null)
throw new GdxRuntimeException("Called begin twice before calling end.");
this.camera = camera;
this.context = context;
// Set render context.
this.context.setDepthTest(GL20.GL_LEQUAL);
this.context.setDepthMask(true);
}
@Override
public void render(Renderable renderable){
ShaderProgram program;
int index;
boolean bonesEnabled;
// 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;
if(renderable.mesh.getVertexAttribute(VertexAttributes.Usage.BoneWeight) != null){
program = skinningProgram;
index = 0;
bonesEnabled = true;
}else{
program = baseProgram;
index = 1;
bonesEnabled = false;
}
program.begin();
// Set camera dependant uniforms.
program.setUniformMatrix(u_projTrans[index], this.camera.combined);
program.setUniformf(u_cameraPos[index], this.camera.position);
// Set model dependant uniforms.
program.setUniformMatrix(u_geomTrans[index], renderable.worldTransform);
program.setUniformMatrix(u_normalMatrix[index], normalMatrix.idt().mul(renderable.worldTransform).inv().tra());
program.setUniformf(u_lightPos[index], lightPosition);
program.setUniformf(u_lightDiffuse[index], diffuseLightColor);
program.setUniformf(u_materialDiffuse[index], diffuseColor);
program.setUniformf(u_specular[index], specularColor);
program.setUniformf(u_ambient[index], ambientColor);
program.setUniformf(u_shiny[index], shininess);
// Set the bones uniforms.
if(bonesEnabled){
for(int i = 0; i < MAX_NUM_BONES; i++){
if(renderable.bones != null && i < renderable.bones.length && renderable.bones[i] != null)
skinningProgram.setUniformMatrix(u_bones[i], renderable.bones[i]);
else
skinningProgram.setUniformMatrix(u_bones[i], IDENTITY);
}
}
renderable.mesh.render(program, renderable.primitiveType, renderable.meshPartOffset, renderable.meshPartSize);
program.end();
}
@Override
public void end(){
this.camera = null;
this.context = null;
}
}

View File

@@ -15,7 +15,7 @@
*/ */
package ve.ucv.ciens.ccg.nxtar.interfaces; package ve.ucv.ciens.ccg.nxtar.interfaces;
public interface AndroidFunctionalityWrapper{ public interface ActionResolver{
public void showShortToast(String msg); public void showShortToast(String msg);
public void showLongToast(String msg); public void showLongToast(String msg);
public void enableMulticast(); public void enableMulticast();

View File

@@ -40,6 +40,7 @@ import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.graphics.glutils.ShaderProgram; import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
@@ -93,6 +94,7 @@ public class CameraCalibrationState extends BaseState{
public CameraCalibrationState(final NxtARCore core){ public CameraCalibrationState(final NxtARCore core){
TextButtonStyle tbs; TextButtonStyle tbs;
FreeTypeFontGenerator generator; FreeTypeFontGenerator generator;
FreeTypeFontParameter param;
this.core = core; this.core = core;
frameMonitor = VideoFrameMonitor.getInstance(); frameMonitor = VideoFrameMonitor.getInstance();
@@ -124,8 +126,12 @@ public class CameraCalibrationState extends BaseState{
// Set up the sampling button. // Set up the sampling button.
// Create the font. // Create the font.
param = new FreeTypeFontParameter();
param.characters = ProjectConstants.FONT_CHARS;
param.size = ProjectConstants.MENU_BUTTON_FONT_SIZE;
param.flip = false;
generator = new FreeTypeFontGenerator(Gdx.files.internal("data/fonts/d-puntillas-B-to-tiptoe.ttf")); generator = new FreeTypeFontGenerator(Gdx.files.internal("data/fonts/d-puntillas-B-to-tiptoe.ttf"));
font = generator.generateFont(ProjectConstants.MENU_BUTTON_FONT_SIZE, ProjectConstants.FONT_CHARS, false); font = generator.generateFont(param);
generator.dispose(); generator.dispose();
// Load the textures. // Load the textures.

View File

@@ -22,10 +22,12 @@ import ve.ucv.ciens.ccg.nxtar.NxtARCore.game_states_t;
import ve.ucv.ciens.ccg.nxtar.entities.EntityCreatorBase; import ve.ucv.ciens.ccg.nxtar.entities.EntityCreatorBase;
import ve.ucv.ciens.ccg.nxtar.entities.MarkerTestEntityCreator; import ve.ucv.ciens.ccg.nxtar.entities.MarkerTestEntityCreator;
import ve.ucv.ciens.ccg.nxtar.graphics.CustomPerspectiveCamera; import ve.ucv.ciens.ccg.nxtar.graphics.CustomPerspectiveCamera;
import ve.ucv.ciens.ccg.nxtar.graphics.LightSource;
import ve.ucv.ciens.ccg.nxtar.graphics.RenderParameters; import ve.ucv.ciens.ccg.nxtar.graphics.RenderParameters;
import ve.ucv.ciens.ccg.nxtar.interfaces.ImageProcessor.MarkerData; import ve.ucv.ciens.ccg.nxtar.interfaces.ImageProcessor.MarkerData;
import ve.ucv.ciens.ccg.nxtar.network.monitors.MotorEventQueue; import ve.ucv.ciens.ccg.nxtar.network.monitors.MotorEventQueue;
import ve.ucv.ciens.ccg.nxtar.network.monitors.VideoFrameMonitor; import ve.ucv.ciens.ccg.nxtar.network.monitors.VideoFrameMonitor;
import ve.ucv.ciens.ccg.nxtar.systems.AnimationSystem;
import ve.ucv.ciens.ccg.nxtar.systems.MarkerPositioningSystem; import ve.ucv.ciens.ccg.nxtar.systems.MarkerPositioningSystem;
import ve.ucv.ciens.ccg.nxtar.systems.MarkerRenderingSystem; import ve.ucv.ciens.ccg.nxtar.systems.MarkerRenderingSystem;
import ve.ucv.ciens.ccg.nxtar.systems.ObjectRenderingSystem; import ve.ucv.ciens.ccg.nxtar.systems.ObjectRenderingSystem;
@@ -36,6 +38,7 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input; import com.badlogic.gdx.Input;
import com.badlogic.gdx.controllers.Controller; import com.badlogic.gdx.controllers.Controller;
import com.badlogic.gdx.controllers.mappings.Ouya; import com.badlogic.gdx.controllers.mappings.Ouya;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.Pixmap;
@@ -45,6 +48,7 @@ import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.Texture.TextureWrap; import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion; 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.FrameBuffer;
import com.badlogic.gdx.graphics.glutils.ShaderProgram; import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Matrix4; import com.badlogic.gdx.math.Matrix4;
@@ -52,58 +56,66 @@ import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.Vector3;
public class InGameState extends BaseState{ public class InGameState extends BaseState{
private static final String TAG = "IN_GAME_STATE"; private static final String TAG = "IN_GAME_STATE";
private static final String CLASS_NAME = InGameState.class.getSimpleName(); private static final String CLASS_NAME = InGameState.class.getSimpleName();
private static final String BACKGROUND_SHADER_PATH = "shaders/bckg/bckg"; private static final String BACKGROUND_SHADER_PATH = "shaders/bckg/bckg";
private static final float NEAR = 0.01f; private static final float NEAR = 0.01f;
private static final float FAR = 100.0f; private static final float FAR = 100.0f;
private static final float FAR_PLUS_NEAR = FAR + NEAR; private static final float FAR_PLUS_NEAR = FAR + NEAR;
private static final float FAR_LESS_NEAR = FAR - NEAR; private static final float FAR_LESS_NEAR = FAR - NEAR;
private static final Vector3 LIGHT_POSITION = new Vector3(2.0f, 2.0f, 4.0f);
private static final Color AMBIENT_COLOR = new Color(0.0f, 0.1f, 0.3f, 1.0f);
private static final Color DIFFUSE_COLOR = new Color(1.0f, 1.0f, 1.0f, 1.0f);
private static final Color SPECULAR_COLOR = new Color(1.0f, 0.8f, 0.0f, 1.0f);
private static final float SHINYNESS = 50.0f;
// Background related fields. // Background related fields.
private float uScaling[]; private float uScaling[];
protected Sprite background; protected Sprite background;
private Texture backgroundTexture; private Texture backgroundTexture;
private ShaderProgram backgroundShader; private ShaderProgram backgroundShader;
// 3D rendering fields. // 3D rendering fields.
private Matrix4 projectionMatrix; private ModelBatch modelBatch;
private FrameBuffer frameBuffer; private Matrix4 projectionMatrix;
private Sprite frameBufferSprite; private FrameBuffer frameBuffer;
private Sprite frameBufferSprite;
// Game objects. // Game world objects.
private World gameWorld; private World gameWorld;
private EntityCreatorBase entityCreator; private EntityCreatorBase entityCreator;
private MarkerRenderingSystem markerRenderingSystem;
private ObjectRenderingSystem objectRenderingSystem;
// Cameras. // Cameras.
private OrthographicCamera unitaryOrthoCamera; private OrthographicCamera unitaryOrthoCamera;
private OrthographicCamera pixelPerfectOrthoCamera; private OrthographicCamera pixelPerfectOrthoCamera;
private CustomPerspectiveCamera perspectiveCamera; private CustomPerspectiveCamera perspectiveCamera;
// Video stream graphics. // Video stream graphics.
private Texture videoFrameTexture; private Texture videoFrameTexture;
private Sprite renderableVideoFrame; private Sprite renderableVideoFrame;
private Pixmap videoFrame; private Pixmap videoFrame;
// Interface buttons. // Interface buttons.
private Texture buttonTexture; private Texture buttonTexture;
private Texture buttonTexture2; private Texture buttonTexture2;
private Sprite motorA; private Sprite motorA;
private Sprite motorB; private Sprite motorB;
private Sprite motorC; private Sprite motorC;
private Sprite motorD; private Sprite motorD;
private Sprite headA; private Sprite headA;
private Sprite headB; private Sprite headB;
private Sprite headC; private Sprite headC;
// Button touch helper fields. // Button touch helper fields.
private boolean[] motorButtonsTouched; private boolean[] motorButtonsTouched;
private int[] motorButtonsPointers; private int[] motorButtonsPointers;
private boolean[] motorGamepadButtonPressed; private boolean[] motorGamepadButtonPressed;
// Monitors. // Monitors.
private VideoFrameMonitor frameMonitor; private VideoFrameMonitor frameMonitor;
private MotorEventQueue queue; private MotorEventQueue queue;
public InGameState(final NxtARCore core){ public InGameState(final NxtARCore core){
this.core = core; this.core = core;
@@ -171,19 +183,28 @@ public class InGameState extends BaseState{
uScaling[1] = Gdx.graphics.getHeight() > Gdx.graphics.getWidth() ? 16.0f : 9.0f; uScaling[1] = Gdx.graphics.getHeight() > Gdx.graphics.getWidth() ? 16.0f : 9.0f;
// Set up the 3D rendering. // Set up the 3D rendering.
modelBatch = new ModelBatch();
projectionMatrix = new Matrix4().idt(); projectionMatrix = new Matrix4().idt();
frameBuffer = null; frameBuffer = null;
perspectiveCamera = null; perspectiveCamera = null;
frameBufferSprite = null; frameBufferSprite = null;
RenderParameters.setLightSource1(new LightSource(LIGHT_POSITION, AMBIENT_COLOR, DIFFUSE_COLOR, SPECULAR_COLOR, SHINYNESS));
// Set up the game world. // Set up the game world.
gameWorld = new World(); gameWorld = new World();
entityCreator = new MarkerTestEntityCreator(); entityCreator = new MarkerTestEntityCreator();
entityCreator.setWorld(gameWorld); entityCreator.setWorld(gameWorld);
entityCreator.createAllEntities(); entityCreator.createAllEntities();
gameWorld.setSystem(new MarkerPositioningSystem()); gameWorld.setSystem(new MarkerPositioningSystem());
gameWorld.setSystem(new MarkerRenderingSystem(), true); gameWorld.setSystem(new AnimationSystem());
gameWorld.setSystem(new ObjectRenderingSystem(), true);
markerRenderingSystem = new MarkerRenderingSystem(modelBatch);
objectRenderingSystem = new ObjectRenderingSystem(modelBatch);
gameWorld.setSystem(markerRenderingSystem, true);
gameWorld.setSystem(objectRenderingSystem, true);
gameWorld.initialize(); gameWorld.initialize();
} }
@@ -232,11 +253,6 @@ public class InGameState extends BaseState{
perspectiveCamera.update(); perspectiveCamera.update();
} }
// Apply the undistortion method if the camera has been calibrated already.
/*if(core.cvProc.isCameraCalibrated()){
frame = core.cvProc.undistortFrame(frame);
}*/
// Attempt to find the markers in the current video frame. // Attempt to find the markers in the current video frame.
data = core.cvProc.findMarkersInFrame(frame); data = core.cvProc.findMarkersInFrame(frame);
@@ -265,10 +281,9 @@ public class InGameState extends BaseState{
// Set the 3D frame buffer for rendering. // Set the 3D frame buffer for rendering.
frameBuffer.begin();{ frameBuffer.begin();{
// Set OpenGL state. // Set OpenGL state.
Gdx.gl.glDisable(GL20.GL_CULL_FACE);
Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
Gdx.gl.glClearColor(0, 0, 0, 0); Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
Gdx.gl.glDisable(GL20.GL_TEXTURE_2D);
// Build the projection matrix. // Build the projection matrix.
focalPointX = core.cvProc.getFocalPointX(); focalPointX = core.cvProc.getFocalPointX();
@@ -302,11 +317,13 @@ public class InGameState extends BaseState{
RenderParameters.setEyePosition(perspectiveCamera.position); RenderParameters.setEyePosition(perspectiveCamera.position);
// Call rendering systems. // Call rendering systems.
gameWorld.getSystem(MarkerRenderingSystem.class).setMarkerData(data); markerRenderingSystem.begin(perspectiveCamera, data);
gameWorld.getSystem(MarkerRenderingSystem.class).process(); markerRenderingSystem.process();
gameWorld.getSystem(ObjectRenderingSystem.class).process(); markerRenderingSystem.end();
Gdx.gl.glDisable(GL20.GL_DEPTH_TEST); objectRenderingSystem.begin(perspectiveCamera);
objectRenderingSystem.process();
objectRenderingSystem.end();
}frameBuffer.end(); }frameBuffer.end();
// Set the frame buffer object texture to a renderable sprite. // Set the frame buffer object texture to a renderable sprite.
@@ -375,6 +392,9 @@ public class InGameState extends BaseState{
@Override @Override
public void dispose(){ public void dispose(){
if(modelBatch != null)
modelBatch.dispose();
if(entityCreator != null) if(entityCreator != null)
entityCreator.dispose(); entityCreator.dispose();

View File

@@ -31,6 +31,7 @@ import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.graphics.glutils.ShaderProgram; import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
@@ -90,6 +91,7 @@ public abstract class MainMenuStateBase extends BaseState{
TextureRegion region; TextureRegion region;
TextButtonStyle tbs; TextButtonStyle tbs;
FreeTypeFontGenerator generator; FreeTypeFontGenerator generator;
FreeTypeFontParameter param;
this.pixelPerfectCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); this.pixelPerfectCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
@@ -104,8 +106,12 @@ public abstract class MainMenuStateBase extends BaseState{
menuButtonPressed9p = new NinePatch(new TextureRegion(menuButtonPressedTexture, 0, 0, menuButtonPressedTexture.getWidth(), menuButtonPressedTexture.getHeight()), 49, 49, 45, 45); menuButtonPressed9p = new NinePatch(new TextureRegion(menuButtonPressedTexture, 0, 0, menuButtonPressedTexture.getWidth(), menuButtonPressedTexture.getHeight()), 49, 49, 45, 45);
// Create the start button font. // Create the start button font.
param = new FreeTypeFontParameter();
param.characters = ProjectConstants.FONT_CHARS;
param.size = ProjectConstants.MENU_BUTTON_FONT_SIZE;
param.flip = false;
generator = new FreeTypeFontGenerator(Gdx.files.internal("data/fonts/d-puntillas-B-to-tiptoe.ttf")); generator = new FreeTypeFontGenerator(Gdx.files.internal("data/fonts/d-puntillas-B-to-tiptoe.ttf"));
font = generator.generateFont(ProjectConstants.MENU_BUTTON_FONT_SIZE, ProjectConstants.FONT_CHARS, false); font = generator.generateFont(param);
generator.dispose(); generator.dispose();
// Create the start button. // Create the start button.

View File

@@ -21,7 +21,7 @@ import ve.ucv.ciens.ccg.nxtar.NxtARCore.game_states_t;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.controllers.Controller; import com.badlogic.gdx.controllers.Controller;
import com.badlogic.gdx.controllers.mappings.Ouya; import com.badlogic.gdx.controllers.mappings.Ouya;
import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
@@ -66,7 +66,7 @@ public class OuyaMainMenuState extends MainMenuStateBase{
@Override @Override
public void render(float delta) { public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
core.batch.setProjectionMatrix(pixelPerfectCamera.combined); core.batch.setProjectionMatrix(pixelPerfectCamera.combined);
core.batch.begin();{ core.batch.begin();{

View File

@@ -18,7 +18,7 @@ package ve.ucv.ciens.ccg.nxtar.states;
import ve.ucv.ciens.ccg.nxtar.NxtARCore; import ve.ucv.ciens.ccg.nxtar.NxtARCore;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.graphics.GL20;
public class TabletMainMenuState extends MainMenuStateBase{ public class TabletMainMenuState extends MainMenuStateBase{
@@ -46,7 +46,7 @@ public class TabletMainMenuState extends MainMenuStateBase{
@Override @Override
public void render(float delta){ public void render(float delta){
Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
core.batch.setProjectionMatrix(pixelPerfectCamera.combined); core.batch.setProjectionMatrix(pixelPerfectCamera.combined);
core.batch.begin();{ core.batch.begin();{

View File

@@ -0,0 +1,49 @@
/*
* 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.AnimationComponent;
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;
public class AnimationSystem extends EntityProcessingSystem {
@Mapper ComponentMapper<AnimationComponent> animationMapper;
@SuppressWarnings("unchecked")
public AnimationSystem(){
super(Aspect.getAspectForAll(AnimationComponent.class));
}
@Override
protected void process(Entity e) {
AnimationComponent animation = animationMapper.get(e);
if(animation.current != animation.next && animation.next >= 0 && animation.next < animation.animationsIds.size()){
if(animation.loop)
animation.controller.setAnimation(animation.animationsIds.get(animation.next), -1);
else
animation.controller.setAnimation(animation.animationsIds.get(animation.next));
}
animation.controller.update(Gdx.graphics.getDeltaTime());
}
}

View File

@@ -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
}
}

View File

@@ -62,17 +62,16 @@ public class MarkerPositioningSystem extends EntityProcessingSystem {
Gdx.app.log(TAG, CLASS_NAME + ".process(): Processing markers."); Gdx.app.log(TAG, CLASS_NAME + ".process(): Processing markers.");
for(int i = 0; i < ProjectConstants.MAXIMUM_NUMBER_OF_MARKERS; i++){ for(int i = 0; i < ProjectConstants.MAXIMUM_NUMBER_OF_MARKERS; i++){
if(markers.markerCodes[i] != 1){ if(markers.markerCodes[i] != 1){
Gdx.app.log(TAG, CLASS_NAME + ".process(): Checking marker code: " + Integer.toString(markers.markerCodes[i]));
Gdx.app.log(TAG, CLASS_NAME + ".process(): This entity's code is: " + Integer.toString(marker.code));
if(markers.markerCodes[i] == marker.code){ if(markers.markerCodes[i] == marker.code){
Gdx.app.log(TAG, CLASS_NAME + ".process(): Processing marker code " + Integer.toString(markers.markerCodes[i]) + "."); Gdx.app.log(TAG, CLASS_NAME + ".process(): Processing marker code " + Integer.toString(markers.markerCodes[i]) + ".");
geometry.position.set(markers.translationVectors[i]); geometry.position.set(markers.translationVectors[i]);
geometry.rotation.set(markers.rotationMatrices[i]); geometry.rotation.set(markers.rotationMatrices[i]);
break;
} }
}else{ }else{
Gdx.app.log(TAG, CLASS_NAME + ".process(): Skipping marker number " + Integer.toString(i) + "."); Gdx.app.log(TAG, CLASS_NAME + ".process(): Skipping marker number " + Integer.toString(i) + ".");
} }
} }
markers = null;
} }
} }

View File

@@ -1,10 +1,25 @@
/*
* 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; 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.GeometryComponent;
import ve.ucv.ciens.ccg.nxtar.components.MarkerCodeComponent; 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.components.ShaderComponent;
import ve.ucv.ciens.ccg.nxtar.graphics.RenderParameters;
import ve.ucv.ciens.ccg.nxtar.interfaces.ImageProcessor.MarkerData; import ve.ucv.ciens.ccg.nxtar.interfaces.ImageProcessor.MarkerData;
import ve.ucv.ciens.ccg.nxtar.utils.ProjectConstants; import ve.ucv.ciens.ccg.nxtar.utils.ProjectConstants;
@@ -14,16 +29,18 @@ import com.artemis.Entity;
import com.artemis.annotations.Mapper; import com.artemis.annotations.Mapper;
import com.artemis.systems.EntityProcessingSystem; import com.artemis.systems.EntityProcessingSystem;
import com.badlogic.gdx.Gdx; 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; import com.badlogic.gdx.math.Matrix4;
public class MarkerRenderingSystem extends EntityProcessingSystem { public class MarkerRenderingSystem extends EntityProcessingSystem {
@Mapper ComponentMapper<MarkerCodeComponent> markerMapper; @Mapper ComponentMapper<MarkerCodeComponent> markerMapper;
@Mapper ComponentMapper<GeometryComponent> geometryMapper; @Mapper ComponentMapper<GeometryComponent> geometryMapper;
@Mapper ComponentMapper<ShaderComponent> shaderMapper; @Mapper ComponentMapper<ModelComponent> modelMapper;
@Mapper ComponentMapper<MeshComponent> meshMapper; @Mapper ComponentMapper<EnvironmentComponent> environmentMapper;
@Mapper ComponentMapper<ShaderComponent> 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(); private static final String CLASS_NAME = MarkerRenderingSystem.class.getSimpleName();
/** /**
@@ -41,43 +58,59 @@ public class MarkerRenderingSystem extends EntityProcessingSystem {
*/ */
private Matrix4 scalingMatrix; private Matrix4 scalingMatrix;
/** private MarkerData markers;
* <p>The total transformation to be applied to an entity.</p>
*/
private Matrix4 combinedTransformationMatrix;
MarkerData markers; private PerspectiveCamera camera;
private ModelBatch batch;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public MarkerRenderingSystem(){ public MarkerRenderingSystem(ModelBatch batch){
super(Aspect.getAspectForAll(MarkerCodeComponent.class, GeometryComponent.class, ShaderComponent.class, MeshComponent.class)); super(Aspect.getAspectForAll(MarkerCodeComponent.class, GeometryComponent.class, ShaderComponent.class, EnvironmentComponent.class, ModelComponent.class));
markers = null; markers = null;
camera = null;
this.batch = batch;
translationMatrix = new Matrix4().setToTranslation(0.0f, 0.0f, 0.0f); translationMatrix = new Matrix4().setToTranslation(0.0f, 0.0f, 0.0f);
rotationMatrix = new Matrix4().idt(); rotationMatrix = new Matrix4().idt();
scalingMatrix = new Matrix4().setToScaling(0.0f, 0.0f, 0.0f); scalingMatrix = new Matrix4().setToScaling(0.0f, 0.0f, 0.0f);
combinedTransformationMatrix = new Matrix4();
} }
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.markers = markers;
this.camera = camera;
batch.begin(camera);
}
public void end(){
batch.end();
camera = null;
markers = null;
} }
@Override @Override
protected void process(Entity e) { protected void process(Entity e) {
MarkerCodeComponent marker; MarkerCodeComponent marker;
GeometryComponent geometry; GeometryComponent geometry;
ShaderComponent shaderComp; EnvironmentComponent environment;
MeshComponent meshComp; ModelComponent model;
ShaderComponent shader;
if(markers == null) if(markers == null || camera == null)
return; return;
Gdx.app.log(TAG, CLASS_NAME + ".process(): Getting components."); Gdx.app.log(TAG, CLASS_NAME + ".process(): Getting components.");
marker = markerMapper.get(e); marker = markerMapper.get(e);
geometry = geometryMapper.get(e); geometry = geometryMapper.get(e);
shaderComp = shaderMapper.get(e); model = modelMapper.get(e);
meshComp = meshMapper.get(e); environment = environmentMapper.get(e);
shader = shaderMapper.get(e);
Gdx.app.log(TAG, CLASS_NAME + ".process(): Processing markers."); Gdx.app.log(TAG, CLASS_NAME + ".process(): Processing markers.");
for(int i = 0; i < ProjectConstants.MAXIMUM_NUMBER_OF_MARKERS; i++){ for(int i = 0; i < ProjectConstants.MAXIMUM_NUMBER_OF_MARKERS; i++){
@@ -87,41 +120,39 @@ public class MarkerRenderingSystem extends EntityProcessingSystem {
// Set the geometric transformations. // Set the geometric transformations.
translationMatrix.setToTranslation(geometry.position); translationMatrix.setToTranslation(geometry.position);
rotationMatrix.val[0] = geometry.rotation.val[0]; rotationMatrix.val[Matrix4.M00] = geometry.rotation.val[0];
rotationMatrix.val[1] = geometry.rotation.val[1]; rotationMatrix.val[Matrix4.M10] = geometry.rotation.val[1];
rotationMatrix.val[2] = geometry.rotation.val[2]; rotationMatrix.val[Matrix4.M20] = geometry.rotation.val[2];
rotationMatrix.val[3] = 0; rotationMatrix.val[Matrix4.M30] = 0;
rotationMatrix.val[4] = geometry.rotation.val[3];
rotationMatrix.val[5] = geometry.rotation.val[4]; rotationMatrix.val[Matrix4.M01] = geometry.rotation.val[3];
rotationMatrix.val[6] = geometry.rotation.val[5]; rotationMatrix.val[Matrix4.M11] = geometry.rotation.val[4];
rotationMatrix.val[7] = 0; rotationMatrix.val[Matrix4.M21] = geometry.rotation.val[5];
rotationMatrix.val[8] = geometry.rotation.val[6]; rotationMatrix.val[Matrix4.M31] = 0;
rotationMatrix.val[9] = geometry.rotation.val[7];
rotationMatrix.val[10] = geometry.rotation.val[8]; rotationMatrix.val[Matrix4.M02] = geometry.rotation.val[6];
rotationMatrix.val[11] = 0; rotationMatrix.val[Matrix4.M12] = geometry.rotation.val[7];
rotationMatrix.val[12] = 0; rotationMatrix.val[Matrix4.M22] = geometry.rotation.val[8];
rotationMatrix.val[13] = 0; rotationMatrix.val[Matrix4.M32] = 0;
rotationMatrix.val[14] = 0;
rotationMatrix.val[15] = 1; rotationMatrix.val[Matrix4.M03] = 0;
rotationMatrix.val[Matrix4.M13] = 0;
rotationMatrix.val[Matrix4.M23] = 0;
rotationMatrix.val[Matrix4.M33] = 1;
scalingMatrix.setToScaling(geometry.scaling); 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; // Render the marker;
shaderComp.shader.getShaderProgram().begin();{ batch.render(model.instance, environment.environment, shader.shader);
shaderComp.shader.setUniforms();
meshComp.model.render(shaderComp.shader.getShaderProgram(), GL20.GL_TRIANGLES);
}shaderComp.shader.getShaderProgram().end();
break;
} }
}else{ }else{
Gdx.app.log(TAG, CLASS_NAME + ".process(): Skipping marker number " + Integer.toString(i) + "."); Gdx.app.log(TAG, CLASS_NAME + ".process(): Skipping marker number " + Integer.toString(i) + ".");
} }
} }
markers = null;
} }
} }

View File

@@ -15,37 +15,30 @@
*/ */
package ve.ucv.ciens.ccg.nxtar.systems; 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.GeometryComponent;
import ve.ucv.ciens.ccg.nxtar.components.MarkerCodeComponent; 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.components.ShaderComponent;
import ve.ucv.ciens.ccg.nxtar.graphics.LightSource;
import ve.ucv.ciens.ccg.nxtar.graphics.RenderParameters;
import com.artemis.Aspect; import com.artemis.Aspect;
import com.artemis.ComponentMapper; import com.artemis.ComponentMapper;
import com.artemis.Entity; import com.artemis.Entity;
import com.artemis.annotations.Mapper; import com.artemis.annotations.Mapper;
import com.artemis.systems.EntityProcessingSystem; import com.artemis.systems.EntityProcessingSystem;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.math.Matrix4; import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;
/** /**
* <p>Entity processing system in charge of rendering 3D objects using OpenGL. The * <p>Entity processing system in charge of rendering 3D objects using OpenGL. The
* entities to be rendered must have a geometry, shader and mesh component associated.</p> * entities to be rendered must have a geometry, shader and mesh component associated.</p>
*/ */
public class ObjectRenderingSystem extends EntityProcessingSystem { public class ObjectRenderingSystem extends EntityProcessingSystem {
@Mapper ComponentMapper<GeometryComponent> geometryMapper; @Mapper ComponentMapper<GeometryComponent> geometryMapper;
@Mapper ComponentMapper<ShaderComponent> shaderMapper; @Mapper ComponentMapper<ShaderComponent> shaderMapper;
@Mapper ComponentMapper<MeshComponent> modelMapper; @Mapper ComponentMapper<ModelComponent> modelMapper;
@Mapper ComponentMapper<EnvironmentComponent> environmentMapper;
private static final Vector3 LIGHT_POSITION = new Vector3(2.0f, 2.0f, 4.0f);
private static final Color AMBIENT_COLOR = new Color(0.0f, 0.1f, 0.2f, 1.0f);
private static final Color DIFFUSE_COLOR = new Color(1.0f, 1.0f, 1.0f, 1.0f);
private static final Color SPECULAR_COLOR = new Color(1.0f, 0.8f, 0.0f, 1.0f);
private static final float SHINYNESS = 50.0f;
/** /**
* <p>A matrix representing 3D translations.</p> * <p>A matrix representing 3D translations.</p>
@@ -62,21 +55,32 @@ public class ObjectRenderingSystem extends EntityProcessingSystem {
*/ */
private Matrix4 scalingMatrix; private Matrix4 scalingMatrix;
/** private PerspectiveCamera camera;
* <p>The total transformation to be applied to an entity.</p>
*/ private ModelBatch batch;
private Matrix4 combinedTransformationMatrix;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public ObjectRenderingSystem() { public ObjectRenderingSystem(ModelBatch batch) {
super(Aspect.getAspectForAll(GeometryComponent.class, ShaderComponent.class, MeshComponent.class).exclude(MarkerCodeComponent.class)); super(Aspect.getAspectForAll(GeometryComponent.class, ShaderComponent.class, ModelComponent.class, EnvironmentComponent.class).exclude(MarkerCodeComponent.class));
RenderParameters.setLightSource1(new LightSource(LIGHT_POSITION, AMBIENT_COLOR, DIFFUSE_COLOR, SPECULAR_COLOR, SHINYNESS));
camera = null;
this.batch = batch;
translationMatrix = new Matrix4().setToTranslation(0.0f, 0.0f, 0.0f); translationMatrix = new Matrix4().setToTranslation(0.0f, 0.0f, 0.0f);
rotationMatrix = new Matrix4().idt(); rotationMatrix = new Matrix4().idt();
scalingMatrix = new Matrix4().setToScaling(0.0f, 0.0f, 0.0f); scalingMatrix = new Matrix4().setToScaling(0.0f, 0.0f, 0.0f);
combinedTransformationMatrix = new Matrix4(); }
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;
} }
/** /**
@@ -87,28 +91,25 @@ public class ObjectRenderingSystem extends EntityProcessingSystem {
*/ */
@Override @Override
protected void process(Entity e) { protected void process(Entity e) {
GeometryComponent geometryComponent; EnvironmentComponent environment;
ShaderComponent shaderComponent; GeometryComponent geometryComponent;
MeshComponent meshComponent; ShaderComponent shaderComponent;
ModelComponent modelComponent;
// Get the necessary components. // Get the necessary components.
geometryComponent = geometryMapper.get(e); geometryComponent = geometryMapper.get(e);
meshComponent = modelMapper.get(e); modelComponent = modelMapper.get(e);
shaderComponent = shaderMapper.get(e); shaderComponent = shaderMapper.get(e);
environment = environmentMapper.get(e);
// Calculate the geometric transformation for this entity. // Calculate the geometric transformation for this entity.
translationMatrix.setToTranslation(geometryComponent.position); translationMatrix.setToTranslation(geometryComponent.position);
rotationMatrix.set(geometryComponent.rotation); rotationMatrix.set(geometryComponent.rotation);
scalingMatrix.setToScaling(geometryComponent.scaling); scalingMatrix.setToScaling(geometryComponent.scaling);
combinedTransformationMatrix.idt().mul(translationMatrix).mul(rotationMatrix).mul(scalingMatrix); modelComponent.instance.transform.idt().mul(translationMatrix).mul(rotationMatrix).mul(scalingMatrix);
modelComponent.instance.calculateTransforms();
// Set up the global rendering parameters for this frame.
RenderParameters.setTransformationMatrix(combinedTransformationMatrix);
// Render this entity. // Render this entity.
shaderComponent.shader.getShaderProgram().begin();{ batch.render(modelComponent.instance, environment.environment, shaderComponent.shader);
shaderComponent.shader.setUniforms();
meshComponent.model.render(shaderComponent.shader.getShaderProgram(), GL20.GL_TRIANGLES);
}shaderComponent.shader.getShaderProgram().end();
} }
} }