715122, 1105 JOGL - Rotacisn
JOGL - Rotacién
En este capitulo le explicamos cémo rotar un objeto usando JOGL. La rotacién de objetos se
puede realizar a lo largo de cualquiera de los tres ejes, usando el método glRotatef(float angle,
float x, float y, float z) de la interfaz GLMatrixFunc . Debe pasar un angulo de rotacién y los ejes
x, ¥, Z como parémetros para este método.
Los siguientes pasos lo guian para rotar un objeto con éxito:
* — Borre el biifer de color y el btifer de profundidad inicialmente usando el método
gl.giClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH BUFFER BIT) . Este
metodo borra el estado anterior del objeto y aclara la vista.
+ Restablezca la matriz de proyeccién utilizando el método glLoadidentity() .
Cree una instancia de la clase de animador e inicie el animador usando el método start()
Clase FPSAnimator
Acontinuacién se muestran los diversos constructores de la clase FPSAnimator.
No Métodos y descripciones
Sefior.
dl FPSAnimator (GLAutoDrawable dibujable, fps int)
Crea un FPSAnimator con un valor objetivo de fotogramas por segundo y un dibujo
inicial para animar.
7 FPSAnimator (GLAutoDrawable dibujable, int fps, booleano cheduleAtFixedRate)
Crea un FPSAnimator con un valor de fotogramas por segundo de destino
determinado, un dibujo inicial para animar y una marca que indica si se debe usar la
programacién de tasa fja.
3 Animador FPS(int fps)
Crea un FPSAnimator con un valor objetivo de fotogramas por segundo.
4 Crea un FPSAnimator con un valor objetivo de cuadros por segundo y una bandera que
indica si se debe usar la programacién de tasa fija
htps:wwttarilspoint comjogljogl_ rotation him
M6715122, 1105
Crea un
se debe
JOGL -Rotacisn
FPSAnimator con un valor objetivo de cuadros por segundo y una bandera que indica si
usar la programacién de tasa fija.
start() y stop() son los dos métodos importantes en esta clase. El siguiente programa muestra
cémo rotar un triangulo usando la clase FPSAnimator:
import
import
import
import
import
import
import
import
public
pri
@ov
pub:
hitpshwwtutorialspaint comjogliog
javax.media.opengl.GL2;
javax.media. opengl .GLautoDrawable;
javax.media.opengl.GLCapabilities;
javax.media.opengl.GLEventListener;
javax.media.opengl.GLProfile;
javax.media. opengl. awt .GLCanvas;
javax. swing. JFrame;
com. jogamp.openg].util.FPSAnimator;
class TriangleRotation implements GLeventListener {
vate float rtri; //for angle of rotation
erride
lic void display( GLautoDrawable drawable ) {
final GL2 gl = drawable. getGL().getL2();
gl.glClear (GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER BIT );
// Clear The Screen And The Depth Buffer
gl.glloadidentity(); // Reset The View
//triangle rotation
gl.glRotatef( rtri, @.0f, 1.0f, 0.0F );
// Drawing Using Triangles
gl.glBegin( GL2.GL_TRIANGLES );
gl.glColor3f( 1.0f, @.0f, @.0F ); // Red
gl.glvertex3f( @.5f,0.7f,0.0F ); // Top
gl.glColor3#( 0.0f,1.0f,0.0f ); // blue
gl.glvertex3f( -0.2F,-0.50f,0.0f ); // Bottom Left
gl.glColor3f( 0.0f,0.0f,1.0f ); // green
gl.glvertex3f( @.5f,-0.5f,0.0F ); // Bottom Right
gl.glend();
gl.glFlush();
avon nim 216‘715122, 1105 JOGL - Retacién
rua t= e275 syussiymung une ungee
}
@0verride
public void dispose( GLAutoDrawable argd ) {
//method body
@0verride
public void init( GLautoDrawable argo ) (
// method body
@override
public void reshape( GLAutoDrawable drawable, int x, int y, int width, int height |
public static void main( string] args ) ¢
//getting the capabilities object of GL2 profile
final GLProfile profile = GLProfile.get(GLProfile.Gl2 );
GLCapabilities capabilities = new GLCapabilities( profile );
// The canvas
final GLCanvas glcanvas = new GLCanvas( capabilities);
TriangleRotation triangle = new TriangleRotation();
glcanvas.addGLEventListener( triangle );
glcanvas.setSize( 400, 40@ );
// creating frame
final JFrame frame = new JFrame ("Rotating Triangle");
// adding canvas to it
frame.getContentPane().add( glcanvas );
frame.setSize(frame.getContentPane() .getPreferredSize());
frame.setVisible( true );
//Instantiating and Initiating Animator
final FPSAnimator animator = new FPSAnimator(glcanvas, 300,true);
animator. start();
} //end of main
} /end of class
hps:wwtutailspoint comjogljogl_ rotation him 316‘71822, 1405 JOGL -Rotacisn
Si compila y ejecuta el programa anterior, genera el siguiente resultado. Aqui, puede observar
varias instantaneas de un triangulo de color girando alrededor del eje x.
htps:wwttarilspoint comjogljogl_ rotation him
46