Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading Textures with SDL for use with OpenGL #1124

Closed
zedseven opened this issue Jul 18, 2021 · 2 comments
Closed

Loading Textures with SDL for use with OpenGL #1124

zedseven opened this issue Jul 18, 2021 · 2 comments
Labels

Comments

@zedseven
Copy link

Apologies if this is a dumb question, but I'm using SDL for window management and OpenGL for rendering, and I want to use SDL for texture loading rather than adding another dependency for something SDL is already capable of.

I know I should be able to use Texture.gl_bind_texture() to actually use the texture, but how can I load the texture in the first place if it requires a TextureCreator which in turn requires a Canvas? Would I really need to create a canvas I'm not going to use in order to accomplish this, or am I missing something?

I found mention of this here which makes me think it's possible, but I'm not sure how to accomplish it:

I am just using SDL2 to manage the window and the textures, while all the rendering is done with my own renderer that is using OpenGL ES 2 with no SDL2 involved at that stage except for retrieving the handle to the actual texture.

Again sorry if it's not a great question - it's just difficult finding documentation for this specific combination.

@Cobrand
Copy link
Member

Cobrand commented Jul 19, 2021

So I only know the theory but I haven't actually used gl_bind_texture myself.

In short it depends what you mean by "rendering".

A Canvas is necessary to create a Texture, because texture does not make sense without one. It's simply a pointer to a specific implementation of hardware-accelerated texture inside your graphic card (so OpenGL texture, DirectX texture, ...). You can't just load a Texture by itself, because without a Canvas it doesn't know what backend to target.

If your "rendering" part is simply your custom vertex/fragment shaders, then you can initialize a Canvas via SDL2, then use TextureCreator to create textures, call gl_bind_texture whenever necessary and it should mostly work. I think you are "tied" to whatever version of OpenGL SDL2 uses for you, but you may have ways to configure that via https://docs.rs/sdl2/0.34.5/sdl2/video/gl_attr/index.html .

If you absolutely want to initialize your context then use a Canvas on top of that, then I don't think it is possible.

But honestly this is thin ice, I would advise you to simply not bother with the rendering side of SDL2 if you already have your implementation, and use a crate like image for texture loading, if you already know OpenGL with ~50 lines of code you can make something basic that still works very well.

Hope that helped.

@zedseven
Copy link
Author

That makes sense, thank you.

I think I'll take that advice and use something else to load the textures - it's not worth the trouble to slightly cut down on dependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants