Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*===============================================================================
- Copyright (c) 2012-2014 Qualcomm Connected Experiences, Inc. All Rights Reserved.
- Vuforia is a trademark of QUALCOMM Incorporated, registered in the United States
- and other countries. Trademarks of QUALCOMM Incorporated are used with permission.
- ===============================================================================*/
- package cz.ackee.ar.vuforia;
- import java.io.IOException;
- import java.util.Vector;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import android.opengl.GLES20;
- import android.opengl.GLSurfaceView;
- import android.opengl.Matrix;
- import android.util.DisplayMetrics;
- import android.util.Log;
- import com.qualcomm.QCAR.QCAR;
- import com.qualcomm.vuforia.CameraCalibration;
- import com.qualcomm.vuforia.CameraDevice;
- import com.qualcomm.vuforia.Matrix44F;
- import com.qualcomm.vuforia.Renderer;
- import com.qualcomm.vuforia.State;
- import com.qualcomm.vuforia.Tool;
- import com.qualcomm.vuforia.Trackable;
- import com.qualcomm.vuforia.TrackableResult;
- import com.qualcomm.vuforia.VIDEO_BACKGROUND_REFLECTION;
- import com.qualcomm.vuforia.Vec2F;
- import com.qualcomm.vuforia.Vuforia;
- import com.qualcomm.vuforia.samples.SampleApplication.SampleApplicationSession;
- import com.qualcomm.vuforia.samples.SampleApplication.utils.CubeShaders;
- import com.qualcomm.vuforia.samples.SampleApplication.utils.LoadingDialogHandler;
- import com.qualcomm.vuforia.samples.SampleApplication.utils.SampleApplication3DModel;
- import com.qualcomm.vuforia.samples.SampleApplication.utils.SampleMath;
- import com.qualcomm.vuforia.samples.SampleApplication.utils.SampleUtils;
- import com.qualcomm.vuforia.samples.SampleApplication.utils.Teapot;
- import com.qualcomm.vuforia.samples.SampleApplication.utils.Texture;
- import com.threed.jpct.Camera;
- import com.threed.jpct.Config;
- import com.threed.jpct.FrameBuffer;
- import com.threed.jpct.Light;
- import com.threed.jpct.Object3D;
- import com.threed.jpct.Primitives;
- import com.threed.jpct.RGBColor;
- import com.threed.jpct.SimpleVector;
- import com.threed.jpct.TextureManager;
- import com.threed.jpct.World;
- import com.threed.jpct.util.BitmapHelper;
- import com.threed.jpct.util.MemoryHelper;
- // The renderer class for the ImageTargets sample.
- public class ImageTargetRenderer implements GLSurfaceView.Renderer {
- private static final String LOGTAG = "ImageTargetRenderer";
- private SampleApplicationSession vuforiaAppSession;
- private Vector<Texture> mTextures;
- private int shaderProgramID;
- private int vertexHandle;
- private int normalHandle;
- private int textureCoordHandle;
- private int mvpMatrixHandle;
- private int texSampler2DHandle;
- private Teapot mTeapot;
- private float kBuildingScale = 12.0f;
- private SampleApplication3DModel mBuildingsModel;
- private Renderer mRenderer;
- boolean mIsActive = false;
- private static final float OBJECT_SCALE_FLOAT = 3.0f;
- /**
- * Reference to main activity *
- */
- public ImageTargets mActivity;
- private FrameBuffer fb;
- private World world;
- private float[] modelViewMat;
- private Light sun;
- private Object3D cube;
- private Camera cam;
- private float fov;
- private float fovy;
- public ImageTargetRenderer(ImageTargets activity,
- SampleApplicationSession session) {
- mActivity = activity;
- vuforiaAppSession = session;
- world = new World();
- world.setAmbientLight(20, 20, 20);
- sun = new Light(world);
- sun.setIntensity(250, 250, 250);
- // Create a texture out of the icon...:-)
- TextureManager txtMgr = TextureManager.getInstance();
- if (!txtMgr.containsTexture("texture")) {
- com.threed.jpct.Texture texture = new com.threed.jpct.Texture(BitmapHelper.rescale(
- BitmapHelper.convert(mActivity.getResources().getDrawable(R.drawable.vuforia_splash)), 64, 64));
- txtMgr.addTexture("texture", texture);
- }
- cube = Primitives.getCylinder(20, 40);
- cube.calcTextureWrapSpherical();
- cube.setTexture("texture");
- cube.strip();
- cube.build();
- world.addObject(cube);
- cam = world.getCamera();
- SimpleVector sv = new SimpleVector();
- sv.set(cube.getTransformedCenter());
- sv.y += 100;
- sv.z += 100;
- sun.setPosition(sv);
- MemoryHelper.compact();
- }
- // Called to draw the current frame.
- @Override
- public void onDrawFrame(GL10 gl) {
- if (!mIsActive)
- return;
- // Update render view (projection matrix and viewport) if needed:
- // mActivity.updateRenderView();
- // Call our native function to render content
- renderFrame();
- updateCamera();
- world.renderScene(fb);
- world.draw(fb);
- fb.display();
- }
- // Called when the surface is created or recreated.
- @Override
- public void onSurfaceCreated(GL10 gl, EGLConfig config) {
- Log.d(LOGTAG, "GLRenderer.onSurfaceCreated");
- initRendering();
- // Call Vuforia function to (re)initialize rendering after first use
- // or after OpenGL ES context was lost (e.g. after onPause/onResume):
- vuforiaAppSession.onSurfaceCreated();
- }
- // Called when the surface changed size.
- @Override
- public void onSurfaceChanged(GL10 gl, int width, int height) {
- Log.d(LOGTAG, "GLRenderer.onSurfaceChanged");
- if (fb != null) {
- fb.dispose();
- }
- fb = new FrameBuffer(width, height);
- Config.viewportOffsetAffectsRenderTarget = true;
- // Call Vuforia function to handle render surface size changes:
- vuforiaAppSession.onSurfaceChanged(width, height);
- }
- // Function for initializing the renderer.
- private void initRendering() {
- mTeapot = new Teapot();
- mRenderer = Renderer.getInstance();
- GLES20.glClearColor(0.0f, 0.0f, 0.0f, Vuforia.requiresAlpha() ? 0.0f
- : 1.0f);
- for (Texture t : mTextures) {
- GLES20.glGenTextures(1, t.mTextureID, 0);
- GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, t.mTextureID[0]);
- GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
- GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
- GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
- GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
- GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA,
- t.mWidth, t.mHeight, 0, GLES20.GL_RGBA,
- GLES20.GL_UNSIGNED_BYTE, t.mData);
- }
- shaderProgramID = SampleUtils.createProgramFromShaderSrc(
- CubeShaders.CUBE_MESH_VERTEX_SHADER,
- CubeShaders.CUBE_MESH_FRAGMENT_SHADER);
- vertexHandle = GLES20.glGetAttribLocation(shaderProgramID,
- "vertexPosition");
- normalHandle = GLES20.glGetAttribLocation(shaderProgramID,
- "vertexNormal");
- textureCoordHandle = GLES20.glGetAttribLocation(shaderProgramID,
- "vertexTexCoord");
- mvpMatrixHandle = GLES20.glGetUniformLocation(shaderProgramID,
- "modelViewProjectionMatrix");
- texSampler2DHandle = GLES20.glGetUniformLocation(shaderProgramID,
- "texSampler2D");
- try {
- mBuildingsModel = new SampleApplication3DModel();
- mBuildingsModel.loadModel(mActivity.getResources().getAssets(),
- "ImageTargets/Buildings.txt");
- } catch (IOException e) {
- Log.e(LOGTAG, "Unable to load buildings");
- }
- // Hide the Loading Dialog
- mActivity.loadingDialogHandler
- .sendEmptyMessage(LoadingDialogHandler.HIDE_LOADING_DIALOG);
- }
- // The render function.
- private void renderFrame() {
- GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
- State state = mRenderer.begin();
- mRenderer.drawVideoBackground();
- GLES20.glEnable(GLES20.GL_DEPTH_TEST);
- GLES20.glEnable(GLES20.GL_CULL_FACE);
- GLES20.glCullFace(GLES20.GL_BACK);
- if (Renderer.getInstance().getVideoBackgroundConfig().getReflection() == VIDEO_BACKGROUND_REFLECTION.VIDEO_BACKGROUND_REFLECTION_ON)
- GLES20.glFrontFace(GLES20.GL_CW); // Front camera
- else
- GLES20.glFrontFace(GLES20.GL_CCW); // Back camera
- float[] modelviewArray = new float[16];
- for (int tIdx = 0; tIdx < state.getNumTrackableResults(); tIdx++) {
- TrackableResult result = state.getTrackableResult(tIdx);
- Trackable trackable = result.getTrackable();
- Matrix44F modelViewMatrix_Vuforia = Tool.convertPose2GLMatrix(result.getPose());
- float[] modelViewMatrix = modelViewMatrix_Vuforia.getData();
- SampleUtils.rotatePoseMatrix(90.0f, 1.0f, 0, 0, modelViewMatrix);
- modelViewMatrix_Vuforia.setData(modelViewMatrix);
- Matrix44F inverseMV = SampleMath.Matrix44FInverse(modelViewMatrix_Vuforia);
- Matrix44F invTranspMV = SampleMath.Matrix44FTranspose(inverseMV);
- updateModelviewMatrix(invTranspMV.getData());
- }
- // hide the objects when the targets are not detected
- if (state.getNumTrackableResults() == 0) {
- float m[] = {
- 1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 1, 0,
- 0, 0, -10000, 1
- };
- updateModelviewMatrix(m);
- }
- GLES20.glDisable(GLES20.GL_DEPTH_TEST);
- mRenderer.end();
- }
- private void printUserData(Trackable trackable) {
- String userData = (String) trackable.getUserData();
- Log.d(LOGTAG, "UserData:Retreived User Data \"" + userData + "\"");
- }
- public void setTextures(Vector<Texture> textures) {
- mTextures = textures;
- }
- public void updateModelviewMatrix(float mat[]) {
- modelViewMat = mat;
- }
- public void updateCamera() {
- if (modelViewMat != null) {
- float[] m = modelViewMat;
- final SimpleVector camUp;
- // if (mActivity.isPortrait()) {
- // camUp = new SimpleVector(-m[0], -m[1], -m[2]);
- // } else {
- camUp = new SimpleVector(-m[4], -m[5], -m[6]);
- // }
- final SimpleVector camDirection = new SimpleVector(m[8], m[9], m[10]);
- final SimpleVector camPosition = new SimpleVector(m[12], m[13], m[14]);
- cam.setOrientation(camDirection, camUp);
- cam.setPosition(camPosition);
- cam.setFOV(vuforiaAppSession.getFov());
- cam.setYFOV(vuforiaAppSession.getFovy());
- }
- }
- public void setVideoSize(int videoWidth, int videoHeight) {
- DisplayMetrics displaymetrics = new DisplayMetrics();
- mActivity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
- int height = displaymetrics.heightPixels;
- int width = displaymetrics.widthPixels;
- int widestVideo = videoWidth > videoHeight ? videoWidth : videoHeight;
- int widestScreen = width > height ? width : height;
- float diff = (widestVideo - widestScreen) / 2;
- Config.viewportOffsetY = diff / widestScreen;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement