FAQ
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

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Nick Craig-Wood at Dec 4, 2014 at 4:55 pm

    On 04/12/14 13:50, jesse.brands@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 <nick@craig-wood.com> -- http://www.craig-wood.com/nick

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 4, '14 at 4:44p
activeDec 4, '14 at 4:55p
posts2
users2
websitegolang.org

2 users in discussion

Nick Craig-Wood: 1 post Jesse Brands: 1 post

People

Translate

site design / logo © 2022 Grokbase