Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Doing 2d with 3d (OpenGl/Xith/Java3d)  (Read 4984 times)
0 Members and 1 Guest are viewing this topic.
Offline Bombadil

Senior Member





« Posted 2004-05-03 08:45:28 »

I'd like to use OpenGL (in the form of Xith which bases on JOGL in my case) to draw 2d sprites for a fast game, so that it's all HW accelerated and I can use 8bpp alpha blending, blended zooming and rotating etc.
For example all objects contain a planar rectangle (or 2 triangles) in a Shape3D with a texture. Let's call this sprite. The sprite's Z value is zero (usually).

A Xith user suggested not to use perspective projection because then it's difficult to control the zoom factor, but to use PARALLEL_PROJECTION. Then use scaling for zoom effects. This works.
However, I couldn't figure out how to set the rectangles/triangles vertex coordinate dimension compared to the texture and screen resoltion, so that on screen one pixel of the texture maps to one pixel on screen. Is this possible somehow?

Is there a beginner's guide how to use 2d (with or without Parallel_Projection) with Jogl/Xith/Java3d maybe? Thanks!


Part b) of the question: Many textures. I intend to use many textures because most of the sprites will be animated. What would be the fastest and most VRAM efficient method to swap textures in OpenGL (for an animated 2d sprites) ?


PS: Since this question probably applies to Jogl, Java3d and Xith, I posted it here.
Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2004-05-03 09:29:46 »

You'll find buried in the source code of Alien Flux that perspective projection is really really easy to set up such that it's correct when z=0. Altering z is a billion times easier than scaling it yourself.

In fact to save you the bother of searching...
1  
2  
3  
4  
5  
6  
7  
            GL11.glFrustum(
                  - Game.WIDTH / 64.0,
                  Game.WIDTH / 64.0,
                  - Game.HEIGHT / 64.0,
                  Game.HEIGHT / 64.0,
                  8,
                  65536);
initialises the projection matrix and
1  
            GL11.glTranslatef(-Game.WIDTH / 2.0f, -Game.HEIGHT / 2.0f, -256);
initialises the modelview matrix so that 0,0,0 is in the bottom left of the screen.

Cas Smiley

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #2 - Posted 2004-05-03 11:45:42 »

Quote
Part b) of the question: Many textures. I intend to use many textures because most of the sprites will be animated. What would be the fastest and most VRAM efficient method to swap textures in OpenGL (for an animated 2d sprites) ?


Put all animation frames in one texture and only change texture coordinates for each animation frame instead of having lots of textures and binding a texture for each animation frame.

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

Senior Member





« Reply #3 - Posted 2004-05-03 13:26:31 »

Thanks Cas and Erik.
Putting the animation in one texture is a good idea. :-)  In case they won't fit to 1024x1024, I've to start a few textures, but better than hundreds.

Using a Frustum with perspecitve projection sounds also nice. Still I don't get the point why you (Cas) do a "div 64.0" and why you choose 8, 65536 as far and near values of the frustum?
Also how do you ensure your "sprite" polygon's vertex coordinates are so that the mapped texture has a 1:1 zoom ratio? For example in case your texture is 1024x1024 pixel sized: which polygon vertex coordinates do you have to choose (1024, 1024) ?

OpenGL Redbook, Perspective Viewing Volume Specified by glFrustum()
http://www.parallab.uib.no/SGI_bookshelves/SGI_Developer/books/OpenGL_PG/sgi_html/figures/chap3-5.gif
Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #4 - Posted 2004-05-03 20:04:53 »

Just use normal 2D coordinates like you would anywhere else. Just try it and see.

As for those values... I worked them out in my head at the time and can't quite remember the sums but It Just Works.

Cas Smiley

Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #5 - Posted 2004-05-03 20:15:21 »

Doing 2D with Xith is a bit overkill. It's a scenegraph api - ment to do neat 3d enviroments. In a 2D game the "scene" is just way to simplistic.

So... use either lwjgl or jogl. Take a look at Kev's OpenGL space invaders tutorial game (jogl) and you'll see it isn't that difficult. Setup the perspective, put some ogl stuff into methods et voila - pure 2D stuff.

弾幕 ☆ @mahonnaiseblog
Offline Bombadil

Senior Member





