refactored jogl
This commit is contained in:
parent
0c0d5ffc53
commit
785d0ac429
@ -1,98 +0,0 @@
|
|||||||
package lsystem.screen;
|
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.*;
|
|
||||||
|
|
||||||
import com.jogamp.opengl.GL;
|
|
||||||
import com.jogamp.opengl.GL2;
|
|
||||||
import com.jogamp.opengl.GLAutoDrawable;
|
|
||||||
import com.jogamp.opengl.GLEventListener;
|
|
||||||
import com.jogamp.opengl.awt.GLCanvas;
|
|
||||||
import com.jogamp.opengl.util.Animator;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Article1.java author: InfoRital
|
|
||||||
*
|
|
||||||
* Code source du premier article. Article1 implémente {@link GLEventListener}
|
|
||||||
* pour obtenir le mécanisme de callback
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class Jogl implements GLEventListener {
|
|
||||||
|
|
||||||
public static void initialize(String[] args) {
|
|
||||||
// Création de la fenêtre
|
|
||||||
Frame frame = new Frame("L-système 3D");
|
|
||||||
|
|
||||||
// Création du canvas pour dessiner dessus
|
|
||||||
GLCanvas canvas = new GLCanvas();
|
|
||||||
|
|
||||||
// Nous attachons ensuite le méchanisme de callback à notre surface dessinable
|
|
||||||
canvas.addGLEventListener((GLEventListener) new Jogl());
|
|
||||||
|
|
||||||
// dessin -> fenêtre
|
|
||||||
frame.add(canvas);
|
|
||||||
|
|
||||||
// Création de l'animator
|
|
||||||
final Animator animator = new Animator(canvas);
|
|
||||||
|
|
||||||
// croix rouge = fermeture de la fenêtre
|
|
||||||
frame.addWindowListener(new WindowAdapter() {
|
|
||||||
public void windowClosing(WindowEvent e) {
|
|
||||||
// Thread pour arrêter l'animator avant la fenêtre
|
|
||||||
new Thread(animator::stop).start();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Taille de la fenêtre
|
|
||||||
frame.setSize(300, 300);
|
|
||||||
frame.setVisible(true);
|
|
||||||
|
|
||||||
// Démarrage de l'animator qui va se charger de faire des appels à la méthode
|
|
||||||
animator.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
// init() sera appelée une fois au début de l'animation. C'est dans cette méthode
|
|
||||||
// que nous nous chargerons de toutes les opérations d'initialisation
|
|
||||||
public void init(GLAutoDrawable drawable) {
|
|
||||||
// GLEventListener renvoie un contexte (drawable)
|
|
||||||
// que nous allons utiliser pour instancier un objet de type GL
|
|
||||||
// qui nous permettra d'utiliser les fonctions OpenGL
|
|
||||||
GL gl = drawable.getGL();
|
|
||||||
// désactiver la synchronisation verticale indépendamment de la plateforme utilisée
|
|
||||||
gl.setSwapInterval(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Appelée que si la fenêtre d'affichage est redimensionnée
|
|
||||||
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
|
|
||||||
GL gl = drawable.getGL();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// display() sera appelée en boucle tout au long de l'application par la classe Animator.
|
|
||||||
// C'est dans cette fonction qu'on fera tout ce qui doit être affiché
|
|
||||||
public void display(GLAutoDrawable drawable) {
|
|
||||||
|
|
||||||
GL gl = drawable.getGL();
|
|
||||||
((GL2) gl).glBegin( GL2.GL_LINES );
|
|
||||||
((GL2) gl).glVertex3f( -0.75f,0f,3f );// 3 units into the window
|
|
||||||
((GL2) gl).glVertex3f( 0f,-0.75f,3f );
|
|
||||||
((GL2) gl).glEnd();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* displayChanged() est appelée si le mode d'affichage par exemple est modifié.
|
|
||||||
* Cependant nous n'implémenterons pas cette méthode.
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
* public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
|
|
||||||
* boolean deviceChanged) { }
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void dispose(GLAutoDrawable arg0) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
89
src/lsystem/screen/JoglEventListener.java
Normal file
89
src/lsystem/screen/JoglEventListener.java
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
package lsystem.screen;
|
||||||
|
|
||||||
|
import com.jogamp.opengl.GL;
|
||||||
|
import com.jogamp.opengl.GL2;
|
||||||
|
import com.jogamp.opengl.GLAutoDrawable;
|
||||||
|
import com.jogamp.opengl.GLEventListener;
|
||||||
|
import com.jogamp.opengl.glu.GLU;
|
||||||
|
import com.jogamp.opengl.util.gl2.GLUT;
|
||||||
|
|
||||||
|
public class JoglEventListener implements GLEventListener {
|
||||||
|
|
||||||
|
private GLU glu = new GLU();
|
||||||
|
private GLUT glut = new GLUT();
|
||||||
|
|
||||||
|
private float camera [] = {0f, 0f, 5f};
|
||||||
|
|
||||||
|
private float [] light_0_ambient = {0.01f, 0.01f, 0.01f, 0.01f};
|
||||||
|
private float [] light_0_diffuse = {1.0f, 1.0f, 1.0f, 1.0f};
|
||||||
|
private float [] light_0_specular = {1.0f,1.0f, 1.0f, 1.0f};
|
||||||
|
private float [] light_0_position = {100f, 0f, 10f, 1f};
|
||||||
|
|
||||||
|
private float [] material_specular = {1.0f, 1.0f, 1.0f, 1.0f};
|
||||||
|
|
||||||
|
private float angle = 0f;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(GLAutoDrawable glAutoDrawable) {
|
||||||
|
GL2 gl = glAutoDrawable.getGL().getGL2();
|
||||||
|
|
||||||
|
gl.glClearColor(0.8f, 0.5f, 0.0f, 1.0f);
|
||||||
|
|
||||||
|
gl.glEnable(GL2.GL_DEPTH_TEST);
|
||||||
|
gl.glClearDepth(1.0f);
|
||||||
|
|
||||||
|
gl.glShadeModel(GL2.GL_SMOOTH);
|
||||||
|
gl.glEnable(GL2.GL_LIGHTING);
|
||||||
|
|
||||||
|
gl.glEnable(GL2.GL_LIGHT0);
|
||||||
|
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, light_0_ambient, 0);
|
||||||
|
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_0_diffuse, 0);
|
||||||
|
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_0_specular, 0);
|
||||||
|
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_0_position, 0);
|
||||||
|
|
||||||
|
gl.glEnable(GL2.GL_COLOR_MATERIAL);
|
||||||
|
gl.glColorMaterial(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE);
|
||||||
|
gl.glMateriali(GL2.GL_FRONT_AND_BACK, GL2.GL_SHININESS, 90);
|
||||||
|
gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, material_specular, 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose(GLAutoDrawable glAutoDrawable) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void display(GLAutoDrawable glAutoDrawable) {
|
||||||
|
GL2 gl = glAutoDrawable.getGL().getGL2();
|
||||||
|
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
|
||||||
|
glu.gluLookAt(
|
||||||
|
camera[0], camera[1], camera[2],
|
||||||
|
0.0f, 0.0f, 0.0f,
|
||||||
|
0.0f, 1.0f, 0.0f
|
||||||
|
);
|
||||||
|
|
||||||
|
gl.glRotatef(angle, 0f, 1f, 0f);
|
||||||
|
|
||||||
|
gl.glColor3f(1.0f, 0.0f, 0.0f);
|
||||||
|
glut.glutSolidSphere(1.0f, 20, 20);
|
||||||
|
|
||||||
|
angle += 0.1f;
|
||||||
|
angle %= 360f;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int width, int height) {
|
||||||
|
GL2 gl = glAutoDrawable.getGL().getGL2();
|
||||||
|
gl.glViewport(x, y, width, height);
|
||||||
|
|
||||||
|
gl.glMatrixMode(GL2.GL_PROJECTION);
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
glu.gluPerspective(60.0f, (float) width/height, 0.1f, 10.0f);
|
||||||
|
|
||||||
|
gl.glMatrixMode(GL2.GL_MODELVIEW);
|
||||||
|
}
|
||||||
|
}
|
34
src/lsystem/screen/SwingGLCanvas.java
Normal file
34
src/lsystem/screen/SwingGLCanvas.java
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package lsystem.screen;
|
||||||
|
|
||||||
|
import com.jogamp.opengl.GLCapabilities;
|
||||||
|
import com.jogamp.opengl.GLProfile;
|
||||||
|
import com.jogamp.opengl.awt.GLCanvas;
|
||||||
|
import com.jogamp.opengl.util.FPSAnimator;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.WindowAdapter;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
|
||||||
|
public class SwingGLCanvas {
|
||||||
|
|
||||||
|
public SwingGLCanvas() {
|
||||||
|
GLProfile glProfile = GLProfile.getDefault();
|
||||||
|
GLCapabilities glCapabilities = new GLCapabilities(glProfile);
|
||||||
|
final GLCanvas glCanvas = new GLCanvas(glCapabilities);
|
||||||
|
glCanvas.addGLEventListener(new JoglEventListener());
|
||||||
|
final JFrame jframe = new JFrame("L-System");
|
||||||
|
final FPSAnimator animator = new FPSAnimator(glCanvas, 60);
|
||||||
|
jframe.addWindowListener(new WindowAdapter() {
|
||||||
|
@Override
|
||||||
|
public void windowClosing(WindowEvent e) {
|
||||||
|
jframe.dispose();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
jframe.getContentPane().add(glCanvas, BorderLayout.CENTER);
|
||||||
|
jframe.setSize(Constants.WIDTH, Constants.HEIGHT);
|
||||||
|
animator.start();
|
||||||
|
jframe.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user