I try to write multiplatform OpenGL wrapper.
OpenGL has function glBufferData(GLenum target, GLsizeiptr size, const GLvoid * data, GLenum usage)
What is the best candidate in kotlin for the third argument?
I find several solutions:
- kotlin-io.IoBuffer
- Array
- Multiplatform OpenGl is bad idea. Its better to write native wrapper for working with untyped arrays.
The best candidate for Buffers is probably kotlin-io.IoBuffer
.
Also if you really want to write this wrapper you might want take a look at GitHub - kotlin-graphics/gln: OpenGL Next: functional programming and DSL
I, @elect and a few others are working on a more kotlin friendly wrapper around lwjgl (opengl java bindings). I don’t know how hard it would be to move this project to mpp but it would probably be worth it in the long run. That way we have only one kotlin project for opengl and not multiple ones.
I personally don’t have much time to help you change it as I don’t have any experience with mpp and don’t have much time atm to learn it.
Native will most likely be better performing (especially if you know what you are doing). It just allows you to use the capabilities of the platform better. That being said, mpp is great since it would make it easier to develop 3d games for android and ios at once. I can’t really say whether it is worth it though. I don’t have much experience with android, none with ios and I don’t know anything about opengl on either platform, so maybe I am way off.
Not sure if you would ever want to share a game between pc and mobile (or at least I would have said that a few years ago), but nowadays there are a few prominent examples out there so maybe this would be useful for that as well.
1 Like
@eshim2009 hi, did you manage to make something workable?
I have a multiplatform (JVM and Native) wrapper for Vulkan here GitHub - Dominaezzz/kgl: Thin multiplatform wrappers for graphics. . It should give you an idea of how to handle void*
. Spoiler, I use kotlinx.io.core.IoBuffer
.
I also have native bindings for OpenGL, since glew, glad, etc don’t work well with cinterop.
Multiplatform OpenGL wrapper is WIP.
@eshim2009
void* pointer can be addressed using “usePinned” and “addressOf(0)” in kotlin
Example below
val vertex_array = floatArrayOf(
-0.8f, -0.8f, 0.0f,
0.8f, -0.8f, 0.0f,
0.0f, 0.8f, 0.0f
)
vertex_array.usePinned {
glBufferData!!(
GL_ARRAY_BUFFER.toUInt(),
vertex_array.size.toLong() * 4,
it.addressOf(0),
GL_STATIC_DRAW.toUInt()
)
}
For GLEW refer this
GLEW Example
For GLAD refer this
GLAD Example
For Vulkan refer this
Vulkan Triangle Example
using the above examples, you can use same old “C” code with few changes in kotlin as it is