一、opengles工程搭建

文章介绍了在Android平台上使用OpenGLES进行图形渲染的过程,包括在布局中添加GLSurfaceView,创建自定义的GLSurfaceView并实现Renderer接口,以及在C++侧处理渲染的方法,如onSurfaceCreated、onSurfaceChanged和onDrawFrame等关键函数。同时,文章涉及了JNI的使用,动态加载C++库进行图形绘制。
摘要由CSDN通过智能技术生成

一、opengl之初识

界面采用FrameLayout添加自定义的GLSurfaceView的。

第一部分Java端

1)界面

<FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

</FrameLayout>

MainActivity.java中引入界面加载GLSurfaceView

public class MainActivity extends AppCompatActivity {

    private FrameLayout frameLayout;
    private MyGLSurfaceView myglesView;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        frameLayout = findViewById(R.id.frameLayout);
        myglesView = new MyGLSurfaceView(this);
        frameLayout.addView(myglesView);
        Log.d(TAG, "onCreate End");
    }
}

2)自定义的GLSurfaceView

MyGLSurfaceView需要继承GLSurfaceView并且实现GLSurfaceView.Renderer接口,其主要绘画是接口中的

onSurfaceCreated,onSurfaceChanged,onDrawFrame三个接口。

public class MyGLSurfaceView extends GLSurfaceView {
    private static final String TAG = "MyGLSurfaceView";

    public static final int IMAGE_FORMAT_RGBA = 0x01;
    public static final int IMAGE_FORMAT_NV21 = 0x02;
    public static final int IMAGE_FORMAT_NV12 = 0x03;
    public static final int IMAGE_FORMAT_I420 = 0x04;

    private MyGLRender mGLRender;
    private MyNativeRender mNativeRender;

    public MyGLSurfaceView(Context context) {
        this(context, null);
    }

    public MyGLSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.d(TAG, "MyGLSurfaceView");
        this.setEGLContextClientVersion(3);
        mNativeRender = new MyNativeRender();
        mGLRender = new MyGLRender(mNativeRender);
        setRenderer(mGLRender);//设置render
        setRenderMode(RENDERMODE_CONTINUOUSLY);//持续刷新
    }

    public MyNativeRender getNativeRender() {
        return mNativeRender;
    }

    public static class MyGLRender implements GLSurfaceView.Renderer {
        private MyNativeRender mReNativeRender;

        MyGLRender(MyNativeRender myNativeRender) {
            mReNativeRender = myNativeRender;
        }

        @Override
        public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
            Log.d(TAG, "onSurfaceCreated() called with: gl = [" + gl10 + "], config = [" + eglConfig + "]");
            mReNativeRender.native_OnSurfaceCreated();
        }

        @Override
        public void onSurfaceChanged(GL10 gl10, int width, int height) {
            Log.d(TAG, "onSurfaceChanged() called with: gl = [" + gl10 + "], width = [" + width + "], height = [" + height + "]");
            mReNativeRender.native_OnSurfaceChanged(width, height);
        }

        @Override
        public void onDrawFrame(GL10 gl10) {
            Log.d(TAG, "onDrawFrame() called with: gl = [" + gl10 + "]");
            mReNativeRender.native_OnDrawFrame();
        }
    }
}

3)native类

public class MyNativeRender {
    static {
        System.loadLibrary("Drawtriangle");
    }

    public native void native_OnInit();

    public native void native_OnUnInit();

    public native void native_SetImageData(int format, int width, int height, byte[] bytes);

    public native void native_OnSurfaceCreated();

    public native void native_OnSurfaceChanged(int width, int height);

    public native void native_OnDrawFrame();
}

第二部分C++端

1)使用动态jni加载的方式

//
// Created by CreatWall_zhouwen on 2023/3/30.
//

#include "jni.h"
#include <android/log.h>
#include <GLES2/gl2.h>
#include "Util.h"
#define NATIVE_RENDER_CLASS_NAME "com/example/onedrawtriangle/MyNativeRender"
#define TAG "GLTRIANGLE"
#ifdef __cplusplus
extern "C" {
#endif

/*
 * Class:     com_example_onedrawtriangle_MyNativeRender
 * Method:    native_OnInit
 * Signature: ()V
 */
JNIEXPORT void JNICALL native_OnInit (JNIEnv *env, jobject instance)
{

}

/*
 * Class:     com_example_onedrawtriangle_MyNativeRender
 * Method:    native_OnUnInit
 * Signature: ()V
 */
JNIEXPORT void JNICALL native_OnUnInit(JNIEnv *env, jobject instance)
{

}

/*
 * Class:     com_example_onedrawtriangle_MyNativeRender
 * Method:    native_SetImageData
 * Signature: (III[B)V
 */
JNIEXPORT void JNICALL native_SetImageData(JNIEnv *env, jobject instance, jint format, jint width, jint height, jbyteArray imageData)
{

}

/*
 * Class:     com_example_onedrawtriangle_MyNativeRender
 * Method:    native_OnSurfaceCreated
 * Signature: ()V
 */
JNIEXPORT void JNICALL native_OnSurfaceCreated (JNIEnv *env, jobject instance)
{
    // 指定刷新颜色缓冲区的颜色
    glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
}

/*
 * Class:     com_example_onedrawtriangle_MyNativeRender
 * Method:    native_OnSurfaceChanged
 * Signature: (II)V
 */
JNIEXPORT void JNICALL native_OnSurfaceChanged(JNIEnv *env, jobject instance, jint width, jint height)
{
    // 设置视口
    glViewport(0, 0, width, height);
}


/*
 * Class:     com_example_onedrawtriangle_MyNativeRender
 * Method:    native_OnDrawFrame
 * Signature: ()V
 */
JNIEXPORT void JNICALL native_OnDrawFrame (JNIEnv *env, jobject instance)
{
    // 清除颜色缓冲区
    glClear(GL_COLOR_BUFFER_BIT);
}
#ifdef __cplusplus
}
#endif

