Java LWJGL透明度不起作用?
有没有线索说明为什么我不能使用LWJGL创建透明纹理?我使用的是Java LWJGL透明度不起作用?,java,transparency,lwjgl,Java,Transparency,Lwjgl,有没有线索说明为什么我不能使用LWJGL创建透明纹理?我使用的是.gif图像,因为.PNG图像会导致纹理出现奇怪的斑点问题。我尝试了一些我找到的其他答案,但它并没有解决我的问题。我不是OpenGL最棒的,但我已经花了很多天的时间试图解决它 texure.java public class Sprite { /** The texture that stores the image for this sprite */ private Texture texture; /** The wid
.gif
图像,因为.PNG
图像会导致纹理出现奇怪的斑点问题。我尝试了一些我找到的其他答案,但它并没有解决我的问题。我不是OpenGL最棒的,但我已经花了很多天的时间试图解决它
texure.java
public class Sprite {
/** The texture that stores the image for this sprite */
private Texture texture;
/** The width in pixels of this sprite */
private int width;
/** The height in pixels of this sprite */
private int height;
/**
* Create a new sprite from a specified image.
*
* @param loader the texture loader to use
* @param ref A reference to the image on which this sprite should be based
*/
public Sprite(TextureLoader loader, String ref) {
try {
texture = loader.getTexture(ref);
width = texture.getImageWidth();
height = texture.getImageHeight();
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(-1);
}
}
/**
* Get the width of this sprite in pixels
*
* @return The width of this sprite in pixels
*/
public int getWidth() {
return texture.getImageWidth();
}
/**
* Get the height of this sprite in pixels
*
* @return The height of this sprite in pixels
*/
public int getHeight() {
return texture.getImageHeight();
}
/**
* Draw the sprite at the specified location
*
* @param x The x location at which to draw this sprite
* @param y The y location at which to draw this sprite
*/
public void draw(int x, int y) {
// store the current model matrix
glPushMatrix();
// bind to the appropriate texture for this sprite
texture.bind();
// translate to the right location and prepare to draw
glTranslatef(x, y, 0);
// draw a quad textured to match the sprite
glBegin(GL_QUADS);
{
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(0, texture.getHeight());
glVertex2f(0, height);
glTexCoord2f(texture.getWidth(), texture.getHeight());
glVertex2f(width, height);
glTexCoord2f(texture.getWidth(), 0);
glVertex2f(width, 0);
}
glEnd();
// restore the model view matrix to prevent contamination
glPopMatrix();
}
public void draw(int x, int y,int rotate) {
// store the current model matrix
glPushMatrix();
// bind to the appropriate texture for this sprite
texture.bind();
// translate to the right location and prepare to draw
//glTranslatef(x, y, 0);
glTranslatef(x+getWidth()/2, y+getHeight()/2, 0); // M1 - 2nd translation
glRotatef(rotate, 0.0f, 0.0f, 1.0f); // M2
glTranslatef( -getWidth()/2, -getHeight()/2, 0); // M3 - 1st translation
// draw a quad textured to match the sprite
glBegin(GL_QUADS);
{
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(0, texture.getHeight());
glVertex2f(0, height);
glTexCoord2f(texture.getWidth(), texture.getHeight());
glVertex2f(width, height);
glTexCoord2f(texture.getWidth(), 0);
glVertex2f(width, 0);
}
glEnd();
// restore the model view matrix to prevent contamination
glPopMatrix();
}
texture.java
public class Texture {
/** The GL target type */
private int target;
/** The GL texture ID */
private int textureID;
/** The height of the image */
private int height;
/** The width of the image */
private int width;
/** The width of the texture */
private int texWidth;
/** The height of the texture */
private int texHeight;
/** The ratio of the width of the image to the texture */
private float widthRatio;
/** The ratio of the height of the image to the texture */
private float heightRatio;
/**
* Create a new texture
*
* @param target The GL target
* @param textureID The GL texture ID
*/
public Texture(int target, int textureID) {
this.target = target;
this.textureID = textureID;
}
/**
* Bind the specified GL context to a texture
*/
public void bind() {
glBindTexture(target, textureID);
}
/**
* Set the height of the image
*
* @param height The height of the image
*/
public void setHeight(int height) {
this.height = height;
setHeight();
}
/**
* Set the width of the image
*
* @param width The width of the image
*/
public void setWidth(int width) {
this.width = width;
setWidth();
}
/**
* Get the height of the original image
*
* @return The height of the original image
*/
public int getImageHeight() {
return height;
}
/**
* Get the width of the original image
*
* @return The width of the original image
*/
public int getImageWidth() {
return width;
}
/**
* Get the height of the physical texture
*
* @return The height of physical texture
*/
public float getHeight() {
return heightRatio;
}
/**
* Get the width of the physical texture
*
* @return The width of physical texture
*/
public float getWidth() {
return widthRatio;
}
/**
* Set the height of this texture
*
* @param texHeight The height of the texture
*/
public void setTextureHeight(int texHeight) {
this.texHeight = texHeight;
setHeight();
}
/**
* Set the width of this texture
*
* @param texWidth The width of the texture
*/
public void setTextureWidth(int texWidth) {
this.texWidth = texWidth;
setWidth();
}
/**
* Set the height of the texture. This will update the
* ratio also.
*/
private void setHeight() {
if (texHeight != 0) {
heightRatio = ((float) height) / texHeight;
}
}
/**
* Set the width of the texture. This will update the
* ratio also.
*/
private void setWidth() {
if (texWidth != 0) {
widthRatio = ((float) width) / texWidth;
}
}
textureLoader.java
public class TextureLoader {
/** The table of textures that have been loaded in this loader */
private HashMap<String, Texture> table = new HashMap<String, Texture>();
/** The colour model including alpha for the GL image */
private ColorModel glAlphaColorModel;
/** The colour model for the GL image */
private ColorModel glColorModel;
/** Scratch buffer for texture ID's */
private IntBuffer textureIDBuffer = BufferUtils.createIntBuffer(1);
/**
* Create a new texture loader based on the game panel
*/
public TextureLoader() {
glAlphaColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {8,8,8,8},
true,
false,
ComponentColorModel.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
glColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {8,8,8,0},
false,
false,
ComponentColorModel.OPAQUE,
DataBuffer.TYPE_BYTE);
}
/**
* Create a new texture ID
*
* @return A new texture ID
*/
private int createTextureID() {
glGenTextures(textureIDBuffer);
return textureIDBuffer.get(0);
}
/**
* Load a texture
*
* @param resourceName The location of the resource to load
* @return The loaded texture
* @throws IOException Indicates a failure to access the resource
*/
public Texture getTexture(String resourceName) throws IOException {
Texture tex = table.get(resourceName);
if (tex != null) {
return tex;
}
tex = getTexture(resourceName,
GL_TEXTURE_2D, // target
GL_RGBA, // dst pixel format
GL_LINEAR, // min filter (unused)
GL_LINEAR);
table.put(resourceName,tex);
return tex;
}
/**
* Load a texture into OpenGL from a image reference on
* disk.
*
* @param resourceName The location of the resource to load
* @param target The GL target to load the texture against
* @param dstPixelFormat The pixel format of the screen
* @param minFilter The minimising filter
* @param magFilter The magnification filter
* @return The loaded texture
* @throws IOException Indicates a failure to access the resource
*/
public Texture getTexture(String resourceName,
int target,
int dstPixelFormat,
int minFilter,
int magFilter) throws IOException {
int srcPixelFormat;
// create the texture ID for this texture
int textureID = createTextureID();
Texture texture = new Texture(target,textureID);
// bind this texture
glBindTexture(target, textureID);
BufferedImage bufferedImage = loadImage(resourceName);
texture.setWidth(bufferedImage.getWidth());
texture.setHeight(bufferedImage.getHeight());
if (bufferedImage.getColorModel().hasAlpha()) {
srcPixelFormat = GL_RGBA;
} else {
srcPixelFormat = GL_RGB;
}
// convert that image into a byte buffer of texture data
ByteBuffer textureBuffer = convertImageData(bufferedImage,texture);
if (target == GL_TEXTURE_2D) {
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilter);
}
// produce a texture from the byte buffer
glTexImage2D(target,
0,
dstPixelFormat,
get2Fold(bufferedImage.getWidth()),
get2Fold(bufferedImage.getHeight()),
0,
srcPixelFormat,
GL_UNSIGNED_BYTE,
textureBuffer );
return texture;
}
/**
* Get the closest greater power of 2 to the fold number
*
* @param fold The target number
* @return The power of 2
*/
private static int get2Fold(int fold) {
int ret = 2;
while (ret < fold) {
ret *= 2;
}
return ret;
}
/**
* Convert the buffered image to a texture
*
* @param bufferedImage The image to convert to a texture
* @param texture The texture to store the data into
* @return A buffer containing the data
*/
private ByteBuffer convertImageData(BufferedImage bufferedImage,Texture texture) {
ByteBuffer imageBuffer;
WritableRaster raster;
BufferedImage texImage;
int texWidth = 2;
int texHeight = 2;
// find the closest power of 2 for the width and height
// of the produced texture
while (texWidth < bufferedImage.getWidth()) {
texWidth *= 2;
}
while (texHeight < bufferedImage.getHeight()) {
texHeight *= 2;
}
texture.setTextureHeight(texHeight);
texture.setTextureWidth(texWidth);
// create a raster that can be used by OpenGL as a source
// for a texture
if (bufferedImage.getColorModel().hasAlpha()) {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,texWidth,texHeight,4,null);
texImage = new BufferedImage(glAlphaColorModel,raster,false,new Hashtable());
} else {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,texWidth,texHeight,3,null);
texImage = new BufferedImage(glColorModel,raster,false,new Hashtable());
}
// copy the source image into the produced image
Graphics g = texImage.getGraphics();
g.setColor(new Color(0f,0f,0f,0f));
g.fillRect(0,0,texWidth,texHeight);
g.drawImage(bufferedImage,0,0,null);
// build a byte buffer from the temporary image
// that be used by OpenGL to produce a texture.
byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();
imageBuffer = ByteBuffer.allocateDirect(data.length);
imageBuffer.order(ByteOrder.nativeOrder());
imageBuffer.put(data, 0, data.length);
imageBuffer.flip();
return imageBuffer;
}
/**
* Load a given resource as a buffered image
*
* @param ref The location of the resource to load
* @return The loaded buffered image
* @throws IOException Indicates a failure to find a resource
*/
private BufferedImage loadImage(String ref) throws IOException {
URL url = TextureLoader.class.getClassLoader().getResource(ref);
if (url == null) {
throw new IOException("Cannot find: " + ref);
}
// due to an issue with ImageIO and mixed signed code
// we are now using good oldfashioned ImageIcon to load
// images and the paint it on top of a new BufferedImage
Image img = new ImageIcon(url).getImage();
BufferedImage bufferedImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bufferedImage.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
return bufferedImage;
}
}
公共类TextureLoader{
/**在此加载程序中加载的纹理表*/
私有HashMap table=newhashmap();
/**GL图像的包含alpha的颜色模型*/
私人ColorModel glAlphaColorModel;
/**GL图像的颜色模型*/
私有颜色模型;
/**纹理ID的划痕缓冲区*/
私有IntBuffer textureIDBuffer=BufferUtils.createIntBuffer(1);
/**
*基于游戏面板创建一个新的纹理加载器
*/
公共纹理阅读器(){
glAlphaColorModel=新组件colorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
新int[]{8,8,8,8},
是的,
假,,
ComponentColorModel.半透明,
DataBuffer.TYPE_字节);
glColorModel=新组件ColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
新int[]{8,8,8,0},
假,,
假,,
ComponentColorModel.不透明,
DataBuffer.TYPE_字节);
}
/**
*创建一个新的纹理ID
*
*@返回一个新的纹理ID
*/
私有int createTextureID(){
glGenTextures(纹理缓冲区);
返回textureIDBuffer.get(0);
}
/**
*加载纹理
*
*@param resourceName要加载的资源的位置
*@返回加载的纹理
*@IOException表示访问资源失败
*/
公共纹理getTexture(字符串resourceName)引发IOException{
纹理tex=table.get(resourceName);
如果(tex!=null){
返回tex;
}
tex=getTexture(resourceName,
GL_纹理_2D,//目标
GL_RGBA,//dst像素格式
GL_线性,//最小过滤器(未使用)
GL_线性);
表.put(resourceName,tex);
返回tex;
}
/**
*将纹理从上的图像引用加载到OpenGL
*磁盘。
*
*@param resourceName要加载的资源的位置
*@param target加载纹理的GL目标
*@param dstPixelFormat屏幕的像素格式
*@param minFilter最小化过滤器
*@param magFilter放大过滤器
*@返回加载的纹理
*@IOException表示访问资源失败
*/
公共纹理getTexture(字符串resourceName,
int目标,
整数像素格式,
int minFilter,
int magFilter)引发IOException{
整数像素格式;
//创建此纹理的纹理ID
int-textureID=createTextureID();
纹理=新纹理(目标,纹理ID);
//绑定此纹理
glBindTexture(目标,textureID);
BuffereImage BuffereImage=loadImage(resourceName);
setWidth(buffereImage.getWidth());
setHeight(bufferedImage.getHeight());
if(buffereImage.getColorModel().hasAlpha()){
srcPixelFormat=GL_RGBA;
}否则{
srcPixelFormat=GL_RGB;
}
//将该图像转换为纹理数据的字节缓冲区
ByteBuffer textureBuffer=转换图像数据(BuffereImage,纹理);
如果(目标==GL\U纹理\U 2D){
glTexParameteri(目标、GL_纹理、minu过滤器、minFilter);
glTexParameteri(目标、GL_纹理、MAG_过滤器、magFilter);
}
//从字节缓冲区生成纹理
glTexImage2D(目标,
0,
DST像素格式,
get2Fold(BuffereImage.getWidth()),
get2Fold(BuffereImage.getHeight()),
0,
像素格式,
GL_无符号字节,
纹理缓冲);
返回纹理;
}
/**
*获得与折叠数最接近的2的最大幂
*
*@param折叠目标编号
*@返回2的力量
*/
私有静态int-get2Fold(int-fold){
int-ret=2;
while(retglEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);