Update Android sample, make it sim-friendly.

This commit is contained in:
Nikolay Igotti
2019-09-30 00:35:51 +03:00
committed by Nikolay Igotti
parent 3b36537dc9
commit 8e45e74937
17 changed files with 32 additions and 48 deletions
+3 -1
View File
@@ -12,7 +12,9 @@ We use JniBridge to call vibration service on the Java side for short tremble on
To build use `ANDROID_HOME=<your path to android sdk> ../gradlew assemble`. To build use `ANDROID_HOME=<your path to android sdk> ../gradlew assemble`.
Run `$ANDROID_HOME/platform-tools/adb install -r build/outputs/apk/debug/androidNativeActivity-debug.apk` Run `$ANDROID_HOME/platform-tools/adb install -r build/outputs/apk/debug/androidNativeActivity-debug.apk`
to deploy the apk on the Android device or emulator (note that only ARM-based devices are currently supported). to deploy the apk on the Android device or emulator.
Note that "Emulated Performance - Graphics" in AVD manager must be set to "Software - GLES 2.0".
Note: If you are importing project to IDEA for the first time, you might need to put `local.properties` file Note: If you are importing project to IDEA for the first time, you might need to put `local.properties` file
with the following content: with the following content:
+11 -7
View File
@@ -17,6 +17,8 @@ plugins {
val appDir = buildDir.resolve("Polyhedron") val appDir = buildDir.resolve("Polyhedron")
val libsDir = appDir.resolve("libs") val libsDir = appDir.resolve("libs")
// Set to true to build only for the simulator.
val simulatorOnly = true
val androidPresets = mapOf( val androidPresets = mapOf(
"arm32" to ("androidNativeArm32" to "$libsDir/armeabi-v7a"), "arm32" to ("androidNativeArm32" to "$libsDir/armeabi-v7a"),
@@ -40,7 +42,7 @@ android {
sourceSets { sourceSets {
val main by getting { val main by getting {
setRoot("src/arm32Main") setRoot("src/x86Main")
jniLibs.srcDir(libsDir) jniLibs.srcDir(libsDir)
} }
} }
@@ -63,13 +65,15 @@ kotlin {
} }
sourceSets { sourceSets {
val arm32Main by getting
val arm64Main by getting
val x86Main by getting val x86Main by getting
val x64Main by getting if (!simulatorOnly) {
arm64Main.dependsOn(arm32Main) val x64Main by getting
x86Main.dependsOn(arm32Main) val arm32Main by getting
x64Main.dependsOn(arm32Main) val arm64Main by getting
arm32Main.dependsOn(x86Main)
arm64Main.dependsOn(x86Main)
x64Main.dependsOn(x86Main)
}
} }
} }
@@ -6,6 +6,7 @@
android:versionName="1.0"> android:versionName="1.0">
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.VIBRATE" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<!-- This .apk has no Java code itself, so set hasCode to false. --> <!-- This .apk has no Java code itself, so set hasCode to false. -->
<application <application

Before

Width:  |  Height:  |  Size: 48 KiB

After

Width:  |  Height:  |  Size: 48 KiB

@@ -9,42 +9,7 @@ import kotlinx.cinterop.*
import platform.android.* import platform.android.*
import platform.egl.* import platform.egl.*
import platform.posix.* import platform.posix.*
import platform.gles3.* import platform.gles.*
import platform.gles.GL_PERSPECTIVE_CORRECTION_HINT
import platform.gles.GL_SMOOTH
import platform.gles.GL_PROJECTION
import platform.gles.glLoadIdentity
import platform.gles.glFrustumf
import platform.gles.glMatrixMode
import platform.gles.GL_MODELVIEW
import platform.gles.glTranslatef
import platform.gles.glShadeModel
import platform.gles.GL_LIGHT0
import platform.gles.GL_POSITION
import platform.gles.glMaterialfv
import platform.gles.GL_DIFFUSE
import platform.gles.GL_SHININESS
import platform.gles.glPushMatrix
import platform.gles.glRotatef
import platform.gles.glMultMatrixf
import platform.gles.GL_MODELVIEW_MATRIX
import platform.gles.glPopMatrix
import platform.gles.glTexEnvf
import platform.gles.GL_TEXTURE_ENV
import platform.gles.GL_TEXTURE_ENV_MODE
import platform.gles.glLightfv
import platform.gles.GL_LIGHTING
import platform.gles.GL_SPECULAR
import platform.gles.glMaterialf
import platform.gles.glTexEnvfv
import platform.gles.glEnableClientState
import platform.gles.GL_VERTEX_ARRAY
import platform.gles.GL_NORMAL_ARRAY
import platform.gles.GL_TEXTURE_COORD_ARRAY
import platform.gles.glVertexPointer
import platform.gles.glTexCoordPointer
import platform.gles.glNormalPointer
import platform.gles.GL_TEXTURE_ENV_COLOR
import sample.androidnative.bmpformat.BMPHeader import sample.androidnative.bmpformat.BMPHeader
class Renderer(val container: DisposableContainer, class Renderer(val container: DisposableContainer,
@@ -147,18 +112,20 @@ class Renderer(val container: DisposableContainer,
val ratio = width.value.toFloat() / height.value val ratio = width.value.toFloat() / height.value
glMatrixMode(GL_PROJECTION) glMatrixMode(GL_PROJECTION)
checkErrors()
glLoadIdentity() glLoadIdentity()
glFrustumf(-ratio, ratio, -1.0f, 1.0f, 1.0f, 10.0f) glFrustumf(-ratio, ratio, -1.0f, 1.0f, 1.0f, 10.0f)
glMatrixMode(GL_MODELVIEW) glMatrixMode(GL_MODELVIEW)
checkErrors()
glTranslatef(0.0f, 0.0f, -2.0f) glTranslatef(0.0f, 0.0f, -2.0f)
glLightfv(GL_LIGHT0, GL_POSITION, cValuesOf(1.25f, 1.25f, -2.0f, 0.0f)) glLightfv(GL_LIGHT0, GL_POSITION, cValuesOf(1.25f, 1.25f, -2.0f, 0.0f))
glEnable(GL_LIGHTING) glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0) glEnable(GL_LIGHT0)
glEnable(GL_TEXTURE_2D) glEnable(GL_TEXTURE_2D)
glMaterialfv(GL_FRONT, GL_DIFFUSE, cValuesOf(0.0f, 1.0f, 1.0f, 1.0f)) glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, cValuesOf(0.0f, 1.0f, 1.0f, 1.0f))
glMaterialfv(GL_FRONT, GL_SPECULAR, cValuesOf(0.3f, 0.3f, 0.3f, 1.0f)) glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, cValuesOf(0.3f, 0.3f, 0.3f, 1.0f))
glMaterialf(GL_FRONT, GL_SHININESS, 30.0f) glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 30.0f)
loadTexture("kotlin_logo.bmp") loadTexture("kotlin_logo.bmp")
@@ -167,6 +134,12 @@ class Renderer(val container: DisposableContainer,
} }
} }
fun checkErrors() {
val error = glGetError()
if (error.toInt() != GL_NO_ERROR)
throw Error("OpenGL error 0x${error.toInt().toString(16)}")
}
fun getState() = matrix to 16 * 4 fun getState() = matrix to 16 * 4
fun rotateBy(vector: Vector2) { fun rotateBy(vector: Vector2) {
@@ -180,6 +153,7 @@ class Renderer(val container: DisposableContainer,
glPushMatrix() glPushMatrix()
glMatrixMode(GL_MODELVIEW) glMatrixMode(GL_MODELVIEW)
checkErrors()
glLoadIdentity() glLoadIdentity()
glRotatef(angle, x, y, 0.0f) glRotatef(angle, x, y, 0.0f)
glMultMatrixf(matrix) glMultMatrixf(matrix)
@@ -191,6 +165,7 @@ class Renderer(val container: DisposableContainer,
private fun loadTexture(assetName: String): Unit = memScoped { private fun loadTexture(assetName: String): Unit = memScoped {
val asset = AAssetManager_open(nativeActivity.assetManager, assetName, AASSET_MODE_BUFFER.convert()) val asset = AAssetManager_open(nativeActivity.assetManager, assetName, AASSET_MODE_BUFFER.convert())
?: throw Error("Error opening asset $assetName") ?: throw Error("Error opening asset $assetName")
println("loading texture $assetName")
try { try {
val length = AAsset_getLength(asset) val length = AAsset_getLength(asset)
val buffer = allocArray<ByteVar>(length) val buffer = allocArray<ByteVar>(length)
@@ -209,6 +184,7 @@ class Renderer(val container: DisposableContainer,
data[i] = data[i + 2] data[i] = data[i + 2]
data[i + 2] = t data[i + 2] = t
} }
println("loaded texture ${width}x${height}")
glBindTexture(GL_TEXTURE_2D, 1) glBindTexture(GL_TEXTURE_2D, 1)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
@@ -234,6 +210,7 @@ class Renderer(val container: DisposableContainer,
glPushMatrix() glPushMatrix()
glMatrixMode(GL_MODELVIEW) glMatrixMode(GL_MODELVIEW)
checkErrors()
glMultMatrixf(matrix) glMultMatrixf(matrix)
@@ -294,7 +271,7 @@ class Renderer(val container: DisposableContainer,
} }
fun start() { fun start() {
logInfo("Starting renderer..") logInfo("Starting renderer.")
if (initialized) { if (initialized) {
if (eglMakeCurrent(display, surface, surface, context) == 0u) { if (eglMakeCurrent(display, surface, surface, context) == 0u) {
throw Error("eglMakeCurrent() returned error ${eglGetError()}") throw Error("eglMakeCurrent() returned error ${eglGetError()}")