Sprite transparency in OpenGL?

Apprentice
Posts: 5
Joined: 2011.12
Post: #1
Drawing sprites on a single textured quad: easy

Drawing sprites with transparency on a single textured quad: JSH*@(GDGDSODGSJDSBGJDSbA}fsa;

Is it even possible to do in OpenGL? Because using the CPU to do alpha testing = slower than dirt.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
Yes. You need to read about blending.

glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2011.12
Post: #3
The idea is to draw multiple sprites on a lone textured quad, with transparency that preserves the existing texture wherever each sprite has pixels with 0 alpha. Unless I've been using them wrong, GL_BLEND and glBlendFunc kind of don't do that.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
Why do you want to do that? Draw each sprite on its own quad (though it's a good idea to put many sprite images into a single texture, aka a "sprite sheet", in order to reduce the number of times you have to call glDrawElements).
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2011.12
Post: #5
Because drawing each sprite on its own quad is crazy inefficient and slow vs. drawing them all on a single quad.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
100% wrong.
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2011.12
Post: #7
100% impossible. Unless we're talking about some weird new definition of time here or I just fell into the Twilight Zone, drawing 30 textured quads can't possibly be quicker than drawing 1 textured quad. It's like saying one iteration through a while() loop is slower than fifty. What the hell?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
Why do you assume that the number of quads is the bottleneck? It almost never will be.

Usually, the bottleneck will be on the GPU, in the number and complexity of pixels filled. That means that many smaller quads will be faster so long as they cover fewer pixels (including the transparent ones!) than the one large quad, and especially so if shading them is vastly simpler, which it should be.

Sometimes, the bottleneck will be the CPU overhead in making many draw calls, hence my advice to use a sprite sheet.

How do you think all the 2D games in OpenGL work?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #9
Most games nowadays draw scenes with hundreds of thousands of triangles. Even on mobile devices you can draw tens of thousands of textured triangles. As OneSadCookie said, vertexes are cheaper than most games care too much about, draw calls and fill rate are not.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 96
Joined: 2011.07
Post: #10
(Dec 13, 2011 03:08 PM)Guest! Wrote:  Drawing sprites on a single textured quad: easy

Drawing sprites with transparency on a single textured quad: JSH*@(GDGDSODGSJDSBGJDSbA}fsa;

Is it even possible to do in OpenGL? Because using the CPU to do alpha testing = slower than dirt.

I think there's a way to use the fragment shader to process data if you can get it to handle the alpha values for you, i.e., input textures into the shader program and read back the data – just a hunch. OpenCL anyone?

respect,
pk

iFrog is coming.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #11
I'm not saying you *can't* use a single quad, though exactly how much you can accomplish with a single quad may depend on hardware limitations, but it would be ridiculous to do so.

OpenCL might be a little better, I guess, but why bother?
Quote this message in a reply
Moderator
Posts: 449
Joined: 2003.08
Post: #12
One quad to rule them all, and in the alpha, blend them!
Alex
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2011.12
Post: #13
So in otherwords, an operation like glTexSubImage2D is so expensive(comparatively anyway) that no matter how it's used, putting each sprite on its own quad is faster? Just inherently?

@ipeku

Thought about that, but Nvidia decided oooh, nooo, the Geforce2MX isn't good enough for fragment shaders...
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #14
Updating textures tends to be very expensive. Sending a few thousand triangles is not.

Again, the expensive part of rendering 2D games is rarely related to the number of vertexes. It's generally state changes (by giving each sprite it's own texture and constantly switching between them), draw calls (by making a draw call for each sprite), or fill rate (the number of pixels that have to be drawn).

So the best way to speed up 2D rendering is to make a sprite sheet by packing lots of sprites onto a single texture. Then you can send a bunch of quads that share the same texture all in one draw call.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 338
Joined: 2004.07
Post: #15
(Dec 20, 2011 04:16 AM)Guest! Wrote:  So in otherwords, an operation like glTexSubImage2D is so expensive(comparatively anyway) that no matter how it's used, putting each sprite on its own quad is faster? Just inherently?

In practice, yeah, pretty much. In all the games I've made (both 2d and 3d) I've always been limited by fill rate or CPU.

Justin Ficarrotta
http://www.justinfic.com
"It is better to be The Man than to work for The Man." - Alexander Seropian
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Sprite game with OpenGL/GLKit InvisibleMoustache 6 8,570 Oct 10, 2012 09:20 AM
Last Post: OneSadCookie
  OpenGL ES 2.0, 2D Alpha Transparency Artifacts Macmenace 3 7,945 Mar 28, 2010 11:18 PM
Last Post: AnotherJake
  libpng transparency problem wyrmmage 3 5,344 Mar 1, 2007 05:53 PM
Last Post: OneSadCookie
  OpenGL Sprite Structure Stalin55 2 4,329 Aug 18, 2006 03:40 PM
Last Post: AnotherJake
  SDL + OpenGL Sprite Loading Problem Greywhind 3 5,041 Jun 29, 2006 04:44 PM
Last Post: OneSadCookie