📜  JOGL-转型

📅  最后修改于: 2020-11-13 04:55:09             🧑  作者: Mango


OpenGL提供了更多功能,例如将颜色应用于对象,缩放,照明,旋转对象等。本章介绍了使用JOGL对对象进行的一些转换。

在窗口上移动对象

在前面的章节中,我们讨论了用于绘制直线和使用简单直线绘制各种形状的程序。以这种方式创建的形状可以显示在窗口内的任何位置。使用glTranslatef方法(float x,float y,float z)完成此操作

此方法属于GLMatrixFunc接口,该接口位于javax.media.opengl.fixedfunc包中。

GLMatrixFunc接口

接口-GLMatrixFunc

-javax.media.opengl.fixedfunc

下表列出了此接口的一些重要方法-

Sr.No. Methods and Description
1

void glRotatef(float angle, float x, float y, float z)

Rotates the current matrix.

2

void glScalef(float x, float y, float z)

Used to scale the current matrix.

3

void glTranslatef(float x, float y,float z)

Used to translate the current matrix.

4

void glLoadIdentity()

Loads the current matrix with identity matrix.

glTranslate()方法将坐标系的原点移动到参数(x,y,z)指定的点,并以如下形式传递给glTranslate()方法:

论据。要保存和还原未转换的坐标系,请使用glPushMatrix()glPopMatrix()方法。

gl.glTranslatef(0f, 0f, -2.5f); 

每当使用glTranslate()时,它都会更改组件在屏幕上的位置。因此,应重写GLEventListener接口的reshape()方法,并应初始化OpenGL视口和投影矩阵。

以下代码显示了初始化视口和投影矩阵的模板-

public void reshape(GLAutoDrawable drawable, int x,  int y, int width, int height) { 
  
   // TODO Auto-generated method stub 
   final GL2 gl = drawable.getGL().getGL2();  
            
   // get the OpenGL 2 graphics object   
   if(height <= 0) height = 1; 
       
   //preventing devided by 0 exception height = 1; 
   final float h = (float) width / (float) height; 
            
   // display area to cover the entire window 
   gl.glViewport(0, 0, width, height); 
            
   //transforming projection matrix 
   gl.glMatrixMode(GL2.GL_PROJECTION); 
   gl.glLoadIdentity(); 
   glu.gluPerspective(45.0f, h, 1.0, 20.0); 
      
   //transforming model view gl.glLoadIdentity(); 
   gl.glMatrixMode(GL2.GL_MODELVIEW); 
   gl.glLoadIdentity(); 
}