00001
00002
00003
00004
00005 package org.dronus.gl;
00006
00007 import java.awt.Image;
00008 import java.awt.image.BufferedImage;
00009 import java.nio.ByteBuffer;
00010 import java.nio.ByteOrder;
00011
00012 import org.lwjgl.opengl.GL11;
00013 import org.lwjgl.opengl.GL14;
00014
00037 public class Texture {
00038
00039 protected int level=0, internalformat=GL11.GL_RGBA, border=0,
00040 format=GL11.GL_RGBA,type=GL11.GL_UNSIGNED_BYTE;
00041
00042
00043 public int width,height;
00044 public float imagewidth,imageheight;
00045
00046 protected int texid;
00047
00048 protected Texture(){
00049 texid=GLUtil.genTexture();
00050 imagewidth=imageheight=1;
00051 }
00052
00064 public Texture(Image image){
00065 this(image.getWidth(null), image.getHeight(null), GL11.GL_RGBA, true);
00066
00067 setPixels(getImageBuffer(image), width, height);
00068 }
00069
00070 public static ByteBuffer getImageBuffer(Image image){
00071
00072 BufferedImage tmpImage;
00073 int w=image.getWidth(null), h=image.getHeight(null);
00074
00075
00076 if (image instanceof BufferedImage
00077 && ((BufferedImage)image).getType()==BufferedImage.TYPE_4BYTE_ABGR)
00078 tmpImage=(BufferedImage)image;
00079 else {
00080 tmpImage=new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
00081 tmpImage.getGraphics().drawImage(image,0,0,null);
00082 }
00083
00084 byte pixelData[] = (byte[])tmpImage.getRaster().getDataElements(0,0,w,h,null);
00085
00086 ByteBuffer pixels = ByteBuffer.allocateDirect(pixelData.length);
00087 pixels.order(ByteOrder.nativeOrder());
00088 pixels.put(pixelData);
00089 pixels.rewind();
00090
00091 return pixels;
00092 }
00093
00094 public Texture(ByteBuffer pixels, int w, int h, int glPixelFormat){
00095 this(w, h, glPixelFormat, true);
00096 setPixels(pixels,w,h);
00097 }
00098
00099
00100 public Texture(int w, int h, int format, boolean mipmap){
00101 this.format=format;
00102 texid=GLUtil.genTexture();
00103
00104 width=w; height=h;
00105
00106
00107
00108 if (Integer.bitCount(w)!=1) w=Integer.highestOneBit(w)*2;
00109 if (Integer.bitCount(h)!=1) h=Integer.highestOneBit(h)*2;
00110
00111 imagewidth=width/(float)w; imageheight=height/(float)h;
00112
00113 GL11.glBindTexture(GL11.GL_TEXTURE_2D,texid);
00114 GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
00115 GL11.glTexParameteri(GL11.GL_TEXTURE_2D,GL14.GL_GENERATE_MIPMAP, mipmap ? GL11.GL_TRUE : GL11.GL_FALSE);
00116 GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, mipmap ? GL11.GL_LINEAR_MIPMAP_LINEAR : GL11.GL_LINEAR);
00117 GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
00118
00119 GL11.glTexImage2D(GL11.GL_TEXTURE_2D,level,internalformat,w,h,border,format,type,(ByteBuffer)null);
00120 }
00121
00122 protected void setPixels(ByteBuffer pixels, int width, int height){
00123 GL11.glBindTexture(GL11.GL_TEXTURE_2D,texid);
00124
00125
00126
00127 GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D,level,0,0,width,height,format,type,pixels);
00128 }
00129
00130
00134 public void render() {
00135 GL11.glEnable(GL11.GL_TEXTURE_2D);
00136 GL11.glBindTexture(GL11.GL_TEXTURE_2D,texid);
00137 };
00138
00142 protected void finalize() throws Throwable {
00143
00144 GL11.glDeleteTextures(Buffers.i(texid));
00145 super.finalize();
00146 }
00147 }