新建项目
该项目的github地址:
https://github.com/sz66cm/SimplestGLSurfaceUse.git
按照NDK第一行代码构建JNI环境
构建NDK环境地址:
http://blog.csdn.net/sz66cm/article/details/52599224
C代码
#include <jni.h>
#include <android/log.h>
#include <GLES/gl.h>
#include <GLES/glext.h>
#define LOG_TAG "render.c"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
void InitializeOpenGL();
void resizeViewport(jint, jint);
void renderFrame();
JNIEXPORT jint JNI_OnLoad(JavaVM* pVM, void* reserved) {
LOGI("render.c JNI_OnLoad() start!");
return JNI_VERSION_1_6;
}
JNIEXPORT void Java_com_cm_glsurfaceview_Libcm_init(JNIEnv* env, jobject thiz)
{
LOGI("c init()");
InitializeOpenGL();
}
JNIEXPORT void Java_com_cm_glsurfaceview_Libcm_resize(JNIEnv* env,
jobject thiz,
jint width,
jint height)
{
LOGI("c resize()");
resizeViewport(width, height);
}
JNIEXPORT void Java_com_cm_glsurfaceview_Libcm_render(JNIEnv* env, jobject thiz)
{
LOGI("c render()");
renderFrame();
}
void InitializeOpenGL()
{
glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
}
void resizeViewport(jint w, jint h)
{
glViewport(0, 0, w, h);
}
void renderFrame()
{
glClear(GL_COLOR_BUFFER_BIT);
}
Java 调用 C 的包装代码:
package com.cm.glsurfaceview;
public class Libcm {
static {
System.loadLibrary("cmopengl");
}
public static native void init();
public static native void resize(int width, int height);
public static native void render();
}
GLSurfaceView使用代码:
package com.cm.glsurfaceview;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
private GLSurfaceView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new GLSurfaceView(MainActivity.this);
view.setEGLContextClientVersion(2);//这一行代码及其重要,不然可能会出现在JNI层,glCreateShader(...)一直返回0的问题
//set the renderer
view.setRenderer(new CMRenderer());
setContentView(view);
}
@Override
protected void onPause() {
super.onPause();
view.onPause();
}
@Override
protected void onResume() {
super.onResume();
view.onResume();
}
class CMRenderer implements GLSurfaceView.Renderer {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Libcm.init();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
Libcm.resize(width, height);
}
@Override
public void onDrawFrame(GL10 gl) {
Libcm.render();
}
}
}
Android.mk配置文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := cmopengl
LOCAL_SRC_FILES := ./cmopengles/render.c
LOCAL_LDLIBS += -llog
LOCAL_LDLIBS += -lGLESv2
# use GL ext model
LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES
include $(BUILD_SHARED_LIBRARY)
Application.mk配置文件
APP_ABI := all
# 以下该句不填写,在执行ndk-build的时候上报"GLES/gl.h : NO such file"错误.
APP_PLATFORM := android-14
出现的问题
问题1 “GLES/gl.h : No such file”
解决:
在Application.mk文件中添加如下代码:APP_PLATFORM := android-14
问题2 “将Apk烧入手机时报java.lang.UnsatisfiedLinkError”
解决:
将lib文件下所有的.so文件以及外层文件夹如:armeabi等删除,重新ndk-build解决问题.
问题3 java.lang.UnsatisfiedLinkError 找不到相对应的方法
原因是Android.mk文件中的LOCAL_SRC_FILES
写成了LOCAL_SRC_FILE
少了个S,居然ndk-build
没有任何的报错,还生成了.so包.我的天,搞了我一个中午的时间.