Converting an unsafe.Pointer to []byte?

195 views
Skip to first unread message

jesse....@gmail.com

unread,
Dec 4, 2014, 8:50:06 AM12/4/14
to golan...@googlegroups.com
Hey Gophers!

My first time posting here or on any mailing list, so please go gentle on me.

I'm writing a small project using OpenGL and I am trying to create a function to make a screenshot, I've done this before in C and C++ with no problem, but I'm struggling here. The bindings I use (https://github.com/go-gl/glow) have the pixels returned in the format of unsafe.Pointer, rather than a byte array. That's an useless format for me and I'm trying to convert the unsafe.Pointer to a []byte.

Here's my current code:

 width, height := r.window.GetSize()
 m
:= image.NewRGBA(image.Rect(0, 0, width, height))
 pixels
:= make([]byte, len(m.Pix))


 
// Read in the pixels
 gl
.ReadPixels(0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, pixels)

Which results in the following error:
video\renderer.go:94: cannot use pixels (type []byte) as type unsafe.Pointer in argument gl.ReadPixels

That makes sense of course, but I have no idea how unsafe.Pointer works and I'm struggling to find a good example on the internet. I've tried a couple of things (storing the result in var buf unsafe.Pointer) and then converting that to a []byte, but that doesn't work.

Anyone care to show me how it's done? Just to note: I'm using the result with the image package to write an .png to disk.

Thanks!
--Jesse

Nick Craig-Wood

unread,
Dec 4, 2014, 11:55:48 AM12/4/14
to jesse....@gmail.com, golan...@googlegroups.com
On 04/12/14 13:50, jesse....@gmail.com wrote:
> width, height := r.window.GetSize()
> m := image.NewRGBA(image.Rect(0, 0, width, height))
> pixels := make([]byte, len(m.Pix))
> // Read in the pixels
> gl.ReadPixels(0,0,int32(width),int32(height),gl.RGBA,gl.UNSIGNED_BYTE,pixels)
> |
>
> Which results in the following error:
> video\renderer.go:94: cannot use pixels (type []byte) as type
> unsafe.Pointer in argument gl.ReadPixels

Try unsafe.Pointer(&pixels[0]) that should be what you want.

--
Nick Craig-Wood <ni...@craig-wood.com> -- http://www.craig-wood.com/nick
Reply all
Reply to author
Forward
0 new messages