Смена цвета сторон куба по таймеру и источник света [OpenGL] - Java
Формулировка задачи:
Здравствуйте, я новичок изучаю OpenGL и не получается реализовать две функции: смена цвета сторон куба по событию и добавления источника света. Пожалуйста помогите советом или примером! Заранее спасибо!
Вот код куба
import com.jogamp.opengl.*; import com.jogamp.opengl.awt.GLCanvas; import com.jogamp.opengl.glu.GLU; import com.jogamp.opengl.util.FPSAnimator; import java.awt.*; import javax.swing.*; import static com.sun.tools.doclint.Entity.lt; public class Cube implements GLEventListener { public static DisplayMode dm, dm_old; private GLU glu = new GLU(); private float rquad = 0.0f; @Override public void display( GLAutoDrawable drawable ) { final GL2 gl = drawable.getGL().getGL2(); gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT ); gl.glLoadIdentity(); gl.glTranslatef( 0f, 0f, -5.0f ); // Rotate The Cube On X, Y & Z gl.glRotatef(rquad, 1.0f, 1.0f, 1.0f); //giving different colors to different sides gl.glBegin(GL2.GL_QUADS); // Start Drawing The Cube gl.glColor3f(1f,0f,0f); //red color gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top) gl.glVertex3f( -1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top) gl.glVertex3f( -1.0f, 1.0f, 1.0f ); // Bottom Left Of The Quad (Top) gl.glVertex3f( 1.0f, 1.0f, 1.0f ); // Bottom Right Of The Quad (Top) gl.glColor3f( 0f,1f,0f ); //green color gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Top Right Of The Quad gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Top Left Of The Quad gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad gl.glColor3f( 0f,0f,1f ); //blue color gl.glVertex3f( 1.0f, 1.0f, 1.0f ); // Top Right Of The Quad (Front) gl.glVertex3f( -1.0f, 1.0f, 1.0f ); // Top Left Of The Quad (Front) gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Bottom Left Of The Quad gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Bottom Right Of The Quad gl.glColor3f( 1f,1f,0f ); //yellow (red + green) gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad gl.glVertex3f( -1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Back) gl.glVertex3f( 1.0f, 1.0f, -1.0f ); // Top Left Of The Quad (Back) gl.glColor3f( 1f,0f,1f ); //purple (red + green) gl.glVertex3f( -1.0f, 1.0f, 1.0f ); // Top Right Of The Quad (Left) gl.glVertex3f( -1.0f, 1.0f, -1.0f ); // Top Left Of The Quad (Left) gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Bottom Right Of The Quad gl.glColor3f( 0f,1f, 1f ); //sky blue (blue +green) gl.glVertex3f( 1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Right) gl.glVertex3f( 1.0f, 1.0f, 1.0f ); // Top Left Of The Quad gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Bottom Left Of The Quad gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad gl.glEnd(); // Done Drawing The Quad gl.glFlush(); rquad -= 0.15f; } @Override public void dispose( GLAutoDrawable drawable ) { // TODO Auto-generated method stub } @Override public void init( GLAutoDrawable drawable ) { final GL2 gl = drawable.getGL().getGL2(); gl.glShadeModel( GL2.GL_SMOOTH ); gl.glClearColor( 0f, 0f, 0f, 0f ); gl.glClearDepth( 1.0f ); gl.glEnable( GL2.GL_DEPTH_TEST ); gl.glDepthFunc( GL2.GL_LEQUAL ); gl.glHint( GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST ); } @Override public void reshape( GLAutoDrawable drawable, int x, int y, int width, int height ) { // TODO Auto-generated method stub final GL2 gl = drawable.getGL().getGL2(); //if( height lt;=0 ) //height = 1; final float h = ( float ) width / ( float ) height; gl.glViewport( 0, 0, width, height ); gl.glMatrixMode( GL2.GL_PROJECTION ); gl.glLoadIdentity(); glu.gluPerspective( 45.0f, h, 1.0, 20.0 ); gl.glMatrixMode( GL2.GL_MODELVIEW ); gl.glLoadIdentity(); } public static void main( String[] args ) { final GLProfile profile = GLProfile.get( GLProfile.GL2 ); GLCapabilities capabilities = new GLCapabilities( profile ); // The canvas final GLCanvas glcanvas = new GLCanvas( capabilities ); Cube cube = new Cube(); glcanvas.addGLEventListener( cube ); glcanvas.setSize( 400, 400 ); final JFrame frame = new JFrame ( " Multicolored cube" ); frame.getContentPane().add( glcanvas ); frame.setSize( frame.getContentPane().getPreferredSize() ); frame.setVisible( true ); final FPSAnimator animator = new FPSAnimator(glcanvas, 300,true); animator.start(); } }
С источником света я смог разобраться, теперь все получается. Остается только понять, как заставить по таймеру менять цвет сторон куба
gl.glEnable(GL2.GL_LIGHTING); gl.glEnable(GL2.GL_LIGHT0);//активируем нулевой свет gl.glEnable(GL2.GL_COLOR_MATERIAL);//включаем освещение материала (???)
Решение задачи: «Смена цвета сторон куба по таймеру и источник света [OpenGL]»
textual
Листинг программы
import com.jogamp.opengl.*; import com.jogamp.opengl.awt.GLCanvas; import com.jogamp.opengl.glu.GLU; import com.jogamp.opengl.util.FPSAnimator; import javax.swing.*; import java.awt.*; public class Cube implements GLEventListener { public static DisplayMode dm, dm_old; private GLU glu = new GLU(); private float rquad = 0.0f; private float[] color1 = new float[]{1f, 0f, 0f}; //red color private float[] color2 = new float[]{0f, 1f, 0f}; //green color private float[] color3 = new float[]{0f, 0f, 1f}; //blue color private float[] color4 = new float[]{1f, 1f, 0f}; //yellow (red + green) private float[] color5 = new float[]{1f, 0f, 1f}; //purple (red + green) private float[] color6 = new float[]{0f, 1f, 1f}; //sky blue (blue +green) private Thread thread = new Thread(); public static void main(String[] args) { final GLProfile profile = GLProfile.get(GLProfile.GL2); GLCapabilities capabilities = new GLCapabilities(profile); // The canvas final GLCanvas glcanvas = new GLCanvas(capabilities); Cube cube = new Cube(); glcanvas.addGLEventListener(cube); glcanvas.setSize(400, 400); final JFrame frame = new JFrame(" Multicolored cube"); frame.getContentPane().add(glcanvas); frame.setSize(frame.getContentPane().getPreferredSize()); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setVisible(true); final FPSAnimator animator = new FPSAnimator(glcanvas, 300, true); animator.start(); } @Override public void display(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2(); gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glTranslatef(0f, 0f, -5.0f); // Rotate The Cube On X, Y & Z gl.glRotatef(rquad, 1.0f, 1.0f, 1.0f); //giving different colors to different sides gl.glBegin(GL2.GL_QUADS); // Start Drawing The Cube gl.glColor3fv(color1, 0); gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top) gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top) gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top) gl.glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top) gl.glColor3fv(color2, 0); gl.glVertex3f(1.0f, -1.0f, 1.0f); // Top Right Of The Quad gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Top Left Of The Quad gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad gl.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad gl.glColor3fv(color3, 0); gl.glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front) gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front) gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad gl.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad gl.glColor3fv(color4, 0); gl.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Back) gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Back) gl.glColor3fv(color5, 0); gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left) gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Left) gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad gl.glColor3fv(color6, 0); gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Right) gl.glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Quad gl.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad gl.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad gl.glEnd(); // Done Drawing The Quad gl.glFlush(); rquad -= 0.15f; if (!thread.isAlive()) { thread = new Thread(() -> { try {Thread.sleep(2000);} catch (InterruptedException e) {} color1[0] = (float) Math.random(); color1[1] = (float) Math.random(); color1[2] = (float) Math.random(); color2[0] = (float) Math.random(); color2[1] = (float) Math.random(); color2[2] = (float) Math.random(); color3[0] = (float) Math.random(); color3[1] = (float) Math.random(); color3[2] = (float) Math.random(); color4[0] = (float) Math.random(); color4[1] = (float) Math.random(); color4[2] = (float) Math.random(); color5[0] = (float) Math.random(); color5[1] = (float) Math.random(); color5[2] = (float) Math.random(); color6[0] = (float) Math.random(); color6[1] = (float) Math.random(); color6[2] = (float) Math.random(); }); thread.start(); } } @Override public void dispose(GLAutoDrawable drawable) { // TODO Auto-generated method stub } @Override public void init(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2(); gl.glShadeModel(GL2.GL_SMOOTH); gl.glClearColor(0f, 0f, 0f, 0f); gl.glClearDepth(1.0f); gl.glEnable(GL2.GL_DEPTH_TEST); gl.glDepthFunc(GL2.GL_LEQUAL); gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST); } @Override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { // TODO Auto-generated method stub final GL2 gl = drawable.getGL().getGL2(); //if( height lt;=0 ) //height = 1; final float h = (float) width / (float) height; gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(45.0f, h, 1.0, 20.0); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity(); } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д