Opengl - draw cube on screen


SUBMITTED BY: Laka

DATE: May 19, 2016, 7:21 a.m.

FORMAT: Text only

SIZE: 4.7 kB

HITS: 1190

  1. // Use OpenGL and JOGL to draw a simple cube
  2. // with each face being a different color. Rotations
  3. // can be applied with the arrow keys, the page up
  4. // key and the page down key. The home key will set
  5. // all rotations to 0.
  6. import javax.media.opengl.*;
  7. import java.awt.Dimension;
  8. import java.awt.event.KeyEvent;
  9. import java.awt.event.KeyListener;
  10. import javax.swing.JFrame;
  11. import javax.media.opengl.awt.GLJPanel;
  12. public class SimpleCube extends GLJPanel implements GLEventListener, KeyListener {
  13. public static void main(String[] args) {
  14. JFrame window = new JFrame("JOGL Scene");
  15. GLCapabilities caps = new GLCapabilities(null);
  16. SimpleCube panel = new SimpleCube(caps);
  17. window.setContentPane(panel);
  18. window.pack();
  19. window.setLocation(50,50);
  20. window.setResizable(false);
  21. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22. window.setVisible(true);
  23. panel.requestFocusInWindow();
  24. }
  25. private float rotateX, rotateY, rotateZ; // rotation amounts about axes, controlled by keyboard
  26. public SimpleCube(GLCapabilities capabilities) {
  27. super(capabilities);
  28. setPreferredSize( new Dimension(500,500) );
  29. addGLEventListener(this);
  30. addKeyListener(this);
  31. rotateX = 15;
  32. rotateY = 15;
  33. rotateZ = 0;
  34. }
  35. // ------------ methods of the KeyListener interface ------------
  36. public void keyPressed(KeyEvent e) {
  37. int key = e.getKeyCode();
  38. if ( key == KeyEvent.VK_LEFT )
  39. rotateY -= 15;
  40. else if ( key == KeyEvent.VK_RIGHT )
  41. rotateY += 15;
  42. else if ( key == KeyEvent.VK_DOWN)
  43. rotateX += 15;
  44. else if ( key == KeyEvent.VK_UP )
  45. rotateX -= 15;
  46. else if ( key == KeyEvent.VK_PAGE_UP )
  47. rotateZ += 15;
  48. else if ( key == KeyEvent.VK_PAGE_DOWN )
  49. rotateZ -= 15;
  50. else if ( key == KeyEvent.VK_HOME )
  51. rotateX = rotateY = rotateZ = 0;
  52. repaint();
  53. }
  54. public void keyReleased(KeyEvent e) {
  55. }
  56. public void keyTyped(KeyEvent e) {
  57. }
  58. // ----------------- define some drawing methods for this program ----------
  59. private void square(GL2 gl, float r, float g, float b) {
  60. gl.glColor3f(r,g,b); // The color for the square.
  61. gl.glTranslatef(0,0,0.5f); // Move square 0.5 units forward.
  62. gl.glNormal3f(0,0,1); // Normal vector to square (this is actually the default).
  63. gl.glBegin(GL2.GL_TRIANGLE_FAN);
  64. gl.glVertex2f(-0.5f,-0.5f); // Draw the square (before the
  65. gl.glVertex2f(0.5f,-0.5f); // the translation is applied)
  66. gl.glVertex2f(0.5f,0.5f); // on the xy-plane, with its
  67. gl.glVertex2f(-0.5f,0.5f); // at (0,0,0).
  68. gl.glEnd();
  69. }
  70. private void cube(GL2 gl) {
  71. gl.glPushMatrix();
  72. square(gl,1,0,0); // front face is red
  73. gl.glPopMatrix();
  74. gl.glPushMatrix();
  75. gl.glRotatef(180,0,1,0); // rotate square to back face
  76. square(gl,0,1,1); // back face is cyan
  77. gl.glPopMatrix();
  78. gl.glPushMatrix();
  79. gl.glRotatef(-90,0,1,0); // rotate square to left face
  80. square(gl,0,1,0); // left face is green
  81. gl.glPopMatrix();
  82. gl.glPushMatrix();
  83. gl.glRotatef(90,0,1,0); // rotate square to right face
  84. square(gl,1,0,1); // right face is magenta
  85. gl.glPopMatrix();
  86. gl.glPushMatrix();
  87. gl.glRotatef(-90,1,0,0); // rotate square to top face
  88. square(gl,0,0,1); // top face is blue
  89. gl.glPopMatrix();
  90. gl.glPushMatrix();
  91. gl.glRotatef(90,1,0,0); // rotate square to bottom face
  92. square(gl,1,1,0); // bottom face is yellow
  93. gl.glPopMatrix();
  94. }
  95. // --------------- Methods of the GLEventListener interface -----------
  96. public void display(GLAutoDrawable drawable) {
  97. // called when the panel needs to be drawn
  98. GL2 gl = drawable.getGL().getGL2();
  99. gl.glClearColor(0,0,0,0);
  100. gl.glClear( GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
  101. gl.glMatrixMode(GL2.GL_PROJECTION); // Set up the projection.
  102. gl.glLoadIdentity();
  103. gl.glOrtho(-1,1,-1,1,-2,2);
  104. gl.glMatrixMode(GL2.GL_MODELVIEW);
  105. gl.glLoadIdentity(); // Set up modelview transform.
  106. gl.glRotatef(rotateZ,0,0,1);
  107. gl.glRotatef(rotateY,0,1,0);
  108. gl.glRotatef(rotateX,1,0,0);
  109. cube(gl);
  110. }
  111. public void init(GLAutoDrawable drawable) {
  112. // called when the panel is created
  113. GL2 gl = drawable.getGL().getGL2();
  114. gl.glClearColor(0.8F, 0.8F, 0.8F, 1.0F);
  115. gl.glEnable(GL.GL_DEPTH_TEST);
  116. gl.glEnable(GL2.GL_LIGHTING);
  117. gl.glEnable(GL2.GL_LIGHT0);
  118. gl.glEnable(GL2.GL_COLOR_MATERIAL);
  119. }
  120. public void dispose(GLAutoDrawable drawable) {
  121. // called when the panel is being disposed
  122. }
  123. public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
  124. // called when user resizes the window
  125. }
  126. }

comments powered by Disqus