Advertisement
Guest User

Untitled

a guest
Mar 6th, 2020
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. public class GLVectorCanvas extends GLCanvas implements GLEventListener{
  2. private GL2 gl;
  3. private GLU glu = new GLU();
  4. private final ArrayList<Vector> vectors;
  5. private FPSAnimator animator;
  6. private final float XYAngle;
  7. private final float cameraX;
  8. private final float cameraY;
  9. private final float cameraHeight;
  10.  
  11. public GLVectorCanvas(ArrayList<Vector> vectors, float XYAngle, float cameraX, float cameraY, float cameraHeight) {
  12. this.vectors = vectors;
  13. this.XYAngle = XYAngle;
  14. this.cameraX = cameraX;
  15. this.cameraY = cameraY;
  16. this.cameraHeight = cameraHeight;
  17. }
  18.  
  19. @Override
  20. public void display(GLAutoDrawable drawable) {
  21.  
  22. gl = drawable.getGL().getGL2();
  23. gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  24.  
  25. /* X component of grid*/
  26. for(int i = 0; i <= 10; i++) {
  27. gl.glBegin (GL2.GL_LINES);
  28. gl.glColor3f(1.0f,0.0f,0.0f);
  29. gl.glVertex3f(1.0f,-1.0f + 0.2f*i,0.0f);
  30. gl.glVertex3f(-1.0f,-1.0f + 0.2f*i,0.0f);
  31. gl.glPopMatrix();
  32. gl.glEnd();
  33. }
  34.  
  35. /* Y component of grid*/
  36. for(int i = 0; i <= 10; i++) {
  37. gl.glBegin (GL2.GL_LINES);
  38. gl.glColor3f(0.0f,0.0f,1.0f);
  39. gl.glVertex3f(-1.0f + 0.2f*i,1.0f, 0.0f);
  40. gl.glVertex3f(-1.0f + 0.2f*i,-1.0f, 0.0f);
  41. gl.glPopMatrix();
  42. gl.glEnd();
  43. }
  44.  
  45. /* Z Axis*/
  46. gl.glBegin (GL2.GL_LINES);
  47. gl.glColor3f(0.0f,1.0f,0.0f);
  48. gl.glVertex3f(0.0f,0.0f,1.0f);
  49. gl.glVertex3f(0.0f,0.0f,-1.0f);
  50. gl.glPopMatrix();
  51. gl.glEnd();
  52.  
  53. /* Z Axis ticks parallel to X axis*/
  54. gl.glBegin (GL2.GL_LINES);
  55. gl.glColor3f(0.0f,1.0f,0.0f);
  56. gl.glVertex3f(-0.2f,0.0f,-1.0f);
  57. gl.glVertex3f(0.2f,0.0f,-1.0f);
  58. gl.glPopMatrix();
  59. gl.glEnd();
  60.  
  61. gl.glBegin (GL2.GL_LINES);
  62. gl.glColor3f(0.0f,1.0f,0.0f);
  63. gl.glVertex3f(-0.2f,0.0f,1.0f);
  64. gl.glVertex3f(0.2f,0.0f,1.0f);
  65. gl.glPopMatrix();
  66. gl.glEnd();
  67.  
  68. /* Z Axis ticks Parallel to Y AXis*/
  69. gl.glBegin (GL2.GL_LINES);
  70. gl.glColor3f(0.0f,1.0f,0.0f);
  71. gl.glVertex3f(0.0f,-0.2f,-1.0f);
  72. gl.glVertex3f(0.0f,0.2f,-1.0f);
  73. gl.glPopMatrix();
  74. gl.glEnd();
  75.  
  76. gl.glBegin (GL2.GL_LINES);
  77. gl.glColor3f(0.0f,1.0f,0.0f);
  78. gl.glVertex3f(0.0f,-0.2f,1.0f);
  79. gl.glVertex3f(0.0f,0.2f,1.0f);
  80. gl.glPopMatrix();
  81. gl.glEnd();
  82.  
  83. /* Draw vectors*/
  84. for(Vector vector : vectors) {
  85. gl.glBegin(GL2.GL_LINES);
  86. gl.glColor3f(1.0f,1.0f,1.0f);
  87. gl.glVertex3f(0.0f,0.0f,0.0f);
  88. gl.glVertex3f(vector.getX(),vector.getY(),vector.getZ());
  89. gl.glPopMatrix();
  90. gl.glEnd();
  91. }
  92.  
  93. /* End List*/
  94. gl.glEndList();
  95. gl.glFinish();
  96.  
  97. // Change to projection matrix.
  98. gl.glMatrixMode(GL2.GL_PROJECTION);
  99. gl.glLoadIdentity();
  100.  
  101. //Calculate components from angle;
  102. Vector vec = this.rotate3DVector(this.XYAngle, this.cameraX, this.cameraY);
  103.  
  104. // Perspective.
  105. float widthHeightRatio = (float) getWidth() / (float) getHeight();
  106. glu.gluPerspective(45.0f, widthHeightRatio, 1, 1000);
  107. glu.gluLookAt(vec.getX(), vec.getY(), this.cameraHeight, 0, 0, 0, 0, 0, 1);
  108.  
  109. // Change back to model view matrix.
  110. gl.glMatrixMode(GL2.GL_MODELVIEW);
  111. gl.glLoadIdentity();
  112.  
  113. }
  114.  
  115. @Override
  116. public void dispose(GLAutoDrawable arg0) {
  117. //method body
  118. }
  119.  
  120. @Override
  121. public void init(GLAutoDrawable drawable) {
  122. gl = drawable.getGL().getGL2();
  123. glu = new GLU();
  124.  
  125. // Global settings.
  126. gl.glEnable(GL2.GL_DEPTH_TEST);
  127. gl.glDepthFunc(GL2.GL_LEQUAL);
  128. gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
  129. gl.glClearColor(0f, 0f, 0f, 1f);
  130.  
  131. animator = new FPSAnimator(this, 24);
  132. animator.start();
  133. }
  134.  
  135. @Override
  136. public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
  137. gl = drawable.getGL().getGL2();
  138. gl.glViewport(0, 0, width, height);
  139. }
  140.  
  141.  
  142.  
  143. /* Rotate 3d vecotr*/
  144. public Vector rotate3DVector(float angle, float xComp, float yComp) {
  145. float[] pt = {xComp, yComp};
  146. AffineTransform.getRotateInstance(Math.toRadians(angle), 0, 0)
  147. .transform(pt, 0, pt, 0, 1); // specifying to use this double[] to hold coords
  148. float newX = pt[0];
  149. float newY = pt[1];
  150. return new Vector(newX, newY, 0);
  151. }
  152.  
  153. /* Test on device with head*/
  154. public static void main(String[] args) {
  155. ArrayList<Vector> list = new ArrayList<>();
  156. list.add(new Vector(1.0f,0.5f,-0.5f));
  157. list.add(new Vector(-0.6f,0.2f,0.4f));
  158.  
  159. GLVectorCanvas canvas = new GLVectorCanvas(list, 180.0f, 2.0f, 2.0f, -0.5f);
  160. //System.out.println(canvas.getSize());
  161. JFrame frame = new JFrame("GLVectorCanvas Frame");
  162. frame.getContentPane().add(canvas, BorderLayout.CENTER);
  163. frame.setSize(1000, 1000);
  164. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  165. frame.setVisible(true);
  166. canvas.requestFocus();
  167. canvas.addGLEventListener(canvas);
  168. }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement