Hello! I'm looking into Kotlin/Native and wanted t...
# kotlin-native
u
Hello! I'm looking into Kotlin/Native and wanted to use
cglm
library (https://github.com/recp/cglm) for supporting OpenGL maths. Though, I have some problems with it. Details in 🧵.
It works fine when using `FloatArray`:
Copy code
fun foo() {
    val fdata = floatArrayOf(
        1f, 2f, 4f,
        2f, 5f, 3f,
        1f, 1f, 1f,
    )
    fdata.usePinned {
        glm_mat3_inv(it.addressOf(0), it.addressOf(0))
    }
    println(fdata.joinToString())
}
-----
-0.25 -0.25 1.75 -0.125 0.375 -0.625 0.375 -0.125 -0.125
Though, I have some weird behavoiur regarding defined constants:
Copy code
fun bar() {
    val x = GLM_YUP
    x?.let {
        println("${x[0]} ${x[1]} ${x[2]}")
    }
}
-----
0.0 1.1750706E-38 0.0
-----
where

typedef float                   vec3[3];
#define GLM_YUP       ((vec3){0.0f,  1.0f,  0.0f})
GLM_YUP
type is
CPointer<FloatVar>
, so I thought it was the right way to dereference it, but apparently not. What's the right way to get values from it?
d
What you've written looks right.
u
Then maybe I'm building/interoping it wrong way? Gradle tasks look like that:
Copy code
val cmakeGlm by tasks.creating(Exec::class) {
    dependsOn(unzipGlm)
    environment("PATH", "${mingwPath.resolve("bin").absolutePath};${System.getenv("PATH")}")
    glmBuildDir.mkdir()
    workingDir = glmBuildDir
    executable(cmakePath.resolve("cmake"))
    args("..", "-DCGLM_STATIC=ON", "-G", "\"MinGW Makefiles\"")
}

val makeGlm by tasks.creating(Exec::class) {
    dependsOn(cmakeGlm)
    workingDir = glmBuildDir
    executable(mingwPath.resolve("bin/mingw32-make"))
}
CInterop:
Copy code
val glm by creating {
                tasks[interopProcessingTaskName].dependsOn(makeGlm)
                includeDirs(glmDir.resolve("include/cglm"))
            }

----
glm.def
package = glm
headers = cglm.h struct.h call.h

staticLibraries = libcglm.a
libraryPaths = build/downloads/cglm-0.7.9/build
compilerOpts = -DCGLM_USE_ANONYMOUS_STRUCT -DCGLM_STATIC
Also, I don't get some of the defines. For example:
include/cglm/sturct/vec2.h
contains
#define GLMS_VEC2_ONE_INIT   {GLM_VEC2_ONE_INIT}
which doesn't end up in klib symbols.
Apparently the problem is related to some of the functions and constants as this works too:
Copy code
fun zar() {
    memScoped {
        val projection: mat3 = allocArrayOf(
            1f, 2f, 4f,
            2f, 5f, 3f,
            1f, 1f, 1f
        )
        glm_mat3_inv(projection, projection)
        for (i in 0 until 9) {
            print(projection[i])
            print(' ')
        }
    }
}
-----
-0.25 -0.25 1.75 -0.125 0.375 -0.625 0.375 -0.125 -0.125
And this doesn't:
Copy code
fun zar() {
    memScoped {
        val view: mat4 = allocArray(16)
        glm_mat4_identity(view)
        for (i in 0 until 16) {
            print(view[i])
            print(' ')
        }
    }
}
-----
Execution failed for task ':sandbox:runDebugExecutableNative'.
> Process 'command 'C:\_Code\opengl-kotlin\sandbox\build\bin\native\debugExecutable\sandbox.exe'' finished with non-zero exit value -1073741819