Cube Mapping with JOGL

To create cube mapping in JOGL, simply follow the steps in OpenGL Implementation page. For this section, the author will demonstrate the texturing of a cube in the scene. To begin with, Figure 22 illustrates the cube with no texture and Figure 23, the texture that will be applied to this cube.

Figure 22: Shows the cube without texture.

Figure 23: The texture image that will be used to texture the cube.

Step 1: Load the Image

box2Tex=loadTexture(gl, "Texture/Base.png", 1);

This will invoke the method loadTexture();

loadTexture(gl, image, mipmap) //if mipmap = 1 then mipmapping is enabled
private Texture loadTexture(GL2 gl, String filename, int n) {    
 Texture tex = null;     
// since file loading is involved, must use try...catch    
try {
File f = new File(filename);
      // The following line results in a texture that is flipped vertically (i.e. is upside down)     
  // due to OpenGL and Java (0,0) position being different:      
 // tex = TextureIO.newTexture(new File(filename), false);
      // So, instead, use the following three lines which flip the image vertically:     
BufferedImage img =; // read file into BufferedImage

Stage 2: Set Texture Parameter

if(n == 1) // mipmapping if n = 1   
tex = AWTTextureIO.newTexture(GLProfile.getDefault(), img, true);
tex = AWTTextureIO.newTexture(GLProfile.getDefault(), img, false);
         if( n == 1) // mipmapping if n = 1  
              GL2.GL_LINEAR_MIPMAP_LINEAR );
}  else // No mipmapping.
        GL2.GL_LINEAR );
     tex.setTexParameteri(gl, GL2.GL_TEXTURE_MAG_FILTER,  GL2.GL_LINEAR );
 // Wrap.  
tex.setTexParameteri(gl, GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT);
  tex.setTexParameteri(gl, GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT);
 catch(Exception e) {
       System.out.println("Error loading texture " + filename);  
 return tex;   

Step 3: Enable Texture

The setTexture() will set the mesh’s texture and at the same time enable it.

public void displayTexture(GL2 gl)  

Step 4: Draw the function by supplying the co-ordinates of the texture and object

In, we specifiy the 6 faces of the cube.

  private void createMeshHardCube(double width, double height, double depth) {
     Vertex[] vertices=new Vertex[24];
   Triangle[] triangles=new Triangle[12];
    // Need 3 different copies of each vertex   
 for (int i=0; i<3; i++) {
int offset=i*8;
 vertices[offset]=new Vertex(-width/2, -height/2, -depth/2);
      vertices[offset+1]=new Vertex(-width/2, -height/2, depth/2);
   vertices[offset+2]=new Vertex(-width/2, height/2, -depth/2);
   vertices[offset+3]=new Vertex(-width/2, height/2, depth/2);
   vertices[offset+4]=new Vertex(width/2, -height/2, -depth/2);
    vertices[offset+5]=new Vertex(width/2, -height/2, depth/2);
    vertices[offset+6]=new Vertex(width/2, height/2, -depth/2);
    vertices[offset+7]=new Vertex(width/2, height/2, depth/2);
    // Left (x -ve)
triangles[0]=new Triangle(0, 1, 3);
 triangles[1]=new Triangle(3, 2, 0);
   vertices[0].setTextureCoord(0.0f, 0.0f);
  vertices[1].setTextureCoord(1.0f, 0.0f);
  vertices[2].setTextureCoord(0.0f, 1.0f);
vertices[3].setTextureCoord(1.0f, 1.0f);

  // Right (x +ve)   
  triangles[2]=new Triangle(4, 6, 7);
   triangles[3]=new Triangle(7, 5, 4);
vertices[4].setTextureCoord(1.0f, 0.0f);
 vertices[5].setTextureCoord(0.0f, 0.0f);
 vertices[6].setTextureCoord(1.0f, 1.0f);
 vertices[7].setTextureCoord(0.0f, 1.0f);
     // Front (z +ve)    
 triangles[4]=new Triangle(9, 13, 15);
     triangles[5]=new Triangle(15, 11, 9);
  vertices[9].setTextureCoord(0.0f, 0.0f);
 vertices[11].setTextureCoord(0.0f, 1.0f);
 vertices[13].setTextureCoord(1.0f, 0.0f);
 vertices[15].setTextureCoord(1.0f, 1.0f);

     // Back (z -ve)    
 triangles[6]=new Triangle(8, 10, 14);
   triangles[7]=new Triangle(14, 12, 8);
  vertices[8].setTextureCoord(1.0f, 0.0f);
   vertices[10].setTextureCoord(1.0f, 1.0f);
    vertices[12].setTextureCoord(0.0f, 0.0f);
   vertices[14].setTextureCoord(0.0f, 1.0f);

     // Bottom (y -ve)   
 triangles[8]=new Triangle(16, 20, 21);
   triangles[9]=new Triangle(21, 17, 16);
  vertices[16].setTextureCoord(0.0f, 0.0f);
   vertices[17].setTextureCoord(0.0f, 1.0f);
  vertices[20].setTextureCoord(1.0f, 0.0f);
 vertices[21].setTextureCoord(1.0f, 1.0f);

   // Top (y +ve)  
  triangles[10]=new Triangle(23, 22, 18);
  triangles[11]=new Triangle(18, 19, 23);
  vertices[18].setTextureCoord(0.0f, 1.0f);
    vertices[19].setTextureCoord(0.0f, 0.0f);
     vertices[22].setTextureCoord(1.0f, 1.0f);
     vertices[23].setTextureCoord(1.0f, 0.0f);

    setVertices(vertices, true);
    // Calc Normals;

Then finally,



Figure 24: The result of cube mapping.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

  • Declarion of own work

    I declare that this work is my own Author Roldan Fritz Tagaro, contact me for more information at:
%d bloggers like this: