I am trying to render a quad with transparency in it.
This is my test-texture, a PNG image with alpha info:
And this is what I get rendering it:
The transparent parts turn up white?? Do you know how to fix this?
Here is the full source code:
package Render3D;
import net.java.games.jogl.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.nio.ByteBuffer;
public class Renderer implements GLEventListener
{
int textureID;
public void init(GLDrawable gLDrawable)
{
final GL gl = gLDrawable.getGL();
textureID=readTexture(gl,"C:\\test24.png");
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBindTexture(GL.GL_TEXTURE_2D,textureID);
gl.glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
gl.glEnable ( GL.GL_BLEND );
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
}
public void displayChanged(GLDrawable drawable,boolean modeChanged,boolean deviceChanged)
{
}
public void display(GLDrawable drawable)
{
GL gl=drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0,0,-400);
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2f(0,0);
gl.glVertex3f(-100.0f, -100.0f, 0.0f);
gl.glTexCoord2f(1,0);
gl.glVertex3f( 100.0f, -100.0f, 0.0f);
gl.glTexCoord2f(1,1);
gl.glVertex3f( 100.0f,100.0f, 0.0f);
gl.glTexCoord2f(0,1);
gl.glVertex3f(-100.0f,100.0f, 0.0f);
gl.glEnd();
gl.glFlush();
}
public void reshape(GLDrawable gLDrawable, int x, int y, int width, int height)
{
final GL gl = gLDrawable.getGL();
final GLU glu = gLDrawable.getGLU();
final float h = (float)width / (float)height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, Double.MAX_VALUE);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
public static int[] readTextures(GL gl, String names[])
{
int textures[] = new int[names.length];
gl.glGenTextures(names.length, textures);
for (int i = 0; i < names.length; i++) {
Texture texture = readTexture(names[i]);
//Create Nearest Filtered Texture
gl.glBindTexture(GL.GL_TEXTURE_2D, textures[i]);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
gl.glTexImage2D(GL.GL_TEXTURE_2D,
0,
3,
texture.getWidth(),
texture.getHeight(),
0,
GL.GL_RGBA,
GL.GL_UNSIGNED_BYTE,
texture.getPixels());
}
return textures;
}
private static Texture readTexture(String filename)
{
try {
BufferedImage bufferedImage;
bufferedImage = ImageIO.read(new File(filename));
return readPixels(bufferedImage);
} catch (Exception e) {
System.out.println("Could not read texture" + filename);
return null;
}
}
public static int readTexture(GL gl, String name) {
String n[] = new String[1];
n[0] = name;
int id[] = readTextures(gl, n);
return id[0];
}
private static Texture readPixels(BufferedImage img)
{
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 = 4;
ByteBuffer unpackedPixels = ByteBuffer.allocateDirect(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));
unpackedPixels.put((byte) ((packedPixel >> 24) & 0xFF));
}
}
return new Texture(unpackedPixels, img.getWidth(), img.getHeight());
}
private 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;
}
}
}