title: In opengl es 1 im trying to implement a ray picker for an android app
body: <p>I'm trying to implement ray picking in my android app and I've been using <a href="http://android-raypick.blogspot.ca/2012/04/first-i-want-to-state-this-is-my-first.html" rel="nofollow">http://android-raypick.blogspot.ca/2012/04/first-i-want-to-state-this-is-my-first.html</a> as a reference guide. when I import the classes and copy some of the classes from the site I get errors in the code. They do not explain very well where to put the code either. I will be converting everything to the newer Opengl versions later I'm just trying to get a working code and understand it. There is a lot of code but I will list what I have. I realize that this may be duplicate post but ive looked all over the web and have found alot of information about this topic but nothing that I can use as an example already and mess with to understand it.</p>
  
  <pre><code>public class CadActivity extends ActionBarActivity  {
  
  GLSurfaceView cadLayout;
  TextView xV = (TextView) findViewById(R.id.x);
  TextView yV = (TextView) findViewById(R.id.y);
  TextView zV = (TextView) findViewById(R.id.z);
  
  
  
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      cadLayout= new CadGlSurfaceView(this);
      cadLayout.setGLWrapper(new GLSurfaceView.GLWrapper() {
          @Override
          public GL wrap(GL gl) {
              return new MatrixTrackingGL(gl);
          }
      });
      setContentView(R.layout.activity_cad);
      RelativeLayout v = (RelativeLayout) findViewById(R.id.surfacegl);
      v.addView(cadLayout);
  
  
  
  
  }
  </code></pre>
  
  <p>The above code is my main Activity with the GL Wrapper.</p>
  
  <p>This code below is my GLSurfaceView:</p>
  
  <pre><code>public class CadGlSurfaceView extends GLSurfaceView {
  float x,y,sX,sY,fX,fY;
   CadActivity main;
  
  
  public CadGlSurfaceView(Context context) {
  
      super(context);
  
      setRenderer(new CadRenderer());
  }
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
  int action = event.getActionMasked();
  x = event.getX();
  y = event.getY();
      switch(action){
          case MotionEvent.ACTION_DOWN:
              x = event.getX();
              y = event.getY();
              sX = event.getX();
              sY = event.getY();
  
              return true;
          case MotionEvent.ACTION_MOVE:
              x = event.getX();
              y = event.getY();
              return true;
          case MotionEvent.ACTION_UP:
              x = event.getX();
              y = event.getY();
              fX = event.getX();
              fY = event.getY();
  
              return true;
  
      }
  
  
  
  
      return true;
    }
  }
  </code></pre>
  
  <p>This next code is my Renderer:</p>
  
  <pre><code>public class CadRenderer implements GLSurfaceView.Renderer {
  
  private GlObjects objects;
  MatrixGrabber matrixGrabber = new MatrixGrabber();
  public CadRenderer(){
      objects = new GlObjects();
  
  }
  
  
  
  @Override
  public void onSurfaceCreated(GL10 gl, EGLConfig eglconfig) {
      gl.glDisable(GL10.GL_DITHER);
      gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_FASTEST);
      gl.glClearColor(.8f,0f,.2f,1f);
      gl.glClearDepthf(1f);
  }
  
  @Override
  public void onSurfaceChanged(GL10 gl, int width, int height) {
      gl.glViewport(0,0,width,height);
      int[] viewport = {0, 0, width, height};
      float ratio = (float) width/height;
      gl.glMatrixMode(GL10.GL_PROJECTION);
      gl.glLoadIdentity();
      gl.glFrustumf(-ratio,ratio,-1,1f,1,25);
      matrixGrabber.getCurrentState(gl);
      matrixGrabber.mModelView;
      matrixGrabber.mProjection;
  
  }
  
  @Override
  public void onDrawFrame(GL10 gl) {
      gl.glDisable(GL10.GL_DITHER);
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  
      gl.glMatrixMode(GL10.GL_MODELVIEW);
      gl.glLoadIdentity();
      GLU.gluLookAt(gl,0,0,-5,0,0,0,0,2,0);
  
      objects.draw(gl);
  
    }
  }
  </code></pre>
  
  <p>matrixGrabber.mModelView;
  matrixGrabber.mProjection;
  These two pieces of code have a red line under them saying (Not a Statement).</p>
  
  <p>This is my Object that's being drawn. I will be drawing lines so I know I need to change some things.</p>
  
  <pre><code>public class GlObjects {
  private float vertices[]={
      0f,1f,
      1f,-1f,
      -1f,-1f
  };
  
  private short pIndex[]={0,1,2};
  
  private ShortBuffer pBuff;
  private FloatBuffer vertBuff;
  
  public GlObjects(){
      ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length*4);
      bBuff.order(ByteOrder.nativeOrder());
      vertBuff = bBuff.asFloatBuffer();
      vertBuff.put(vertices);
      vertBuff.position(0);
  
      ByteBuffer pbBuff = ByteBuffer.allocateDirect(pIndex.length*2);
      pbBuff.order(ByteOrder.nativeOrder());
      pBuff = pbBuff.asShortBuffer();
      pBuff.put(pIndex);
      pBuff.position(0);
  }
  
  public void draw(GL10 gl){
      gl.glFrontFace(GL10.GL_CW);
      gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
      gl.glVertexPointer(2,GL10.GL_FLOAT,0,vertBuff);
      gl.glDrawElements(GL10.GL_TRIANGLES,pIndex.length,GL10.GL_UNSIGNED_SHORT,pBuff);
      gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
  
    }
  }
  </code></pre>
  
  <p>This is the Ray class from the website.</p>
  
  <pre><code>public class Ray {
  public Ray(GL10 gl, int width, int height, float xTouch, float yTouch) {
      MatrixGrabber matrixGrabber = new MatrixGrabber();
      matrixGrabber.getCurrentState(gl);
  
      int[] viewport = {0, 0, width, height};
  
      float[] nearCoOrds = new float[3];
      float[] farCoOrds = new float[3];
      float[] temp = new float[4];
      float[] temp2 = new float[4];
      // get the near and far ords for the click
  
      float winx = xTouch, winy =(float)viewport[3] - yTouch;
  
  //        Log.d(TAG, "modelView is =" + Arrays.toString(matrixGrabber.mModelView));
  //        Log.d(TAG, "projection view is =" + Arrays.toString( matrixGrabber.mProjection ));
  
      int result = GLU.gluUnProject(winx, winy, 1.0f, matrixGrabber.mModelView, 0, 
  matrixGrabber.mProjection, 0, viewport, 0, temp, 0);
  
      Matrix.multiplyMV(temp2, 0, matrixGrabber.mModelView, 0, temp, 0);
      if(result == GL10.GL_TRUE){
          nearCoOrds[0] = temp2[0] / temp2[3];
          nearCoOrds[1] = temp2[1] / temp2[3];
          nearCoOrds[2] = temp2[2] / temp2[3];
  
      }
  
      result = GLU.gluUnProject(winx, winy, 0, matrixGrabber.mModelView, 0,    
  matrixGrabber.mProjection, 0, viewport, 0, temp, 0);
      Matrix.multiplyMV(temp2,0,matrixGrabber.mModelView, 0, temp, 0);
      if(result == GL10.GL_TRUE){
          farCoOrds[0] = temp2[0] / temp2[3];
          farCoOrds[1] = temp2[1] / temp2[3];
          farCoOrds[2] = temp2[2] / temp2[3];
      }
      this.P0 = farCoOrds;
      this.P1 = nearCoOrds;
    }
  
  }
  </code></pre>
  
  <p>this.P0 = farCoOrds;
  this.P1 = nearCoOrds;
  Has a red line under them saying (Cannot Resolve Symbol).</p>
  
  <p>This is the Triangle class from the website:</p>
  
  <pre><code>public class Triangle {
  public float[] V0;
  public float[] V1;
  public float[] V2;
  
  public Triangle(float[] V0, float[] V1, float[] V2){
      this.V0 =V0;
      this.V1 = V1;
      this.V2 = V2;
  }
  
  
  private static final float SMALL_NUM =  0.00000001f; // anything that avoids division overflow
  
  
  // intersectRayAndTriangle(): intersect a ray with a 3D triangle
  //    Input:  a ray R, and a triangle T
  //    Output: *I = intersection point (when it exists)
  //    Return: -1 = triangle is degenerate (a segment or point)
  //             0 = disjoint (no intersect)
  //             1 = intersect in unique point I1
  //             2 = are in the same plane
  public static int intersectRayAndTriangle(Ray R, Triangle T, float[] I)
  {
      float[]    u, v, n;             // triangle vectors
      float[]    dir, w0, w;          // ray vectors
      float     r, a, b;             // params to calc ray-plane intersect
  
      // get triangle edge vectors and plane normal
      u =  Vector.minus(T.V1, T.V0);
      v =  Vector.minus(T.V2, T.V0);
      n =  Vector.crossProduct(u, v);             // cross product
  
      if (Arrays.equals(n, new float[]{0.0f, 0.0f, 0.0f})){           // triangle is degenerate
          return -1;                 // do not deal with this case
      }
      dir =  Vector.minus(R.P1, R.P0);             // ray direction vector
      w0 = Vector.minus( R.P0 , T.V0);
      a = - Vector.dot(n,w0);
      b =  Vector.dot(n,dir);
      if (Math.abs(b) &lt; SMALL_NUM) {     // ray is parallel to triangle plane
          if (a == 0){                // ray lies in triangle plane
              return 2;
          }else{
              return 0;             // ray disjoint from plane
          }
      }
  
      // get intersect point of ray with triangle plane
      r = a / b;
      if (r &lt; 0.0f){                   // ray goes away from triangle
          return 0;                  // =&gt; no intersect
      }
      // for a segment, also test if (r &gt; 1.0) =&gt; no intersect
  
      float[] tempI =  Vector.addition(R.P0,  Vector.scalarProduct(r, dir));             
     //   intersect point of ray and plane
      I[0] = tempI[0];
      I[1] = tempI[1];
      I[2] = tempI[2];
  
      // is I inside T?
      float    uu, uv, vv, wu, wv, D;
      uu =  Vector.dot(u,u);
      uv =  Vector.dot(u,v);
      vv =  Vector.dot(v,v);
      w =  Vector.minus(I, T.V0);
      wu =  Vector.dot(w,u);
      wv = Vector.dot(w,v);
      D = (uv * uv) - (uu * vv);
  
      // get and test parametric coords
      float s, t;
      s = ((uv * wv) - (vv * wu)) / D;
      if (s &lt; 0.0f || s &gt; 1.0f)        // I is outside T
          return 0;
      t = (uv * wu - uu * wv) / D;
      if (t &lt; 0.0f || (s + t) &gt; 1.0f)  // I is outside T
          return 0;
  
      return 1;                      // I is in T
     }
  
  
  }
  </code></pre>
  
  <p>Then last the Vector class from the website.</p>
  
  <pre><code>public class Vector {
  // dot product (3D) which allows vector operations in arguments
  public static float dot(float[] u,float[] v) {
      return ((u[X] * v[X]) + (u[Y] * v[Y]) + (u[Z] * v[Z]));
  }
  public static float[] minus(float[] u, float[] v){
      return new float[]{u[X]-v[X],u[Y]-v[Y],u[Z]-v[Z]};
  }
  public static float[] addition(float[] u, float[] v){
      return new float[]{u[X]+v[X],u[Y]+v[Y],u[Z]+v[Z]};
  }
  //scalar product
  public static float[] scalarProduct(float r, float[] u){
      return new float[]{u[X]*r,u[Y]*r,u[Z]*r};
  }
  // (cross product)
  public static float[] crossProduct(float[] u, float[] v){
      return new float[]{(u[Y]*v[Z]) - (u[Z]*v[Y]),(u[Z]*v[X]) - (u[X]*v[Z]),(u[X]*v[Y]) - (u[Y]*v 
  [X])};
  }
  //mangnatude or length
  public static float length(float[] u){
      return (float) Math.abs(Math.sqrt((u[X] *u[X]) + (u[Y] *u[Y]) + (u[Z] *u[Z])));
  }
  
  public static final int X = 0;
  public static final int Y = 1;
  public static final int Z = 2;
  }
  </code></pre>
  
  <p>Like I was saying I'm trying to get a working example and understand how to implement this code.</p>