📜  JOGL旋转

📅  最后修改于: 2021-01-05 00:41:11             🧑  作者: Mango

JOGL旋转

在JOGL中,对象可以沿三个轴中的任何一个沿任何方向旋转。要旋转对象,您需要执行以下步骤:-

  • 删除对象的先前状态以清除视图。为此,您需要使用以下方法清除颜色和深度缓冲区:-
gl.glClear (GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT )
  • 现在,借助glLoadIntensity()方法重置项目矩阵
  • 调用GLMatrixFunc接口的glRotatef()方法。

每秒帧动画师类

FPSAnimator类是Animator类的子类,用于以每秒帧数的速度旋转对象。

FPSAnimator类的构造方法

Constructor Description
FPSAnimator(int fps) It creates an FPSAnimator with specified value of target frame-per-second.
FPSAnimator(GLAutoDrawabledrawable, int fps) It creates an FPSAnimator with an intialdrawable to animate and specified value of target frame-per-second.
FPSAnimator(GLAutoDrawabledrawable, int fps, booleanscheduleAtFixedRate) It creates an FPSAnimator with an intialdrawable to animate, specified value of target frame-per-second and a flag that specifies whether to use fixed-rate scheduling.
FPSAnimator(int fps, booleanscheduleAtFixedRate) It creates an FPSAnimator with specified value of target frame-per-second a flag that specifies whether to use fixed-rate scheduling.

JOGL旋转示例

在此示例中,我们将旋转一个三角形。

package com.javatpoint.jogl;
import javax.media.opengl.GL2; 
    

import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas; 

import javax.swing.JFrame; 

import com.jogamp.opengl.util.FPSAnimator; 

public class JRotation implements GLEventListener { 
public float rotation; 

   @Override 
public void display( GLAutoDrawable drawable ) {

final GL2 gl = drawable.getGL().getGL2(); 
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( rotation, 0.0f, 1.0f, 0.0f );  


gl.glBegin(GL2.GL_TRIANGLES);     
      //Green Color
gl.glColor3f( 0.0f,1.0f,0.0f ); 
gl.glVertex2d(0,0.5);
gl.glVertex2d(-0.5,-0.5);
gl.glVertex2d(0.5,-0.5);

gl.glEnd();

gl.glFlush(); 
      //Assign the angle
rotation += 0.6f;  
   } 
    
   @Override 
public void dispose( GLAutoDrawable arg0 ) { 
      //method body 
   } 

   @Override 
public void init( GLAutoDrawable arg0 ) { 
      // method body 
   }

   @Override 
public void reshape( GLAutoDrawable drawable, int x, int y, int width, int height ) { 
   }
public static void main( String[] args ) {
        
final GLProfile gp  = GLProfile.get(GLProfile.GL2 ); 
GLCapabilities cap  = new GLCapabilities(gp);

final GLCanvas gc = new GLCanvas(cap); 
JRotation jr = new JRotation(); 
gc.addGLEventListener(jr); 
gc.setSize( 400, 400 );  

final JFrame frame = new JFrame("JOGL Rotation");

frame.add(gc);
    frame.setSize(500,400);
    frame.setVisible(true); 

final FPSAnimator animator = new FPSAnimator(gc, 400,true); 
animator.start(); 

   } 
    
} 

输出: