1.创建NKD项目
2.引入openglES 依赖
target_link_libraries(android GLESv2)
3.具体的代码
setEGLContextClientVersion(2);
glClearColor(.1f,.4f,.6f,1.0f);
glViewport(0,0,width,height);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
public class GlSurfaceViewRenderer implements GLSurfaceView.Renderer {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
onSurfaceCreated();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
onSurfaceChanged(width,height);
}
@Override
public void onDrawFrame(GL10 gl) {
onDrawFrame();
}
public static native void onSurfaceCreated();
public static native void onSurfaceChanged(int width,int height);
public static native void onDrawFrame();
}
public class MainActivity extends AppCompatActivity {
static {
System.loadLibrary("native-lib");
}
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MGLSurfaceView mglSurfaceView = new MGLSurfaceView(this);
setContentView(mglSurfaceView);
}
class MGLSurfaceView extends GLSurfaceView{
private GlSurfaceViewRenderer glSurfaceViewRenderer;
public MGLSurfaceView(Context context) {
super(context);
setEGLContextClientVersion(2);
glSurfaceViewRenderer = new GlSurfaceViewRenderer();
setRenderer(glSurfaceViewRenderer);
}
}
public native String stringFromJNI();
}
#include <jni.h>
#include <string>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <GLES2/gl2platform.h>
extern "C"
JNIEXPORT void JNICALL
Java_com_example_openglesdemo1_GlSurfaceViewRenderer_onSurfaceCreated(JNIEnv *env, jclass clazz) {
glClearColor(.1f,.4f,.6f,1.0f);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_openglesdemo1_GlSurfaceViewRenderer_onSurfaceChanged(JNIEnv *env, jclass clazz,
jint width, jint height) {
glViewport(0,0,width,height);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_openglesdemo1_GlSurfaceViewRenderer_onDrawFrame(JNIEnv *env, jclass clazz) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
}
4.运行结果
|