Skip to content

Commit

Permalink
android opengl example
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriy-chumak committed Nov 30, 2024
1 parent e2b773a commit 8ae91c9
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 131 deletions.
6 changes: 5 additions & 1 deletion examples/android-opengl/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ lib/
obj/

assets/libraries/
src/lang/
src/lang/otuslisp/Ol.java
src/name/yuriychumak/ol/gui/R.java
debug.keystore

*.apk
22 changes: 15 additions & 7 deletions examples/android-opengl/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ help:
@echo "Targets:"
@echo " clean - clean the folder from temp files"
@echo " build - build a regular android OpenGL app"
@echo " install - install the apk"
@echo " install - uninstall installed package"
@echo " start - start installed package"

# ---------------------------------------------------
# link assets libraries
$(shell [ -d assets/libraries ] || mkdir -p 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)
$(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)
Expand All @@ -30,7 +34,7 @@ ifeq ("$(wildcard $(ANDROID_NDK)/)","")
$(error ANDROID_NDK not set or invalid!)
endif

.PHONY: all build logcat
.PHONY: all build clean start stop restart install uninstall reinstall logcat

build:
./build
Expand All @@ -39,9 +43,9 @@ 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
adb -d install -r 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

Expand All @@ -51,8 +55,12 @@ start:
stop:
adb shell am force-stop name.yuriychumak.ol.gui

restart:
@$(MAKE) stop
@$(MAKE) start

logcat:
adb logcat -v color \
ol:D name.yuriychumak.ol.gui:V gl2es:D \
ol:D name.yuriychumak.ol.gui:V gl2es:E \
threaded_app:V nativeloader:E \
AndroidRuntime:E DEBUG:V *:F
40 changes: 40 additions & 0 deletions examples/android-opengl/assets/cube.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
(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)
))

(define (cube:draw)
(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))
60 changes: 12 additions & 48 deletions examples/android-opengl/assets/main.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,15 @@
(lib GLU)
(OpenGL 2.1))

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

(glClearColor 0.2 0.2 0.2 1)
,load "cube.lisp"

(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)
))
; common code
(import (scheme inexact))

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

; draw
(gl:set-renderer (lambda (mouse)
Expand All @@ -53,25 +25,17 @@
(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))

(glMatrixMode GL_MODELVIEW)
(glLoadIdentity)
(gluLookAt 0 Y R
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) ; на самом деле не нужен, но пусть будет
(define t (/ (mod (time-ms) 6283) #i1000))
(glTranslatef 0 0 0)
(glRotatef (* t 360/3.14) 0 1 0)
(cube:draw)
))
(print "ok.")
20 changes: 0 additions & 20 deletions examples/android-opengl/proguard-project.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.microedition.khronos.egl.EGLConfig;
import android.opengl.GLES20;

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

Expand All @@ -29,9 +30,9 @@ public class OpenGLActivity extends Activity
}

// load native libraries
System.loadLibrary("GL2");
System.loadLibrary("SOIL");
System.loadLibrary("GLU");
System.loadLibrary("gl2es"); // OpenGL 2.1 over GLES
System.loadLibrary("GLU"); // OpenGL Utility Library
System.loadLibrary("SOIL"); // Image loading Library
}

@Override
Expand All @@ -43,7 +44,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN);

// ol jni setup (if we want to use "assets")
// ol jni setup (we need to use assets data)
try {
Ol.nativeSetAssetManager(this.getAssets());
} catch (Exception ex) {
Expand All @@ -54,31 +55,33 @@ protected void onCreate(Bundle savedInstanceState) {
glView.setEGLContextClientVersion(2);
glView.setRenderer(this);
// render mode style
glView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); /* or RENDERMODE_CONTINUOUSLY */
// glView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
// 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()");
Log.d(TAG, "onSurfaceCreated()");

Ol.load("main.lisp");
// we needed (gl:force-render)
Ol.eval("(import (lib gl))");

}

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

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

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

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

Expand Down
7 changes: 7 additions & 0 deletions examples/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
bin/
dex/
gen/
lib/
obj/

src/name/yuriy_chumak/ol/R.java
2 changes: 1 addition & 1 deletion extensions/java/lang/otuslisp/Ol.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Ol
* just load and run.
*/
public static void load(String filename) {
Log.i(TAG, "load");
Log.i(TAG, "load \"" + filename + "\"");
eval(
"(define-library (" + "!!" + ")" +
"(import (otus lisp))" +
Expand Down
Loading

0 comments on commit 8ae91c9

Please sign in to comment.