Sign in to follow this  
Acharis

OpenGL + SDL

Recommended Posts

Is there a way to use both OpenGL and SDL (not just for keyboard input but for 2D graphics)? I want to render a 3D scene in the background and blit on top of it various 2D interface elements via SDL.

I'm thinking along the lines of "render OpenGL to SDL surface instead of screen and then blit it" solution. Performance is of extremely low importance.

Share this post


Link to post
Share on other sites
OpenGL supports both 2D and 3D graphics, it uses polygons to render graphics which means that you can render both 2D and 3D graphics in the same application (did you notice the Captain Obvious in there? :P ) You would just have to render your 2D interface in relation to the screen and the 3D world in relation to a camera that exist within that 3D space.
I don't know how familiar you are with OpenGL, but here's a useful website that gets you comfortable with the library: http://nehe.gamedev.net/
Hope this helps :)

Share this post


Link to post
Share on other sites
Yes, I know I can use OpenGL for drawing 2D primitives. But I hoped for SDL blit solution. This would let me share my already existing 2D drawing code, true type fonts code, etc mixed with 3D.

Is it possible?

Share this post


Link to post
Share on other sites
Oh, sorry.
I don't think you can use SDLs blitting when you use OpenGL since you can (by the looks of it) only enable one of them at a time, you can write "SDL_SetVideoMode( x, y, 32, SDL_OPENGL )" to enable OpenGLs rendering and "SDL_SetVideoMode( x, y, 32, SDL_SWSURFACE )" to enable SDLs rendering, but never enable both at the same time with that method.
Maybe there's a way to use both OpenGLs rendering and SDLs rendering in the same application, but my answer to you would be that you probably can't.

Something you can do is to load an SDL_Surface and render it on a polygon using OpenGL, but that, I think, is about as "close" as you can get to SDLs way of rendering while using OpenGL. Whatever the case, I think that it's easier to just switch over to OpenGLs rendering, OpenGL is much more useful and faster compared to SDL.

Share this post


Link to post
Share on other sites
Well, you can render fonts with SDL_ttf to surfaces, and then render the pixel data as a texture in OpenGL. If you are adventurous, you can do roughly the same thing. You create a blank surface, blit your scene on it, and then extract the pixels from the surface to map them on a texture. You will have to repeat the process every time you change something in SDL.

Share this post


Link to post
Share on other sites
Sorry to just randomly jump in here but I was looking through gamedev and just decided to create an account because I saw this question. I am new to SDL and openGL but I thought maybe I could help with a bit of code from one of my projects. This may be helpful or may not, but it does seem to answer the question of how to get SDL_SWSURFACE and SDL_OPENGL on at the same time.

[source lang="cpp"] int videoFlags;
const SDL_VideoInfo* videoInfo;

SDL_Init( SDL_INIT_EVERYTHING );

videoInfo = SDL_GetVideoInfo();
if(!videoInfo)
return false;

videoFlags = SDL_OPENGL;
videoFlags |= SDL_GL_DOUBLEBUFFER;
videoFlags |= SDL_HWPALETTE;
videoFlags |= SDL_RESIZABLE;

if( videoInfo->hw_available )
videoFlags |= SDL_HWSURFACE;
else
videoFlags |= SDL_SWSURFACE;

if( videoInfo->blit_hw )
videoFlags |= SDL_HWACCEL;

SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

screen = SDL_SetVideoMode( screenW,screenH,screenBPP,videoFlags );[/source]

Share this post


Link to post
Share on other sites
To clarify, I need pixel perfect accuracy, so converting surface to a texture and then display it as 2 triangles is not an option. If my memory is correct, the only way to do pixel perfect render in OpenGL is the use of glDrawPixels (which is unfortunatelly not hardware accelerated, IIRC).

Also, it needs to run on OpenGL 1.3 max (compatibility with older intel video onboard cards, especially in a bit older laptops).

[quote name='dilyan_rusev' timestamp='1343582397' post='4964279']
Well, you can render fonts with SDL_ttf to surfaces, and then render the pixel data as a texture in OpenGL. If you are adventurous, you can do roughly the same thing. You create a blank surface, blit your scene on it, and then extract the pixels from the surface to map them on a texture. You will have to repeat the process every time you change something in SDL.
[/quote]Hmm, if I'm reading this right, you are saying I can first render the 3D scene via OpenGL, then read it via SDL and create SDL_surface of it and the clear the screen and redraw everything I want using SDL blits?

But it would require changing some SDL video setting between these two (from OpenGL to SDL video)? Edited by Acharis

Share this post


Link to post
Share on other sites
OK, what you do is that you create a surface in memory with [url="http://sdl.beuc.net/sdl.wiki/SDL_CreateRGBSurface"]SDL_CreateRGBSurface[/url] . SDL blitting functions should not care that you are blitting on a memory surface instead of blitting on the screen surface. You just make sure that the memory surface is in the same format as your images (you probably want to convert your sprites with [url="http://sdl.beuc.net/sdl.wiki/SDL_ConvertSurface"]SDL_ConvertSurface[/url] when you load to to be of the same format as the one you create with SDL_CreateRGBSurface).

Finally, when you are done drawing to the memory surface, you just [url="http://content.gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL"]generate a texture out of it[/url], and draw it on the whole screen, as if it were a texture you loaded from a file.

So far as fonts go, I do love the simplicity of SDL_ttf. It renders to a SDL_Surface, which you can simply treat as yet another texture, if you were using the OpenGL API directly.

Share this post


Link to post
Share on other sites
It actually isn't that hard to create an SDL-like texture render API in OpenGL. You can render your 3D scene, clear/disable the depth buffer, and then render your ortho 2D graphics on top if it.

If you're too lazy to do it yourself, I basically already did it for you in my [url="http://lazyfoo.net/tutorials/OpenGL/index.php"]OpenGL Tutorial[/url], both in modern OpenGL and OpenGL 2.1.

Share this post


Link to post
Share on other sites
Clarification what I need:
- I need PIXEL PERFECT solution, so absolutelly no "textures" involved at all. I need a control of a position of a single pixel (for 2D render only).
- Max OpenGL 1.3 (I need it to run on old laptops with IBM gfx card onboard, these sux a lot when it comes to OpenGL support, even if not that old), also it needs to run under outdated drivers (yes, I'm making a very low specs retro game, which is to be played on inferior hardware that is not capable of even running any modern game).
- Ideally (but I can live without it) would be if I could directly use SDL code to rended 2D stuff (interface on top of a 3D scene, no mixing these two together, a separate 3D scene at the bottom and separate 2D on top of it), that would save me a lot of rewriting and keeping 2 versions of the render engine (plus, SDL is much faster when it comes to render compared to glDrawPixels since it can use VRAM).

Yes, I know, these requirements are odd and no one makes games this way nowadays :)



I think, something like this would be the most convenient:
- render 3D scene in OpenGL
- instead of swapping the scene from secondary to primary buffer (screen), copy/convert the scene to SDL_surface
- disable OpenGL, enable SDL video and clear everything on the screen/buffer
- blit via SDL the 3D scene (stored as SDL_surface) and then do the normal SDL blits for the rest
- swap the buffers (SDL) so the primary screen/surface is updated

Is this possible?

Share this post


Link to post
Share on other sites
JTippets answered a similar question [url=http://www.gamedev.net/topic/629496-dynamic-objects-in-isometric-map-drawing-algorythms/]here[/url]. Just because you need something 'pixel perfect' does not mean you cannot involve textures.

Share this post


Link to post
Share on other sites
If you want pixel perfect rendering, just remember that when polygons rasterize they rasterize to the center of the pixel. If you wanted to draw a line from 0 to 5 inclusive, you draw from 0.5 to 5.5.

Try setting up your ortho projection matrix and then translate it <0.5, 0.5, 0.0> to center your pixels.

Share this post


Link to post
Share on other sites
[quote name='BitMaster' timestamp='1344862716' post='4969042']
JTippets answered a similar question [url="http://www.gamedev.net/topic/629496-dynamic-objects-in-isometric-map-drawing-algorythms/"]here[/url]. Just because you need something 'pixel perfect' does not mean you cannot involve textures.
[/quote]Hmmm, very interesting. I thought 3D cards do not provide such capability and the exact accuracy depends on the card used. If textures can be used then indeed it's very nice performance wise.

OK, so now it's mostly convenience question. Is there a way to use BOTH OpenGL (for 3D) and SDL_Blit (for primitive 2D overlay), so I don't need to rewrite my already existing SDL code?

Also, I would like to understand how exactly opening screen/context/canvas works? A screen is a screen, it should be a buffer on GFX card VRAM which should be usable the same way by all APIs. What exactly happens when I define the screen as "SDL_OPENGL" and why can't I use normal SDL Blits then?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this