Advertisement
Smardoi

Untitled

Mar 19th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. import com.jogamp.opengl.GL;
  2. import com.jogamp.opengl.GL2;
  3. import com.jogamp.opengl.GLAutoDrawable;
  4. import com.jogamp.opengl.GLCapabilities;
  5. import com.jogamp.opengl.GLEventListener;
  6. import com.jogamp.opengl.awt.GLCanvas;
  7. import com.jogamp.opengl.GLProfile;
  8. import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
  9. import javax.swing.JFrame;
  10.  
  11. import com.jogamp.opengl.util.Animator;
  12.  
  13. public class MainFrame extends JFrame implements GLEventListener {
  14.  
  15. private GLCanvas canvas;
  16. private Animator animator;
  17.  
  18. // For specifying the positions of the clipping planes (increase/decrease the distance) modify this variable.
  19. // It is used by the glOrtho method.
  20. private double v_size = 2.0;
  21.  
  22. // Application main entry point
  23. public static void main(String args[]) {
  24. new MainFrame();
  25. }
  26.  
  27. // Default constructor
  28. public MainFrame() {
  29. super("Java OpenGL");
  30.  
  31. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32.  
  33. this.setSize(800, 600);
  34.  
  35. this.initializeJogl();
  36.  
  37. this.setVisible(true);
  38. }
  39.  
  40. private void initializeJogl() {
  41. // Creating a new GL profile.
  42. GLProfile glprofile = GLProfile.getDefault();
  43. // Creating an object to manipulate OpenGL parameters.
  44. GLCapabilities capabilities = new GLCapabilities(glprofile);
  45.  
  46. // Setting some OpenGL parameters.
  47. capabilities.setHardwareAccelerated(true);
  48. capabilities.setDoubleBuffered(true);
  49.  
  50. // Try to enable 2x anti aliasing. It should be supported on most hardware.
  51. capabilities.setNumSamples(2);
  52. capabilities.setSampleBuffers(true);
  53.  
  54. // Creating an OpenGL display widget -- canvas.
  55. this.canvas = new GLCanvas(capabilities);
  56.  
  57. // Adding the canvas in the center of the frame.
  58. this.getContentPane().add(this.canvas);
  59.  
  60. // Adding an OpenGL event listener to the canvas.
  61. this.canvas.addGLEventListener(this);
  62.  
  63. // Creating an animator that will redraw the scene 40 times per second.
  64. this.animator = new Animator(this.canvas);
  65.  
  66. // Starting the animator.
  67. this.animator.start();
  68. }
  69.  
  70. public void init(GLAutoDrawable canvas) {
  71. // Obtaining the GL instance associated with the canvas.
  72. GL2 gl = canvas.getGL().getGL2();
  73.  
  74. // Setting the clear color -- the color which will be used to erase the canvas.
  75. gl.glClearColor(0, 0, 0, 0);
  76.  
  77. // Selecting the modelview matrix.
  78. gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
  79.  
  80. }
  81.  
  82. public double cy = 0.001;
  83.  
  84. double px = -0.5d;
  85. double py = -0.5d;
  86. double qx = 0.5d;
  87. double qy = 0.5d;
  88. double pas = 0.005;
  89.  
  90. double pqy = qy - py;
  91. double pqx = qx - px;
  92.  
  93. public void display(GLAutoDrawable canvas) {
  94.  
  95. GL2 gl = canvas.getGL().getGL2();
  96. gl.glClear(GL.GL_COLOR_BUFFER_BIT);
  97.  
  98. gl.glPointSize(10);
  99. gl.glBegin(GL2.GL_POINTS);
  100. gl.glVertex2d(px, py);
  101. gl.glVertex2d(qx, qy);
  102.  
  103. gl.glEnd();
  104. gl.glFlush();
  105. px = px + pqx * pas;
  106. py = py + pqy * pas;
  107. }
  108.  
  109. public void reshape(GLAutoDrawable canvas, int left, int top, int width, int height) {
  110. GL2 gl = canvas.getGL().getGL2();
  111.  
  112. // Selecting the viewport -- the display area -- to be the entire widget.
  113. gl.glViewport(0, 0, width, height);
  114.  
  115. // Determining the width to height ratio of the widget.
  116. double ratio = (double) width / (double) height;
  117.  
  118. // Selecting the projection matrix.
  119. gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
  120.  
  121. gl.glLoadIdentity();
  122.  
  123. // Selecting the view volume to be x from 0 to 1, y from 0 to 1, z from -1 to 1.
  124. // But we are careful to keep the aspect ratio and enlarging the width or the height.
  125. if (ratio < 1) {
  126. gl.glOrtho(-v_size, v_size, -v_size, v_size / ratio, -1, 1);
  127. } else {
  128. gl.glOrtho(-v_size, v_size * ratio, -v_size, v_size, -1, 1);
  129. }
  130.  
  131. // Selecting the modelview matrix.
  132. gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
  133. }
  134.  
  135. public void displayChanged(GLAutoDrawable canvas, boolean modeChanged, boolean deviceChanged) {
  136. return;
  137. }
  138.  
  139. @Override public void dispose(GLAutoDrawable arg0) {
  140. // TODO Auto-generated method stub
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement