Смена цвета сторон куба по таймеру и источник света [OpenGL] - Java

Узнай цену своей работы

Формулировка задачи:

Здравствуйте, я новичок изучаю OpenGL и не получается реализовать две функции: смена цвета сторон куба по событию и добавления источника света. Пожалуйста помогите советом или примером! Заранее спасибо! Вот код куба
Листинг программы
  1. import com.jogamp.opengl.*;
  2. import com.jogamp.opengl.awt.GLCanvas;
  3. import com.jogamp.opengl.glu.GLU;
  4. import com.jogamp.opengl.util.FPSAnimator;
  5. import java.awt.*;
  6. import javax.swing.*;
  7. import static com.sun.tools.doclint.Entity.lt;
  8.  
  9. public class Cube implements GLEventListener {
  10. public static DisplayMode dm, dm_old;
  11. private GLU glu = new GLU();
  12. private float rquad = 0.0f;
  13. @Override
  14. public void display( GLAutoDrawable drawable ) {
  15. final GL2 gl = drawable.getGL().getGL2();
  16. gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT );
  17. gl.glLoadIdentity();
  18. gl.glTranslatef( 0f, 0f, -5.0f );
  19. // Rotate The Cube On X, Y & Z
  20. gl.glRotatef(rquad, 1.0f, 1.0f, 1.0f);
  21. //giving different colors to different sides
  22. gl.glBegin(GL2.GL_QUADS); // Start Drawing The Cube
  23. gl.glColor3f(1f,0f,0f); //red color
  24. gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top)
  25. gl.glVertex3f( -1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top)
  26. gl.glVertex3f( -1.0f, 1.0f, 1.0f ); // Bottom Left Of The Quad (Top)
  27. gl.glVertex3f( 1.0f, 1.0f, 1.0f ); // Bottom Right Of The Quad (Top)
  28. gl.glColor3f( 0f,1f,0f ); //green color
  29. gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Top Right Of The Quad
  30. gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Top Left Of The Quad
  31. gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad
  32. gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad
  33. gl.glColor3f( 0f,0f,1f ); //blue color
  34. gl.glVertex3f( 1.0f, 1.0f, 1.0f ); // Top Right Of The Quad (Front)
  35. gl.glVertex3f( -1.0f, 1.0f, 1.0f ); // Top Left Of The Quad (Front)
  36. gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Bottom Left Of The Quad
  37. gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Bottom Right Of The Quad
  38. gl.glColor3f( 1f,1f,0f ); //yellow (red + green)
  39. gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad
  40. gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad
  41. gl.glVertex3f( -1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Back)
  42. gl.glVertex3f( 1.0f, 1.0f, -1.0f ); // Top Left Of The Quad (Back)
  43. gl.glColor3f( 1f,0f,1f ); //purple (red + green)
  44. gl.glVertex3f( -1.0f, 1.0f, 1.0f ); // Top Right Of The Quad (Left)
  45. gl.glVertex3f( -1.0f, 1.0f, -1.0f ); // Top Left Of The Quad (Left)
  46. gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad
  47. gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Bottom Right Of The Quad
  48. gl.glColor3f( 0f,1f, 1f ); //sky blue (blue +green)
  49. gl.glVertex3f( 1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Right)
  50. gl.glVertex3f( 1.0f, 1.0f, 1.0f ); // Top Left Of The Quad
  51. gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Bottom Left Of The Quad
  52. gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad
  53. gl.glEnd(); // Done Drawing The Quad
  54. gl.glFlush();
  55. rquad -= 0.15f;
  56. }
  57. @Override
  58. public void dispose( GLAutoDrawable drawable ) {
  59. // TODO Auto-generated method stub
  60. }
  61. @Override
  62. public void init( GLAutoDrawable drawable ) {
  63. final GL2 gl = drawable.getGL().getGL2();
  64. gl.glShadeModel( GL2.GL_SMOOTH );
  65. gl.glClearColor( 0f, 0f, 0f, 0f );
  66. gl.glClearDepth( 1.0f );
  67. gl.glEnable( GL2.GL_DEPTH_TEST );
  68. gl.glDepthFunc( GL2.GL_LEQUAL );
  69. gl.glHint( GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST );
  70. }
  71. @Override
  72. public void reshape( GLAutoDrawable drawable, int x, int y, int width, int height ) {
  73. // TODO Auto-generated method stub
  74. final GL2 gl = drawable.getGL().getGL2();
  75. //if( height lt;=0 )
  76. //height = 1;
  77. final float h = ( float ) width / ( float ) height;
  78. gl.glViewport( 0, 0, width, height );
  79. gl.glMatrixMode( GL2.GL_PROJECTION );
  80. gl.glLoadIdentity();
  81. glu.gluPerspective( 45.0f, h, 1.0, 20.0 );
  82. gl.glMatrixMode( GL2.GL_MODELVIEW );
  83. gl.glLoadIdentity();
  84. }
  85. public static void main( String[] args ) {
  86. final GLProfile profile = GLProfile.get( GLProfile.GL2 );
  87. GLCapabilities capabilities = new GLCapabilities( profile );
  88. // The canvas
  89. final GLCanvas glcanvas = new GLCanvas( capabilities );
  90. Cube cube = new Cube();
  91. glcanvas.addGLEventListener( cube );
  92. glcanvas.setSize( 400, 400 );
  93. final JFrame frame = new JFrame ( " Multicolored cube" );
  94. frame.getContentPane().add( glcanvas );
  95. frame.setSize( frame.getContentPane().getPreferredSize() );
  96. frame.setVisible( true );
  97. final FPSAnimator animator = new FPSAnimator(glcanvas, 300,true);
  98. animator.start();
  99. }
  100.  
  101. }
С источником света я смог разобраться, теперь все получается. Остается только понять, как заставить по таймеру менять цвет сторон куба
Листинг программы
  1. gl.glEnable(GL2.GL_LIGHTING);
  2. gl.glEnable(GL2.GL_LIGHT0);//активируем нулевой свет
  3. gl.glEnable(GL2.GL_COLOR_MATERIAL);//включаем освещение материала (???)

Решение задачи: «Смена цвета сторон куба по таймеру и источник света [OpenGL]»

textual
Листинг программы
  1. import com.jogamp.opengl.*;
  2. import com.jogamp.opengl.awt.GLCanvas;
  3. import com.jogamp.opengl.glu.GLU;
  4. import com.jogamp.opengl.util.FPSAnimator;
  5.  
  6. import javax.swing.*;
  7. import java.awt.*;
  8.  
  9.  
  10. public class Cube implements GLEventListener {
  11.  
  12.     public static DisplayMode dm, dm_old;
  13.     private GLU glu = new GLU();
  14.     private float rquad = 0.0f;
  15.  
  16.     private float[] color1 = new float[]{1f, 0f, 0f};   //red color
  17.     private float[] color2 = new float[]{0f, 1f, 0f};   //green color
  18.     private float[] color3 = new float[]{0f, 0f, 1f};   //blue color
  19.     private float[] color4 = new float[]{1f, 1f, 0f};   //yellow (red + green)
  20.     private float[] color5 = new float[]{1f, 0f, 1f};   //purple (red + green)
  21.     private float[] color6 = new float[]{0f, 1f, 1f};   //sky blue (blue +green)
  22.  
  23.     private Thread thread = new Thread();
  24.  
  25.     public static void main(String[] args) {
  26.  
  27.         final GLProfile profile = GLProfile.get(GLProfile.GL2);
  28.         GLCapabilities capabilities = new GLCapabilities(profile);
  29.  
  30.         // The canvas
  31.         final GLCanvas glcanvas = new GLCanvas(capabilities);
  32.         Cube cube = new Cube();
  33.  
  34.         glcanvas.addGLEventListener(cube);
  35.         glcanvas.setSize(400, 400);
  36.  
  37.         final JFrame frame = new JFrame(" Multicolored cube");
  38.         frame.getContentPane().add(glcanvas);
  39.         frame.setSize(frame.getContentPane().getPreferredSize());
  40.         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  41.         frame.setVisible(true);
  42.  
  43.         final FPSAnimator animator = new FPSAnimator(glcanvas, 300, true);
  44.  
  45.         animator.start();
  46.     }
  47.  
  48.     @Override
  49.     public void display(GLAutoDrawable drawable) {
  50.  
  51.         final GL2 gl = drawable.getGL().getGL2();
  52.         gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
  53.         gl.glLoadIdentity();
  54.         gl.glTranslatef(0f, 0f, -5.0f);
  55.  
  56.         // Rotate The Cube On X, Y & Z
  57.         gl.glRotatef(rquad, 1.0f, 1.0f, 1.0f);
  58.  
  59.         //giving different colors to different sides
  60.         gl.glBegin(GL2.GL_QUADS); // Start Drawing The Cube
  61.         gl.glColor3fv(color1, 0);
  62.         gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top)
  63.         gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top)
  64.         gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
  65.         gl.glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
  66.  
  67.         gl.glColor3fv(color2, 0);
  68.         gl.glVertex3f(1.0f, -1.0f, 1.0f); // Top Right Of The Quad
  69.         gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Top Left Of The Quad
  70.         gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad
  71.         gl.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad
  72.  
  73.         gl.glColor3fv(color3, 0);
  74.         gl.glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
  75.         gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
  76.         gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad
  77.         gl.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad
  78.  
  79.         gl.glColor3fv(color4, 0);
  80.         gl.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad
  81.         gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad
  82.         gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Back)
  83.         gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Back)
  84.  
  85.         gl.glColor3fv(color5, 0);
  86.         gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
  87.         gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Left)
  88.         gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad
  89.         gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad
  90.  
  91.         gl.glColor3fv(color6, 0);
  92.         gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Right)
  93.         gl.glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Quad
  94.         gl.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad
  95.         gl.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad
  96.         gl.glEnd(); // Done Drawing The Quad
  97.         gl.glFlush();
  98.  
  99.         rquad -= 0.15f;
  100.  
  101.         if (!thread.isAlive()) {
  102.             thread = new Thread(() -> {
  103.                 try {Thread.sleep(2000);} catch (InterruptedException e) {}
  104.                 color1[0] = (float) Math.random();
  105.                 color1[1] = (float) Math.random();
  106.                 color1[2] = (float) Math.random();
  107.  
  108.                 color2[0] = (float) Math.random();
  109.                 color2[1] = (float) Math.random();
  110.                 color2[2] = (float) Math.random();
  111.  
  112.                 color3[0] = (float) Math.random();
  113.                 color3[1] = (float) Math.random();
  114.                 color3[2] = (float) Math.random();
  115.  
  116.                 color4[0] = (float) Math.random();
  117.                 color4[1] = (float) Math.random();
  118.                 color4[2] = (float) Math.random();
  119.  
  120.                 color5[0] = (float) Math.random();
  121.                 color5[1] = (float) Math.random();
  122.                 color5[2] = (float) Math.random();
  123.  
  124.                 color6[0] = (float) Math.random();
  125.                 color6[1] = (float) Math.random();
  126.                 color6[2] = (float) Math.random();
  127.             });
  128.             thread.start();
  129.         }
  130.     }
  131.  
  132.     @Override
  133.     public void dispose(GLAutoDrawable drawable) {
  134.         // TODO Auto-generated method stub
  135.     }
  136.  
  137.     @Override
  138.     public void init(GLAutoDrawable drawable) {
  139.  
  140.         final GL2 gl = drawable.getGL().getGL2();
  141.         gl.glShadeModel(GL2.GL_SMOOTH);
  142.         gl.glClearColor(0f, 0f, 0f, 0f);
  143.         gl.glClearDepth(1.0f);
  144.         gl.glEnable(GL2.GL_DEPTH_TEST);
  145.         gl.glDepthFunc(GL2.GL_LEQUAL);
  146.         gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
  147.     }
  148.  
  149.     @Override
  150.     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
  151.  
  152.         // TODO Auto-generated method stub
  153.         final GL2 gl = drawable.getGL().getGL2();
  154.         //if( height lt;=0 )
  155.         //height = 1;
  156.  
  157.         final float h = (float) width / (float) height;
  158.         gl.glViewport(0, 0, width, height);
  159.         gl.glMatrixMode(GL2.GL_PROJECTION);
  160.         gl.glLoadIdentity();
  161.  
  162.         glu.gluPerspective(45.0f, h, 1.0, 20.0);
  163.         gl.glMatrixMode(GL2.GL_MODELVIEW);
  164.         gl.glLoadIdentity();
  165.     }
  166.  
  167. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 3.833 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут