Å tegne:>Boks med tekstur
Textured Cube
TextureReader
Dette er en forenklet versjon av klassen som brukes i NeHe [1] . Du kan gå til koders [2] , og søke med TextureReader
oneTexturedBox
Teksturene lagres i en array og lastes ved construction:
// 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()); } } }
Tegning av den første siden:
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();
De andre sidene genereres etter dettte mønsteret:
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();
Den komplette klassen, oneTexturedBox