Install OpenGL ES and compile code for android

18,795

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
18,795
Denys S.
Author by

Denys S.

We keep moving forward, opening new doors, and doing new things, because we're curious and curiosity keeps leading us down new paths. (Walt Disney) Imagination is more important than knowledge. For knowledge is limited to all we now know and understand, while imagination embraces the entire world, and all there ever will be to know and understand. (Albert Einstein)

Updated on July 28, 2022

Comments

  • Denys S.
    Denys S. almost 2 years

    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, 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)
    
  • Matthew Willis
    Matthew Willis about 13 years
    I'm able to successfully ndk-build with my added instructions
  • Denys S.
    Denys S. about 13 years
    Yeah, I added a default.properties file with target=android-8 string and it compiled (the glbuffer).
  • Denys S.
    Denys S. about 13 years
    Hm.. I'm too slow, yet. :D Thanks for help. As for the book sources, guy compiled with a separate arm gcc and libs pulled from android device, so he used things like EGL/egl.h in it, not sure if it's strictly required or if this lib is being fetched automatically. Will have to check. Thanks again!
  • Denys S.
    Denys S. about 13 years
    It has some syntax mistakes which compiler brings up, but that's the other thing. I successfully compiled glbuffer, so everything else is my next task of "proting" the sources and will be mostly trivial.
  • Zennichimaro
    Zennichimaro almost 12 years
    # The ARMv7 is significanly faster due to the use of the hardware FPU APP_ABI := armeabi armeabi-v7a APP_PLATFORM := android-9 This solves my problem as well
  • asloob
    asloob almost 11 years
    arrived at this question looking for how to compile EGL/egl.h header in Android. This worked for me. Compiled using APP_PLATFORM = android-14