Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
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  
  2D Sprite Engine  (Read 2031 times)
0 Members and 1 Guest are viewing this topic.
Offline veediot

Junior Newbie




Java games rock!


« Posted 2003-10-17 02:36:16 »

Hello all,

I'm working on a 2D sprite engine using the JOGL and I've come across some weird problems. I realize this is probably more of an OpenGL question, but I thought maybe some of you might have some experience with this.

Basically, what I am doing is using texture mapped quads to draw the sprites. Also, it should be noted that I am reading in a png image into the GL_RGBA format so I can have transparency.

Now, here are the two problems I have been having:

1.) For some reason, the texture mapped quad won't even show up unless I specify a color like so:
gl.glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
before drawing the quad.

2.) When I move the quad around the screen using the translate function, the very edge of the non-transparent area of the texture fades in and out.

I know I am probably making some fairly stupid mistake. Anyone have any ideas just from my general description?

Offline kevglass

JGO Kernel


Medals: 164
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2003-10-17 04:45:36 »

Not sure what could be going on to be honest, the colour thing is fairly normal but the fading is wierd, its not just a laptop screen or something is it?

You might consider looking at the GL Impl of J2DA (http://j2da.sf.net) to see if you're doing anything different.

Kev

Offline Guoshima

Junior Member




Java games rock! Do they .. ?


« Reply #2 - Posted 2003-10-17 06:41:45 »

Don't know but the transparent part and so in a PNG doesn't always work to will to my knowledge so if I need to use transparency in my textures I always use masking for better effect. You can get a good explenation at www.gametutorials.com -> OpenGL tutorials and then Masking. It's C++, but of coarse the OpenGL is still the same.

Link:
http://www.gametutorials.com/Tutorials/opengl/OpenGL_Pg2.htm

here somewhere near the bottom: "Masking And Ortho Mode"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Preston

Senior Member


Medals: 4



« Reply #3 - Posted 2003-10-17 10:25:07 »

Quote
Don't know but the transparent part and so in a PNG doesn't always work to will to my knowledge

If with "transparent" you mean alpha transparency then PNG works perfectly for RGBA textures.
Since Java can load such PNG pictures directly they're pretty handy for JOGL, too. :-)
Offline Guoshima

Junior Member




Java games rock! Do they .. ?


« Reply #4 - Posted 2003-10-17 11:52:48 »

hmm .. have to check it out then. Come from C++ OpenGL and there you always needed the masking stuff. Good tip !
Offline Guoshima

Junior Member




Java games rock! Do they .. ?


« Reply #5 - Posted 2003-10-17 12:11:19 »

gives me some strange stuff, but I make a small mistake somewhere probably.

Do you need to use blending, and is so, which Blend function .. ?

I use the TextureReader from Pepijn Van Eeckhoudt in common, as follows:

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  
private void loadRGBAGLTextures(GL gl, int textureID, String textureName)
            throws IOException {

            int[] temp = new int[1]; // create blank
           gl.glGenTextures(1, temp); // fill with proper gl texture id
           texturesID[textureID] = temp[0];

            // store gl texture id in correct slot of global array.
           TextureReader.Texture texture = TextureReader.readTexture(textureName,true);

            //Create Linear Filtered Texture
           gl.glBindTexture(GL.GL_TEXTURE_2D, texturesID[textureID]);
            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());

      }


presume this is correct .. ?
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #6 - Posted 2003-10-17 12:32:08 »

Quote
hmm .. have to check it out then. Come from C++ OpenGL and there you always needed the masking stuff. Good tip !


Ugh, that masking example is pretty hacky, its wasting time drawing things twice when it could be done with a single texture Shocked

What its effectly doing is storing the RGB in one texture (with the crosshairs) and the alpha in the other (as a greyscale). If instead you stored them both in a single .png file then you could do it in one step with the right blend mode. ( GL_SRC_ALPHA and GL_ONE off the top of my head).

You do loose a slight bit of flexibility (you might want to scroll the mask one way and the image the other for example) but thats really not needed here.

veediot: Are you blending your sprite when drawing it? Sounds like normal behaviour if you are. If you want a hard edge then alpha testing may be more appropriate (although you'd have to add an alpha channel if you've not already got one). Whats your current blend func & state for your sprites?

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline veediot

Junior Newbie




Java games rock!


« Reply #7 - Posted 2003-10-17 14:51:18 »

Okay, here is what I am doing:

To save the png, I am using Photoshop. I have a transparent area in the image in photoshop, so I am not creating a seperate alpha channel, then I am using the save for web feature to save it as a png-24 with transparency checked (which should store an alpha channel then for the transparency, correct?) Because the transparent area does appear transparent inside my app, so I'm assuming there's nothing wrong with the way I am handling the image.  The texture is 64x64, btw.

Here is how I am initing OpenGL:

1  
2  
3  
4  
5  
6  
gl.glBlendFunc (GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable (GL.GL_BLEND);
gl.glEnable(GL.GL_TEXTURE_2D);                        
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);                  // Black Background
gl.glColor3f( 0.0f, 0.0f, 0.0f );
gl.glPointSize(4.0f);


I also tried turning off blending, and enabling alpha testing in this way:

1  
2  
gl.glAlphaFunc(gl.GL_LESS, 1);
gl.glEnable(gl.GL_ALPHA_TEST);


However, when I did that, only the outside edge of the non-transparent part of the image was showing up (but it was still fading in and out)

This is how I setup the texture:

1  
2  
3  
4  
5  
6  
  gl.glBindTexture(GL.GL_TEXTURE_2D, texture[0]);
            gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);
            gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR);
            gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, 4, imageWidth,
                           imageHeight, 0, GL.GL_RGBA,
                          GL.GL_UNSIGNED_BYTE, imageRGBA);


Notice the use of GL_RGBA here. imageRGBA is the data from the image loaded using the same method used by the "Jumping into JOGL" tutorial, using the Image toolkit.

And finally, here is how I attempt to draw a sprite:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
      gl.glPushMatrix();

        gl.glTranslatef(x_loc,y_loc,0.0f);
        gl.glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
        gl.glBindTexture(GL.GL_TEXTURE_2D, texture[0]);

        gl.glBegin (GL.GL_POLYGON);
        gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex2i (0, 0);
        gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex2i (63, 0);
        gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex2i (63, 63);
        gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex2i (0, 63);
        gl.glEnd();    

            gl.glPopMatrix();


That having been said, does that give anyone any more clues as to what I may be doing wrong to get this fading in and out business around the edge?

I appreciate all your help!

-veediot!

Offline tom
« Reply #8 - Posted 2003-10-17 16:15:04 »

Difficult to know whats going on without seeing it.

One tip is to floor() your x_loc and y_loc values. You wont get sub pixel accuracy if your texture is not scaled anyway.

I use blending and the alpha test:
GL.glAlphaFunc(GL.GL_GREATER, 0);
But I don't think it matters.

Any reason why you clear your backbuffer alpha to 0.5?

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.

BurntPizza (9 views)
2014-09-21 02:42:18

BurntPizza (9 views)
2014-09-21 01:30:30

BurntPizza (12 views)
2014-09-21 00:34:41

moogie (13 views)
2014-09-21 00:26:15

UprightPath (23 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

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

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

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

Tekkerue (49 views)
2014-09-09 02:24:56
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!