Mixing OpenGL and 2D

Stupid question but how do I do this?

Ok, I’ll be more specific :slight_smile:
I’ve got a 3D app that requires 2D overlays (menues, status, etc)
If I try and access the SDLSurface for the 2D stuff the app crashes.

I’m guessing you can’t use normal SDL 2D stuff when you’re using
OpenGL, so how do I put on these overlays? A nice simple example
would be helpfull. :slight_smile:

Thanks.

                    -fjr

Go to http://sdldoc.csn.ul.ie/sdlsetvideomode.php and read about
SDL_OPENGLBLIT. When you set up your video with this flag you can use
SDL_UpdateRects to blit from an SDL surface to the GL surface. It works
nicely. Here is an example I wrote to test this video mode:

void testOpenGLBlit()
{
int i;
SDL_Surface *screen = NULL;
initOrQuit(SDL_INIT_VIDEO);
atexit(SDL_Quit);

for (i = 0; i < numBitsPP; i++)
{
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, bitsPR[i]);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, bitsPG[i]);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, bitsPB[i]);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);

//screen = SDL_SetVideoMode(640, 480, bitsPP[i], (SDL_HWSURFACE | SDL_OPENGLBLIT | SDL_FULLSCREEN));
screen = SDL_SetVideoMode(640, 480, bitsPP[i], SDL_OPENGLBLIT | SDL_FULLSCREEN);
//screen = SDL_SetVideoMode(320, 240, bitsPP[i], SDL_OPENGLBLIT);
//screen = SDL_SetVideoMode(320, 240, bitsPP[i], 0);
//screen = SDL_SetVideoMode(640, 480, bitsPP[i], SDL_FULLSCREEN);
//screen = SDL_SetVideoMode(640, 480, bitsPP[i], 0);
//screen = SDL_SetVideoMode(1024, 768, bitsPP[i], SDL_FULLSCREEN);
if (NULL == screen)
{
  printf("Can't set video mode\n");
}
else
{
  printf("Surface -------------\n");
  printSDLSurface(screen);
}

printSDLOpenGLAttrs();
SDL_WM_SetCaption("OpenGL Blit", "Direct Access");

SDL_PixelFormat *pf = screen->format;

Uint32 black = SDL_MapRGB(pf, 0x00, 0x00, 0x00);
Uint32 red   = SDL_MapRGB(pf, 0xff, 0x00, 0x00);
Uint32 green = SDL_MapRGB(pf, 0x00, 0xff, 0x00);
Uint32 blue  = SDL_MapRGB(pf, 0x00, 0x00, 0xff);
Uint32 white = SDL_MapRGB(pf, 0xff, 0xff, 0xff);

SDL_Rect area;

area.x = 0;
area.y = 0;
area.w = screen->w;
area.h = screen->h;

SDL_FillRect(screen, &area, red);
SDL_UpdateRects(screen, 1, &area);
SDL_GL_SwapBuffers();

int frames = 0;
Uint32 start = SDL_GetTicks();
Uint32 duration = 5 * 1000;

Uint8 r = 0;
Uint8 g = 0;
Uint8 b = 0;

while (SDL_GetTicks() < (start + duration))
{
  area.w = screen->w / 20;
  area.h = screen->h / 20;
  area.x = (random() % (screen->w - area.w));
  area.y = (random() % (screen->h - area.h));

  r = random();
  g = random();
  b = random();

  Uint32 color = SDL_MapRGB(pf, r, g, b);

  SDL_FillRect(screen, &area, color);
  SDL_UpdateRects(screen, 1, &area);

  SDL_GL_SwapBuffers();
  frames++;
}

Uint32 elapsed = SDL_GetTicks() - start;
printf("elapsed=%u frames=%d fps=%f\n", 
       elapsed, frames, (((float)frames) / ((float)elapsed)) * 1000);

}
}

I hope that helps. To compile it you will have to do some editing as the
code includes tracing functions that I have not sent you.

“Frank J. Ramsay” wrote:>

Stupid question but how do I do this?

Ok, I’ll be more specific :slight_smile:
I’ve got a 3D app that requires 2D overlays (menues, status, etc)
If I try and access the SDLSurface for the 2D stuff the app crashes.

I’m guessing you can’t use normal SDL 2D stuff when you’re using
OpenGL, so how do I put on these overlays? A nice simple example
would be helpfull. :slight_smile:

Thanks.

                    -fjr

SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


±-----------------------------------+

  • Bob Pendleton is seeking contract +
  • and consulting work. Find out more +
  • at http://www.jump.net/~bobp +
    ±-----------------------------------+

Thanks, this is exactly the info I needed.

      -fjr

Bob Pendleton wrote:

Go to http://sdldoc.csn.ul.ie/sdlsetvideomode.php and read about
SDL_OPENGLBLIT.

Or better don’t. Just forget that it exists

Mattias Engdeg?rd wrote:

