Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411423 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 18, 2024, 05:06:29 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)[SOLVED] GLSL and 3D rendering in SDL 2 WITH SDL_Image?
Pages: [1]
Print
Author Topic: [SOLVED] GLSL and 3D rendering in SDL 2 WITH SDL_Image?  (Read 5150 times)
oahda
Level 10
*****



View Profile
« on: January 19, 2015, 05:39:11 PM »

SOLVED: see this post: http://forums.tigsource.com/index.php?topic=45726.msg1102559#new

So I'd been assuming forever that I should be able to apply GLSL shaders onto a scene rendered using SDL_Image (for SDL 2) rather than using my own gritty OpenGL code from the ground up, now that SDL 2 does use OpenGL underlyingly instead of the blitting of SDL 1.

Not sure how to, and now wondering whether that really is possible.

Was also hoping to get some simple 3D objects rendered into my otherwise 2D SDL_Image scene, tho that part I can bear doing grittily myself, but of course I'd like to avoid that too, if there is a faster way.

Even if it isn't built straight into SDL 2 or SDL_Image, surely there must be some other solution out there already? I can't be the only one.

Is my problem understandable, and how can I go about this? SDL_Image is really neat now that it's no longer blitted, so I really would like to stick with it. But I need shaders. And I do want simple 3D objects to facilitate animations and also flat, textured polygons in some places for the same reason, so that I can animate the polygons programmatically by moving their points (or possibly just animating them in some way using shaders, but yeah).

Heeeeelp.
« Last Edit: January 22, 2015, 06:28:26 AM by Prinsessa » Logged

oahda
Level 10
*****



View Profile
« Reply #1 on: January 19, 2015, 05:55:53 PM »

Also related:

I just upgraded to OS X 10.10 and did everything I could to get the OpenGL version to report a higher number which is now supposedly to be shipped with the latest SDK. But it's still saying 2.1.

I downloaded some premade OpenGL projects for Xcode 6.1.1, the latest version, and running those it does say 4.1. They are not using SDL, tho, so could that be it? But I made sure to change my SDL framework to the latest one from the site, and to put it in the new SDK's framework folder and not in the old one, thinking there could be relative paths involved or something.

I really can't see any other differences between my project and those sample ones except the usage of SDL, but mine was created in an older version of Xcode so I'm wondering if there's still some setting lurking somewhere.

But I've made sure my base SDK (and deployment target) is set to 10.10 everywhere and I do get to choose 10.10 frameworks now when I choose to add new ones. I'm wondering if there's something else loading an old version of gl.h somewhere. The sample projects don't even explicitly include the file.

Nonetheless, I am able to compile and link and run with some modern code, creating shader programs and so on instead of using the old intermediate mode with glBegin() and so on. Can't see the rectangle I'm trying to render tho, but that's probably an interference with my SDL setup, still rendering with SDL_Image and so on (tho I did create a GL context).
« Last Edit: January 20, 2015, 02:26:41 AM by Prinsessa » Logged

Whiskas
Level 0
**



View Profile
« Reply #2 on: January 19, 2015, 06:30:43 PM »

SDL_Renderer does not allow shaders, you'd have to write your own stuff, not sure if there's any way to mix your code with SDL (would surprise me since the renderer is abstracted). Also last time I checked the OpenGL version of SDL_Renderer was using the fixed function pipeline (probably for compatibility reasons) so the 2.1 version makes sense.

You should maybe check SFML which has everything you're asking for: shader support, custom shapes and ability to mix with your own rendering code.
Logged
nickgravelyn
Guest
« Reply #3 on: January 19, 2015, 07:54:14 PM »

If you want above OpenGL 2.1 on OS X you have to explicitly ask for a Core profile. Apple doesn't support the new "Compatibility" profile which is the default in SDL2. Basically you need to modify your startup code with this before you create the GL context:

Code:
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

I'm pretty sure that's it and you'll start seeing OpenGL 4.1 reported for you. Smiley

Though note that if you're using the SDL_Renderer stuff, as mentioned above, this won't matter too much. If you want to use SDL_Renderer, you should probably stick to just that. If you go OpenGL, you'll probably want to handle all rendering yourself.

If you're just doing basic 2D/3D, you probably can just stick with OpenGL 2.1, also. 4.1 has some nice features, but (from my I'm-not-a-professional-graphics-programmer standpoint) it's mostly performance stuff for high end work. For most indie games you'll be just as fine using 2.1 as 4.1.
Logged
Boreal
Level 6
*


