Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 10th, 2012  |  syntax: None  |  size: 6.88 KB  |  hits: 5  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. how to build a GUI/HUD over top a glSurfaceView in android
  2. public class GLLayer extends GLSurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback, Renderer {
  3.  
  4.     private Context context;
  5.  
  6.     private Circle cRing;
  7.     private CompassNeedle cNeedle;
  8.  
  9.     private MarkerNorth mNorth;
  10.     private MarkerEast mEast;
  11.     private MarkerSouth mSouth;
  12.     private MarkerWest mWest;
  13.     private MarkerSouthWest mSWest;
  14.     private MarkerSouthEast mSEast;
  15.     private MarkerNorthWest mNWest;
  16.     private MarkerNorthEast mNEast;
  17.     private MarkerWest mGod;
  18.     private MarkerCustom userTag;
  19.     ArrayList <MarkerCustom> locationTags;
  20.  
  21.     private PhoneOrientation phoneOri;
  22.     private boolean randomSelection[][][]=new boolean[10][10][10];
  23.     ArrayList<double[]> tags = new ArrayList<double[]>();
  24.     double tempOr[] = new double[3];
  25.     ARLaunch _parent;
  26.  
  27.     RelativeLayout rel;
  28.  
  29.     public GLLayer(Context context, int orientation, ArrayList<MarkerCustom> custMarkers,ARLaunch parent) {
  30.         super(context);
  31.  
  32.         locationTags = custMarkers;
  33.         _parent = parent;
  34.  
  35.         this.context = context;
  36.  
  37.         this.mGod = new MarkerWest();
  38.         this.cRing = new Circle();
  39.         this.cNeedle = new CompassNeedle();
  40.  
  41.         this.mNorth = new MarkerNorth();
  42.         this.mEast = new MarkerEast();
  43.         this.mSouth = new MarkerSouth();
  44.         this.mWest = new MarkerWest();
  45.  
  46.         this.mSWest = new MarkerSouthWest();
  47.         this.mSEast = new MarkerSouthEast();
  48.         this.mNWest = new MarkerNorthWest();
  49.         this.mNEast = new MarkerNorthEast();
  50.  
  51.         phoneOri=new PhoneOrientation(context); // sensor manager and interpreter
  52.  
  53.         // settings for translucent glView
  54.         this.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
  55.         this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
  56.  
  57.         // set render to inline
  58.         this.setRenderer(this);
  59.  
  60.         phoneOri.start(context, orientation);
  61.  
  62.  
  63.     }
  64.  
  65.     @Override
  66.     public void onDrawFrame(GL10 gl) {
  67.  
  68.         gl.glEnable(GL10.GL_TEXTURE_2D);
  69.  
  70.         // clear Screen and Depth Buffer
  71.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  72.  
  73.         // Reset the Modelview Matrix
  74.         gl.glLoadIdentity();
  75.  
  76.         //GLU.gluLookAt(gl, eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);
  77.         GLU.gluLookAt(gl,0, 0, 0, 0, 0, 0, 0, 0, 0);
  78.  
  79.         float floatMat[]=phoneOri.getMatrix();
  80.  
  81.         gl.glMatrixMode(GL10.GL_MODELVIEW);
  82.  
  83.  
  84.         gl.glLoadMatrixf(floatMat, 1);
  85.         //ArrayList<Square[]> squares = new ArrayList<Square[]>();
  86.  
  87.  
  88.         gl.glPushMatrix();
  89.         gl.glTranslatef(0,0,-10.0f);
  90.         cNeedle.draw(gl);
  91.         gl.glLoadMatrixf(floatMat,0);
  92.  
  93.         gl.glTranslatef(0,0,10.0f);
  94.         cRing.draw(gl);
  95.  
  96.  
  97.         //Draw South
  98.         gl.glTranslatef(0.0f,-150.0f,-10.0f);
  99.         mSouth.draw(gl);
  100.  
  101.         //Draw West
  102.         gl.glTranslatef(-150.0f,150.0f,0.0f);
  103.         mWest.draw(gl);
  104.  
  105.         //DrawNorth
  106.         gl.glTranslatef(150.0f,150.0f,0.0f);
  107.         mNorth.draw(gl);
  108.  
  109.         //DrawEast
  110.         gl.glTranslatef(150.0f,-150.0f,0.0f);
  111.         mEast.draw(gl);
  112.  
  113.         //SW
  114.         gl.glTranslatef(-225.0f, -75.0f, 0.0f);
  115.         mSWest.draw(gl);
  116.  
  117.         // NW
  118.         gl.glTranslatef(0.0f,150.f,0);
  119.         mNWest.draw(gl);
  120.  
  121.         gl.glTranslatef(150.0f, 0f, 0f);
  122.         mNEast.draw(gl);
  123.  
  124.         gl.glTranslatef(0.0f,-150.0f,0.0f);
  125.         mSEast.draw(gl);
  126.  
  127.  
  128.  
  129.          gl.glPushMatrix();
  130.  
  131.  
  132.     }
  133.  
  134.     @Override
  135.     public void onSurfaceChanged(GL10 gl, int width, int height) {
  136.       if(height == 0) {                    //Prevent A Divide By Zero By
  137.         height = 1;                         //Making Height Equal One
  138.         }
  139.         float ratio = (float) width / height;
  140.         gl.glViewport(0, 0, width, height);     //Reset The Current Viewport
  141.         gl.glMatrixMode(GL10.GL_PROJECTION);    //Select The Projection Matrix
  142.         gl.glLoadIdentity();                    //Reset The Projection Matrix
  143.                                                 //Calculate The Aspect Ratio Of The Window
  144.  
  145.         //gl.glFrustumf(-ratio, ratio, -1, 1, 1, 100);
  146.         GLU.gluPerspective(gl, 35.0f, (float)width / (float)height, 5.0f, 200.0f);
  147.         gl.glMatrixMode(GL10.GL_MODELVIEW);     //Select The Modelview Matrix
  148.         gl.glLoadIdentity();                    //Reset The Modelview Matrix
  149.  
  150.         GLU.gluLookAt(gl, 0, 1.0f, 5.0f, 0, 0, 0, 0, 1.0f, 0);
  151.  
  152.  
  153.     }
  154.  
  155.     @Override
  156.     public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
  157.         // Load the texture for the square
  158.         //square.loadGLTexture(gl, this.context);
  159.  
  160.         for(int i=0;i<locationTags.size();i++){
  161.             userTag=locationTags.get(i);
  162.             userTag.loadGLTexture(gl, this.context);
  163.             //Figure out how to load textures for each
  164.         }
  165.         cRing.loadGLTexture(gl, this.context);
  166.         cNeedle.loadGLTexture(gl, this.context);
  167.         mNorth.loadGLTexture(gl, this.context);
  168.         mEast.loadGLTexture(gl, this.context);
  169.         mSouth.loadGLTexture(gl, this.context);
  170.         mWest.loadGLTexture(gl, this.context);
  171.         mSWest.loadGLTexture(gl, this.context);
  172.         mNWest.loadGLTexture(gl, this.context);
  173.         mSEast.loadGLTexture(gl, this.context);
  174.         mNEast.loadGLTexture(gl, this.context);
  175.  
  176.         gl.glEnable(GL10.GL_TEXTURE_2D);            //Enable Texture Mapping ( NEW )
  177.         gl.glShadeModel(GL10.GL_SMOOTH);            //Enable Smooth Shading
  178.         gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    //Black Background
  179.         gl.glClearDepthf(1.0f);                     //Depth Buffer Setup
  180.         gl.glEnable(GL10.GL_DEPTH_TEST);            //Enables Depth Testing
  181.         gl.glDepthFunc(GL10.GL_LEQUAL);             //The Type Of Depth Testing To Do
  182.  
  183.         //Really Nice Perspective Calculations
  184.         //gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
  185.         gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_SMOOTH);
  186.  
  187.     }
  188.  
  189.     @Override
  190.     public void onPreviewFrame(byte[] data, Camera camera) {
  191.         // TODO Auto-generated method stub
  192.  
  193.     }
  194.        
  195. setContentView(frame);
  196.  
  197.                 frame.addView(camPreview, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  198.                 frame.addView(glView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  199.                 frame.addView(lin);
  200.                 lin.addView(rel);
  201.  
  202. RelativeLayout.LayoutParams pNav = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  203.  
  204.             pNav.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,1);
  205.             pNav.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
  206.             pNav.rightMargin = 20;
  207.  
  208.  
  209.             rel.addView(imageView,pNav);
  210.             lin.bringToFront();
  211.             ;
  212.  
  213.  
  214.             imageView.setOnClickListener(new OnClickListener() {
  215.  
  216.                 @Override
  217.                 public void onClick(View v) {
  218.                     // TODOn Auto-generated method stub
  219.                     glView.newTag();
  220.  
  221.                 }
  222.             });