Bob Pendleton wrote:

Go to http://sdldoc.csn.ul.ie/sdlsetvideomode.php and read about
SDL_OPENGLBLIT.

Or better don’t. Just forget that it exists

Mattias,

Comments like the one above are non-productive. If you want people to
stop using a feature you need to give some explanation of why they
should stop using it. I’m afraid that your personal opinion, with no
other explanation, is not enough to convince me to pay any attention to
what you say. I need to see the facts to be convinced. I suspect that
that is true for many people on the list.

	Bob Pendleton> 

SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


±-----------------------------------+

  • Bob Pendleton is seeking contract +
  • and consulting work. Find out more +
  • at http://www.jump.net/~bobp +
    ±-----------------------------------+

Bob Pendleton wrote:

Comments like the one above are non-productive.

so is your “advice” if it’s misleading. If you have been reading this
list for more than a few weeks you should know using OPENGLBLIT is a
bad idea, and kept for compatibility. If you are new to the list, you
should not hand out advice that easily

(To be fair, the docs need updating. And the list archives need a search
mechanism. Sam, if you fix the latter I’ll do the former)

Just so people cannot say that I was never nice about OPENGLBLIT,On Mon, Jan 28, 2002 at 03:23:07PM -0500, Frank J. Ramsay wrote:

Thanks, this is exactly the info I needed.

No it’s not. If you use SDL_OPENGLBLIT, I can guarantee that your program
will not work on some people’s hardware and that it will be slow even
where it does work. VERY slow. Essentially, it sucks.

As a fan of the principle of doing it right or not bothering to do it at
all, I highly suggest you note the following two functions, you can find
them and their context in SDL12/test/testgl.c:

void SDL_GL_Enter2DMode()
{
SDL_Surface *screen = SDL_GetVideoSurface();

/* Note, there may be other things you need to change,
   depending on how you have your OpenGL state set up.
*/
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);

/* This allows alpha blending of 2D textures with the scene */
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glViewport(0, 0, screen->w, screen->h);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

}

void SDL_GL_Leave2DMode()
{
glMatrixMode(GL_MODELVIEW);
glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPopMatrix();

glPopAttrib();

}

Simple usage instructions:

  • Draw 3D stuff
  • SDL_GL_Enter2DMode
  • Draw your 2D stuff using glTexCoord2f/glVertex2f
  • SDL_GL_Leave2DMode

The coordinates are actual pixel values of the OpenGL window.
SDL_GL_Enter2DMode sets up OpenGL’s coordinate space to start with 0,0 in
the upper left corner, with single pixel increments. Isn’t that better
than using the often incompatible and exponentially slower SDL_OPENGLBLIT?

Let’s review: SDL_OPENGLBLIT does not always work. It is always slow.
As in, it will cut your framerate down to a fraction of whatever it is
now. It works by taking all the hard work your graphics card put into
rendering your 3D really really fast and copying it off into a 2D buffer
so it can paint on top of it. This takes a very, very long time. Much
longer than any straight 2D blit. A program which gets 90fps can easily
be cut down to 30 as a result of SDL_OPENGLBLIT. DO NOT use it if you
know what’s good for you, and your code.

OTOH, you should already know how to load a texture into OpenGL. Even
NeHe is capible of demonstrating how to do that reasonably well, so it
can’t exactly be a hard thing to do. (Oh c’mon, you guys knew I’d have to
say something about NeHe in here right?) The above functions are pretty
simple to understand if you spend about 20 minutes with the redbook. It
will keep your fps up closer to 90, too. OPENGL good! OPENGLBLIT bad!


Joseph Carter Have chainsaw will travel

I can just see it now: nomination-terrorism :wink:
– Manoj

haha! i nominate manoj.
– seeS

-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 273 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020128/2c31430f/attachment.pgp

— Mattias Engdeg?rd wrote:

Bob Pendleton wrote:

Comments like the one above are non-productive.

so is your “advice” if it’s misleading. If you have been
reading this
list for more than a few weeks you should know using
OPENGLBLIT is a
bad idea, and kept for compatibility. If you are new to the
list, you
should not hand out advice that easily

(To be fair, the docs need updating. And the list archives
need a search
mechanism. Sam, if you fix the latter I’ll do the former)

That would be nice, but until then Google can serve nicely.
Just type “site:libsdl.org any keywords you want” and it’ll
search the sdl site, including the mail archives.=====
Dave Brondsema
dave at brondsema.net
http://www.brondsema.net


Do You Yahoo!?
Great stuff seeking new owners in Yahoo! Auctions!
http://auctions.yahoo.com

Dave Brondsema wrote:

That would be nice, but until then Google can serve nicely.
Just type “site:libsdl.org any keywords you want” and it’ll
search the sdl site, including the mail archives.

Good point. Sam, would it be possible to put this in the FAQ, and if
possible also near the list archives (perhaps even with a link)?