« Reply #6 - Posted 2004-05-04 13:06:13 »

Thanks again for your hints.
So Cas suggests to use Perspecitve_Projection, while Onyx' hint to Kevin's tutorial uses Parallel_Projection, like the OpenGL FAQ suggests: OpenGL Technical FAQ, 9 Transformations, section "9.030 How do I draw 2D controls over my 3D rendering?".

With an OpenGL binding this should work. However, I still would like to use Xith if possible, because I'm more familiar with it compared to pure OpenGL. Well, let's see. :-)
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #7 - Posted 2004-05-04 13:42:38 »

Heh... you asked for 2d - so ortho Wink

Oh and don't panic, you won't need much opengl and as I already pointed out, after a bit capsulation it's just like using a neat 2D api. Kev's tutorial should give you a good starting point Smiley

弾幕 ☆ @mahonnaiseblog
Offline Bombadil

Senior Member





« Reply #8 - Posted 2004-05-04 14:17:06 »

Quote
Heh... you asked for 2d - so ortho ;)

Yes, it's been my first thought, too, and since the OpenGL FAQ suggests to use that, too, I'm problably using it, too.
However, Cas suggests to use perspective projection and well, he's the man who wrote a commercial 2d with 3d-HW Java game. Btw it's been mentioned on the OpenGL.org portal some days ago - really nice!

Quote
Oh and don't panic, you won't need much opengl and as I already pointed out, after a bit capsulation it's just like using a neat 2D api. Kev's tutorial should give you a good starting point :)

Yes, it's nice, I've taken a look at (Kevin should publish books, hehe).
Offline Bombadil

Senior Member





« Reply #9 - Posted 2004-05-10 17:48:35 »

OK, so now with the nice Jogl I draw those planar quad-polygon "sprites" on screen, usually with Z=0 and all in parallel projection.
However, there will be several layers of sprites, like background, normal, front, on-top, etc.

Probably I could sort of "sort the sprites" before drawing, because since it's no real 3d game, drawing them from back to front isn't too difficult (much simpler than with isometric 3d-2d, hehe).
Wait... there's this nice depth buffer thing in OpenGL we all know and love (and missed a lot back in the retro gaming days). Should I use it for a 2d game? Of course the HW on the 3d card has to do extra bits then like clearing the buffer every frame, and for any texel to be written, it need compare it to the buffer etc.

So... is it overkill to use the z-buffer? Or does it "really just" disburden my valuable CPU time?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #10 - Posted 2004-05-10 18:46:45 »

No - do NOT use the depth buffer.

Cas Smiley

Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #11 - Posted 2004-05-10 22:38:30 »

It would waste cpu power and besides that it will lead to a major headache. Z is always 0 -> z-fight massacre Grin

In most 2d games you wouldn't need any sorting. The order is always the same: background, background tiles, items, enemies, player, bullets/particles, hud - just build your loop for drawing it in that order and everything is ok Smiley

Also if you draw everything (the whole screen) all the time, you can disable clearing (that will result in about +3% speed on older hardware).

弾幕 ☆ @mahonnaiseblog
Offline cfmdobbie

Senior Member


Medals: 1


Who, me?


« Reply #12 - Posted 2004-05-11 00:21:04 »

Quote
Also if you draw everything (the whole screen) all the time, you can disable clearing (that will result in about +3% speed on older hardware).


John Carmack used a neat z-buffer trick on Quake 2.  As he knew was redrawing the whole screen every frame, he never cleared the buffer, only used half the precision, and flipped the depth-test direction every frame.  Nice!

A bit convoluted, but I guess if he thought it was worth doing it must have been - on machines of that era, at least.

But yes, for a 2D game the z-buffer will likely cause you more grief than help, unless you've got very good reason for using it.  Your sprites are likely to be so small compared to the background that you wouldn't save much overdraw even if you did take full advantage of the z-buffer.

Hellomynameis Charlie Dobbie.
Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #13 - Posted 2004-05-11 08:31:25 »

The real reason for not using the depth buffer is that you will then no longer be able to blend antialiased sprites (and fonts) with the background, as you'll have to use the alpha test. Result == ugly as hell.

Cas Smiley

Offline Bombadil

Senior Member





« Reply #14 - Posted 2004-05-11 09:04:05 »

