Transparent Sprite?

I’ve created a PNG with a transparent background and an alpha channel. I was able to get it to be transparent with blending, but the “main” part of the image blends in with the background as well. How do I get the “sprite” to be solid. I switched from blending to Alpha, but my image isnt loading correctly. It’s all “fuzzy” now. Im using the PNG format with an Alpha map.

Im trying simulate 2D in Ortho mode. Im using the texture loader from the Nehe demos.

It seems to have issues when I changed my glTexImage2D values to GL_RBGA… I have checked my image out, it is a 32 bit image with alpha channel.

Here are my settings:


GL.glEnable(GL.GL_TEXTURE_2D); // Enable Texture Mapping
GL.glShadeModel(GL.GL_SMOOTH); // Enable Smooth Shading
GL.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
GL.glClearDepth(1.0); // Depth Buffer Setup
GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
GL.glAlphaFunc(GL.GL_GREATER,0.0f);                                    // Set Alpha Testing     (disable blending)
GL.glEnable(GL.GL_ALPHA_TEST);                                    // Enable Alpha Testing  (disable blending)
GL.glEnable(GL.GL_CULL_FACE);                                          // Remove Back Face
GL.glViewport(0,0,Window.getWidth(),Window.getHeight());                                    // Reset The Current Viewport
GL.glMatrixMode(GL.GL_PROJECTION);                                    // Select The Projection Matrix
GL.glLoadIdentity();                                          // Reset The Projection Matrix
GL.glOrtho(0.0f,Window.getWidth(),Window.getHeight() ,0.0f,-100f,100f);
GL.glMatrixMode(GL.GL_MODELVIEW);                                    // Select The Modelview Matrix
GL.glLoadIdentity();                                          // Reset The Modelview Matrix

Here is my quad draw code


                        GL.glBindTexture(GL.GL_TEXTURE_2D, texture[tile]);
                  
                        GL.glBegin(GL.GL_QUADS);
                        
                        GL.glTexCoord2f(0.0f, 0.0f); GL.glVertex3f(x*32f, (y + 1)*32f, z);
                        GL.glTexCoord2f(1.0f, 0.0f); GL.glVertex3f((x + 1)*32f, (y + 1)*32f,z);
                        GL.glTexCoord2f(1.0f, 1.0f); GL.glVertex3f((x + 1)*32f, y*32f, z);
                        GL.glTexCoord2f(0.0f, 1.0f); GL.glVertex3f(x*32f, y*32f, z);
                        
                        GL.glEnd();


Loader:


      private final static int loadTexture(String path,int trans) {

            Image image = (new javax.swing.ImageIcon(path)).getImage();
            
            // Exctract The Image
            BufferedImage tex =
            new BufferedImage(
            image.getWidth(null),
            image.getHeight(null),
            BufferedImage.TYPE_3BYTE_BGR);
            Graphics2D g = (Graphics2D) tex.getGraphics();
            g.drawImage(image, null, null);
            g.dispose();
            
            // Flip Image
            AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
            tx.translate(0, -image.getHeight(null));
            AffineTransformOp op =
            new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
            tex = op.filter(tex, null);
            
            // Put Image In Memory
            ByteBuffer scratch =
            ByteBuffer.allocateDirect(4 * tex.getWidth() * tex.getHeight());
            
            byte data[] =
            (byte[]) tex.getRaster().getDataElements(
            0,
            0,
            tex.getWidth(),
            tex.getHeight(),
            null);
            scratch.clear();
            scratch.put(data);
            scratch.rewind();
            
            // Create A IntBuffer For Image Address In Memory
            IntBuffer buf =
            ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
            GL.glGenTextures(buf); // Create Texture In OpenGL
            
            GL.glBindTexture(GL.GL_TEXTURE_2D, buf.get(0));
            // Typical Texture Generation Using Data From The Image
            
            // Linear Filtering
            GL.glTexParameteri(
            GL.GL_TEXTURE_2D,
            GL.GL_TEXTURE_MIN_FILTER,
            GL.GL_LINEAR);
            // Linear Filtering
            GL.glTexParameteri(
            GL.GL_TEXTURE_2D,
            GL.GL_TEXTURE_MAG_FILTER,
            GL.GL_LINEAR);
            
int type=GL.GL_RGB;
if (trans==1) { type=GL.GL_RGBA; }

            // Generate The Texture
            GL.glTexImage2D(
            GL.GL_TEXTURE_2D,
            0,
            type,
            tex.getWidth(),
            tex.getHeight(),
            0,
            type,
            GL.GL_UNSIGNED_BYTE,
            scratch);

            
            return buf.get(0); // Return Image Address In Memory
      }


thanks!

ack, finally figured it out!!!

It was a problem with my texture loader.

I was loading the images with no alpha…

A simple change from


//      BufferedImage.TYPE_3BYTE_BGR);

to 
      BufferedImage.TYPE_4BYTE_ABGR);

solved it.