Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Transparency  (Read 750 times)
0 Members and 1 Guest are viewing this topic.
Offline sinsro

Senior Newbie




Java games rock!


« Posted 2004-09-30 09:40:03 »

I am trying to render a quad with transparency in it.

This is my test-texture, a PNG image with alpha info:

http://img.photobucket.com/albums/v463/sinsro/test24.png

And this is what I get rendering it:

http://img.photobucket.com/albums/v463/sinsro/screen.jpg

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);
           //Create Nearest Filtered Texture
           gl.glBindTexture(GL.GL_TEXTURE_2D, textures);

           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;
       }
   }
}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (35 views)
2014-04-15 18:08:23

BurntPizza (31 views)
2014-04-15 03:46:01

UprightPath (46 views)
2014-04-14 17:39:50

UprightPath (29 views)
2014-04-14 17:35:47

Porlus (46 views)
2014-04-14 15:48:38

tom_mai78101 (67 views)
2014-04-10 04:04:31

BurntPizza (127 views)
2014-04-08 23:06:04

tom_mai78101 (227 views)
2014-04-05 13:34:39

trollwarrior1 (192 views)
2014-04-04 12:06:45

CJLetsGame (199 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!