Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (540)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  Loading Alpha TGA for font rendering  (Read 1870 times)
0 Members and 1 Guest are viewing this topic.
Offline ap_kelly

Junior Devvie




Java rocks!


« Posted 2003-05-24 05:47:23 »

Now I know this topic has been covered quite a bit, and I've done several searches on this forum and others, but I still can't get this quite right. I've managed it with PNG files without an alpha.

Here is my image that I want to use (it's been borrowed from Quake till I design my own).
http://users.bigpond.net.au/ap_kelly/projects/screenshots/bigchars.tga

Here is the code I use to init GL
1  
        gl = new GL(this.getClass().getName(), 50, 50, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_COLOUR_DEPTH, 1, 0, 0);


Here is my loading code.
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  
  private static int _load32bitImage(Image image) {
    BufferedImage tex = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
    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());
    int scratchAddress = Sys.getDirectBufferAddress(scratch);

    byte data[] = (byte[])tex.getRaster().getDataElements(0, 0, tex.getWidth(), tex.getHeight(), null);
    scratch.clear();
    scratch.put(data);

      // Create A IntBuffer For Image Address In Memory            
    IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
    int bufPtr = Sys.getDirectBufferAddress(buf);                  // Get Image Address
    gl.genTextures(1, bufPtr);                                                // Create Texture In OpenGL            

    gl.bindTexture(GL.TEXTURE_2D, buf.get(0));                        // Typical Texture Generation Using Data From The Image

    // Linear Filtering
    gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR);
    gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR);

    // Generate The Texture
    gl.texImage2D(GL.TEXTURE_2D, 0, GL.RGBA, tex.getWidth(), tex.getHeight(), 0, GL.RGBA, GL.UNSIGNED_BYTE, scratchAddress);

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



Here is my rendering code.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
  public static void print(int pX, int pY, String pMsg, int pFontset) {
    if (!initialised) {
      System.out.println("FontUtils.init(GL pGL, GLU pGLU) must be called first.");
    } else {
      gl.enable(GL.TEXTURE_2D);
      gl.enable(GL.BLEND);
      gl.enable(GL.ALPHA_TEST);
      gl.blendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);
//      gl.blendFunc(GL.SRC_ALPHA, GL.ONE);
//      gl.blendFunc(GL.ONE, GL.ONE);
      gl.bindTexture(GL.TEXTURE_2D, fontTextures[pFontset]);
      gl.translated(pX, pY, 0);

      gl.listBase(fontSets[pFontset]);

      ByteBuffer scratch = ByteBuffer.allocateDirect(pMsg.getBytes().length);
      scratch.put(pMsg.getBytes());
      gl.callLists(pMsg.length(), GL.BYTE, Sys.getDirectBufferAddress(scratch));
//      gl.translated(-(pX + (pMsg.length() * FONT_WIDTH)), -pY, 0);
      gl.disable(GL.ALPHA_TEST);
      gl.disable(GL.BLEND);
      gl.disable(GL.TEXTURE_2D);
    }
  }


All I get is white boxes on the screen rather than the text with transparent/alpha blended backgrounds.

Any advice would be appreciated.

Andy.

Offline princec

« JGO Spiffy Duke »


Medals: 437
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2003-05-24 09:34:12 »

You haven't specified the alpha test value. Besides, you don't even need to do alpha testing for fonts. Disable it. Alpha testing is only for when you're trying to do depth buffer writes - for example, when you're rendering "leaves" textures for drawing trees.

Cas Smiley

Offline ap_kelly

Junior Devvie




Java rocks!


« Reply #2 - Posted 2003-05-24 10:31:45 »

Then how do I get transparent backgrounds on my fonts if I don't use Alpha blending?

At the moment with my PNGs I get a black background all the time and the gl.colour3f method changes the text colour. I'm looking for a solution to get rid of the black backgrounds and reveal the colour underneath.

Thanks,

Andy.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline elias

Senior Devvie





« Reply #3 - Posted 2003-05-24 11:31:36 »

It sounds like you forgot to render the font _after_ all your other rendering. Also remember to disable the zbuffer when rendering the font or simply render the quads with smaller Z values.

- elias

Offline ap_kelly

Junior Devvie




Java rocks!


« Reply #4 - Posted 2003-05-24 21:52:43 »

I setup my ortho view in the following way

1  
2  
3  
4  
5  
6  
7  
8  
    pGL.disable(GL.DEPTH_TEST);                                          // Disables Depth Testing
    pGL.matrixMode(GL.PROJECTION);                                    // Select The Projection Matrix
    pGL.pushMatrix();                                                            // Store The Projection Matrix
    pGL.loadIdentity();                                                      // Reset The Projection Matrix
    pGL.ortho(0, 800, 0, 600, -1, 1);
    pGL.matrixMode(GL.MODELVIEW);                                          // Select The Modelview Matrix
    pGL.pushMatrix();                                                            // Store The Modelview Matrix
    pGL.loadIdentity();                                                      // Reset The Modelview Matrix


My fonts are rendered on top of my grey button quads, when I use my 24bit png file to do this the fonts are rendered ok, but the background of the font is black, I want the background of my font to be transparent so that my grey button quads show through. That is why I switched to 32bit tga files cos I thought using the alpha channel would solve my problem.

How do you render a font with transparancy so that what is underneath the text shows through? Code examples or pointers to code would be good. I've looked on Chmans site and through the ported NeHe stuff but I haven't  found what I'm looking for.

Here is an example of it almost working with my png file. I'd like the grey of the boxes to show through behind the text.


Here it is not working with my tga file.


Thanks,

Andy.

Offline ap_kelly

Junior Devvie




Java rocks!


« Reply #5 - Posted 2003-05-25 00:15:54 »

I think what I might be asking is this, the Alpha channel of my font contains a mask, how do I go about using this with my font, to give me transparent backgrounds?

Thanks,

Andy.

Offline ap_kelly

Junior Devvie




Java rocks!


« Reply #6 - Posted 2003-05-25 02:40:23 »

After much chopping and changing around of code trying all sorts of ideas, rereading NeHe #17 I managed to get this output with my original 24bit png.



Unfortunately the colours seem a little washed out/pale, the word 'preferences' should be bright red.

Any thoughts?

Thanks,

Andy.

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #7 - Posted 2003-05-25 09:23:19 »

You're rendering your text onto a grey quad - texture colours and vertex colours get multiplied/modulated by default. Either change your vertex colour to white, or change the texture mode to DECAL (i think it's glTexEnv*() with GL_DECAL you want).

To use the alpha channel, you'll want something like:
gl.enable(GL.BLEND);
gl.blendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);

Edit: actually, seems like you've already done that if the grey quads are separate ones sitting behind. In that case check that the alpha values where there is supposed to be solid text are actually 1, or if they are becomming transparent and blending onto the the quad.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
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.

Mr.CodeIt (23 views)
2014-12-23 03:34:11

rwatson462 (53 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (85 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (78 views)
2014-12-05 12:39:02

SHC (89 views)
2014-12-03 16:27:13

CopyableCougar4 (94 views)
2014-11-29 21:32:03

toopeicgaming1999 (155 views)
2014-11-26 15:22:04

toopeicgaming1999 (151 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!