Tangent128 / luasdl2

A pure C binding of SDL 2.0 for Lua 5.1, Lua 5.2, and LuaJIT.
ISC License
385 stars 73 forks source link

Opengl Texture binding #36

Open Kaitto opened 9 years ago

Kaitto commented 9 years ago

I'm a little confused. The Wiki is to me very useful and has solved many of my questions, since the way in which everything is written coincides with the original library. But it seems I can not find the way to create a surface and find the pointer to give it to OpenGL and use textures. I have reviewed the source (with some effort) but each function returns a pointer to surface, and does not expose methods or arrays, or at least can not find the way. May need to see the "texture class" but can not find similar documentation. It may be a function called "SDL_GL_BindTexture" but it's too much for me and do not know to do. [Using google translate]

Kaitto commented 9 years ago

I know that i should do something like this:


SDL_Surface* Surface = IMG_Load("someimage.jpg"); (...) glTexImage2D(GL_TEXTURE_2D, 0, Mode, Surface->w, Surface->h, 0, Mode, GL_UNSIGNED_BYTE, Surface->pixels);


But how?

Kaitto commented 9 years ago

Whatever... i fixed it (the wrong way i guess) ive added a extra function to wrap surf->h,surf->w,surf->format->BytesPerPixel and surf->pixels with the (light) userdata function glTexImage2D from luagl... i dont know how use github, (also i dont want to create a new branch), and i dont know if there is a function that does what i needed but... if not, you may need to consider this: //***** static int l_surface_getPixelData(lua_State _L) { SDL_Surface surf = commonGetAs(L, 1, SurfaceName, SDL_Surface ); luapushlightuserdata (L, surf->pixels); //LuaGL Uses Light UserData. return commonPush(L,"iii",surf->h,surf->w,surf->format->BytesPerPixel)+1; } //****

So you can use it with:

local surf=SDL.image.load("Lua-SDL2.png") local imgdata={} imgdata.pixels,imgdata.h,imgdata.w,imgdata.BytesPerPixel= surf:getPixelData()

local TextureID=gl.GenTextures(1)[1] gl.BindTexture(gl.TEXTURE_2D, TextureID);

local Mode = gl.RGB;

if(imgdata.BytesPerPixel == 4) then Mode = gl.RGBA end gl.TexImage2D(0, Mode, imgdata.w, imgdata.h, 0, Mode, gl.UNSIGNED_BYTE, imgdata.pixels)

gl.TexParameter(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR); gl.TexParameter(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR);

(and... yes, ive had to mess with the sources xD)

Tangent128 commented 9 years ago

Hmm... it appears that SDL_GL_BindTexture is in fact not bound yet.

I'll need to implement that.

(However, you might prefer using a non-SDL image loader anyways, so that you can call the gl.Tex* functions yourself. SDL doesn't give much control.)

Kaitto commented 9 years ago

Thanks, I'm glad to have "contributed" a little, and especially do not to have added anything that might have existed. Is true that could use a non-SDL image loader, but as this should already be implemented, and also I pretend to have the minimum amount of libraries to compile/use, this just works for me. This way I can take advantage of existing pointers to the pixels and pass them directly to OpenGL or to SDL reusing textures (for 2D graphics, a gui, load a font with sdl_ttf, etc) without loading for different ways with other libraries and having to wrap it all together.