Didn’t it get added to a FAQ or something the last few times it was asked?On Tue, Jan 29, 2002 at 10:26:36AM +0100, Mattias Engdegard wrote:

That would be nice, but until then Google can serve nicely.
Just type “site:libsdl.org any keywords you want” and it’ll
search the sdl site, including the mail archives.

Good point. Sam, would it be possible to put this in the FAQ, and if
possible also near the list archives (perhaps even with a link)?


Joseph Carter What’re you looking at?

is a surgical war where you go give the foreign troops nose jobs?

-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 273 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020129/d1688350/attachment.pgp

SDL_OPENGLBLIT is a hack. It’s an ugly hack. It’s also terribly slow, and
abuse of SDL. Think of your worst nightmare come true, multiply it by itself
a couple of times, and throw in all other people’s worst nightmares as well.
That’s a fraction of how ugly a hack SDL_OPENGLBLIT is. The only reason it’s
in SDL in the first place is that Loki, may she rest in peace, wanted to ease
the porting of some game tool (sim city 3k building architect i think ?).

Anyway, if you want 2D on OpenGL, either use OpenGL directly (see testgl.c in
the test dir in the SDL sources for an example, run ‘./testgl -logo’ to see),
or use David Olofsons glSDL backend (which currently only lives inside the
kobo deluxe engine).

Hope this helps =)On Monday 28. January 2002 21:49, you wrote:

Mattias Engdeg?rd wrote:

Bob Pendleton wrote:

Go to http://sdldoc.csn.ul.ie/sdlsetvideomode.php and read about
SDL_OPENGLBLIT.

Or better don’t. Just forget that it exists

Mattias,

Comments like the one above are non-productive. If you want people to
stop using a feature you need to give some explanation of why they


Trick


Linux User #229006 * http://counter.li.org

“There is no magic” - Nakor, magic user

Dave Brondsema wrote:

That would be nice, but until then Google can serve nicely.
Just type “site:libsdl.org any keywords you want” and it’ll
search the sdl site, including the mail archives.

Good point. Sam, would it be possible to put this in the FAQ, and if
possible also near the list archives (perhaps even with a link)?

Yep. I’m back online at home, so hopefully this weekend I’ll have time
to update the site and update some FAQ entries.

See ya!
-Sam Lantinga, Software Engineer, Blizzard Entertainment

I think this is even better!

Put this on the site…

…and you will give the user a nice clustered search result page.On 2002.01.29 11:26 Mattias Engdegard wrote:

Dave Brondsema wrote:

That would be nice, but until then Google can serve nicely.
Just type “site:libsdl.org any keywords you want” and it’ll
search the sdl site, including the mail archives.

Good point. Sam, would it be possible to put this in the FAQ, and if
possible also near the list archives (perhaps even with a link)?

Hi!
I don’t wanna give an example, but a GL_HINT:
use glDrawPixels() in conjunction with a bitmap containig your
2d-stuff. or even cooler but much harder:
make your 2d-stuff squared and do some texturemapping. this idea should
have come to you by yourself.
St0fF 64

At 11:34 28.01.2002 -0500, you wrote:>Stupid question but how do I do this?

Ok, I’ll be more specific :slight_smile:
I’ve got a 3D app that requires 2D overlays (menues, status, etc)
If I try and access the SDLSurface for the 2D stuff the app crashes.

I’m guessing you can’t use normal SDL 2D stuff when you’re using
OpenGL, so how do I put on these overlays? A nice simple example
would be helpfull. :slight_smile:

Thanks.

                    -fjr

SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

No, it’s really not a good solution. Not as slow perhaps as OPENGLBLIT,
but it is slow enough that you should really do it another way. Setting
glOrtho is probably the best solution, just remember to push your frusum
settings first and pop them afterward and you’ll be fine. (This is done
by SDL_GL_{Enter,Leave}2DMode for you…)On Mon, Jan 28, 2002 at 10:23:49PM +0100, St0fF 64 wrote:

I don’t wanna give an example, but a GL_HINT:
use glDrawPixels() in conjunction with a bitmap containig your
2d-stuff. or even cooler but much harder:
make your 2d-stuff squared and do some texturemapping. this idea should
have come to you by yourself.


Joseph Carter If this sig were funny…

  • Twilight1 will have to hang his Mozilla beanie dinosaur in effigy if
    Netscape sells-out to Alot Of Losers…

-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 273 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020130/0b42a67e/attachment.pgp

Not that much harder, really - but infinitely superior, especially on
the majority of non high end 3D cards.

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Monday 28 January 2002 22:23, St0fF 64 wrote:

Hi!
I don’t wanna give an example, but a GL_HINT:
use glDrawPixels() in conjunction with a bitmap containig your
2d-stuff. or even cooler but much harder:
make your 2d-stuff squared and do some texturemapping. this idea
should have come to you by yourself.