Hi.
How can I mask textures (meaning that when a texture contains a certain color, that color is completely transparent) in LWJGL without manually creating a black/white texture for each texture that I'm going to use?
LWJGL is driving me completely crazy in that regard. I'm using slicks TextureLoader and Texture classes to load a PNG file, but since I have no way of manipulating the actual texture data inside those classes (texture.getTextureData() only returns copies of the values and additionally the data returned is always 65543 something entries long, which is ridiculous for a 100x100 texture), I can't manually set the alpha of pixels to 0.0f which match the color that is to be masked.
Since I don't know how to load PNGs myself, I'm now trying to create the texture manually a second time after loading it with TextureLoader, but I can't even get normal texturing to work manually, lol!:
try
{
m_texture = ResourceManager.getTexture(file_location);
//safe the texture ID given by glGenTextures()
m_textureID = GL11.glGenTextures();
//bind it
GL11.glBindTexture(GL11.GL_TEXTURE_2D, m_textureID);
//get pixel data out of m_texture
byte[] pixels = m_texture.getTextureData();
System.out.println(m_texture.getTextureData().length);
ByteBuffer bb = ByteBuffer.allocate(pixels.length);
bb.put(pixels);
IntBuffer ib = bb.asIntBuffer();
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, 4, m_texture.getTextureWidth(),
m_texture.getTextureHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, ib);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D,GL11.GL_TEXTURE_MIN_FILTER,GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D,GL11.GL_TEXTURE_MAG_FILTER,GL11.GL_LINEAR);
}
This block always throws an exception which says that the remaining buffer contains 0 elements.
This is my sprite drawing function btw, it works flawlessly if I just m_texture.bind() instead of manually binding my texture:
public void draw()
{
GL11.glPushMatrix();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, m_textureID);
GL11.glTranslatef(m_position.x+(m_size.x/2.0f), m_position.y+(m_size.y/2.0f), 0.0f);
GL11.glRotatef(m_angle, 0.0f, 0.0f,1.0f);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0.0f,0.0f);
GL11.glVertex3f(m_spriterect.left,m_spriterect.bottom,0.0f);
GL11.glTexCoord2f(m_tex_coord_value,0.0f);
GL11.glVertex3f(m_spriterect.right,m_spriterect.bottom,0.0f);
GL11.glTexCoord2f(m_tex_coord_value,m_tex_coord_value);
GL11.glVertex3f(m_spriterect.right,m_spriterect.top,0.0f);
GL11.glTexCoord2f(0.0f,m_tex_coord_value);
GL11.glVertex3f(m_spriterect.left,m_spriterect.top,0.0f);
GL11.glEnd();
GL11.glPopMatrix();
}
I'm coming from C++ and wanted to code a little 2D engine in Java, but it's driving me nuts.
Can I even create a new manual texture this way? Or how could I go about "auto-texture-masking" while still using Texture and TextureLoader?