Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. import javax.swing.JFrame;
  2.  
  3. import com.jogamp.opengl.GL;
  4. import com.jogamp.opengl.GL2;
  5. import com.jogamp.opengl.GLAutoDrawable;
  6. import com.jogamp.opengl.GLCapabilities;
  7. import com.jogamp.opengl.GLEventListener;
  8. import com.jogamp.opengl.GLProfile;
  9. import com.jogamp.opengl.awt.GLCanvas;
  10. import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
  11. import com.jogamp.opengl.glu.GLU;
  12. import com.jogamp.opengl.util.Animator;
  13.  
  14. public class MainFrame
  15. extends JFrame
  16. implements GLEventListener
  17. {
  18.  
  19. private GLCanvas canvas;
  20. private Animator animator;
  21.  
  22. private GLU glu;
  23.  
  24. // Application main entry point
  25. public static void main(String args[])
  26. {
  27. new MainFrame();
  28. }
  29.  
  30. // Default constructor;
  31. public MainFrame()
  32. {
  33. super("Java OpenGL");
  34.  
  35. // Registering a window event listener to handle the closing event.
  36. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37.  
  38. this.setSize(800, 600);
  39.  
  40. this.initializeJogl();
  41.  
  42. this.setVisible(true);
  43. }
  44.  
  45. private void initializeJogl()
  46. {
  47. // Creating a new GL profile.
  48. GLProfile glprofile = GLProfile.getDefault();
  49. // Creating an object to manipulate OpenGL parameters.
  50. GLCapabilities capabilities = new GLCapabilities(glprofile);
  51.  
  52. // Setting some OpenGL parameters.
  53. capabilities.setHardwareAccelerated(true);
  54. capabilities.setDoubleBuffered(true);
  55.  
  56. // Try to enable 2x anti aliasing. It should be supported on most hardware.
  57. capabilities.setNumSamples(2);
  58. capabilities.setSampleBuffers(true);
  59.  
  60. // Creating an OpenGL display widget -- canvas.
  61. this.canvas = new GLCanvas(capabilities);
  62.  
  63. // Adding the canvas in the center of the frame.
  64. this.getContentPane().add(this.canvas);
  65.  
  66. // Adding an OpenGL event listener to the canvas.
  67. this.canvas.addGLEventListener(this);
  68.  
  69. // Creating an animator that will redraw the scene 40 times per second.
  70. this.animator = new Animator(this.canvas);
  71.  
  72. // // Registering the canvas to the animator.
  73. // this.animator.add(this.canvas);
  74.  
  75. // Starting the animator.
  76. this.animator.start();
  77. }
  78.  
  79. public void init(GLAutoDrawable canvas)
  80. {
  81. // Obtaining the GL instance associated with the canvas.
  82. GL2 gl = canvas.getGL().getGL2();
  83.  
  84. // Initialize GLU. We'll need it for perspective and camera setup.
  85. this.glu = GLU.createGLU();
  86.  
  87. // Setting the clear color -- the color which will be used to erase the canvas.
  88. gl.glClearColor(0, 0, 0, 0);
  89.  
  90. // Selecting the modelview matrix.
  91. gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
  92.  
  93. }
  94.  
  95. public void display(GLAutoDrawable canvas)
  96. {
  97. GL2 gl = canvas.getGL().getGL2();
  98.  
  99. // Erasing the canvas -- filling it with the clear color.
  100. gl.glClear(GL.GL_COLOR_BUFFER_BIT);
  101.  
  102. gl.glLoadIdentity();
  103. // Add your scene code here
  104.  
  105. // Patrat
  106. gl.glBegin(GL2.GL_LINES);
  107. gl.glColor3f(1.0f, 0.0f, 0.0f);
  108.  
  109. gl.glVertex3f(0.2f, 0.2f, 0);
  110. gl.glVertex3f(0.4f, 0.2f, 0);
  111.  
  112. gl.glVertex3f(0.2f, 0.4f, 0);
  113. gl.glVertex3f(0.4f, 0.4f, 0);
  114.  
  115. gl.glVertex3f(0.2f, 0.2f, 0);
  116. gl.glVertex3f(0.2f, 0.4f, 0);
  117.  
  118. gl.glVertex3f(0.4f, 0.4f, 0);
  119. gl.glVertex3f(0.4f, 0.2f, 0);
  120.  
  121. // Miscator
  122.  
  123. if (topLeftX > 0.95f) {
  124. pas = -pas;
  125. }
  126. if (topLeftX < 0) {
  127. pas = -pas;
  128. }
  129. drawSquare(gl);
  130. topLeftX += pas;
  131.  
  132.  
  133. gl.glEnd();
  134.  
  135. // Forcing the scene to be rendered.
  136. gl.glFlush();
  137. }
  138.  
  139. float topLeftX = 0.2f;
  140. float topLeftY = 0.4f;
  141. float pas = 0.1f;
  142.  
  143. public void drawSquare(GL2 gl) {
  144. gl.glBegin(GL2.GL_LINES);
  145.  
  146. gl.glVertex3f(topLeftX, topLeftY, 0);
  147. gl.glVertex3f(topLeftX+0.2f, topLeftY, 0);
  148.  
  149. gl.glVertex3f(topLeftX+0.2f, topLeftY, 0);
  150. gl.glVertex3f(topLeftX+0.2f, topLeftY-0.2f, 0);
  151.  
  152. gl.glVertex3f(topLeftX+0.2f, topLeftY-0.2f, 0);
  153. gl.glVertex3f(topLeftX, topLeftY-0.2f, 0);
  154.  
  155. gl.glVertex3f(topLeftX, topLeftY-0.2f, 0);
  156. gl.glVertex3f(topLeftX, topLeftY, 0);
  157.  
  158. gl.glEnd();
  159. }
  160.  
  161.  
  162. public void reshape(GLAutoDrawable canvas, int left, int top, int width, int height)
  163. {
  164. GL2 gl = canvas.getGL().getGL2();
  165.  
  166. // Selecting the viewport -- the display area -- to be the entire widget.
  167. gl.glViewport(0, 0, width, height);
  168.  
  169. // Determining the width to height ratio of the widget.
  170. double ratio = (double) width / (double) height;
  171.  
  172. // Selecting the projection matrix.
  173. gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
  174.  
  175. gl.glLoadIdentity();
  176.  
  177. glu.gluPerspective (38, ratio, 0.1, 100);
  178.  
  179. // Selecting the modelview matrix.
  180. gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
  181. }
  182.  
  183. public void displayChanged(GLAutoDrawable canvas, boolean modeChanged, boolean deviceChanged)
  184. {
  185. return;
  186. }
  187.  
  188. @Override
  189. public void dispose(GLAutoDrawable arg0) {
  190. // TODO Auto-generated method stub
  191.  
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement