Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing bitmap as TextureResource in android target #23

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions kgl-android/src/main/kotlin/com/danielgergely/kgl/KglAndroid.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.danielgergely.kgl

import android.graphics.Bitmap
import android.opengl.GLES20
import android.opengl.GLES30
import android.opengl.GLUtils

typealias GL = GLES20

Expand Down Expand Up @@ -184,6 +186,40 @@ object KglAndroid : Kgl {
}
}

override fun texSubImage2D(
target: Int,
level: Int,
xOffset: Int,
yOffset: Int,
width: Int,
height: Int,
format: Int,
type: Int,
buffer: Buffer
) {
buffer.withJavaBuffer { javaBuffer ->
GL.glTexSubImage2D(target, level, xOffset, yOffset, width, height, format, type, javaBuffer)
}
}

override fun texImage2D(target: Int, level: Int, internalFormat: Int, border: Int, resource: TextureAsset) {
resource.texImage2D(kgl = this, target, level, internalFormat, border)
}

override fun texSubImage2D(
target: Int,
level: Int,
xOffset: Int,
yOffset: Int,
width: Int,
height: Int,
format: Int,
type: Int,
resource: TextureAsset
) {
resource.texSubImage2D(kgl = this, target, level, xOffset, yOffset, width, height, format, type)
}

override fun activeTexture(texture: Int) = GL.glActiveTexture(texture)
override fun bindTexture(target: Int, texture: Texture?) = GL.glBindTexture(target, texture ?: 0)
override fun generateMipmap(target: Int) = GL.glGenerateMipmap(target)
Expand Down
60 changes: 60 additions & 0 deletions kgl-android/src/main/kotlin/com/danielgergely/kgl/Types.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.danielgergely.kgl

import android.graphics.Bitmap
import android.opengl.GLUtils

