Markers rendered almost sucessfully.
This commit is contained in:
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -138,7 +138,7 @@ public class VideoStreamingThread extends Thread{
|
|||||||
VideoFrameDataMessage dataMessage;
|
VideoFrameDataMessage dataMessage;
|
||||||
Object tmpMessage;
|
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{
|
try{
|
||||||
packet = new DatagramPacket(size, size.length);
|
packet = new DatagramPacket(size, size.length);
|
||||||
socket.receive(packet);
|
socket.receive(packet);
|
||||||
@@ -148,11 +148,11 @@ public class VideoStreamingThread extends Thread{
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Creating buffers.");
|
//Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Creating buffers.");
|
||||||
intSize = byteArray2Int(size);
|
intSize = byteArray2Int(size);
|
||||||
data = new byte[intSize];
|
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{
|
try{
|
||||||
packet = new DatagramPacket(data, data.length);
|
packet = new DatagramPacket(data, data.length);
|
||||||
socket.receive(packet);
|
socket.receive(packet);
|
||||||
@@ -164,17 +164,16 @@ public class VideoStreamingThread extends Thread{
|
|||||||
|
|
||||||
ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
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{
|
try{
|
||||||
ObjectInputStream ois = new ObjectInputStream(bais);
|
ObjectInputStream ois = new ObjectInputStream(bais);
|
||||||
tmpMessage = ois.readObject();
|
tmpMessage = ois.readObject();
|
||||||
|
|
||||||
if(tmpMessage instanceof VideoFrameDataMessage){
|
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;
|
dataMessage = (VideoFrameDataMessage) tmpMessage;
|
||||||
|
|
||||||
Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Received frame dimensions are: " +
|
//Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Received frame dimensions are: " + Integer.toString(dataMessage.imageWidth) + "x" + Integer.toString(dataMessage.imageHeight));
|
||||||
Integer.toString(dataMessage.imageWidth) + "x" + Integer.toString(dataMessage.imageHeight));
|
|
||||||
frameMonitor.setFrameDimensions(dataMessage.imageWidth, dataMessage.imageHeight);
|
frameMonitor.setFrameDimensions(dataMessage.imageWidth, dataMessage.imageHeight);
|
||||||
frameMonitor.setNewFrame(dataMessage.data);
|
frameMonitor.setNewFrame(dataMessage.data);
|
||||||
|
|
||||||
@@ -218,7 +217,7 @@ public class VideoStreamingThread extends Thread{
|
|||||||
try{ pauseMonitor.wait(); }catch(InterruptedException ie){ }
|
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){
|
if(netListener != null && !coreNotified && frameMonitor.getCurrentFrame() != null){
|
||||||
coreNotified = true;
|
coreNotified = true;
|
||||||
netListener.networkStreamConnected(THREAD_NAME);
|
netListener.networkStreamConnected(THREAD_NAME);
|
||||||
|
|||||||
@@ -210,9 +210,9 @@ public class CameraCalibrationState extends BaseState{
|
|||||||
frame = frameMonitor.getCurrentFrame();
|
frame = frameMonitor.getCurrentFrame();
|
||||||
|
|
||||||
// Apply the undistortion method if the camera has been calibrated already.
|
// Apply the undistortion method if the camera has been calibrated already.
|
||||||
if(core.cvProc.isCameraCalibrated()){
|
/*if(core.cvProc.isCameraCalibrated()){
|
||||||
frame = core.cvProc.undistortFrame(frame);
|
frame = core.cvProc.undistortFrame(frame);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
// Find the calibration points in the video frame.
|
// Find the calibration points in the video frame.
|
||||||
CalibrationData data = core.cvProc.findCalibrationPattern(frame);
|
CalibrationData data = core.cvProc.findCalibrationPattern(frame);
|
||||||
@@ -248,8 +248,9 @@ public class CameraCalibrationState extends BaseState{
|
|||||||
if(lastSampleTaken == ProjectConstants.CALIBRATION_SAMPLES){
|
if(lastSampleTaken == ProjectConstants.CALIBRATION_SAMPLES){
|
||||||
Gdx.app.log(TAG, CLASS_NAME + "render(): Last sample taken.");
|
Gdx.app.log(TAG, CLASS_NAME + "render(): Last sample taken.");
|
||||||
|
|
||||||
core.toast("Calibrating camera", false);
|
|
||||||
core.cvProc.calibrateCamera(calibrationSamples, frame);
|
core.cvProc.calibrateCamera(calibrationSamples, frame);
|
||||||
|
msg = "Camera successfully calibrated";
|
||||||
|
core.toast(msg, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
import ve.ucv.ciens.ccg.nxtar.NxtARCore.game_states_t;
|
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.TestGameEntityCreator;
|
import ve.ucv.ciens.ccg.nxtar.entities.MarkerTestEntityCreator;
|
||||||
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;
|
||||||
@@ -171,9 +171,8 @@ public class InGameState extends BaseState{
|
|||||||
|
|
||||||
// Set up the game world.
|
// Set up the game world.
|
||||||
gameWorld = new World();
|
gameWorld = new World();
|
||||||
entityCreator = new TestGameEntityCreator();
|
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 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.
|
// Create the 3D perspective camera and the frame buffer object if they don't exist.
|
||||||
if(camera3D == null && frameBuffer == null){
|
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);
|
frameBuffer.getColorBufferTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
|
||||||
|
|
||||||
camera3D = new PerspectiveCamera(67, w, h);
|
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.near = 0.01f;
|
||||||
camera3D.far = 100.0f;
|
camera3D.far = 100.0f;
|
||||||
camera3D.lookAt(0.0f, 0.0f, 0.0f);
|
camera3D.lookAt(0.0f, 0.0f, -1.0f);
|
||||||
camera3D.update();
|
camera3D.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply the undistortion method if the camera has been calibrated already.
|
// Apply the undistortion method if the camera has been calibrated already.
|
||||||
if(core.cvProc.isCameraCalibrated()){
|
/*if(core.cvProc.isCameraCalibrated()){
|
||||||
frame = core.cvProc.undistortFrame(frame);
|
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);
|
||||||
@@ -258,11 +257,11 @@ public class InGameState extends BaseState{
|
|||||||
|
|
||||||
// Set the 3D frame buffer for rendering.
|
// Set the 3D frame buffer for rendering.
|
||||||
frameBuffer.begin();{
|
frameBuffer.begin();{
|
||||||
|
Gdx.gl.glDisable(GL20.GL_CULL_FACE);
|
||||||
Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
|
Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
|
||||||
Gdx.gl.glClearColor(1, 1, 1, 0);
|
Gdx.gl.glClearColor(1, 1, 1, 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);
|
||||||
|
|
||||||
// Render the current state of the game.
|
|
||||||
RenderParameters.setModelViewProjectionMatrix(camera3D.combined);
|
RenderParameters.setModelViewProjectionMatrix(camera3D.combined);
|
||||||
RenderParameters.setEyePosition(camera3D.position);
|
RenderParameters.setEyePosition(camera3D.position);
|
||||||
gameWorld.getSystem(MarkerRenderingSystem.class).setMarkerData(data);
|
gameWorld.getSystem(MarkerRenderingSystem.class).setMarkerData(data);
|
||||||
|
|||||||
@@ -86,9 +86,36 @@ public class MarkerRenderingSystem extends EntityProcessingSystem {
|
|||||||
Gdx.app.log(TAG, CLASS_NAME + ".process(): Rendering marker code " + Integer.toString(markers.markerCodes[i]) + ".");
|
Gdx.app.log(TAG, CLASS_NAME + ".process(): Rendering marker code " + Integer.toString(markers.markerCodes[i]) + ".");
|
||||||
// Set the geometric transformations.
|
// Set the geometric transformations.
|
||||||
translationMatrix.setToTranslation(geometry.position);
|
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);
|
scalingMatrix.setToScaling(geometry.scaling);
|
||||||
combinedTransformationMatrix.idt().mul(scalingMatrix).mul(rotationMatrix).mul(translationMatrix);
|
combinedTransformationMatrix.idt().mul(translationMatrix).mul(rotationMatrix).mul(scalingMatrix);
|
||||||
RenderParameters.setTransformationMatrix(combinedTransformationMatrix);
|
RenderParameters.setTransformationMatrix(combinedTransformationMatrix);
|
||||||
|
|
||||||
// Render the marker;
|
// Render the marker;
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ public class ObjectRenderingSystem extends EntityProcessingSystem {
|
|||||||
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(scalingMatrix).mul(rotationMatrix).mul(translationMatrix);
|
combinedTransformationMatrix.idt().mul(translationMatrix).mul(rotationMatrix).mul(scalingMatrix);
|
||||||
|
|
||||||
// Set up the global rendering parameters for this frame.
|
// Set up the global rendering parameters for this frame.
|
||||||
RenderParameters.setTransformationMatrix(combinedTransformationMatrix);
|
RenderParameters.setTransformationMatrix(combinedTransformationMatrix);
|
||||||
|
|||||||
Reference in New Issue
Block a user