Jump to content

  • Log In with Google      Sign In   
  • Create Account


OpenGL + SDL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Acharis   Crossbones+   -  Reputation: 2350

Like
0Likes
Like

Posted 27 July 2012 - 02:49 PM

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.

Europe1300.eu - Historical Realistic Medieval Sim (BETA)


Sponsor:

#2 The Magical Pot   Members   -  Reputation: 171

Like
0Likes
Like

Posted 28 July 2012 - 11:02 AM

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 :)

#3 Acharis   Crossbones+   -  Reputation: 2350

Like
0Likes
Like

Posted 28 July 2012 - 11:23 AM

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?

Europe1300.eu - Historical Realistic Medieval Sim (BETA)


#4 The Magical Pot   Members   -  Reputation: 171

Like
0Likes
Like

Posted 28 July 2012 - 02:37 PM

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.

#5 dilyan_rusev   Members   -  Reputation: 748

Like
0Likes
Like

Posted 29 July 2012 - 11:19 AM

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.

#6 nooblz   Members   -  Reputation: 106

Like
1Likes
Like

Posted 29 July 2012 - 11:50 PM

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]

#7 Acharis   Crossbones+   -  Reputation: 2350

Like
0Likes
Like

Posted 11 August 2012 - 10:51 AM

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).

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.

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, 11 August 2012 - 10:53 AM.

Europe1300.eu - Historical Realistic Medieval Sim (BETA)


#8 dilyan_rusev   Members   -  Reputation: 748

Like
0Likes
Like

Posted 11 August 2012 - 01:48 PM

OK, what you do is that you create a surface in memory with SDL_CreateRGBSurface . 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 SDL_ConvertSurface 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 generate a texture out of it, 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.

#9 Lazy Foo   Members   -  Reputation: 1092

Like
0Likes
Like

Posted 11 August 2012 - 04:08 PM

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 OpenGL Tutorial, both in modern OpenGL and OpenGL 2.1.
Learn to make games with my SDL Tutorials.
Transition from OpenGL 2 to modern OpenGL using my OpenGL Tutorial.

#10 Acharis   Crossbones+   -  Reputation: 2350

Like
0Likes
Like

Posted 13 August 2012 - 05:00 AM

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?

Europe1300.eu - Historical Realistic Medieval Sim (BETA)


#11 BitMaster   Crossbones+   -  Reputation: 3006

Like
1Likes
Like

Posted 13 August 2012 - 06:58 AM

JTippets answered a similar question here. Just because you need something 'pixel perfect' does not mean you cannot involve textures.

#12 Lazy Foo   Members   -  Reputation: 1092

Like
1Likes
Like

Posted 13 August 2012 - 07:01 AM

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.
Learn to make games with my SDL Tutorials.
Transition from OpenGL 2 to modern OpenGL using my OpenGL Tutorial.

#13 Acharis   Crossbones+   -  Reputation: 2350

Like
0Likes
Like

Posted 20 August 2012 - 02:04 AM

JTippets answered a similar question here. Just because you need something 'pixel perfect' does not mean you cannot involve textures.

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?

Europe1300.eu - Historical Realistic Medieval Sim (BETA)





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS