Skip to content

Commit

Permalink
android works (jni + java + opengl)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriy-chumak committed Nov 26, 2024
1 parent 0caae97 commit 048238d
Show file tree
Hide file tree
Showing 15 changed files with 463 additions and 17 deletions.
8 changes: 8 additions & 0 deletions examples/android-opengl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
bin/
dex/
gen/
lib/
obj/

assets/libraries/
src/lang/
33 changes: 33 additions & 0 deletions examples/android-opengl/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="name.yuriychumak.ol.gui"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET" />

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="22" />

<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:screenOrientation="landscape"
android:name=".OpenGLActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
58 changes: 58 additions & 0 deletions examples/android-opengl/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
.PHONY: help
help:
@echo "Targets:"
@echo " clean - clean the folder from temp files"
@echo " build - build a regular android OpenGL app"

# ---------------------------------------------------
# link assets libraries
$(shell [ -d assets/libraries/OpenGL ] || ln -s ../../../../libraries/OpenGL assets/libraries/OpenGL)
$(shell [ -d assets/libraries/lib ] || ln -s ../../../../libraries/lib assets/libraries/lib)
$(shell [ -d assets/libraries/otus ] || ln -s ../../../../libraries/otus assets/libraries/otus)

# link sources
$(shell [ -d src/lang/otuslisp ] || mkdir -p src/lang/otuslisp)
$(shell [ -f src/lang/otuslisp/Ol.java ] || ln -s ../../../../../extensions/java/lang/otuslisp/Ol.java src/lang/otuslisp/Ol.java)


# ===================================================
APK_BASENAME?=debug

ANDROID_SDK?=/opt/android/sdk
ANDROID_NDK?=/opt/android/ndk
BUILD_TOOLS?=$(ANDROID_SDK)/build-tools/29.0.2

ifeq ("$(wildcard $(ANDROID_SDK)/)","")
$(error ANDROID_SDK not set or invalid!)
endif

ifeq ("$(wildcard $(ANDROID_NDK)/)","")
$(error ANDROID_NDK not set or invalid!)
endif

.PHONY: all build logcat

build:
./build

clean:
rm -rf bin dex gen lib obj

install:
adb -d install debug.apk
# grant default permissions
# adb shell pm grant name.yuriychumak.ol.gui android.permission.READ_EXTERNAL_STORAGE
uninstall:
adb -d uninstall name.yuriychumak.ol.gui

start:
adb shell am start -n name.yuriychumak.ol.gui/name.yuriychumak.ol.gui.OpenGLActivity

stop:
adb shell am force-stop name.yuriychumak.ol.gui

logcat:
adb logcat -v color \
ol:D name.yuriychumak.ol.gui:V gl2es:D \
threaded_app:V nativeloader:E \
AndroidRuntime:E DEBUG:V *:F
9 changes: 9 additions & 0 deletions examples/android-opengl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Type `make` or `make help` to view the targets list.

Targets:
* Type `make build` to build the regular OpenGL example ("name.yuriychumak.ol.gui.opengl").
* ~~Type `make build-stereo` to build the simple stereo pair OpenGL example ("name.yuriychumak.ol.gui.opengl.stereo").~~
* ~~Type `make build-anaglyph` to build the anaglypth stereo pair OpenGL example.~~
* ~~Type `make build-vr` to build the cheap VR OpenGL example (for the lenses and regular smartphones).~~
* ~~Type `make build-vr-quest` to build ~~
* ~~Type `make build-xr` to build real vr based on OpenXR for vr headset (Oculus Quest).~~
77 changes: 77 additions & 0 deletions examples/android-opengl/assets/main.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env ol
(print "*path*: " *path*)

(import (lib gl)
(lib GLU)
(OpenGL 2.1))

(import (lib soil))
(import (scheme inexact))

(glClearColor 0.2 0.2 0.2 1)

(define vertices [
[ 1 1 1] ;1
[ 1 1 -1] ;2
[-1 1 -1] ;3
[-1 1 1] ;4

[ 1 -1 1] ;5
[ 1 -1 -1] ;6
[-1 -1 -1] ;7
[-1 -1 1] ;8
])
(define indices (list
[1 2 3 4] ; top (yellow)
[1 5 6 2] ; right (orange)
[1 4 8 5] ; front (green)
[4 3 7 8] ; red (left)
[2 6 7 3] ; back (blue)
[5 8 7 6] ; while (bottom)
))
(define colors (list
'(1 1 0)
'(1 0.31 0)
'(0 1 0)
'(1 0 0)
'(0 0 1)
'(1 1 1)
))

; init
(glEnable GL_DEPTH_TEST)
(glEnable GL_CULL_FACE)
(glCullFace GL_BACK)

; draw
(gl:set-renderer (lambda (mouse)
(define aspect (/ (gl:get-window-width) (gl:get-window-height)))
(glClear (bor GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))

; classical projection matrix
(glMatrixMode GL_PROJECTION)
(glLoadIdentity)
(gluPerspective 45 aspect 0.1 1000)

(glMatrixMode GL_MODELVIEW)
(glLoadIdentity)
(define t (/ (mod (time-ms) 6283) #i1000))
(define Y 2)
(define R 5)
(gluLookAt (* R (sin t)) Y (* R (cos t))
0 0 0
0 1 0)

(glBegin GL_QUADS)
(for-each (lambda (index color)
(glColor3fv color)
(glVertex3fv (ref vertices (ref index 1)))
(glVertex3fv (ref vertices (ref index 2)))
(glVertex3fv (ref vertices (ref index 3)))
(glVertex3fv (ref vertices (ref index 4))) )
indices colors)
(glEnd)

(glFinish) ; на самом деле не нужен, но пусть будет
))
(print "ok.")
72 changes: 72 additions & 0 deletions examples/android-opengl/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/bash

ANDROID_SDK=${ANDROID_SDK:=/opt/android/sdk}

[ x$ANDROID_SDK == x ] && {
printf '\e[31mANDROID_SDK not set\e[30m\n'
exit 1
}

[ ! -d $ANDROID_SDK ] && {
printf "\e[31mInvalid ANDROID_SDK ---> $ANDROID_SDK\e[30m\n"
exit 1
}

# .
APK_BASENAME=debug

export PATH=$PATH:$ANDROID_SDK/platform-tools
BUILD_TOOLS=$ANDROID_SDK/build-tools/29.0.2

# Build Native Code
make -C ../.. android || exit

# Build
$BUILD_TOOLS/aapt package -f -m \
-S res -J src -M AndroidManifest.xml \
-I $ANDROID_SDK/platforms/android-29/android.jar

# Compile java
# Compile NativeLoader.java
mkdir -p obj
javac -verbose -source 1.8 -target 1.8 -d obj \
-bootclasspath jre/lib/rt.jar \
-classpath $ANDROID_SDK/platforms/android-29/android.jar:obj \
-sourcepath src `find . -name *.java` || exit


# Generate android bytecode
mkdir -p dex
$BUILD_TOOLS/dx --verbose --dex --output=dex/classes.dex obj

# Create APK with code, resources, and assets
$BUILD_TOOLS/aapt package -f \
-M AndroidManifest.xml -S res -A assets \
-I $ANDROID_SDK/platforms/android-29/android.jar -F debug.apk dex

# Add shared libraries to APK
mkdir -p lib
cp -r ../../libs/* lib
#rm lib/arm64-v8a/libvrapi.so
$BUILD_TOOLS/aapt add debug.apk `find -L lib/ -name *.so`

# # keytool -genkey -v -validity 14000 -dname "CN=Ol,O=Android,C=ES" -keystore debug.keystore -storepass android -keypass android -alias androiddebugkey -keyalg RSA
# # keytool -genkeypair -validity 1000 -dname "CN=Ol,O=Android,C=ES" -keystore otuslisp.keystore -storepass 'otuslisp' -keypass 'otuslisp' -alias projectKey -keyalg RSA
# jarsigner -keystore otuslisp.keystore -storepass otuslisp -keypass otuslisp \
# -signedjar debug.apk debug.apk projectKey
# jarsigner -keystore ./debug.keystore -sigalg SHA1withRSA -digestalg SHA1 -storepass android \
# -signedjar debug.apk debug.apk androiddebugkey

# Sign APK
# NOTE: If you changed the storepass and keypass in the setup process, change them here too
[ -f debug.keystore ] || keytool -genkeypair -validity 1000 -dname "CN=debug,O=Android,C=ES" -keystore debug.keystore -storepass 'debug0' -keypass 'debug0' -alias projectKey -keyalg RSA

jarsigner -keystore debug.keystore -storepass debug0 -keypass debug0 \
-signedjar debug.apk debug.apk projectKey

# Zipalign APK
$BUILD_TOOLS/zipalign -f 4 debug.apk debug.final.apk
mv -f debug.final.apk debug.apk

# Done
exit
20 changes: 20 additions & 0 deletions examples/android-opengl/proguard-project.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
14 changes: 14 additions & 0 deletions examples/android-opengl/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

# Project target.
target=android-21
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions examples/android-opengl/res/layout/main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="name.yuriychumak.ol.gui.OpenGLActivity" />
6 changes: 6 additions & 0 deletions examples/android-opengl/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Otus Lisp OpenGL Example</string>

</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package name.yuriychumak.ol.gui;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

// OpenGL support
import android.opengl.GLSurfaceView;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.egl.EGLConfig;
import android.opengl.GLES20;

import lang.otuslisp.Ol;
import name.yuriychumak.ol.gui.R;

public class OpenGLActivity extends Activity
implements GLSurfaceView.Renderer
{
private static String TAG = "ol";
private GLSurfaceView glView;

static {
// we store our libraries in the assets "libraries" folder:
try {
android.system.Os.setenv("OL_HOME", "libraries", true);
} catch (Exception ex) {
Log.e("ol", ex.toString());
}

// load native libraries
System.loadLibrary("GL2");
System.loadLibrary("SOIL");
System.loadLibrary("GLU");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate()");
super.onCreate(savedInstanceState);

requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN);

// ol jni setup (if we want to use "assets")
try {
Ol.nativeSetAssetManager(this.getAssets());
} catch (Exception ex) {
Log.e("ol", ex.toString());
}

glView = new GLSurfaceView(this);
glView.setEGLContextClientVersion(2);
glView.setRenderer(this);
// render mode style
glView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); /* or RENDERMODE_CONTINUOUSLY */
// glView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
setContentView(glView);
}


// GLSurfaceView.Renderer
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
Log.i(TAG, "onSurfaceCreated()");

Ol.load("main.lisp");
}

public void onSurfaceChanged(GL10 unused, int width, int height) {
Log.i(TAG, "onSurfaceChanged(" + width + ", " + height + ")");

// 'resize gl event
Ol.eval("(mail 'opengl ['resize " + width + " " + height + "])");
}

public void onDrawFrame(GL10 unused) {
Log.i(TAG, "onDrawFrame()");

// 'render gl event
Ol.eval("(import (lib gl))");
Ol.eval("(gl:force-render)");
}

}
Loading

0 comments on commit 048238d

Please sign in to comment.