Textured Cube
TextureReader
This is a simplified version. The complete class as used by NeHe [1] may be found at koders [2] , search with TextureReader
oneTexturedBox
The textures are stored in a array, and are loaded during onconstruction:
// keep an image for each side of the cube TextureReader.Texture[] textures=new TextureReader.Texture[6]; public oneTexturedBox() { // load images as textures textures=new TextureReader.Texture[6]; for(int ix=0;ix<6;ix++) { try { String filename="images/bs-" + (ix + 1) + ".png"; textures[ix] = TextureReader.readTexture(filename); } catch (IOException ex) { System.out.println(ex.getMessage()); } } }
The drawing of the first side:
gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_S,GL.GL_CLAMP); gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_T,GL.GL_CLAMP); gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_NEAREST); gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_NEAREST); gl.glTexEnvi(GL.GL_TEXTURE_ENV,GL.GL_TEXTURE_ENV_MODE,GL.GL_MODULATE); gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT,GL.GL_NICEST); gl.glDisable(GL.GL_TEXTURE_2D); gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, textures[0].getWidth(),textures[0].getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, textures[0].getPixels()); gl.glEnable(GL.GL_TEXTURE_2D); gl.glBegin(GL.GL_POLYGON);/* f1: front */ gl.glNormal3f(-1.0f,0.0f,0.0f); gl.glTexCoord2f(0.0f, 0.0f);gl.glVertex3f(0.0f,0.0f,0.0f); gl.glTexCoord2f(1.0f, 0.0f);gl.glVertex3f(0.0f,0.0f,1.0f); gl.glTexCoord2f(1.0f, 1.0f);gl.glVertex3f(1.0f,0.0f,1.0f); gl.glTexCoord2f(0.0f, 1.0f);gl.glVertex3f(1.0f,0.0f,0.0f); gl.glEnd();
The other sides are rendered according to this pattern:
gl.glDisable(GL.GL_TEXTURE_2D); gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, textures[1].getWidth(),textures[1].getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, textures[1].getPixels()); gl.glEnable(GL.GL_TEXTURE_2D); gl.glBegin(GL.GL_POLYGON);/* f2: bottom */ gl.glNormal3f(0.0f,0.0f,-1.0f); gl.glTexCoord2f(0.0f, 0.0f);gl.glVertex3f(0.0f,0.0f,0.0f); gl.glTexCoord2f(1.0f, 0.0f);gl.glVertex3f(1.0f,0.0f,0.0f); gl.glTexCoord2f(1.0f, 1.0f);gl.glVertex3f(1.0f,1.0f,0.0f); gl.glTexCoord2f(0.0f, 1.0f);gl.glVertex3f(0.0f,1.0f,0.0f); gl.glEnd();
The complete class, oneTexturedBox