Reinventing the wheel


View Profile
« Reply #4 on: January 19, 2015, 08:03:26 PM »

SDL2's built in rendering still sucks.  You can hardly draw a decently large tiled scene without it slowing to a crawl on older hardware.  I'd advocate using OpenGL even for 2D games.
Logged

"In software, the only numbers of significance are 0, 1, and N." - Josh Barczak

magma - Reconstructed Mantle API
oahda
Level 10
*****



View Profile
« Reply #5 on: January 20, 2015, 02:37:44 AM »

SDL_Renderer does not allow shaders, you'd have to write your own stuff, not sure if there's any way to mix your code with SDL (would surprise me since the renderer is abstracted). Also last time I checked the OpenGL version of SDL_Renderer was using the fixed function pipeline (probably for compatibility reasons) so the 2.1 version makes sense.

You should maybe check SFML which has everything you're asking for: shader support, custom shapes and ability to mix with your own rendering code.
Way too late into the project for that now. I'm not only using SDL for rendering, either, but other stuff too. I've worked with SFML before but actually moved on to SDL when SDL 2 came out, because it seemed so much better in every aspect. Not being able to use an SDL_Renderer is fine, but surely doing it manually must be a common enough thing that I should be able to download someone's work and use it instead of having to reïnvent that wheel for the five thousandth time in the history of SDL 2 applications? That was what I was hoping for.

If you want above OpenGL 2.1 on OS X you have to explicitly ask for a Core profile. Apple doesn't support the new "Compatibility" profile which is the default in SDL2. Basically you need to modify your startup code with this before you create the GL context:

Code:
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

I'm pretty sure that's it and you'll start seeing OpenGL 4.1 reported for you. Smiley
Nah, already doing that (tho with 3.1, not 4.1, but maybe 4.1 would work, since that's the version that should be available? – I'll check after breakfast).

Though note that if you're using the SDL_Renderer stuff, as mentioned above, this won't matter too much. If you want to use SDL_Renderer, you should probably stick to just that. If you go OpenGL, you'll probably want to handle all rendering yourself.
I guess, but again, surely there must be finished code available for that?

If you're just doing basic 2D/3D, you probably can just stick with OpenGL 2.1, also. 4.1 has some nice features, but (from my I'm-not-a-professional-graphics-programmer standpoint) it's mostly performance stuff for high end work. For most indie games you'll be just as fine using 2.1 as 4.1.
Yeah, it's supposed to be a 2D game primarily, but it's running at a pretty high resolution, and it seems much faster to render some really low-poly models with pretty, unlit textures, more or less like sprites, that I can animate (I want a big crane to turn 180 degrees, for example, and putting that in as an animated texture would be enormous and really unnecessary). I don't need anything advanced 3D-wise. But I do need advanced shaders for the actual 2D game. It relies heavily on lighting and obscuring, for example. Could 2.1 still do that? But if I ditch the SDL_Renderer, I should be able to use any GL version I want, right?

SDL2's built in rendering still sucks.  You can hardly draw a decently large tiled scene without it slowing to a crawl on older hardware.  I'd advocate using OpenGL even for 2D games.
Runs fine in my 1920x1080 window on my old 2011 ~ 2012 MacBook so far, tho it does get slow when I turn on my temporary CPU lighting system using polygons and clipping, which is one of the things I wish to turn into a shader-based solution eventually, and it did run terribly slow on any smartphone weaker than the iPhone 5 when I made an iOS/Android game with it, so that's probably true. Another reason to upgrade. Everything's nicely abstracted into my own library, tho, so hopefully updates shouldn't take too long, BUT hopefully not as long as it should have to, because:

But again, even if I need to use pure GL and not SDL_Renderer after all, surely there must be ready-made code for the basics available so that I don't have to reïnvent that wheel?

And I don't really mean stitching various tutorial code together or something, but some sort of lightweight add-on module or set of classes or functions or whatever for SDL 2 made and released by some nice fellow.

I'll do it myself if I have to, but it does feel weird that I should have to for something as common and fundamental as this.
« Last Edit: January 20, 2015, 03:43:19 AM by Prinsessa » Logged

Raptor85
Level 5
*****



View Profile WWW
« Reply #6 on: January 20, 2015, 03:07:26 AM »

