Edit for Transparency

below is the nehe snippet for loading textures. what changes need to be made to add transparncy to them?
I tried switching to TYPE_4BYTE_BGRA but to no success.

Thanks
:slight_smile:


/**
       * Texture loading directly from LWJGL examples
       */
      private static final int loadTexture(String path) {
            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();
            GL11.glGenTextures(buf); // Create Texture In OpenGL   

            GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
            // Typical Texture Generation Using Data From The Image

            // Linear Filtering
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
            // Linear Filtering
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
            // Generate The Texture
            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tex.getWidth(), tex.getHeight(), 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, scratch);

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

Change :


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

To:


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

Basically change RGB to RGBA. A is the alpha layer, or, the transparancy layer.

still no transparency. Can’t get a simple color to be transluscent either


                        GL11.glDisable(GL11.GL_TEXTURE_2D);
                        GL11.glColor4f(1.0f, 0.4f, 0.3f, 0.2f);
                        shield.draw(1.0f, 20, 16);
                        GL11.glEnable(GL11.GL_TEXTURE_2D);
                        GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

Is just solid orange still…I am obviously missing something larger here. :slight_smile:

Did you glEnable(GL_BLENDING) and set a suitable blend fuction up via glBlendFunc?

Thanks OT, had this:

GL11.glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE);

but not this:

GL11.glEnable(GL11.GL_BLEND);

Now my shield sphere is translucent!!!

You guys rock!