static JNINativeMethod g_RenderMethods[] = {
      {"native_OnInit",             "()V",       (void *)(native_OnInit)},
      {"native_OnUnInit",           "()V",       (void *)(native_OnUnInit)},
      {"native_SetImageData",       "(III[B)V",  (void *)(native_SetImageData)},
      {"native_OnSurfaceCreated",   "()V",       (void *)(native_OnSurfaceCreated)},
      {"native_OnSurfaceChanged",   "(II)V",     (void *)(native_OnSurfaceChanged)},
      {"native_OnDrawFrame",        "()V",       (void *)(native_OnDrawFrame)},
};

static int RegisterNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods, int methodNum)
{
   LOGD("RegisterNativeMethods");
   jclass clazz = env->FindClass(className);
   if (clazz == NULL)
   {
      LOGD("RegisterNativeMethods fail. clazz == NULL");
      return JNI_FALSE;
   }
   if (env->RegisterNatives(clazz, methods, methodNum) < 0)
   {
      LOGD("RegisterNativeMethods fail");
      return JNI_FALSE;
   }
   return JNI_TRUE;
}

static void UnregisterNativeMethods(JNIEnv *env, const char *className)
{
   LOGD("UnregisterNativeMethods");
   jclass clazz = env->FindClass(className);
   if (clazz == NULL)
   {
      LOGD("UnregisterNativeMethods fail. clazz == NULL");
      return;
   }
   if (env != NULL)
   {
      env->UnregisterNatives(clazz);
   }
}

// call this func when loading lib
extern "C" jint JNI_OnLoad(JavaVM *jvm, void *p)
{
   LOGD("===== JNI_OnLoad =====");
   jint jniRet = JNI_ERR;
   JNIEnv *env = NULL;
   if (jvm->GetEnv((void **) (&env), JNI_VERSION_1_6) != JNI_OK)
   {
      return jniRet;
   }

   jint regRet = RegisterNativeMethods(env, NATIVE_RENDER_CLASS_NAME, g_RenderMethods,
                              sizeof(g_RenderMethods) /
                              sizeof(g_RenderMethods[0]));
   if (regRet != JNI_TRUE)
   {
      return JNI_ERR;
   }

   return JNI_VERSION_1_6;
}

extern "C" void JNI_OnUnload(JavaVM *jvm, void *p)
{
   JNIEnv *env = NULL;
   if (jvm->GetEnv((void **) (&env), JNI_VERSION_1_6) != JNI_OK)
   {
      return;
   }

   UnregisterNativeMethods(env, NATIVE_RENDER_CLASS_NAME);
}

2)cmake编写

记得需要加上链接这个库GLESv3

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.10.2)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

# Declares and names the project.
set(jnilibs ${CMAKE_SOURCE_DIR}/../jniLibs)
set(libname Drawtriangle)

include_directories(
        include
        ${CMAKE_SOURCE_DIR}
)

link_directories(
        ${jnilibs}/${ANDROID_ABI})

file(GLOB src-files
        ${CMAKE_SOURCE_DIR}/*.cpp
        )
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        ${libname}

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        ${src-files}
        )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

#set(third-party-libs
#        GLES
#        )
set(native-libs
        android
        log
        m
        z
        )
target_link_libraries( # Specifies the target library.
        ${libname}

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib}
        ${native-libs}
        EGL
        GLESv3
        )

第三部分bulid.gradle(app)引入cmake

defaultConfig {
    ...
    externalNativeBuild {
        cmake {
            cppFlags ''
            abiFilters "arm64-v8a"
            //arguments "-DANDROID_STL=c++_shared"
        }
    }
}
externalNativeBuild {
    cmake {
        path file('src/main/cpp/CMakeLists.txt')
        version '3.10.2'
    }
}

链接:

Android GLSurfaceView 的使用 https://blog.csdn.net/xfb1989/article/details/115334149

Android GlSurfaceView总结及代码示例讲解 https://blog.csdn.net/u013914309/article/details/124679081

OpenGLES2之Android&iOS跨平台开发教 https://blog.csdn.net/suwk1009/article/details/80583830

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值