我刚刚开始在 Android 上学习 OpenGL ES(使用 this book ),遇到了采用 source 的问题从第 5 章的代码到在 android 中使用 jni 的现有方法(实际上,它还涉及简单地运行 native GL 应用程序)。我正在尝试编译 native 代码以获取 .so 库并在 .apk 存档中进一步使用它。但如果某些库不存在(如 GLES/gl.h、EGL/egl.h、GLES/gl.h、GLES/glext.h),则无法进行编译。
所以问题是如何安装这些库(AFAIU、OpenGL ES 和 EGL 安装)并编译最简单的 native 代码? (教程非常受欢迎)。
提前致谢。
编辑:我试过 glbuffer建议的示例(稍微更改 .mk 文件),但仍然没有成功。编译器给我和以前一样的结果:
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
这是 cube.c 代码:
#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); }
这太琐碎了,还没有用。
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)
最佳答案
这些库由 Android 本身提供。但是,设置您的项目以找到它们并正确编译您的 JNI( native )代码可能会令人生畏。
我推荐使用 glbuffer作为一个起始项目,因为它将为您提供一个 GLSurfaceView
以供您使用并设置适当的 Android 库。
链接到 Android 库的详细信息包含在该项目的 jni/Android.mk
中,如果您想自己从头开始尝试的话。
编辑 - 显然缺少 glbuffer jni/Application.mk
。创建它并将其放入:
APP_ABI := armeabi armeabi-v7a
APP_PLATFORM := android-8
然后 ndk 将知道在 android-8 平台内部查找您的包含。您可以根据需要将其更改为其他版本。
关于android - 安装 OpenGL ES 并为 android 编译代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5300686/