Quote
The real reason for not using the depth buffer is that you will then no longer be able to blend antialiased sprites (and fonts) with the background, as you'll have to use the alpha test. Result == ugly as hell.

Oh... Now I think this confuses me a bit.

My sprites are in RGBA format. The alpha layer is 8bpp because in future they will be anti-aliased like in your example. Currently I use alpha 255 for visible sprite pixels and alpha 0 for invislbe sprite pixels.
To make this work with full alpha range (in future) I've to enable blending and use a BlendFunction, isn't it? So I do:
Quote

gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

Well, it looks like it works, however I can't see a difference with or without depth buffer...

Maybe it's another topic when I don't use blending, but just alpha testing? Like with this
Quote

gl.glEnable(GL.GL_ALPHA_TEST);
gl.glAlphaFunc(GL.GL_GREATER, 0);

Then the sprite's edges are sharp (not blended) but I don't think I like the result...
Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #15 - Posted 2004-05-11 09:13:57 »

That's because you're not using any intermediate alpha values.

Anyway, it's a bit slower and needs more video RAM. So don't Tongue

Cas  Smiley

Offline Bombadil

Senior Member





« Reply #16 - Posted 2004-05-11 12:05:09 »

Quote
That's because you're not using any intermediate alpha values.

Ah, I see, it's because currently I use an alpha layer just consting of 0's and 255's.

Quote
Anyway, it's a bit slower and needs more video RAM. So don't :P

So... do you suggest to use a 2 bit alpha mask for the sprites? And not use blending but glAlphaFunc instead? Or maybe I get you wrong - lost the .. context I'm afraid. :-)
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #17 - Posted 2004-05-11 12:15:54 »

Dunno if its possible to use an RGBA image with only two bits of alpha (though I think one of the texture compression modes allows it). But alpha testing rather than blending will be much faster for the same results - a single comparison instead of two multiplies, and thats before you start worrying about the framebuffer reads needed.

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

Senior Member





« Reply #18 - Posted 2004-05-21 09:47:07 »

Quote
But alpha testing rather than blending will be much faster for the same results - a single comparison instead of two multiplies, and thats before you start worrying about the framebuffer reads needed.

I see. :)
Offline Bombadil

Senior Member





« Reply #19 - Posted 2004-05-21 09:51:34 »

Quote
Put all animation frames in one texture and only change texture coordinates for each animation frame instead of having lots of textures and binding a texture for each animation frame.

Well then, say for each frame of a sprite you've got a bitmap file on your harddisc.
Is there maybe a nice editor/tool which would paste the bitmap files into one big texture (or more, in case 512x512 or 1024x1024 can't hold all animation frames) and produce the needed UV float values?
For a start this tool could place the frames regularly on the texture, but in case it's really clever it could pack the differently sized frames so close next to each other on the texture so that you save (3d-card) memory during runtime...

Since there are many 2d games using 3d hardware out there, I wonder if such a cool tool exists, maybe... :-)
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #20 - Posted 2004-05-21 10:11:14 »

Cas' image packer in the SPGL libs will do that for you, and spit out an image + xml describing sprite positions. However its pretty closely tied to the whole SPGL sprite system, so it might not be useful straight away. At the very least the source might give you a few hints for writing your own.

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

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #21 - Posted 2004-05-21 12:34:45 »

Yeah, it's easy to chop out the guts and make it do what you want.

Cas Smiley

Offline Bombadil

Senior Member





« Reply #22 - Posted 2004-05-21 13:39:10 »

Thanks Orangy Tang & Cas. I'll have a look at.
However when I go on http://www.lwjgl.org/links.php and click to Spgl on Sourceforge, there's nothing to download...? :-)
Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #23 - Posted 2004-05-21 15:20:14 »

'srite, you need to use CVS to browse the source. There is no SPGL build - it's a window onto my real working code. So it breaks frequently.

Cas Smiley

Offline Bombadil

Senior Member





« Reply #24 - Posted 2004-06-01 18:49:49 »

Say you intend to use many animations and (texture) memory on the 3d card could become a problem...
Is it advisable to use 8bpp textures (with an own palette for each) in the game? (Still I would need 8bpp for the picture and 8bpp for the alpha.)

Can OpenGL handle such 8bpp textures + palette smartly? When I read the OpenGL red book there's the difference between RGB(A) mode and a so called color indexed 8 bpp mode... but I guess this means the bpp onscreen, not only the textures...?

What do you OpenGL experts say...? :-)

A friend tells me that several commercial games today use a kind of 8 bpp texture (with own color indexed) and that with a resolution of >= 1024x768 it's hard for the human eye to distinguish 8bpp from 24bpp textures.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #25 - Posted 2004-06-01 19:12:14 »

There was an extension for using 8bit palletised images as textures, but its been depreciated unfortunatly. Sad I doubt many current games use anything less that 16bit textures.

Memory usage isn't too much of a concern, since OpenGL will automatically swap them out to system memory. It might slow things down but its not a catastrophy. You might find better results with one of the texture compression extensions if you've got loads of huge textures.

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

Senior Member





« Reply #26 - Posted 2004-06-02 08:57:02 »

Quote
There was an extension for using 8bit palletised images as textures, but its been depreciated unfortunatly. :(

A pity, but I think your mentioned compression extensions can compensate that.

Quote
Memory usage isn't too much of a concern, since OpenGL will automatically swap them out to system memory. It might slow things down but its not a catastrophy. You might find better results with one of the texture compression extensions if you've got loads of huge textures.

Yes, I'll have to use these, because memory is a concern. Since it's a 2d game (using 3d OpenGL) with hopefully smooth movement, I should avoid to let OpenGL swap texture memory to system memory where possible.

When using S3tc compression for example, my question actually becomes obsolete, because the texture images are always lossy compressed on the fly by OpenGL, or at compile time by an external tool to an S3tc RGB or RGBA format (with various compression ratios).

There's a nice short docu giving an overview of how to use the GL_ARB_texture_compression and GL_EXT_texture_compression_s3tc on the Nvidia site: http://developer.nvidia.com/attach/6585
Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #27 - Posted 2004-06-02 09:31:19 »

Unfortunately with a 2D game you will seriously notice compressed textures. I tried it with Alien Flux - looks awful!

You won't run out of texture RAM, don't worry.

Cas Smiley

Offline Bombadil

Senior Member





« Reply #28 - Posted 2004-06-02 12:20:09 »

Quote
Unfortunately with a 2D game you will seriously notice compressed textures. I tried it with Alien Flux - looks awful!

You mean because of the lossy compression nature of S3tc compression (like with JPEG artefacts) ?
That would be bad, indeed. Since for the 2d game I use mainly artificial pictures in contrast to photographic ones, the artefacts would be noticeable... Well, then maybe compression just as an user's option.

Quote
You won't run out of texture RAM, don't worry.

Well, I'll try to quote some numbers soon.
Offline Bombadil

Senior Member





« Reply #29 - Posted 2004-06-02 12:27:34 »

On that compression topic: do all modern 3d cards support S3tc texture compression? Because when with my Ati 9600, I use glTexImage2D(..) with the internal_format parameter set to GL_COMPRESSED_RGB_ARB (or GL_COMPRESSED_RGBA_ARB for textures with alpha) the result is this:
Using glGetTexLevelParameteriv with GL_TEXTURE_COMPRESSED_ARB says "is compressed" (> 0).
Asking for the GL_TEXTURE_INTERNAL_FORMAT, the returning number says
GL_COMPRESSED_RGB_S3TC_DXT1_EXT, when the input image has been RGB (24bpp),
and
GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, when the input image has been RGBA (32bpp).

So maybe I could do this: when the GL_ARB_texture_compression extension is available and the user has enabled the "use compressed textures" game option, I'm going to use GL_COMPRESSED_RGBA_ARB on the glTexImage2D() call. This would mean: no need to store the textures lossy s3tc compressed on disc but use normal PNGs for both cases: uncompressed and compressed textures at runtime.
So for 3d cards with no texture compression extension, I'd feed them directly to the card, with the result that OpenGL will regularly swap texture mem with sys mem and unavoidable jerkyness would be the result.

So far the theory. Is it going to clash with the practice? :)
Pages: [1] 2
  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.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36

Riven (46 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (32 views)
2014-07-23 20:56:16
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

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

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

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

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

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
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!