public class BitmapTextureAsset(
public val bitmap: Bitmap,
public val type: Int = -1,
) : TextureAsset {

public inline val width: Int get() = bitmap.width
public val height: Int get() = bitmap.height

override fun isValid(): Boolean {
return !bitmap.isRecycled
}

public override fun dispose() {}

override fun texImage2D(kgl : Kgl, target: Int, level: Int, internalFormat: Int, border: Int) {
if (internalFormat == -1) {
GLUtils.texImage2D(
target, level, bitmap, border
)
} else {
if (type == -1) {
GLUtils.texImage2D(
target, level, internalFormat, bitmap, border
)
} else {
GLUtils.texImage2D(
target, level, internalFormat, bitmap, type, border
)
}
}
}

override fun texSubImage2D(
kgl : Kgl,
target: Int,
level: Int,
xOffset: Int,
yOffset: Int,
width: Int,
height: Int,
format: Int,
type: Int
) {
if(format != -1 && type != -1){
GLUtils.texSubImage2D(
target, level, xOffset, yOffset, bitmap, format, type
)
} else {
GLUtils.texSubImage2D(
target, level, xOffset, yOffset, bitmap
)
}
}

}
34 changes: 34 additions & 0 deletions kgl-jogl/src/main/kotlin/com/danielgergely/kgl/KglJogl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,24 @@ class KglJogl(private val gl: GL) : Kgl {

override fun deleteTexture(texture: Texture) = gl.glDeleteTextures(1, intArrayOf(texture), 0)

override fun texImage2D(target: Int, level: Int, internalFormat: Int, border: Int, resource: TextureAsset) {
resource.texImage2D(kgl = this, target, level, internalFormat, border)
}

override fun texSubImage2D(
target: Int,
level: Int,
xOffset: Int,
yOffset: Int,
width: Int,
height: Int,
format: Int,
type: Int,
resource: TextureAsset
) {
resource.texSubImage2D(kgl = this, target, level, xOffset, yOffset, width, height, format, type)
}

override fun texImage2D(target: Int, level: Int, internalFormat: Int, border: Int, resource: TextureResource) {
texImage2D(
target = target,
Expand Down Expand Up @@ -241,6 +259,22 @@ class KglJogl(private val gl: GL) : Kgl {
}
}

override fun texSubImage2D(
target: Int,
level: Int,
xOffset: Int,
yOffset: Int,
width: Int,
height: Int,
format: Int,
type: Int,
buffer: Buffer
) {
buffer.withJavaBuffer { javaBuffer->
gl.glTexSubImage2D(target, level, xOffset, yOffset, width, height, format, type, javaBuffer)
}
}

override fun activeTexture(texture: Int) = gl.glActiveTexture(texture)

override fun bindTexture(target: Int, texture: Texture?) = gl.glBindTexture(target, texture ?: 0)
Expand Down
96 changes: 96 additions & 0 deletions kgl-lwjgl/src/main/kotlin/com/danielgergely/kgl/KglLwjgl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,24 @@ object KglLwjgl : Kgl {
)
}

override fun texImage2D(target: Int, level: Int, internalFormat: Int, border: Int, resource: TextureAsset) {
resource.texImage2D(kgl = this, target, level, internalFormat, border)
}

override fun texSubImage2D(
target: Int,
level: Int,
xOffset: Int,
yOffset: Int,
width: Int,
height: Int,
format: Int,
type: Int,
resource: TextureAsset
) {
resource.texSubImage2D(kgl = this, target, level, xOffset, yOffset, width, height, format, type)
}

override fun texImage2D(
target: Int,
level: Int,
Expand Down Expand Up @@ -263,6 +281,84 @@ object KglLwjgl : Kgl {
}
}

override fun texSubImage2D(
target: Int,
level: Int,
xOffset: Int,
yOffset: Int,
width: Int,
height: Int,
format: Int,
type: Int,
buffer: Buffer
) {
buffer.withJavaBuffer { javaBuffer ->
when (javaBuffer) {
is ByteBuffer -> GL.glTexSubImage2D(
target,
level,
xOffset,
yOffset,
width,
height,
format,
type,
javaBuffer
)

is ShortBuffer -> GL.glTexSubImage2D(
target,
level,
xOffset,
yOffset,
width,
height,
format,
type,
javaBuffer
)

is IntBuffer -> GL.glTexSubImage2D(
target,
level,
xOffset,
yOffset,
width,
height,
format,
type,
javaBuffer
)

is FloatBuffer -> GL.glTexSubImage2D(
target,
level,
xOffset,
yOffset,
width,
height,
format,
type,
javaBuffer
)

is DoubleBuffer -> GL.glTexSubImage2D(
target,
level,
xOffset,
yOffset,
width,
height,
format,
type,
javaBuffer
)

else -> throw IllegalArgumentException("unknown buffer type ${javaBuffer.javaClass}")
}
}
}

override fun texParameteri(target: Int, pname: Int, value: Int) {
GL.glTexParameteri(target, pname, value)
}
Expand Down
27 changes: 27 additions & 0 deletions kgl/src/commonMain/kotlin/com/danielgergely/kgl/Kgl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ public interface Kgl {
public fun createTextures(n: Int): Array<Texture>
public fun deleteTexture(texture: Texture)
public fun texImage2D(target: Int, level: Int, internalFormat: Int, border: Int, resource: TextureResource)

public fun texImage2D(target: Int, level: Int, internalFormat: Int, border: Int, resource: TextureAsset)

public fun texSubImage2D(
target: Int,
level: Int,
xOffset : Int,
yOffset : Int,
width: Int,
height: Int,
format: Int,
type: Int,
resource: TextureAsset
)

public fun texImage2D(
target: Int,
level: Int,
Expand All @@ -85,6 +100,18 @@ public interface Kgl {
buffer: Buffer
)

public fun texSubImage2D(
target: Int,
level: Int,
xOffset : Int,
yOffset : Int,
width: Int,
height: Int,
format: Int,
type: Int,
buffer: Buffer
)

public fun activeTexture(texture: Int)
public fun bindTexture(target: Int, texture: Texture?)
public fun generateMipmap(target: Int)
Expand Down
9 changes: 8 additions & 1 deletion kgl/src/commonMain/kotlin/com/danielgergely/kgl/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ public expect class UniformLocation

public expect class GlBuffer

public expect class TextureResource {
public expect interface TextureAsset {

/** if asset is valid, isRecycled == false on Android **/
public fun isValid() : Boolean

public fun dispose()

}

public expect class TextureResource : TextureAsset

public expect class Texture

public expect class VertexArrayObject
Expand Down
Loading