Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (732)
Games in Android Showcase (221)
games submitted by our members
Games in WIP (806)
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  
  Texture Packing, is there any point in doing it manually?  (Read 2445 times)
0 Members and 1 Guest are viewing this topic.
Offline 0gleth0rpe

Junior Newbie

« Posted 2009-01-11 20:19:16 »

Edit: Disclaimer - I just tried running the test on my year old gaming rig, and it performs at 280 fps. Even with 8000 items, it runs smooth. This is the kind of performance I like Smiley
So I suppose there's much blame to be passed to mobile graphics cards, or drivers, or vista, or what have you. But still, if you have any advice to give, shoot! I'd really like this to run well on any decent computer.


I just spent much of my Sunday implementing a seamless (somewhat) texture-packing utility into my texture-manager and renderer,
thinking it could improve performance significantly since I'm doing 2D graphics, and draw thousands of small billboards while binding textures continuously.

The idea is of course to stuff many images into the same 1024x1024 texture and point to different coordinates at render.

But putting it to work, I don't notice any difference in performance at all. Is this a legacy idea that binding is expensive? Or is it already taken care of by the JOGL API?

I'm using com.sun.opengl.util.texture.Texture by the way which does say "For best performance, try to avoid calling enable() / bind() / disable() any more than necessary." in the API.. perhaps it's still insignificant compared to matrix-operating on RGBA images over and over. Who knows Smiley

Slightly disappointing, but this is how it goes sometimes,

Offline 0gleth0rpe

Junior Newbie

« Reply #1 - Posted 2009-01-11 21:07:26 »

Here's a screenshot and a description to give an idea of what I'm doing.
In the following shot I get ~50 fps with about 1400 vegetation sprites rendered.
The computer is a Precision M90, so it's a fairly recent laptop.

My rendering method looks like this
public void drawPackedTexture(float x, float y, float w, float h, PackedTexture texture, 
         Alignment align, BlendMode mode, float[] rgb, TransformMatrix pMatrix) {
      y = height - y;
      float[][] c = getAlignmentOffset(align);
      Rectangle2D.Float bounds = texture.getBounds();

      gl.glTranslatef(x, y, 0.0f);
      gl.glScalef(w, h, 1.0f);
      // gl.glRotatef(0, 0.0f, 0.0f, 1.0f);
      if (pMatrix != null) {
         gl.glMultMatrixf(pMatrix.getMatrix(), 0);
      gl.glColor4f(rgb[0], rgb[1], rgb[2], rgb[3]);
      gl.glTexCoord2f(bounds.x, bounds.y);
      gl.glVertex2f(c[0][0], c[1][0]);
      gl.glTexCoord2f(bounds.x + bounds.width, bounds.y);
      gl.glVertex2f(c[0][1], c[1][0]);
      gl.glTexCoord2f(bounds.x + bounds.width, bounds.y + bounds.height);
      gl.glVertex2f(c[0][1], c[1][1]);
      gl.glTexCoord2f(bounds.x, bounds.y + bounds.height);
      gl.glVertex2f(c[0][0], c[1][1]);

Where bindtexture avoids binding already present textureIds. Transform matrix is used to shear the top of the grass, so that it moves in the wind.
Right now, it only needs to bind one texture for all the vegetation.

Anyway, that's about what I can think of.

Thank you for reading.

Offline Chris61182

Junior Devvie

« Reply #2 - Posted 2009-01-12 07:25:37 »

But putting it to work, I don't notice any difference in performance at all. Is this a legacy idea that binding is expensive? Or is it already taken care of by the JOGL API?

While I haven't spent the time to benchmark it I'd guess it's still a relatively relevant issue, as there's an extension to help reduce the amount of binding needed,

It was even promoted to core in 3.0
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline anarchotron

Junior Devvie

...precious bodily fluids.

« Reply #3 - Posted 2009-01-13 03:54:59 »

If you are having performance issues I would also look at trying to convert your immediate-mode rendering into vertex arrays or vertex buffers.  Also I would get rid of that GL_POLYGON and make it a triangle strip instead.

Immediate mode is slower full-stop but right now you are putting down to opengl 32 floats for each polygon.

+ 6 for translate/scale
+16 for the shear matrix.

So right now ignoring everything else you are sending down 54 floats per polygon, plus some additional overhead for pushing and popping matrices, setting polygon mode, etc...

Now just sending down floats isn't necessarily the big deal but it is an indicator.

First off you can combine the translate/scale/shear into a single matrix so you only need 16 floats to specify the modelview matrix transform.

Secondly if you go back to using 1 texture per image, then you can make a single vertex buffer object that works for all your stuff:

uv=(0,1)    uv=(1,1)
vt=(0,1)    vt=(0,1)

uv=(0,0)    uv=(1,0)
vt=(0,0)    vt=(0,0)

Set it up like this:

Then render it like this:

Do this once per frame:

glBindBuffer(GL_ARRAY_BUFFER, floatBufferOfVerts);
glVertexPointer(2, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, floatBufferOfUvs);
glTexCoordPointer(2, GL_FLOAT, 0, 0);

for each of your visible textures, do this:

  glBindTexture(GL_TEXTURE_2D, textured)

  for each of the quads of that texture, do this:

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

That way you end up with a far more optimized render strategy.  You only send down 16 floats per quad, + the glDrawArrays() command.

Furthermore, if your vegetables are all blowing the same way in the wind (the shear matrix is identical), you could get try loading that matrix at the beginning of the frame and then just translating/scaling against it for each quad.

This is all just pseudocode off the top of my head but I think it is a more performant way to render what you are doing.  An important feature of what I wrote is that you have sorted all your renders by texture in advance so you only have as many texture binds as you have textures, no wasted effort there.  Hope that helps.

At the very least try just converting your immediate-mode renders to vertex buffers and see if that helps.


Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Archive (403 views)
2017-04-27 17:45:51

buddyBro (588 views)
2017-04-05 03:38:00

CopyableCougar4 (1045 views)
2017-03-24 15:39:42

theagentd (1076 views)
2017-03-24 15:32:08

Rule (1052 views)
2017-03-19 12:43:22

Rule (1035 views)
2017-03-19 12:42:17

Rule (1026 views)
2017-03-19 12:36:21

theagentd (1165 views)
2017-03-16 05:07:07

theagentd (1108 views)
2017-03-15 22:37:06

theagentd (834 views)
2017-03-15 22:32:18
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51 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‑
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!