it's pretty easy to use GL3+ functions alongside SDL2, here's an easy way to load texture data using sdl2_image

Code:
SDL_Surface* surface = IMG_Load(filename);

if (surface == NULL)
{
LOGWARN("error loading image %s", IMG_GetError());
return 0;
}

Uint32 texture;

glGenTextures(1, &texture);

glBindTexture(GL_TEXTURE_2D, texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
int ret = glGetError();
if (ret != GL_NO_ERROR)
{
LOGWARN("GL Error in loadimage: %i", ret);
}

SDL_FreeSurface(surface);

you sound like you're trying to use SDL2 about the way i use it, window  management/input/sound/etc cross-platform and doing the rendering in GL, which thankfully is pretty easy to do and 2 is pretty well designed around doing just that.

As for the rest, once your SDL window is created (WITH THE SDL_WINDOW_OPENGL flag or it wont work) you can create a context like so

Code:
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL,true);

        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);

        if(!SDL_GL_CreateContext(window))

then afterwards you can create/bind shaders textures and the like as per usual and just use SDL_GL_SwapWindow(window); at the end of each frame and voila, you have a fully functional sdl2 window that allows normal use of opengl for the drawing code itself. unfortunately for windows support you still need to make calls to wglGetProcAddress for function pointers for any shader functions, which is a shame because outside of that the code for window/input/sound/gl is 100% identical for win/mac/linux
Logged

-Fuzzy Spider
oahda
Level 10
*****



View Profile
« Reply #7 on: January 20, 2015, 03:20:42 AM »

Yeah, I know all that, and how to do it, but I just don't feel like I should have to be the thousandth person to do this from the ground up in SDL. :p

Maybe in the end I will have to be the one doing it from scratch once more and then providing that lightweight library I feel the world needs, then, but I do hope for a suggestion of an existing one before I go there – I can still work on other parts of the game while waiting for it, and keep googling some more on my own. I just couldn't find one as I, of course, spent a while googling before I wrote here. I've got lots of material to help me do it from the ground up, so that's no problem.

Thanks for your input so far, everyone, anyhow! I just hope you understand precisely what I'm asking now that it's been established that I can't use the built-in functionality of SDL_Image (besides loading texture data) and SDL_Renderer: I will be doing this without SDL_Renderer, but if there's already a lightweight set of functions or classes available, so that I don't have to write all the OpenGL code from the ground up myself (which I know how to do if I have to, I just don't feel like I should have to), but possibly go and poke into those files if I need more customisation, then I want that code! Otherwise, I will have to write it myself after all, and try to provide such a lightweight library myself eventually, since I think the world needs it.
Logged

Whiskas
Level 0
**



View Profile
« Reply #8 on: January 20, 2015, 11:19:15 AM »

You could check sdl-gpu which is made to be integrated with SDL : https://code.google.com/p/sdl-gpu/
Logged
oahda
Level 10
*****



View Profile
« Reply #9 on: January 21, 2015, 05:10:27 AM »

Thanks! Found a tutorial too. Not sure I like the way it's built up, tho, or it may interfere with my current setup too much, but there's definitely source code there for me to peek at if nothing else. Couldn't find drawing functions that are not called GPU_Blit()?
Logged

oahda
Level 10
*****



View Profile
« Reply #10 on: January 21, 2015, 08:07:34 AM »

Here we go.

Logged

oahda
Level 10
*****



View Profile
« Reply #11 on: January 22, 2015, 06:26:28 AM »

Seems I got it working well enough to get back to business already, with anti-aliasing and all!



I did find this one neat and helpful set of classes, actually. I'm failing to refind the page from which I downloaded the combined examples as an Xcode project with multiple targets right now, but here's the page for each example: http://www.tomdalling.com/blog/category/modern-opengl/

I only used his bitmap class and the function at the top of the texture class to make sure I get the right formats set up for any texture loaded (kept getting error codes when trying to use SDL_Surface, actually). Did the rest with pure OpenGL and some SDL settings, as well as the lovely GLM library to speed the coding of those pesky matrix transformations up a bit. The license seems like it should allow me to use it, but I tweeted him anyway just to be sure.

Time to move on to some abstraction and then hopefully I should have my game up and running again!
Logged

oahda
Level 10
*****



View Profile
« Reply #12 on: January 22, 2015, 12:42:10 PM »

DONE. Coffee
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic