English 中文(简体)
JOGL - Transformation
  • 时间:2024-11-05

JOGL - Transformation


Previous Page Next Page  

OpenGL provides more features such as applying colors to an object, scapng, pghting, rotating an object, etc. This chapter describes some of the transformations on objects using JOGL.

Moving an Object on the Window

In earper chapters, we discussed the programs for drawing a pne and drawing various shapes using simple pnes. The shapes created in this way can be displayed on any location within the window. It is done using the method glTranslatef (float x, float y, float z).

This method belongs to the GLMatrixFunc interface, which is in the javax.media.opengl.fixedfunc package.

GLMatrixFunc Interface

interface − GLMatrixFunc

package − javax.media.opengl.fixedfunc

The following table psts some important methods of this interface −

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.

The glTranslate() method moves the origin of the coordinate system to the point specified by the parameters (x,y,z), passed to the glTranslate() method as

argument. To save and restore the untranslated coordinate system, glPushMatrix() and glPopMatrix() methods are used.

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

Whenever glTranslate() is used, it changes the position of the component on the screen. Hence, the reshape() method of GLEventListener interface should be overridden and OpenGL viewport and projection matrix should be initiapzed.

The following code shows the template to initiapze a view port and projection matrix −

pubpc 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(); 
}
Advertisements