I didn't set anything, I'm using the glClearColor.
When I add glColor4d(1,1,1,1), nothing happens, but when I change it to glColor4d(1,1,1,0.5), the pictures get more transparent.
There must be something that halves the alpha value...
Here's the whole TextureLoader:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.HashMap;
import java.util.Hashtable;
import javax.swing.ImageIcon;
import org.lwjgl.BufferUtils;
import static org.lwjgl.opengl.GL11.*;
public class TextureLoader
{
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);
}
private int createTextureID()
{
glGenTextures(textureIDBuffer);
return textureIDBuffer.get(0);
}
public Texture getTex(String path)
{
return getTexture("../../resources/graphic/" + path);
}
public Texture getTexture(String path)
{
Texture tex = table.get(path);
if(tex == null)
{
tex = getTexture(path, GL_TEXTURE_2D, GL_RGBA, GL_LINEAR, GL_LINEAR);
table.put(path,tex);
}
return tex;
}
public Texture getTexture(String path, int target, int dstPixelFormat, int minFilter, int magFilter)
{
int srcPixelFormat;
int textureID = createTextureID();
Texture texture = new Texture(target,textureID);
glBindTexture(target, textureID);
BufferedImage bufferedImage = loadImage(path);
texture.setWidth(bufferedImage.getWidth());
texture.setHeight(bufferedImage.getHeight());
if(bufferedImage.getColorModel().hasAlpha())
{
srcPixelFormat = GL_RGBA;
}
else
{
srcPixelFormat = GL_RGB;
}
ByteBuffer textureBuffer = convertImageData(bufferedImage,texture);
if(target == GL_TEXTURE_2D)
{
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilter);
}
glTexImage2D(target, 0, dstPixelFormat, get2Fold(bufferedImage.getWidth()), get2Fold(bufferedImage.getHeight()), 0, srcPixelFormat, GL_UNSIGNED_BYTE, textureBuffer);
return texture;
}
private static int get2Fold(int fold)
{
int ret = 2;
while (ret < fold)
{
ret *= 2;
}
return ret;
}
private ByteBuffer convertImageData(BufferedImage bufferedImage,Texture texture)
{
ByteBuffer imageBuffer;
WritableRaster raster;
BufferedImage texImage;
int texWidth = 2;
int texHeight = 2;
while(texWidth < bufferedImage.getWidth())
{
texWidth *= 2;
}
while(texHeight < bufferedImage.getHeight())
{
texHeight *= 2;
}
texture.setTextureHeight(texHeight);
texture.setTextureWidth(texWidth);
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());
}
Graphics g = texImage.getGraphics();
g.setColor(new Color(0f, 0f, 0f, 0f));
g.fillRect(0, 0, texWidth, texHeight);
g.drawImage(bufferedImage, 0, 0, null);
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;
}
private BufferedImage loadImage(String ref)
{
try
{
Image img = new ImageIcon(ref).getImage();
BufferedImage bufferedImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
return bufferedImage;
}
catch(Exception e)
{
return new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
}
}
private HashMap<String, Texture> table = new HashMap<String, Texture>();
private ColorModel glAlphaColorModel;
private ColorModel glColorModel;
private IntBuffer textureIDBuffer = BufferUtils.createIntBuffer(1);
}
The Texture:
import static org.lwjgl.opengl.GL11.*;
public class Texture
{
public Texture(int target, int textureID)
{
this.target = target;
this.textureID = textureID;
}
public void drawQuad(int x, int y)
{
drawQuad(x, y, 0);
}
public void drawQuad(int x, int y, int z)
{
bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(x, y, z);
glTexCoord2f(0, getHeight());
glVertex3f(x, y + getImageHeight(), z);
glTexCoord2f(getWidth(), getHeight());
glVertex3f(x + getImageWidth(), y + getImageHeight(), z);
glTexCoord2f(getWidth(), 0);
glVertex3f(x + getImageWidth(), y, z);
glEnd();
}
public void drawQuadSize(int x, int y, int width, int height)
{
drawQuadSize(x, y, 0, width, height);
}
public void drawQuadSize(int x, int y, int z, int width, int height)
{
bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(x, y, z);
glTexCoord2f(0, getHeight());
glVertex3f(x, y + height, z);
glTexCoord2f(getWidth(), getHeight());
glVertex3f(x + width, y + height, z);
glTexCoord2f(getWidth(), 0);
glVertex3f(x + width, y, z);
glEnd();
}
public void bind()
{
glBindTexture(target, textureID);
}
public void setHeight(int height)
{
this.height = height;
setHeight();
}
public void setWidth(int width)
{
this.width = width;
setWidth();
}
public int getImageHeight()
{
return height;
}
public int getImageWidth()
{
return width;
}
public float getHeight()
{
return heightRatio;
}
public float getWidth()
{
return widthRatio;
}
public void setTextureHeight(int texHeight)
{
this.texHeight = texHeight;
setHeight();
}
public void setTextureWidth(int texWidth)
{
this.texWidth = texWidth;
setWidth();
}
private void setHeight()
{
if (texHeight != 0)
{
heightRatio = ((float) height) / texHeight;
}
}
private void setWidth()
{
if (texWidth != 0)
{
widthRatio = ((float) width) / texWidth;
}
}
private int target;
private int textureID;
private int height;
private int width;
private int texWidth;
private int texHeight;
private float widthRatio;
private float heightRatio;
}
Where the cursor gets drawn:
Texture cursor = cultrix.renderer.getTex("gui/cursor.png");
cursor.drawQuad(mouseX, mouseY);
The cursor image can be loaded, gets displayed at the right coordinates, just transparent -.-
I attached the cursor image so u can be 100% sure the image is not transparent.
Greetings
Molt