Guest User

Untitled

a guest
Jul 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. public class OpenGLRenderer extends Activity implements GLSurfaceView.Renderer {
  2. public PointCloud ptCloud;
  3. MatrixGrabber mg = new MatrixGrabber();
  4. ...
  5. public void onDrawFrame(GL10 gl) {
  6.  
  7. gl.glDisable(GL10.GL_COLOR_MATERIAL);
  8. gl.glDisable(GL10.GL_BLEND);
  9. gl.glDisable(GL10.GL_LIGHTING);
  10.  
  11. //Background drawing
  12. if(customBackground)
  13. gl.glClearColor(backgroundRed, backgroundGreen, backgroundBlue, 1.0f);
  14. else
  15. gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  16.  
  17. if (PointCloud.doneParsing == true) {
  18. if (envDone == false)
  19. setupEnvironment();
  20.  
  21. // Clears the screen and depth buffer.
  22. gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  23.  
  24. gl.glMatrixMode(GL10.GL_PROJECTION);
  25. gl.glLoadIdentity();
  26. GLU.gluPerspective(gl, 55.0f, (float) screenWidth / (float) screenHeight, 10.0f ,10000.0f);
  27.  
  28. gl.glMatrixMode(GL10.GL_MODELVIEW);
  29.  
  30. gl.glLoadIdentity();
  31. GLU.gluLookAt(gl, eyeX, eyeY, eyeZ,
  32. centerX, centerY, centerZ,
  33. upX, upY, upZ);
  34.  
  35. if(pickPointTrigger)
  36. pickPoint(gl);
  37.  
  38.  
  39. gl.glPushMatrix();
  40.  
  41. gl.glTranslatef(_xTranslate, _yTranslate, _zTranslate);
  42. gl.glTranslatef(centerX, centerY, centerZ);
  43. gl.glRotatef(_xAngle, 1f, 0f, 0f);
  44. gl.glRotatef(_yAngle, 0f, 1f, 0f);
  45. gl.glRotatef(_zAngle, 0f, 0f, 1f);
  46. gl.glTranslatef(-centerX, -centerY, -centerZ);
  47.  
  48. ptCloud.draw(gl);
  49.  
  50. gl.glPopMatrix();
  51. }
  52. }
  53. }
  54.  
  55. public void pickPoint(GL10 gl){
  56.  
  57. mg.getCurrentState(gl);
  58.  
  59. double mvmatrix[] = new double[16];
  60. double projmatrix[] = new double[16];
  61. int viewport[] = {0,0,screenWidth, screenHeight};
  62.  
  63. for(int i=0 ; i<16; i++){
  64. mvmatrix[i] = mg.mModelView[i];
  65. projmatrix[i] = mg.mProjection[i];
  66. }
  67.  
  68. mg.getCurrentState(gl);
  69.  
  70. float realY = ((float) (screenHeight) - pickY);
  71. float nearCoords[] = { 0.0f, 0.0f, 0.0f, 0.0f };
  72. float farCoords[] = { 0.0f, 0.0f, 0.0f, 0.0f };
  73.  
  74.  
  75. GLU.gluUnProject(screenWidth/2, screenHeight/2, 0.0f, mg.mModelView, 0, mg.mProjection, 0,
  76. viewport, 0, nearCoords, 0);
  77. GLU.gluUnProject(screenWidth/2, screenHeight/2, 1.0f, mg.mModelView, 0, mg.mProjection, 0,
  78. viewport, 0, farCoords, 0);
  79.  
  80. System.out.println("Near: " + nearCoords[0] + "," + nearCoords[1] + "," + nearCoords[2]);
  81. System.out.println("Far: " + farCoords[0] + "," + farCoords[1] + "," + farCoords[2]);
  82.  
  83.  
  84.  
  85. //Plot the points in the scene
  86. nearMarker.set(nearCoords);
  87. farMarker.set(farCoords);
  88. markerOn = true;
  89.  
  90. double diffX = nearCoords[0] - farCoords[0];
  91. double diffY = nearCoords[1] - farCoords[1];
  92. double diffZ = nearCoords[2] - farCoords[2];
  93.  
  94. double rayLength = Math.sqrt(Math.pow(diffX, 2) + Math.pow(diffY, 2) + Math.pow(diffZ, 2));
  95. System.out.println("rayLength: " + rayLength);
  96.  
  97. pickPointTrigger = false;
  98. }
  99.  
  100. GLU.gluPerspective(gl, 55.0f, (float) screenWidth / (float) screenHeight, 1.0f ,100.0f);
  101. .....
  102. 07-18 11:23:50.430: INFO/System.out(31795): Near: 57.574852,-88.60514,37.272636
  103. 07-18 11:23:50.430: INFO/System.out(31795): Far: 0.57574844,0.098602295,0.2700405
  104. 07-18 11:23:50.430: INFO/System.out(31795): rayLength: 111.74275719790872
  105.  
  106. GLU.gluPerspective(gl, 55.0f, (float) width / (float) height, 10.0f , 1000.0f);
  107. ...
  108. 07-18 11:25:12.420: INFO/System.out(31847): Near: 5.7575016,-7.965394,3.6339219
  109. 07-18 11:25:12.420: INFO/System.out(31847): Far: 0.057574987,0.90500546,-0.06634784
  110. 07-18 11:25:12.420: INFO/System.out(31847): rayLength: 11.174307289026638
  111.  
  112. public float[] unproject(float rx, float ry, float rz) {//TODO Factor in projection matrix
  113. float[] modelInv = new float[16];
  114. if (!android.opengl.Matrix.invertM(modelInv, 0, mg.mModelView, 0))
  115. throw new IllegalArgumentException("ModelView is not invertible.");
  116. float[] projInv = new float[16];
  117. if (!android.opengl.Matrix.invertM(projInv, 0, mg.mProjection, 0))
  118. throw new IllegalArgumentException("Projection is not invertible.");
  119. float[] combo = new float[16];
  120. android.opengl.Matrix.multiplyMM(combo, 0, modelInv, 0, projInv, 0);
  121. float[] result = new float[4];
  122. float vx = viewport[0];
  123. float vy = viewport[1];
  124. float vw = viewport[2];
  125. float vh = viewport[3];
  126. float[] rhsVec = {((2*(rx-vx))/vw)-1,((2*(ry-vy))/vh)-1,2*rz-1,1};
  127. android.opengl.Matrix.multiplyMV(result, 0, combo, 0, rhsVec, 0);
  128. float d = 1 / result[3];
  129. float[] endResult = {result[0] * d, result[1] * d, result[2] * d};
  130. return endResult;
  131. }
  132.  
  133. public float distanceToDepth(float distance) {
  134. return ((1/fNear) - (1/distance))/((1/fNear) - (1/fFar));
  135. }
  136.  
  137. float[] xyz = {0, 0, 0};
  138. xyz = unproject(mouseX, viewport[3] - mouseY, 1);
  139.  
  140. unproject(mouseX, viewport[3] - mouseY, distanceToDepth(5));
  141.  
  142. float[] xyzw = {0, 0, 0, 0};
  143. android.opengl.GLU.gluUnProject(rx, ry, rz, mg.mModelView, 0, mg.mProjection, 0, this.viewport, 0, xyzw, 0);
  144. xyzw[0] /= xyzw[3];
  145. xyzw[1] /= xyzw[3];
  146. xyzw[2] /= xyzw[3];
  147. //xyzw[3] /= xyzw[3];
  148. xyzw[3] = 1;
  149. return xyzw;
Add Comment
Please, Sign In to add comment