Install OpenGL ES and compile code for android
I've just started learning OpenGL ES on android (using this book) and came across an issue of adopting source code from chapter 5 to existing methods of using jni in android (actually, it also concerns simply running a native GL app). I'm trying to compile the native code to get the .so lib and use it further in .apk archive. But compilation is not possible if certain libs are not present (which are GLES/gl.h, EGL/egl.h, GLES/gl.h, GLES/glext.h).
So the question is how do I install those libs (AFAIU, OpenGL ES and EGL installation) and compile the most trivial native code? (tutorials are highly admired).
Thanks in advance.
EDIT: I've tried the glbuffer example as was suggested (slightly changed .mk file), but still no success. Compiler gives me the same result as before:
ndk-build
Compile thumb: egl <= cube.c
/path/jni/cube.c:5:21: error: GLES/gl.h: No such file or directory // same message for glbuffer when gl.h is being included
Here is the cube.c code:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <GLES/gl.h>
#define FIXED_ONE 0x10000
#define one 1.0f
typedef unsigned char byte;
extern void jni_printf(char *format, ...);
// Cube static
GLfloat vertices[24] = { -one, -one, -one, one, -one,
-one, one, one, -one, -one, one, -one, -one, -one, one, one, -one, one, one, one, one, -one, one, one, };
static GLfloat colors[] = { 0, 0, 0, one, one, 0, 0, one, one, one, 0, one, 0, one, 0> , one, 0, 0, one, one, one, 0, one, one, one, one, one, one, 0, one, one, one, };
static byte indices[] = { 0, 4, 5, 0, 5, 1, 1, 5, 6, 1, 6, 2, 2, 6, 7, 2, 7, 3, 3, 7, 4, 3, 4, 0, 4, 7, 6, 4, 6, 5, 3, 0, 1, 3, 1, 2 };
void Cube_draw() {
glFrontFace(GL_CW);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(4, GL_FLOAT开发者_StackOverflow中文版, 0 , colors);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices); }
It's awfully trivial and not working, yet.
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -lGLESv1_CM.so
LOCAL_MODULE := egl
LOCAL_SRC_FILES := cube.c cuberenderer.c
include $(BUILD_SHARED_LIBRARY)
Those libraries are provided by Android itself. However, setting up your project to find them and compile your JNI (native) code correctly can be daunting.
I recommend using glbuffer as a starting project, as it will provide you with a GLSurfaceView
to draw on and set you up with the proper Android libraries.
The details of linking to the Android libraries are contained in jni/Android.mk
inside that project if you'd like to give it a shot yourself from scratch.
Edit - apparently glbuffer is missing jni/Application.mk
. Create it and put this inside:
APP_ABI := armeabi armeabi-v7a
APP_PLATFORM := android-8
Then the ndk will know to look inside the android-8 platform for your includes. You can change this to other versions as needed.
I searched the NDK for instances of the "EGL/egl.h" header file. This particular example will compile and run on Android API level 15, but some other API levels don't have the header.
I just added
#include <jni.h>
to cube.c & cuberenderer.c
Changed
(*g_VM)->AttachCurrentThread (g_VM, (void **) &env, NULL);
to
(*g_VM)->AttachCurrentThread (g_VM, (const struct JNINativeInterface ***) &env, NULL);
My Android.mk:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libgltest_jni
LOCAL_CFLAGS := -Werror
LOCAL_SRC_FILES := cube.c cuberenderer.c
LOCAL_LDLIBS := -llog
-lGLESv1_CM
include $(BUILD_SHARED_LIBRARY)
My Application.mk:
# The ARMv7 is significanly faster due to the use of the hardware FPU
APP_ABI := armeabi armeabi-v7a
APP_PLATFORM := android-9
And built it on android-ndk-r6
You used the filename GLES/glext.h twice.
精彩评论