
In questo primo esempio tridimensionale vengono utilizzati i solidi predefiniti della libreria GLUT. Viene impostata una sorgente di luce e le caratteristiche del materiale degli oggetti. Per capire il funzionamento dei parametri per la luce e i materiali o l’effetto delle trasformazioni geometriche è utile provare a modificare i valori numerici. Se si avvia l’animazione i solidi ruotano tutti intorno al proprio asse x.
Per i dettagli fare riferimento a: Computer Graphics with OpenGL
- CG 1. Introduction to OpenGL
- CG 2. Computer Graphics – Basic Theory
- CG 3. OpenGL Examples
- References & Resources
GLRenderer.java
public class GLRenderer implements GLEventListener {
float a = 0.0f;
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
float[] ambiente = new float[]{1, 1, 1, 1};
float[] diffusa = new float[]{1, 1, 1, 1};
float[] speculare = new float[]{1, 1, 1, 1};
float[] posizione = new float[]{0, 10, 10, 1};
// imposta le caratteristiche della sorgente di luce 0
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, ambiente, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, diffusa, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, speculare, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, posizione, 0);
// attiva la sorgente di luce 0
gl.glEnable(GL2.GL_LIGHT0);
// attiva l'iluminazione
gl.glEnable(GL2.GL_LIGHTING);
// attiva il test di profondità per disegnare solo parti visibili
gl.glEnable(GL2.GL_DEPTH_TEST);
// imposta il colore di sfondo
gl.glClearColor(0, 0, 0.1f, 0);
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
GLUT glut = new GLUT();
// cancella l'immagine e il buffer per il test di profondità
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
// cono
gl.glLoadIdentity();
gl.glTranslatef(-6, 4, 0);
gl.glRotatef(a, 1, 0, 0);
glut.glutSolidCone(1, 2, 36, 10);
//cubo
gl.glLoadIdentity();
gl.glTranslatef(-2, 4, 0);
gl.glRotatef(a, 1, 0, 0);
glut.glutSolidCube(2);
// cilindro
gl.glLoadIdentity();
gl.glTranslatef(2, 4, 0);
gl.glRotatef(a, 1, 0, 0);
glut.glutSolidCylinder(1, 2, 36, 10);
// dodecaedro
gl.glLoadIdentity();
gl.glTranslatef(6, 4, 0);
gl.glRotatef(a, 1, 0, 0);
glut.glutSolidDodecahedron();
// icosaedro
gl.glLoadIdentity();
gl.glTranslatef(-6, 0, 0);
gl.glRotatef(a, 1, 0, 0);
glut.glutSolidIcosahedron();
// ottaedro
gl.glLoadIdentity();
gl.glTranslatef(-2, 0, 0);
gl.glRotatef(a, 1, 0, 0);
glut.glutSolidOctahedron();
// rombidodecaedro
gl.glLoadIdentity();
gl.glTranslatef(2, 0, 0);
gl.glRotatef(a, 1, 0, 0);
glut.glutSolidRhombicDodecahedron();
// sfera
gl.glLoadIdentity();
gl.glTranslatef(6, 0, 0);
gl.glRotatef(a, 1, 0, 0);
glut.glutSolidSphere(1, 36, 18);
// teiera vista dall'alto
gl.glLoadIdentity();
gl.glTranslatef(-6, -4, 0);
gl.glRotatef(a, 1, 0, 0);
glut.glutSolidTeapot(1, false);
// teiera vista di fianco
gl.glLoadIdentity();
gl.glTranslatef(-2, -4, 0);
gl.glRotatef(a, 1, 0, 0);
glut.glutSolidTeapot(1, true);
// tetraedro
gl.glLoadIdentity();
gl.glTranslatef(2, -4, 0);
gl.glRotatef(a, 1, 0, 0);
glut.glutSolidTetrahedron();
// toro
gl.glLoadIdentity();
gl.glTranslatef(6, -4, 0);
gl.glRotatef(a, 1, 0, 0);
glut.glutSolidTorus(0.5, 1, 36, 36);
// incrementa l'angolo di rotazione di 1°
a += 1.0f;
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
GL2 gl = drawable.getGL().getGL2();
GLU glu = new GLU();
// imposta la matrice di trasformazione per la proiezione
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45, (float) w / h, 5, 50);
glu.gluLookAt(0, 0, 15, 0, 0, 0, 0, 1, 0);
// imposta la matrice di trasformazione per gli oggetti
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
}
}
Progetto Eclipse: EclipseJOGL.zip
N.B.: È probabile che sia necessario eliminare la libreria JOGL2 presente nel progetto e sostituirla con quella del proprio Workspace.