Børre Stenseth
Å tegne:>Boks med tekstur

Textured Cube

Hva
screen
En boks med tekstur på sideflatene

tTeksturer er forklart i modulen: Teksturer. Vi skal skrive et program som legger ut forskjellioge teksturer på sidene på en kloss. Vi trenger kode for å laste inn den grafikken vi skal bruke som tekstur og forberede den for OpenGL Dette er implementert i klassen TextureReader nedefor. Denne klassen er en modifisert versjon av klassen som brukes i NeHe's [1] tekstureksempler.

Vi modifiserer box-klassen som er brukt i Kloss og vi bruker stdMaterial klassen fra Noen materialer

Programmet er musedrevet har en zoomkontroll, alt implementert i klassen Tex1.java.

TextureReader

Dette er en forenklet versjon av klassen som brukes i NeHe [1] . Du kan gå til koders [2] , og søke med TextureReader

_TextureReader.java
package tex1;
/**
 *
 * @author bs
 */
import com.sun.opengl.util.BufferUtil;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
/**
 * Image loading class that converts BufferedImages into a data
 * structure that can be easily passed to OpenGL.
 * @author Pepijn Van Eeckhoudt
 *
 * Modified by bs: simplified: no .bmp load, no resource access
 * Search in: www.koders.com/? for original
 */
public class TextureReader {
    public static Texture readTexture(String filename)
            throws IOException {
        return readTexture(filename, false);
    }
    public static Texture readTexture(String filename, 
                                      boolean storeAlphaChannel)
            throws IOException {
        BufferedImage bufferedImage;
        bufferedImage=ImageIO.read(new FileInputStream(filename));
        return readPixels(bufferedImage, storeAlphaChannel);
    }

    private static Texture readPixels(BufferedImage img,
                                      boolean storeAlphaChannel) {
        int[] packedPixels = new int[img.getWidth() * img.getHeight()];
        PixelGrabber pixelgrabber =
                new PixelGrabber(img, 0, 0,
                                 img.getWidth(), img.getHeight(),
                                 packedPixels, 0, img.getWidth());
        try {
            pixelgrabber.grabPixels();
        } catch (InterruptedException e) {
            throw new RuntimeException();
        }
        int bytesPerPixel = storeAlphaChannel ? 4 : 3;
        ByteBuffer unpackedPixels =
                BufferUtil.newByteBuffer(packedPixels.length * bytesPerPixel);
        for (int row = img.getHeight() - 1; row >= 0; row--) {
            for (int col = 0; col < img.getWidth(); col++) {
                int packedPixel = packedPixels[row * img.getWidth() + col];
                unpackedPixels.put((byte) ((packedPixel >> 16) & 0xFF));
                unpackedPixels.put((byte) ((packedPixel >> 8) & 0xFF));
                unpackedPixels.put((byte) ((packedPixel >> 0) & 0xFF));
                if (storeAlphaChannel) {
                    unpackedPixels.put((byte) ((packedPixel >> 24) & 0xFF));
                }
            }
        }
        unpackedPixels.flip();
        return new Texture(unpackedPixels, img.getWidth(), img.getHeight());
    }
    public static class Texture {
        private ByteBuffer pixels;
        private int width;
        private int height;
        public Texture(ByteBuffer pixels, int width, int height) {
            this.height = height;
            this.pixels = pixels;
            this.width = width;
        }
        public int getHeight() {return height;}
        public ByteBuffer getPixels() {return pixels;}
        public int getWidth() {return width;}
    }
}

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

_oneTexturedBox.java
Referanser
  1. Latest 'NEHE' NewsNEHE, NeonHelium ProductionsOpenGL-tutorials.nehe.gamedev.net/14-03-2009
  1. KodersA site with a lot of searchable code exampleswww.koders.com/?14-05-2009
  • Cube (JOGL/Netbeans): https://svn.hiof.no/svn/psource/JOGL/tex1
Vedlikehold
Mai 2010, Børre Stenseth
(Velkommen) Å tegne:>Boks med tekstur (Bezier med tekstur)