Added correct 3D model rendering.
This commit is contained in:
@@ -5,21 +5,35 @@ import ve.ucv.ciens.icaro.ardemo.ImageProcessor.MarkerData;
|
|||||||
|
|
||||||
import com.badlogic.gdx.ApplicationAdapter;
|
import com.badlogic.gdx.ApplicationAdapter;
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
|
import com.badlogic.gdx.assets.AssetManager;
|
||||||
import com.badlogic.gdx.graphics.GL20;
|
import com.badlogic.gdx.graphics.GL20;
|
||||||
import com.badlogic.gdx.graphics.Pixmap;
|
import com.badlogic.gdx.graphics.Pixmap;
|
||||||
import com.badlogic.gdx.graphics.Texture;
|
import com.badlogic.gdx.graphics.Texture;
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||||
|
import com.badlogic.gdx.graphics.g3d.Model;
|
||||||
|
import com.badlogic.gdx.graphics.g3d.ModelBatch;
|
||||||
|
|
||||||
public class EviDemo extends ApplicationAdapter {
|
public class EviDemo extends ApplicationAdapter {
|
||||||
private static final String TAG = "NXTAR_ANDROID_MAIN";
|
private static final String TAG = "EVI DEMO - CORE";
|
||||||
private static final String CLASS_NAME = EviDemo.class.getSimpleName();
|
private static final String CLASS_NAME = EviDemo.class.getSimpleName();
|
||||||
|
|
||||||
|
private static final int W = 640;
|
||||||
|
private static final int H = 360;
|
||||||
|
private static final float NEAR = 0.01f;
|
||||||
|
private static final float FAR = 10.0f;
|
||||||
|
private static final int CODE = 213;
|
||||||
|
|
||||||
private ImageProcessor cvProc;
|
private ImageProcessor cvProc;
|
||||||
private Texture tex;
|
private Texture tex;
|
||||||
private Pixmap frame;
|
private Pixmap frame;
|
||||||
private SpriteBatch batch;
|
private SpriteBatch batch;
|
||||||
|
private ModelBatch mBatch;
|
||||||
private MarkerData data;
|
private MarkerData data;
|
||||||
private CalibrationData calib;
|
private CalibrationData calib;
|
||||||
|
private AssetManager manager;
|
||||||
|
private boolean doneLoading;
|
||||||
|
private CustomPerspectiveCamera camera;
|
||||||
|
private Monkey monkey;
|
||||||
|
|
||||||
private float[][] calibrationSamples;
|
private float[][] calibrationSamples;
|
||||||
private int lastSampleTaken;
|
private int lastSampleTaken;
|
||||||
@@ -29,10 +43,11 @@ public class EviDemo extends ApplicationAdapter {
|
|||||||
tex = null;
|
tex = null;
|
||||||
cvProc = proc;
|
cvProc = proc;
|
||||||
frame = null;
|
frame = null;
|
||||||
|
doneLoading = false;
|
||||||
lastSampleTaken = 0;
|
lastSampleTaken = 0;
|
||||||
|
|
||||||
calibrationSamples = new float[ProjectConstants.CALIBRATION_SAMPLES][];
|
calibrationSamples = new float[ProjectConstants.CALIBRATION_SAMPLES][];
|
||||||
|
monkey = new Monkey();
|
||||||
|
|
||||||
for(int i = 0; i < calibrationSamples.length; i++){
|
for(int i = 0; i < calibrationSamples.length; i++){
|
||||||
calibrationSamples[i] = new float[ProjectConstants.CALIBRATION_PATTERN_POINTS * 2];
|
calibrationSamples[i] = new float[ProjectConstants.CALIBRATION_PATTERN_POINTS * 2];
|
||||||
}
|
}
|
||||||
@@ -41,26 +56,75 @@ public class EviDemo extends ApplicationAdapter {
|
|||||||
@Override
|
@Override
|
||||||
public void create () {
|
public void create () {
|
||||||
batch = new SpriteBatch();
|
batch = new SpriteBatch();
|
||||||
|
mBatch = new ModelBatch();
|
||||||
|
|
||||||
|
manager = new AssetManager();
|
||||||
|
manager.load("monkey.g3db", Model.class);
|
||||||
|
|
||||||
|
camera = new CustomPerspectiveCamera(67, W, H);
|
||||||
|
camera.near = NEAR;
|
||||||
|
camera.far = FAR;
|
||||||
|
camera.translate(0.0f, 0.0f, 0.0f);
|
||||||
|
camera.lookAt(0.0f, 0.0f, -1.0f);
|
||||||
|
camera.update();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose() {
|
||||||
|
if(tex != null) tex.dispose();
|
||||||
|
manager.dispose();
|
||||||
|
batch.dispose();
|
||||||
|
mBatch.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render () {
|
public void render () {
|
||||||
|
float focalPointX, focalPointY, cameraCenterX, cameraCenterY;
|
||||||
|
|
||||||
Gdx.gl.glClearColor(1, 0, 0, 1);
|
Gdx.gl.glClearColor(1, 0, 0, 1);
|
||||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
if(doneLoading) {
|
||||||
|
|
||||||
if(cvProc.isCameraCalibrated()) {
|
if(cvProc.isCameraCalibrated()) {
|
||||||
data = cvProc.findMarkersInFrame();
|
data = cvProc.findMarkersInFrame();
|
||||||
|
|
||||||
if(data != null) {
|
if(data != null) {
|
||||||
|
|
||||||
|
for(int i = 0; i < ProjectConstants.MAXIMUM_NUMBER_OF_MARKERS; i++) {
|
||||||
|
if(data.markerCodes[i] == CODE) {
|
||||||
|
monkey.position.set(data.translationVectors[i]);
|
||||||
|
monkey.rotation.set(data.rotationMatrices[i]);
|
||||||
|
monkey.applyWorldTransform();
|
||||||
|
monkey.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
focalPointX = cvProc.getFocalPointX();
|
||||||
|
focalPointY = cvProc.getFocalPointY();
|
||||||
|
cameraCenterX = cvProc.getCameraCenterX();
|
||||||
|
cameraCenterY = cvProc.getCameraCenterY();
|
||||||
|
camera.setCustomARProjectionMatrix(focalPointX, focalPointY, cameraCenterX, cameraCenterY, NEAR, FAR, W, H);
|
||||||
|
camera.update(camera.projection);
|
||||||
|
|
||||||
frame = new Pixmap(data.outFrame, 0, data.outFrame.length);
|
frame = new Pixmap(data.outFrame, 0, data.outFrame.length);
|
||||||
tex = new Texture(frame);
|
tex = new Texture(frame);
|
||||||
|
|
||||||
batch.begin();
|
batch.begin(); {
|
||||||
batch.draw(tex, 0, 0);
|
batch.draw(tex, 0, 0);
|
||||||
batch.end();
|
} batch.end();
|
||||||
|
|
||||||
|
if(monkey.isVisible()) {
|
||||||
|
mBatch.begin(camera); {
|
||||||
|
mBatch.render(monkey.instance);
|
||||||
|
} mBatch.end();
|
||||||
|
monkey.setVisible(false);
|
||||||
|
}
|
||||||
|
|
||||||
frame.dispose();
|
frame.dispose();
|
||||||
tex.dispose();
|
tex.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
calib = cvProc.findCalibrationPattern();
|
calib = cvProc.findCalibrationPattern();
|
||||||
|
|
||||||
@@ -98,5 +162,11 @@ public class EviDemo extends ApplicationAdapter {
|
|||||||
tex.dispose();
|
tex.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
doneLoading = manager.update();
|
||||||
|
if(doneLoading)
|
||||||
|
monkey.setModel(manager.get("monkey.g3db", Model.class));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
75
core/src/ve/ucv/ciens/icaro/ardemo/Monkey.java
Normal file
75
core/src/ve/ucv/ciens/icaro/ardemo/Monkey.java
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
package ve.ucv.ciens.icaro.ardemo;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.graphics.g3d.Model;
|
||||||
|
import com.badlogic.gdx.graphics.g3d.ModelInstance;
|
||||||
|
import com.badlogic.gdx.math.Matrix3;
|
||||||
|
import com.badlogic.gdx.math.Matrix4;
|
||||||
|
import com.badlogic.gdx.math.Vector3;
|
||||||
|
|
||||||
|
class Monkey {
|
||||||
|
private Model model;
|
||||||
|
public ModelInstance instance;
|
||||||
|
public Vector3 position;
|
||||||
|
public Matrix3 rotation;
|
||||||
|
public Vector3 scaling;
|
||||||
|
private boolean visible;
|
||||||
|
|
||||||
|
public Monkey() {
|
||||||
|
this.position = new Vector3();
|
||||||
|
this.rotation = new Matrix3();
|
||||||
|
this.scaling = new Vector3(1.0f, 1.0f, 1.0f);
|
||||||
|
this.visible = false;
|
||||||
|
this.instance = null;
|
||||||
|
this.model = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isVisible() {
|
||||||
|
return visible;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVisible(boolean visible) {
|
||||||
|
this.visible = visible;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Model getModel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setModel(Model model) {
|
||||||
|
this.model = model;
|
||||||
|
this.instance = new ModelInstance(this.model);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void applyWorldTransform(){
|
||||||
|
Matrix4 translationMatrix = new Matrix4().setToTranslation(0.0f, 0.0f, 0.0f);;
|
||||||
|
Matrix4 rotationMatrix = new Matrix4().idt();;
|
||||||
|
Matrix4 scalingMatrix = new Matrix4().setToScaling(0.0f, 0.0f, 0.0f);;
|
||||||
|
|
||||||
|
translationMatrix.setToTranslation(this.position);
|
||||||
|
|
||||||
|
rotationMatrix.val[Matrix4.M00] = this.rotation.val[0];
|
||||||
|
rotationMatrix.val[Matrix4.M10] = this.rotation.val[1];
|
||||||
|
rotationMatrix.val[Matrix4.M20] = this.rotation.val[2];
|
||||||
|
rotationMatrix.val[Matrix4.M30] = 0;
|
||||||
|
|
||||||
|
rotationMatrix.val[Matrix4.M01] = this.rotation.val[3];
|
||||||
|
rotationMatrix.val[Matrix4.M11] = this.rotation.val[4];
|
||||||
|
rotationMatrix.val[Matrix4.M21] = this.rotation.val[5];
|
||||||
|
rotationMatrix.val[Matrix4.M31] = 0;
|
||||||
|
|
||||||
|
rotationMatrix.val[Matrix4.M02] = this.rotation.val[6];
|
||||||
|
rotationMatrix.val[Matrix4.M12] = this.rotation.val[7];
|
||||||
|
rotationMatrix.val[Matrix4.M22] = this.rotation.val[8];
|
||||||
|
rotationMatrix.val[Matrix4.M32] = 0;
|
||||||
|
|
||||||
|
rotationMatrix.val[Matrix4.M03] = 0;
|
||||||
|
rotationMatrix.val[Matrix4.M13] = 0;
|
||||||
|
rotationMatrix.val[Matrix4.M23] = 0;
|
||||||
|
rotationMatrix.val[Matrix4.M33] = 1;
|
||||||
|
|
||||||
|
scalingMatrix.setToScaling(this.scaling);
|
||||||
|
|
||||||
|
instance.transform.idt().mul(translationMatrix).mul(rotationMatrix).mul(scalingMatrix);
|
||||||
|
instance.calculateTransforms();
|
||||||
|
}
|
||||||
|
}
|
@@ -28,9 +28,28 @@ public class CVProcessor implements ImageProcessor {
|
|||||||
|
|
||||||
private boolean cameraCalibrated;
|
private boolean cameraCalibrated;
|
||||||
|
|
||||||
private native void getMarkerCodesAndLocations(long inMat, long outMat, int[] codes, long camMat, long distMat, float[] translations, float[] rotations);
|
private native void getMarkerCodesAndLocations(
|
||||||
private native boolean findCalibrationPattern(long inMat, long outMat, float[] points);
|
long inMat,
|
||||||
private native double calibrateCameraParameters(long camMat, long distMat, long frame, float[] calibrationPoints);
|
long outMat,
|
||||||
|
int[] codes,
|
||||||
|
long camMat,
|
||||||
|
long distMat,
|
||||||
|
float[] translations,
|
||||||
|
float[] rotations
|
||||||
|
);
|
||||||
|
|
||||||
|
private native boolean findCalibrationPattern(
|
||||||
|
long inMat,
|
||||||
|
long outMat,
|
||||||
|
float[] points
|
||||||
|
);
|
||||||
|
|
||||||
|
private native double calibrateCameraParameters(
|
||||||
|
long camMat,
|
||||||
|
long distMat,
|
||||||
|
long frame,
|
||||||
|
float[] calibrationPoints
|
||||||
|
);
|
||||||
|
|
||||||
static{
|
static{
|
||||||
try{
|
try{
|
||||||
|
Reference in New Issue
Block a user