ArViewer  Android Version
Arvos - Augmented reality viewer open source
 All Classes Namespaces Files Functions Variables
Ray.java
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Gregory Beauchamp
3  *
4  * Derived from
5  * http://android-raypick.blogspot.com/2012/04/first-i-want-to-state-this-is-my-first.html
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 package com.mission_base.arviewer_android.viewer.utilities;
21 
22 import javax.microedition.khronos.opengles.GL10;
23 
24 import android.opengl.GLU;
25 import android.opengl.Matrix;
26 
27 public class Ray
28 {
29  public float[] P0;
30  public float[] P1;
31 
32  public Ray(float[] modelView, float[] projection, int width, int height, float xTouch, float yTouch)
33  {
34  int[] viewport = { 0, 0, width, height };
35 
36  float[] nearCoOrds = new float[3];
37  float[] farCoOrds = new float[3];
38  float[] temp = new float[4];
39  float[] temp2 = new float[4];
40 
41  // get the near and far cords for the click
42 
43  float winx = xTouch, winy = (float) viewport[3] - yTouch;
44 
45  // Log.d(TAG, "modelView is =" +
46  // Arrays.toString(matrixGrabber.mModelView));
47  // Log.d(TAG, "projection view is =" + Arrays.toString(
48  // matrixGrabber.mProjection ));
49 
50  int result = GLU.gluUnProject(winx, winy, 1.0f, modelView, 0, projection, 0, viewport, 0, temp, 0);
51 
52  Matrix.multiplyMV(temp2, 0, modelView, 0, temp, 0);
53  if (result == GL10.GL_TRUE)
54  {
55  nearCoOrds[0] = temp2[0] / temp2[3];
56  nearCoOrds[1] = temp2[1] / temp2[3];
57  nearCoOrds[2] = temp2[2] / temp2[3];
58  }
59 
60  result = GLU.gluUnProject(winx, winy, 0, modelView, 0, projection, 0, viewport, 0, temp, 0);
61  Matrix.multiplyMV(temp2, 0, modelView, 0, temp, 0);
62  if (result == GL10.GL_TRUE)
63  {
64  farCoOrds[0] = temp2[0] / temp2[3];
65  farCoOrds[1] = temp2[1] / temp2[3];
66  farCoOrds[2] = temp2[2] / temp2[3];
67  }
68  this.P0 = farCoOrds;
69  this.P1 = nearCoOrds;
70  }
71 }