I’m trying to get the contents of an OpenGL rendering into a Picture.
DrawInto doesn’t seem to work.
I’ve been looking for standard function within OpenGL, but it doesn’t seem to exist.
Anyone know how to do this within Xojo?
I’m trying to get the contents of an OpenGL rendering into a Picture.
DrawInto doesn’t seem to work.
I’ve been looking for standard function within OpenGL, but it doesn’t seem to exist.
Anyone know how to do this within Xojo?
Sorry folks, I think I can answer my own question…
Is that the right handling of the alpha channel?
[code]Public Function getPicture() as Picture
dim m as MemoryBlock = new MemoryBlock( widthheight4)
openGL.glReadPixels 0,0,me.Width, me.Height, openGL.GL_RGBA, openGL.GL_UNSIGNED_BYTE, m
dim p as new picture(me.Width, me.Height)
dim pm as new Picture(me.Width, me.Height)
dim i as integer = 0
dim h, w as integer
dim a as integer
for h = me.Height downTo 1
for w = 1 to me.Width
p.RGBSurface.Pixel(w,h) = rgb(m.Byte(i), m.Byte(i+1), m.Byte(i+2))
a = 255-m.Byte(i+3)
pm.RGBSurface.pixel(w,h) = rgb(a,a,a)
i=i+4
next
next
p.ApplyMask pm
return p
End Function[/code]
Some years ago I found this code in the forum. I use it to create a PNG files not from the screen but from the OpnGLSurface control.
It has worked without problems so far.
[code]Function getPicture(glSurf As OpenGLSurface) As Picture
'Capture OpenGLSurface image and puts it into a PNG file
//calculate sizes
dim w As integer = glSurf.Width
dim h As integer = glSurf.Height
dim pad As integer = w mod 4
dim totalBytes As integer = (w * h * 3) + (h * pad)
//create and fill memoryblock with pixel data
glSurf.MakeCurrent
dim m As MemoryBlock = New MemoryBlock(totalBytes)
OpenGL.glReadPixels(0, 0, w, h, OpenGL.GL_BGR, OpenGL.GL_UNSIGNED_BYTE, m)
//create output picture
dim p As Picture = New Picture(w, h, 32)
dim surf As RGBSurface = p.RGBSurface
//scan memoryblock into picture
h = h - 1
w = w - 1
dim x, y, offset As integer //offset starts at 0
for y = h downto 0
for x = 0 to w
surf.Pixel(x, y) = m.ColorValue(offset, 24)
offset = offset + 3
next
offset = offset + pad //skip per line
next
return p //store picture
Exception
LOG "Error: " + CurrentMethodName
End Function[/code]