Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Transparent Sprite?  (Read 877 times)
0 Members and 1 Guest are viewing this topic.
Offline SpuTTer

Senior Member


Medals: 1
Exp: 14 years


Lazy Middle Class Intellectual


« Posted 2003-11-16 23:00:56 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
                        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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
      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!

Sacramento Volleyball
"Whitty phrase goes here."
Offline SpuTTer

Senior Member


Medals: 1
Exp: 14 years


Lazy Middle Class Intellectual


« Reply #1 - Posted 2003-11-17 17:01:43 »

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

1  
2  
3  
4  
//      BufferedImage.TYPE_3BYTE_BGR);

to
      BufferedImage.TYPE_4BYTE_ABGR);


solved it.

Sacramento Volleyball
"Whitty phrase goes here."
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.

Dwinin (20 views)
2014-09-12 09:08:26

Norakomi (55 views)
2014-09-10 13:57:51

TehJavaDev (64 views)
2014-09-10 06:39:09

Tekkerue (32 views)
2014-09-09 02:24:56

mitcheeb (54 views)
2014-09-08 06:06:29

BurntPizza (38 views)
2014-09-07 01:13:42

Longarmx (24 views)
2014-09-07 01:12:14

Longarmx (28 views)
2014-09-07 01:11:22

Longarmx (28 views)
2014-09-07 01:10:19

mitcheeb (36 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!