1.将GL线程执行函数调用转到c层代码。
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
class MyGLRenderer implements GLSurfaceView.Renderer
{
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
NativeInterface.InitOpenGL();
}
public void onSurfaceChanged(GL10 gl, int width, int height)
{
NativeInterface.OnViewportChanged(width,height);
}
public void onDrawFrame(GL10 gl)
{
NativeInterface.RenderOneFrame();
}
}
class MyGLSurface extends GLSurfaceView
{
MyGLRenderer mRenderer;
public MyGLSurface(Context context)
{
super(context);
setEGLContextClientVersion(2);
mRenderer=new MyGLRenderer();
setRenderer(mRenderer);
}
}
public class MainActivity extends AppCompatActivity {
MyGLSurface mView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mView=new MyGLSurface(getApplication());
setContentView(mView);
}
}
2.创建jni接口。
public class NativeInterface {
static {
System.loadLibrary("mygles");
}
public static native void InitOpenGL();
public static native void OnViewportChanged(float width,float height);
public static native void RenderOneFrame();
}
3.jni层处理
Android.mk
LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:=libmygles
LOCAL_SRC_FILES:=opengl.cpp
LOCAL_LDLIBS:=-llog -lGLESv2
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_ABI:=all
APP_STL:=gnustl_static
APP_CPPFLAGS:=-std=c++11
opengl.cpp
#include<android/log.h>
#include<GLES2/gl2.h>
#include<GLES2/gl2ext.h>
#include<jni.h>
extern "C" JNIEXPORT void Java_cn_battlefire_firstandroidapp_NativeInterface_InitOpenGL(JNIEnv*env,jobject obj)
{
glClearColor(0.1f,0.4f,0.7f,1.0f);
}
extern "C" JNIEXPORT void Java_cn_battlefire_firstandroidapp_NativeInterface_OnViewportChanged(JNIEnv*env,jobject obj,jfloat width,jfloat height)
{
glViewport(0,0,width,height);
}
extern "C" JNIEXPORT void Java_cn_battlefire_firstandroidapp_NativeInterface_RenderOneFrame(JNIEnv*env,jobject obj)
{
glClear(GL_COLOR_BUFFER_BIT);
}
画三角形demo:
opengl.cpp
#include<android/log.h>
#include<GLES2/gl2.h>
#include<GLES2/gl2ext.h>
#include<jni.h>
#include"Glm/glm.hpp"
#include"Glm/ext.hpp"
GLuint vbo;
GLuint simple_program;
GLint MLocation,VLocation,PLocation,positionLocation;
glm::mat4 identityMatrix;
glm::mat4 projectionMatrix;
#define info(...) __android_log_print(ANDROID_LOG_INFO,"BattleFire",__VA_ARGS__)
const char*vsCode="attribute vec3 position;\n"
"uniform mat4 M;\n"
"uniform mat4 V;\n"
"uniform mat4 P;\n"
"void main()\n"
"{\n"
"gl_Position=P*V*M*vec4(position,1.0);\n"
"}\n";
const char*fsCode="void main()\n"
"{\n"
"gl_FragColor=vec4(1.0,1.0,1.0,1.0);\n"
"}\n";
GLuint CompileShader(GLenum shaderType,const char*code)
{
if(code==nullptr)
{
info("compile shader error,code is null");
return 0;
}
GLuint shader=glCreateShader(shaderType);
glShaderSource(shader,1,&code,nullptr);
glCompileShader(shader);
//check error
GLint compileResult=0;
glGetShaderiv(shader,GL_COMPILE_STATUS,&compileResult);
if(compileResult==GL_FALSE)
{
info("compile shader error with code : %s",code);
char szLogBuffer[1024]={0};
GLsizei realLogLen=0;
glGetShaderInfoLog(shader,1024,&realLogLen,szLogBuffer);
info("error log : %s",szLogBuffer);
glDeleteShader(shader);
return 0;
}
return shader;
}
GLuint CreateProgram(const char*vsCode,const char*fsCode)
{
GLuint program=glCreateProgram();
GLuint vsShader=CompileShader(GL_VERTEX_SHADER,vsCode);
if(vsShader==0)
{
info("compile vs shader fail");
return 0;
}
GLuint fsShader=CompileShader(GL_FRAGMENT_SHADER,fsCode);
if(fsShader==0)
{
info("compile fs shader fail");
return 0;
}
glAttachShader(program,vsShader);
glAttachShader(program,fsShader);
glLinkProgram(program);
//check error
glDetachShader(program,vsShader);
glDetachShader(program,fsShader);
glDeleteShader(vsShader);
glDeleteShader(fsShader);
GLint linkResult=0;
glGetProgramiv(program,GL_LINK_STATUS,&linkResult);
if(linkResult==GL_FALSE)
{
info("link program error ");
char szLogBuffer[1024];
GLsizei realLogLen=0;
glGetShaderInfoLog(program,1024,&realLogLen,szLogBuffer);
info("error log : %s",szLogBuffer);
glDeleteProgram(program);
return 0;
}
return program;
}
extern "C" JNIEXPORT void Java_cn_battlefire_firstandroidapp_NativeInterface_InitOpenGL(JNIEnv*env,jobject obj)
{
glClearColor(0.1f,0.4f,0.7f,1.0f);
float vertices[]={
-5.0f,-5.0f,-10.0f,
5.0f,-5.0f,-10.0f,
0.0f,5.0f,-10.0f
};
simple_program=CreateProgram(vsCode,fsCode);
positionLocation=glGetAttribLocation(simple_program,"position");
MLocation=glGetUniformLocation(simple_program,"M");
VLocation=glGetUniformLocation(simple_program,"V");
PLocation=glGetUniformLocation(simple_program,"P");
glGenBuffers(1,&vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glBufferData(GL_ARRAY_BUFFER,sizeof(float)*9,vertices,GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER,0);
}
extern "C" JNIEXPORT void Java_cn_battlefire_firstandroidapp_NativeInterface_OnViewportChanged(JNIEnv*env,jobject obj,jfloat width,jfloat height)
{
glViewport(0,0,width,height);
projectionMatrix=glm::perspective(50.0f,width/height,0.1f,1000.0f);
}
extern "C" JNIEXPORT void Java_cn_battlefire_firstandroidapp_NativeInterface_RenderOneFrame(JNIEnv*env,jobject obj)
{
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(simple_program);
//update gpu program args
glUniformMatrix4fv(MLocation,1,GL_FALSE,glm::value_ptr(identityMatrix));
glUniformMatrix4fv(VLocation,1,GL_FALSE,glm::value_ptr(identityMatrix));
glUniformMatrix4fv(PLocation,1,GL_FALSE,glm::value_ptr(projectionMatrix));
glBindBuffer(GL_ARRAY_BUFFER,vbo);
//set vertex data
glEnableVertexAttribArray(positionLocation);
glVertexAttribPointer(positionLocation,3,GL_FLOAT,GL_FALSE,sizeof(float)*3,0);
glBindBuffer(GL_ARRAY_BUFFER,0);
//draw
glDrawArrays(GL_TRIANGLES,0,3);
glUseProgram(0);
glFlush();
}