diff --git a/src/ve/ucv/ciens/ccg/nxtar/entities/MarkerTestEntityCreator.java b/src/ve/ucv/ciens/ccg/nxtar/entities/MarkerTestEntityCreator.java new file mode 100644 index 0000000..be01c0b --- /dev/null +++ b/src/ve/ucv/ciens/ccg/nxtar/entities/MarkerTestEntityCreator.java @@ -0,0 +1,93 @@ +/* + * 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.entities; + +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.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.SingleLightPhongShader; + +import com.artemis.Entity; +import com.badlogic.gdx.Gdx; +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.utils.MeshBuilder; +import com.badlogic.gdx.math.Matrix3; +import com.badlogic.gdx.math.Vector3; + +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 markerMesh; + private CustomShaderBase phongShader; + + @Override + public void createAllEntities() { + MeshBuilder builder; + Matrix3 identity = new Matrix3().idt(); + Entity marker; + + // 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); + 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. + 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(); + } + + // Create the entities. + Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Creating the enitites."); + marker = world.createEntity(); + marker.addComponent(new GeometryComponent(new Vector3(0.0f, 0.0f, 0.0f), identity, new Vector3(1.0f, 1.0f, 1.0f))); + marker.addComponent(new MeshComponent(markerMesh)); + marker.addComponent(new ShaderComponent(phongShader)); + marker.addComponent(new MarkerCodeComponent(213)); + + // Add the entities to the world. + Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Adding entities to the world."); + marker.addToWorld(); + } + + @Override + public void dispose() { + if(phongShader != null && phongShader.getShaderProgram() != null) + phongShader.getShaderProgram().dispose(); + + if(markerMesh != null) + markerMesh.dispose(); + } +} diff --git a/src/ve/ucv/ciens/ccg/nxtar/network/VideoStreamingThread.java b/src/ve/ucv/ciens/ccg/nxtar/network/VideoStreamingThread.java index 629ad22..ac2eb60 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/network/VideoStreamingThread.java +++ b/src/ve/ucv/ciens/ccg/nxtar/network/VideoStreamingThread.java @@ -138,7 +138,7 @@ public class VideoStreamingThread extends Thread{ VideoFrameDataMessage dataMessage; Object tmpMessage; - Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Reading message size from socket."); + //Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Reading message size from socket."); try{ packet = new DatagramPacket(size, size.length); socket.receive(packet); @@ -148,11 +148,11 @@ public class VideoStreamingThread extends Thread{ return; } - Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Creating buffers."); + //Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Creating buffers."); intSize = byteArray2Int(size); data = new byte[intSize]; - Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Reading message from socket."); + //Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Reading message from socket."); try{ packet = new DatagramPacket(data, data.length); socket.receive(packet); @@ -164,17 +164,16 @@ public class VideoStreamingThread extends Thread{ ByteArrayInputStream bais = new ByteArrayInputStream(data); - Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Saving message in monitor."); + //Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Saving message in monitor."); try{ ObjectInputStream ois = new ObjectInputStream(bais); tmpMessage = ois.readObject(); if(tmpMessage instanceof VideoFrameDataMessage){ - Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Received a data message."); + //Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Received a data message."); dataMessage = (VideoFrameDataMessage) tmpMessage; - Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Received frame dimensions are: " + - Integer.toString(dataMessage.imageWidth) + "x" + Integer.toString(dataMessage.imageHeight)); + //Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Received frame dimensions are: " + Integer.toString(dataMessage.imageWidth) + "x" + Integer.toString(dataMessage.imageHeight)); frameMonitor.setFrameDimensions(dataMessage.imageWidth, dataMessage.imageHeight); frameMonitor.setNewFrame(dataMessage.data); @@ -218,7 +217,7 @@ public class VideoStreamingThread extends Thread{ try{ pauseMonitor.wait(); }catch(InterruptedException ie){ } } } - Gdx.app.debug(TAG, CLASS_NAME + ".run() :: Receiving."); + //Gdx.app.debug(TAG, CLASS_NAME + ".run() :: Receiving."); if(netListener != null && !coreNotified && frameMonitor.getCurrentFrame() != null){ coreNotified = true; netListener.networkStreamConnected(THREAD_NAME); diff --git a/src/ve/ucv/ciens/ccg/nxtar/states/CameraCalibrationState.java b/src/ve/ucv/ciens/ccg/nxtar/states/CameraCalibrationState.java index 24a2327..b27b0f8 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/states/CameraCalibrationState.java +++ b/src/ve/ucv/ciens/ccg/nxtar/states/CameraCalibrationState.java @@ -210,9 +210,9 @@ public class CameraCalibrationState extends BaseState{ frame = frameMonitor.getCurrentFrame(); // Apply the undistortion method if the camera has been calibrated already. - if(core.cvProc.isCameraCalibrated()){ + /*if(core.cvProc.isCameraCalibrated()){ frame = core.cvProc.undistortFrame(frame); - } + }*/ // Find the calibration points in the video frame. CalibrationData data = core.cvProc.findCalibrationPattern(frame); @@ -248,8 +248,9 @@ public class CameraCalibrationState extends BaseState{ if(lastSampleTaken == ProjectConstants.CALIBRATION_SAMPLES){ Gdx.app.log(TAG, CLASS_NAME + "render(): Last sample taken."); - core.toast("Calibrating camera", false); core.cvProc.calibrateCamera(calibrationSamples, frame); + msg = "Camera successfully calibrated"; + core.toast(msg, true); } } diff --git a/src/ve/ucv/ciens/ccg/nxtar/states/InGameState.java b/src/ve/ucv/ciens/ccg/nxtar/states/InGameState.java index 286469c..9ed814a 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/states/InGameState.java +++ b/src/ve/ucv/ciens/ccg/nxtar/states/InGameState.java @@ -20,7 +20,7 @@ import ve.ucv.ciens.ccg.networkdata.MotorEvent.motor_t; import ve.ucv.ciens.ccg.nxtar.NxtARCore; 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.TestGameEntityCreator; +import ve.ucv.ciens.ccg.nxtar.entities.MarkerTestEntityCreator; import ve.ucv.ciens.ccg.nxtar.graphics.RenderParameters; import ve.ucv.ciens.ccg.nxtar.interfaces.ImageProcessor.MarkerData; import ve.ucv.ciens.ccg.nxtar.network.monitors.MotorEventQueue; @@ -171,9 +171,8 @@ public class InGameState extends BaseState{ // Set up the game world. gameWorld = new World(); - entityCreator = new TestGameEntityCreator(); + entityCreator = new MarkerTestEntityCreator(); entityCreator.setWorld(gameWorld); - entityCreator.createAllEntities(); gameWorld.setSystem(new MarkerPositioningSystem()); gameWorld.setSystem(new MarkerRenderingSystem(), true); @@ -215,21 +214,21 @@ public class InGameState extends BaseState{ // Create the 3D perspective camera and the frame buffer object if they don't exist. if(camera3D == null && frameBuffer == null){ - frameBuffer = new FrameBuffer(Format.RGBA4444, w, h, true); + frameBuffer = new FrameBuffer(Format.RGBA8888, w, h, true); frameBuffer.getColorBufferTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear); camera3D = new PerspectiveCamera(67, w, h); - camera3D.translate(0.0f, 0.0f, 2.0f); + camera3D.translate(0.0f, 0.0f, 0.0f); camera3D.near = 0.01f; camera3D.far = 100.0f; - camera3D.lookAt(0.0f, 0.0f, 0.0f); + camera3D.lookAt(0.0f, 0.0f, -1.0f); camera3D.update(); } // Apply the undistortion method if the camera has been calibrated already. - if(core.cvProc.isCameraCalibrated()){ + /*if(core.cvProc.isCameraCalibrated()){ frame = core.cvProc.undistortFrame(frame); - } + }*/ // Attempt to find the markers in the current video frame. data = core.cvProc.findMarkersInFrame(frame); @@ -258,11 +257,11 @@ public class InGameState extends BaseState{ // Set the 3D frame buffer for rendering. frameBuffer.begin();{ + Gdx.gl.glDisable(GL20.GL_CULL_FACE); Gdx.gl.glEnable(GL20.GL_DEPTH_TEST); Gdx.gl.glClearColor(1, 1, 1, 0); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); - // Render the current state of the game. RenderParameters.setModelViewProjectionMatrix(camera3D.combined); RenderParameters.setEyePosition(camera3D.position); gameWorld.getSystem(MarkerRenderingSystem.class).setMarkerData(data); diff --git a/src/ve/ucv/ciens/ccg/nxtar/systems/MarkerRenderingSystem.java b/src/ve/ucv/ciens/ccg/nxtar/systems/MarkerRenderingSystem.java index a16880d..2bcf671 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/systems/MarkerRenderingSystem.java +++ b/src/ve/ucv/ciens/ccg/nxtar/systems/MarkerRenderingSystem.java @@ -66,18 +66,18 @@ public class MarkerRenderingSystem extends EntityProcessingSystem { @Override protected void process(Entity e) { MarkerCodeComponent marker; - GeometryComponent geometry; - ShaderComponent shaderComp; - MeshComponent meshComp; + GeometryComponent geometry; + ShaderComponent shaderComp; + MeshComponent meshComp; if(markers == null) return; Gdx.app.log(TAG, CLASS_NAME + ".process(): Getting components."); - marker = markerMapper.get(e); - geometry = geometryMapper.get(e); + marker = markerMapper.get(e); + geometry = geometryMapper.get(e); shaderComp = shaderMapper.get(e); - meshComp = meshMapper.get(e); + meshComp = meshMapper.get(e); Gdx.app.log(TAG, CLASS_NAME + ".process(): Processing markers."); for(int i = 0; i < ProjectConstants.MAXIMUM_NUMBER_OF_MARKERS; i++){ @@ -86,9 +86,36 @@ public class MarkerRenderingSystem extends EntityProcessingSystem { Gdx.app.log(TAG, CLASS_NAME + ".process(): Rendering marker code " + Integer.toString(markers.markerCodes[i]) + "."); // Set the geometric transformations. translationMatrix.setToTranslation(geometry.position); - rotationMatrix.set(geometry.rotation); + + Gdx.app.log(TAG, CLASS_NAME + ".process(): TRANSLATION:"); + Gdx.app.log(TAG, CLASS_NAME + ".process(): (" + Float.toString(geometry.position.x) + ", " + Float.toString(geometry.position.y) + ", " + Float.toString(geometry.position.z) + ")"); + + rotationMatrix.val[0] = geometry.rotation.val[0]; + rotationMatrix.val[1] = geometry.rotation.val[1]; + rotationMatrix.val[2] = geometry.rotation.val[2]; + rotationMatrix.val[3] = 0; + rotationMatrix.val[4] = geometry.rotation.val[3]; + rotationMatrix.val[5] = geometry.rotation.val[4]; + rotationMatrix.val[6] = geometry.rotation.val[5]; + rotationMatrix.val[7] = 0; + rotationMatrix.val[8] = geometry.rotation.val[6]; + rotationMatrix.val[9] = geometry.rotation.val[7]; + rotationMatrix.val[10] = geometry.rotation.val[8]; + rotationMatrix.val[11] = 0; + rotationMatrix.val[12] = 0; + rotationMatrix.val[13] = 0; + rotationMatrix.val[14] = 0; + rotationMatrix.val[15] = 1; + //rotationMatrix.idt(); + + Gdx.app.log(TAG, CLASS_NAME + ".process(): ROTATION:"); + Gdx.app.log(TAG, CLASS_NAME + ".process(): |" + Float.toString(rotationMatrix.val[0]) + ", " + Float.toString(rotationMatrix.val[4]) + ", " + Float.toString(rotationMatrix.val[8]) + ", " + Float.toString(rotationMatrix.val[12]) + "|"); + Gdx.app.log(TAG, CLASS_NAME + ".process(): |" + Float.toString(rotationMatrix.val[1]) + ", " + Float.toString(rotationMatrix.val[5]) + ", " + Float.toString(rotationMatrix.val[9]) + ", " + Float.toString(rotationMatrix.val[13]) + "|"); + Gdx.app.log(TAG, CLASS_NAME + ".process(): |" + Float.toString(rotationMatrix.val[2]) + ", " + Float.toString(rotationMatrix.val[6]) + ", " + Float.toString(rotationMatrix.val[10]) + ", " + Float.toString(rotationMatrix.val[14]) + "|"); + Gdx.app.log(TAG, CLASS_NAME + ".process(): |" + Float.toString(rotationMatrix.val[3]) + ", " + Float.toString(rotationMatrix.val[7]) + ", " + Float.toString(rotationMatrix.val[11]) + ", " + Float.toString(rotationMatrix.val[15]) + "|"); + scalingMatrix.setToScaling(geometry.scaling); - combinedTransformationMatrix.idt().mul(scalingMatrix).mul(rotationMatrix).mul(translationMatrix); + combinedTransformationMatrix.idt().mul(translationMatrix).mul(rotationMatrix).mul(scalingMatrix); RenderParameters.setTransformationMatrix(combinedTransformationMatrix); // Render the marker; @@ -101,7 +128,7 @@ public class MarkerRenderingSystem extends EntityProcessingSystem { Gdx.app.log(TAG, CLASS_NAME + ".process(): Skipping marker number " + Integer.toString(i) + "."); } } - + markers = null; } diff --git a/src/ve/ucv/ciens/ccg/nxtar/systems/ObjectRenderingSystem.java b/src/ve/ucv/ciens/ccg/nxtar/systems/ObjectRenderingSystem.java index 8c631b8..6d0b9d3 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/systems/ObjectRenderingSystem.java +++ b/src/ve/ucv/ciens/ccg/nxtar/systems/ObjectRenderingSystem.java @@ -100,7 +100,7 @@ public class ObjectRenderingSystem extends EntityProcessingSystem { translationMatrix.setToTranslation(geometryComponent.position); rotationMatrix.set(geometryComponent.rotation); scalingMatrix.setToScaling(geometryComponent.scaling); - combinedTransformationMatrix.idt().mul(scalingMatrix).mul(rotationMatrix).mul(translationMatrix); + combinedTransformationMatrix.idt().mul(translationMatrix).mul(rotationMatrix).mul(scalingMatrix); // Set up the global rendering parameters for this frame. RenderParameters.setTransformationMatrix(combinedTransformationMatrix);