FAQ
Hi everyone!

I've been trying out Go for a while now, and I'm currently checking out
go-gl and the glam library. The glam library handles GLSL types (matrices,
vectors etc). The matrix is defined as: type Mat4 [16]float32. Originally
it was [4][4]float32, but go-gl requires *[16]float32. The type has several
methods associated with it.

Now, my problem is that I wish to cast Mat4 back to [16]float32: mat :=
[16]float32(matrix)
Interestingly it works sometimes; I can build the *renderer* package
without any problems (and run associated tests!), but when I try to compile
the *main* package, the compiler complains about the cast. Does it have to
do with the associated methods? Any ideas what I'm doing wrong (and why it
works sometimes)?

Thanks!

--
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/groups/opt_out.

Search Discussions

  • Ian Lance Taylor at Nov 26, 2013 at 2:16 pm

    On Mon, Nov 25, 2013 at 9:48 AM, wrote:
    I've been trying out Go for a while now, and I'm currently checking out
    go-gl and the glam library. The glam library handles GLSL types (matrices,
    vectors etc). The matrix is defined as: type Mat4 [16]float32. Originally it
    was [4][4]float32, but go-gl requires *[16]float32. The type has several
    methods associated with it.

    Now, my problem is that I wish to cast Mat4 back to [16]float32: mat :=
    [16]float32(matrix)
    Interestingly it works sometimes; I can build the renderer package without
    any problems (and run associated tests!), but when I try to compile the main
    package, the compiler complains about the cast. Does it have to do with the
    associated methods? Any ideas what I'm doing wrong (and why it works
    sometimes)?
    For questions like this please show some actual code and the actual
    error messages you get.

    From your description: are you really using "matrix", an unexported
    type, rather than "Matrix", an exported type?

    Ian

    --
    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/groups/opt_out.
  • Oppainless at Nov 26, 2013 at 5:21 pm
    Hi,

    Here is the relevant code (not pretty, I know):

    func (renderer *Renderer) Draw(geometry gl.Buffer,
    uv gl.Buffer,
    num uint32,
    offset uint32,
    prg gl.Program,
    *matrix glam.Mat4*,
    position util.Point) {

    geometry.Bind(gl.ARRAY_BUFFER)
    prg.Use()

    // Set vertex attributes
    vertAttrib := prg.GetAttribLocation("vertices")
    vertAttrib.EnableArray()
    vertAttrib.AttribPointer(2, gl.FLOAT, false, 0, nil)
    *mat := [16]float32(matrix)*// HERE IS THE RELEVANT PART

    // Set uniforms
    matrixLocation := prg.GetUniformLocation("projection")
    matrixLocation.UniformMatrix4f(false, &mat)

    gl.DrawArrays(gl.TRIANGLES, int(offset), int(num))
    vertAttrib.DisableArray()
    }
    Now, the unusual thing is that the *gfx* package, which the above code is
    part of, compiles fine and tests fine. But when I try to compile the whole
    program, the compiler complains about the cast:

    *src/gfx/renderer.go:82: cannot convert matrix (type glam.Mat4) to type
    [16]float32*


    Br.

    Andreas

    --
    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/groups/opt_out.
  • Ian Lance Taylor at Nov 26, 2013 at 5:29 pm

    On Tue, Nov 26, 2013 at 9:21 AM, wrote:
    Here is the relevant code (not pretty, I know):
    mat := [16]float32(matrix)// HERE IS THE RELEVANT PART

    Now, the unusual thing is that the gfx package, which the above code is part
    of, compiles fine and tests fine. But when I try to compile the whole
    program, the compiler complains about the cast:

    src/gfx/renderer.go:82: cannot convert matrix (type glam.Mat4) to type
    [16]float32

    What is the definition of the type glam.Mat4?

    Ian

    --
    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/groups/opt_out.
  • Péter Szilágyi at Nov 26, 2013 at 7:21 pm
    I'm guess he's using this library:
    https://github.com/drakmaniso/glam/blob/master/mat4.go, hence

    type Mat4 [4][4]float32

    On Tue, Nov 26, 2013 at 7:29 PM, Ian Lance Taylor wrote:
    On Tue, Nov 26, 2013 at 9:21 AM, wrote:

    Here is the relevant code (not pretty, I know):
    mat := [16]float32(matrix)// HERE IS THE RELEVANT PART

    Now, the unusual thing is that the gfx package, which the above code is part
    of, compiles fine and tests fine. But when I try to compile the whole
    program, the compiler complains about the cast:

    src/gfx/renderer.go:82: cannot convert matrix (type glam.Mat4) to type
    [16]float32

    What is the definition of the type glam.Mat4?

    Ian

    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
  • Ian Lance Taylor at Nov 26, 2013 at 7:23 pm

    On Tue, Nov 26, 2013 at 11:21 AM, Péter Szilágyi wrote:
    I'm guess he's using this library:
    https://github.com/drakmaniso/glam/blob/master/mat4.go, hence

    type Mat4 [4][4]float32
    Thanks. In that case, the answer is that [4][4]float32 is not
    [16]float32, and that is why the conversion fails.

    Ian

    On Tue, Nov 26, 2013 at 7:29 PM, Ian Lance Taylor wrote:
    On Tue, Nov 26, 2013 at 9:21 AM, wrote:

    Here is the relevant code (not pretty, I know):
    mat := [16]float32(matrix)// HERE IS THE RELEVANT PART

    Now, the unusual thing is that the gfx package, which the above code is
    part
    of, compiles fine and tests fine. But when I try to compile the whole
    program, the compiler complains about the cast:

    src/gfx/renderer.go:82: cannot convert matrix (type glam.Mat4) to type
    [16]float32

    What is the definition of the type glam.Mat4?

    Ian

    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
  • Oppainless at Nov 27, 2013 at 5:46 am
    That was the original version, yes, but as I said in my first post, I've
    modified the Mat4 type to [16]float32 as it makes more sense to me. It is,
    however, useful to know that I can use unsafe.Pointer to convert the types,
    so thanks a lot for the tip!

    Anyways, I still don't understand why I the conversion works when running a
    unit test on the relevant package, but not when I'm compiling the whole
    program; that seems very odd to me...

    Br.
    Andreas

    Den tisdagen den 26:e november 2013 kl. 21:23:51 UTC+2 skrev Ian Lance
    Taylor:
    On Tue, Nov 26, 2013 at 11:21 AM, Péter Szilágyi wrote:
    I'm guess he's using this library:
    https://github.com/drakmaniso/glam/blob/master/mat4.go, hence

    type Mat4 [4][4]float32
    Thanks. In that case, the answer is that [4][4]float32 is not
    [16]float32, and that is why the conversion fails.

    Ian

    On Tue, Nov 26, 2013 at 7:29 PM, Ian Lance Taylor wrote:

    On Tue, Nov 26, 2013 at 9:21 AM, <oppai...@gmail.com <javascript:>>
    wrote:
    Here is the relevant code (not pretty, I know):
    mat := [16]float32(matrix)// HERE IS THE RELEVANT PART

    Now, the unusual thing is that the gfx package, which the above code
    is
    part
    of, compiles fine and tests fine. But when I try to compile the whole
    program, the compiler complains about the cast:

    src/gfx/renderer.go:82: cannot convert matrix (type glam.Mat4) to
    type
    [16]float32

    What is the definition of the type glam.Mat4?

    Ian

    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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/groups/opt_out.
  • Luzon83 at Nov 26, 2013 at 7:28 pm

    To reinterpret [4][4]float32 as [16]float32 package "unsafe" is necessary:

    var a [4][4]float32
    var b [16]float32
    b = *(*[16]float32)(unsafe.Pointer(&a))

    --
    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/groups/opt_out.
  • Ross Light at Nov 26, 2013 at 10:04 pm
    It's not necessary to use unsafe to convert, but it is necessary if you
    want to do it without copying. luzon83's example still performs a copy.

    var a [4][4]float32
    var b *[16]float32
    b = (*[16]float32)(unsafe.Pointer(&a))

    And then presumably, you would pass b to UniformMatrix4f.


    Ross Light | Software Engineer | light@google.com

    On Tue, Nov 26, 2013 at 11:28 AM, wrote:
    On Tuesday, November 26, 2013 8:21:12 PM UTC+1, Péter Szilágyi wrote:

    I'm guess he's using this library: https://github.com/
    drakmaniso/glam/blob/master/mat4.go<https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fdrakmaniso%2Fglam%2Fblob%2Fmaster%2Fmat4.go&sa=D&sntz=1&usg=AFQjCNGhaP4qjDBRjeq2q217T7YQrgobjg>,
    hence

    type Mat4 [4][4]float32
    To reinterpret [4][4]float32 as [16]float32 package "unsafe" is necessary:

    var a [4][4]float32
    var b [16]float32
    b = *(*[16]float32)(unsafe.Pointer(&a))

    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
  • Kevin Gillette at Nov 27, 2013 at 1:08 am
    Every time a [4][4]float32 is passed anywhere it's being copied anyway,
    though it's hardly a large thing to copy.
    On Tuesday, November 26, 2013 3:03:55 PM UTC-7, Ross Light wrote:

    It's not necessary to use unsafe to convert, but it is necessary if you
    want to do it without copying. luzon83's example still performs a copy.

    var a [4][4]float32
    var b *[16]float32
    b = (*[16]float32)(unsafe.Pointer(&a))

    And then presumably, you would pass b to UniformMatrix4f.


    Ross Light | Software Engineer | li...@google.com <javascript:>


    On Tue, Nov 26, 2013 at 11:28 AM, <luz...@gmail.com <javascript:>> wrote:
    On Tuesday, November 26, 2013 8:21:12 PM UTC+1, Péter Szilágyi wrote:

    I'm guess he's using this library: https://github.com/
    drakmaniso/glam/blob/master/mat4.go<https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fdrakmaniso%2Fglam%2Fblob%2Fmaster%2Fmat4.go&sa=D&sntz=1&usg=AFQjCNGhaP4qjDBRjeq2q217T7YQrgobjg>,
    hence

    type Mat4 [4][4]float32
    To reinterpret [4][4]float32 as [16]float32 package "unsafe" is necessary:

    var a [4][4]float32
    var b [16]float32
    b = *(*[16]float32)(unsafe.Pointer(&a))

    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 26, '13 at 1:24p
activeNov 27, '13 at 5:46a
posts10
users6
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase