Advertisement
Guest User

Untitled

a guest
Jul 30th, 2014
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.52 KB | None | 0 0
  1. /*===============================================================================
  2. Copyright (c) 2012-2014 Qualcomm Connected Experiences, Inc. All Rights Reserved.
  3.  
  4. Vuforia is a trademark of QUALCOMM Incorporated, registered in the United States
  5. and other countries. Trademarks of QUALCOMM Incorporated are used with permission.
  6. ===============================================================================*/
  7.  
  8. package cz.ackee.ar.vuforia;
  9.  
  10. import java.io.IOException;
  11. import java.util.Vector;
  12.  
  13. import javax.microedition.khronos.egl.EGLConfig;
  14. import javax.microedition.khronos.opengles.GL10;
  15.  
  16. import android.opengl.GLES20;
  17. import android.opengl.GLSurfaceView;
  18. import android.opengl.Matrix;
  19. import android.util.DisplayMetrics;
  20. import android.util.Log;
  21.  
  22. import com.qualcomm.QCAR.QCAR;
  23. import com.qualcomm.vuforia.CameraCalibration;
  24. import com.qualcomm.vuforia.CameraDevice;
  25. import com.qualcomm.vuforia.Matrix44F;
  26. import com.qualcomm.vuforia.Renderer;
  27. import com.qualcomm.vuforia.State;
  28. import com.qualcomm.vuforia.Tool;
  29. import com.qualcomm.vuforia.Trackable;
  30. import com.qualcomm.vuforia.TrackableResult;
  31. import com.qualcomm.vuforia.VIDEO_BACKGROUND_REFLECTION;
  32. import com.qualcomm.vuforia.Vec2F;
  33. import com.qualcomm.vuforia.Vuforia;
  34. import com.qualcomm.vuforia.samples.SampleApplication.SampleApplicationSession;
  35. import com.qualcomm.vuforia.samples.SampleApplication.utils.CubeShaders;
  36. import com.qualcomm.vuforia.samples.SampleApplication.utils.LoadingDialogHandler;
  37. import com.qualcomm.vuforia.samples.SampleApplication.utils.SampleApplication3DModel;
  38. import com.qualcomm.vuforia.samples.SampleApplication.utils.SampleMath;
  39. import com.qualcomm.vuforia.samples.SampleApplication.utils.SampleUtils;
  40. import com.qualcomm.vuforia.samples.SampleApplication.utils.Teapot;
  41. import com.qualcomm.vuforia.samples.SampleApplication.utils.Texture;
  42. import com.threed.jpct.Camera;
  43. import com.threed.jpct.Config;
  44. import com.threed.jpct.FrameBuffer;
  45. import com.threed.jpct.Light;
  46. import com.threed.jpct.Object3D;
  47. import com.threed.jpct.Primitives;
  48. import com.threed.jpct.RGBColor;
  49. import com.threed.jpct.SimpleVector;
  50.  
  51. import com.threed.jpct.TextureManager;
  52. import com.threed.jpct.World;
  53. import com.threed.jpct.util.BitmapHelper;
  54. import com.threed.jpct.util.MemoryHelper;
  55.  
  56. // The renderer class for the ImageTargets sample.
  57. public class ImageTargetRenderer implements GLSurfaceView.Renderer {
  58.     private static final String LOGTAG = "ImageTargetRenderer";
  59.  
  60.     private SampleApplicationSession vuforiaAppSession;
  61.  
  62.     private Vector<Texture> mTextures;
  63.  
  64.     private int shaderProgramID;
  65.  
  66.     private int vertexHandle;
  67.  
  68.     private int normalHandle;
  69.  
  70.     private int textureCoordHandle;
  71.  
  72.     private int mvpMatrixHandle;
  73.  
  74.     private int texSampler2DHandle;
  75.  
  76.     private Teapot mTeapot;
  77.  
  78.     private float kBuildingScale = 12.0f;
  79.     private SampleApplication3DModel mBuildingsModel;
  80.  
  81.     private Renderer mRenderer;
  82.  
  83.     boolean mIsActive = false;
  84.  
  85.     private static final float OBJECT_SCALE_FLOAT = 3.0f;
  86.  
  87.     /**
  88.      * Reference to main activity *
  89.      */
  90.     public ImageTargets mActivity;
  91.  
  92.     private FrameBuffer fb;
  93.  
  94.     private World world;
  95.  
  96.     private float[] modelViewMat;
  97.  
  98.     private Light sun;
  99.  
  100.     private Object3D cube;
  101.  
  102.     private Camera cam;
  103.  
  104.     private float fov;
  105.  
  106.     private float fovy;
  107.  
  108.  
  109.     public ImageTargetRenderer(ImageTargets activity,
  110.                                SampleApplicationSession session) {
  111.         mActivity = activity;
  112.         vuforiaAppSession = session;
  113.  
  114.         world = new World();
  115.         world.setAmbientLight(20, 20, 20);
  116.  
  117.         sun = new Light(world);
  118.         sun.setIntensity(250, 250, 250);
  119.  
  120.         // Create a texture out of the icon...:-)
  121.         TextureManager txtMgr = TextureManager.getInstance();
  122.         if (!txtMgr.containsTexture("texture")) {
  123.             com.threed.jpct.Texture texture = new com.threed.jpct.Texture(BitmapHelper.rescale(
  124.                     BitmapHelper.convert(mActivity.getResources().getDrawable(R.drawable.vuforia_splash)), 64, 64));
  125.             txtMgr.addTexture("texture", texture);
  126.         }
  127.  
  128.         cube = Primitives.getCylinder(20, 40);
  129.         cube.calcTextureWrapSpherical();
  130.         cube.setTexture("texture");
  131.         cube.strip();
  132.         cube.build();
  133.  
  134.         world.addObject(cube);
  135.  
  136.         cam = world.getCamera();
  137.  
  138.         SimpleVector sv = new SimpleVector();
  139.         sv.set(cube.getTransformedCenter());
  140.         sv.y += 100;
  141.         sv.z += 100;
  142.  
  143.         sun.setPosition(sv);
  144.  
  145.         MemoryHelper.compact();
  146.     }
  147.  
  148.     // Called to draw the current frame.
  149.     @Override
  150.     public void onDrawFrame(GL10 gl) {
  151.         if (!mIsActive)
  152.             return;
  153.  
  154.         // Update render view (projection matrix and viewport) if needed:
  155. //        mActivity.updateRenderView();
  156.  
  157.         // Call our native function to render content
  158.         renderFrame();
  159.  
  160.         updateCamera();
  161.  
  162.         world.renderScene(fb);
  163.         world.draw(fb);
  164.         fb.display();
  165.     }
  166.  
  167.     // Called when the surface is created or recreated.
  168.     @Override
  169.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  170.         Log.d(LOGTAG, "GLRenderer.onSurfaceCreated");
  171.  
  172.  
  173.         initRendering();
  174.  
  175.         // Call Vuforia function to (re)initialize rendering after first use
  176.         // or after OpenGL ES context was lost (e.g. after onPause/onResume):
  177.         vuforiaAppSession.onSurfaceCreated();
  178.     }
  179.  
  180.     // Called when the surface changed size.
  181.     @Override
  182.     public void onSurfaceChanged(GL10 gl, int width, int height) {
  183.         Log.d(LOGTAG, "GLRenderer.onSurfaceChanged");
  184.         if (fb != null) {
  185.             fb.dispose();
  186.         }
  187.         fb = new FrameBuffer(width, height);
  188.         Config.viewportOffsetAffectsRenderTarget = true;
  189.         // Call Vuforia function to handle render surface size changes:
  190.         vuforiaAppSession.onSurfaceChanged(width, height);
  191.     }
  192.  
  193.     // Function for initializing the renderer.
  194.     private void initRendering() {
  195.         mTeapot = new Teapot();
  196.  
  197.         mRenderer = Renderer.getInstance();
  198.  
  199.         GLES20.glClearColor(0.0f, 0.0f, 0.0f, Vuforia.requiresAlpha() ? 0.0f
  200.                 : 1.0f);
  201.  
  202.         for (Texture t : mTextures) {
  203.             GLES20.glGenTextures(1, t.mTextureID, 0);
  204.             GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, t.mTextureID[0]);
  205.             GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
  206.                     GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
  207.             GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
  208.                     GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
  209.             GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA,
  210.                     t.mWidth, t.mHeight, 0, GLES20.GL_RGBA,
  211.                     GLES20.GL_UNSIGNED_BYTE, t.mData);
  212.         }
  213.  
  214.         shaderProgramID = SampleUtils.createProgramFromShaderSrc(
  215.                 CubeShaders.CUBE_MESH_VERTEX_SHADER,
  216.                 CubeShaders.CUBE_MESH_FRAGMENT_SHADER);
  217.  
  218.         vertexHandle = GLES20.glGetAttribLocation(shaderProgramID,
  219.                 "vertexPosition");
  220.         normalHandle = GLES20.glGetAttribLocation(shaderProgramID,
  221.                 "vertexNormal");
  222.         textureCoordHandle = GLES20.glGetAttribLocation(shaderProgramID,
  223.                 "vertexTexCoord");
  224.         mvpMatrixHandle = GLES20.glGetUniformLocation(shaderProgramID,
  225.                 "modelViewProjectionMatrix");
  226.         texSampler2DHandle = GLES20.glGetUniformLocation(shaderProgramID,
  227.                 "texSampler2D");
  228.  
  229.         try {
  230.             mBuildingsModel = new SampleApplication3DModel();
  231.             mBuildingsModel.loadModel(mActivity.getResources().getAssets(),
  232.                     "ImageTargets/Buildings.txt");
  233.         } catch (IOException e) {
  234.             Log.e(LOGTAG, "Unable to load buildings");
  235.         }
  236.  
  237.         // Hide the Loading Dialog
  238.         mActivity.loadingDialogHandler
  239.                 .sendEmptyMessage(LoadingDialogHandler.HIDE_LOADING_DIALOG);
  240.  
  241.  
  242.     }
  243.  
  244.     // The render function.
  245.     private void renderFrame() {
  246.         GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
  247.  
  248.         State state = mRenderer.begin();
  249.         mRenderer.drawVideoBackground();
  250.         GLES20.glEnable(GLES20.GL_DEPTH_TEST);
  251.         GLES20.glEnable(GLES20.GL_CULL_FACE);
  252.         GLES20.glCullFace(GLES20.GL_BACK);
  253.         if (Renderer.getInstance().getVideoBackgroundConfig().getReflection() == VIDEO_BACKGROUND_REFLECTION.VIDEO_BACKGROUND_REFLECTION_ON)
  254.             GLES20.glFrontFace(GLES20.GL_CW); // Front camera
  255.         else
  256.             GLES20.glFrontFace(GLES20.GL_CCW); // Back camera
  257.  
  258.         float[] modelviewArray = new float[16];
  259.         for (int tIdx = 0; tIdx < state.getNumTrackableResults(); tIdx++) {
  260.             TrackableResult result = state.getTrackableResult(tIdx);
  261.             Trackable trackable = result.getTrackable();
  262.             Matrix44F modelViewMatrix_Vuforia = Tool.convertPose2GLMatrix(result.getPose());
  263.             float[] modelViewMatrix = modelViewMatrix_Vuforia.getData();
  264.             SampleUtils.rotatePoseMatrix(90.0f, 1.0f, 0, 0, modelViewMatrix);
  265.             modelViewMatrix_Vuforia.setData(modelViewMatrix);
  266.             Matrix44F inverseMV = SampleMath.Matrix44FInverse(modelViewMatrix_Vuforia);
  267.             Matrix44F invTranspMV = SampleMath.Matrix44FTranspose(inverseMV);
  268.             updateModelviewMatrix(invTranspMV.getData());
  269.  
  270.         }
  271.  
  272.  
  273.         // hide the objects when the targets are not detected
  274.         if (state.getNumTrackableResults() == 0) {
  275.             float m[] = {
  276.                     1, 0, 0, 0,
  277.                     0, 1, 0, 0,
  278.                     0, 0, 1, 0,
  279.                     0, 0, -10000, 1
  280.             };
  281.             updateModelviewMatrix(m);
  282.         }
  283.  
  284.  
  285.         GLES20.glDisable(GLES20.GL_DEPTH_TEST);
  286.         mRenderer.end();
  287.     }
  288.  
  289.     private void printUserData(Trackable trackable) {
  290.         String userData = (String) trackable.getUserData();
  291.         Log.d(LOGTAG, "UserData:Retreived User Data \"" + userData + "\"");
  292.     }
  293.  
  294.     public void setTextures(Vector<Texture> textures) {
  295.         mTextures = textures;
  296.  
  297.     }
  298.  
  299.     public void updateModelviewMatrix(float mat[]) {
  300.         modelViewMat = mat;
  301.     }
  302.  
  303.     public void updateCamera() {
  304.         if (modelViewMat != null) {
  305.             float[] m = modelViewMat;
  306.  
  307.             final SimpleVector camUp;
  308. //            if (mActivity.isPortrait()) {
  309. //                camUp = new SimpleVector(-m[0], -m[1], -m[2]);
  310. //            } else {
  311.             camUp = new SimpleVector(-m[4], -m[5], -m[6]);
  312. //            }
  313.  
  314.             final SimpleVector camDirection = new SimpleVector(m[8], m[9], m[10]);
  315.             final SimpleVector camPosition = new SimpleVector(m[12], m[13], m[14]);
  316.  
  317.             cam.setOrientation(camDirection, camUp);
  318.             cam.setPosition(camPosition);
  319.  
  320.             cam.setFOV(vuforiaAppSession.getFov());
  321.             cam.setYFOV(vuforiaAppSession.getFovy());
  322.         }
  323.     }
  324.  
  325.  
  326.     public void setVideoSize(int videoWidth, int videoHeight) {
  327.  
  328.         DisplayMetrics displaymetrics = new DisplayMetrics();
  329.         mActivity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
  330.         int height = displaymetrics.heightPixels;
  331.         int width = displaymetrics.widthPixels;
  332.  
  333.         int widestVideo = videoWidth > videoHeight ? videoWidth : videoHeight;
  334.         int widestScreen = width > height ? width : height;
  335.  
  336.         float diff = (widestVideo - widestScreen) / 2;
  337.  
  338.         Config.viewportOffsetY = diff / widestScreen;
  339.     }
  340.  
  341.  
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement