Advertisement
DimasDark

Teste JOGL

Aug 15th, 2014
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. package jbdb;
  2.  
  3. import java.awt.Frame;
  4. import java.awt.event.WindowAdapter;
  5. import java.awt.event.WindowEvent;
  6.  
  7. import javax.media.opengl.GL;
  8. import javax.media.opengl.GL2;
  9. import javax.media.opengl.GLAutoDrawable;
  10. import javax.media.opengl.GLCapabilities;
  11. import javax.media.opengl.GLEventListener;
  12. import javax.media.opengl.GLProfile;
  13. import javax.media.opengl.awt.GLCanvas;
  14. import javax.media.opengl.glu.GLU;
  15. import javax.swing.JFrame;
  16.  
  17.  
  18. //Clase que vai criar um triângulo
  19. class Triangle {
  20.     protected static void setup( GL2 gl2, int width, int height ) {
  21.         gl2.glMatrixMode( GL2.GL_PROJECTION );
  22.         gl2.glLoadIdentity();
  23.  
  24.         // as coordenadas começam no canto inferior esquerdo,e largura = tam janela
  25.         GLU glu = new GLU();
  26.         glu.gluOrtho2D( 0.0f, width, 0.0f, height );
  27.  
  28.         gl2.glMatrixMode( GL2.GL_MODELVIEW );
  29.         gl2.glLoadIdentity();
  30.  
  31.         gl2.glViewport( 0, 0, width, height );
  32.     }
  33.  
  34.     protected static void render( GL2 gl2, int width, int height ) {
  35.         gl2.glClear( GL.GL_COLOR_BUFFER_BIT );
  36.  
  37.         // desenha o triângulo
  38.         gl2.glLoadIdentity();
  39.         gl2.glBegin( GL.GL_TRIANGLES ); //pra linhas tem que usar lines
  40.         gl2.glColor3f( 1, 0, 0 );
  41.         gl2.glVertex2f( 0, 0 );
  42.         gl2.glColor3f( 0, 1, 0 );
  43.         gl2.glVertex2f( width, 0 );
  44.         gl2.glColor3f( 0, 0, 1 );
  45.         gl2.glVertex2f( width / 2, height );
  46.         gl2.glEnd();
  47.     }
  48. }
  49.  
  50. public class teste {
  51.    
  52.  
  53.  
  54.         public static void main(String[] args ) {
  55.             GLProfile glprofile = GLProfile.getDefault();
  56.             GLCapabilities glcapabilities = new GLCapabilities( glprofile );
  57.             final GLCanvas glcanvas = new GLCanvas( glcapabilities );
  58.  
  59.             glcanvas.addGLEventListener( new GLEventListener() {
  60.                
  61.                
  62.                 //essas são as funções que vc tem que dar override
  63.                 //é tipo aquelas com c++ que a gente usa que chama outras funções
  64.                 @Override
  65.                 public void reshape( GLAutoDrawable glautodrawable, int x, int y, int width, int height ) {
  66.                     Triangle.setup( glautodrawable.getGL().getGL2(), width, height );
  67.                 }
  68.                
  69.                 @Override
  70.                 public void init( GLAutoDrawable glautodrawable ) {
  71.                 }
  72.                
  73.                 @Override
  74.                 public void dispose( GLAutoDrawable glautodrawable ) {
  75.                 }
  76.                
  77.                 @Override
  78.                 public void display( GLAutoDrawable glautodrawable ) {
  79.                     Triangle.render( glautodrawable.getGL().getGL2(), glautodrawable.getWidth(), glautodrawable.getHeight() );
  80.                 }
  81.             });
  82.             //Cria uma janelinha
  83.             final JFrame frame = new JFrame( "Desenhar triângulo" );
  84.             frame.add( glcanvas );
  85.             //Quando fechar a janelinha destroi o desenho pra não ficar na memoria
  86.             frame.addWindowListener( new WindowAdapter() {
  87.                 public void windowClosing(WindowEvent windowevent ) {
  88.                     frame.remove( glcanvas );
  89.                     frame.dispose();
  90.                     System.exit( 0 );
  91.                 }
  92.             });
  93.  
  94.             frame.setSize( 640, 480 );
  95.             frame.setVisible( true );
  96.         }
  97.  
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement