Android Studio通过NativeActivity运行Opengl ES程序

本文主要是记录如何将opengl es编程指南中的示例代码搭建在Android Studio中。

主要过程包括以下几点:

  • Android Studio创建一个新的支持NDK项目
  • 修改AndroidManifest.xml,注册NativeActivity
  • 拷贝opengl es编程指南的示例代码到cpp目录下
  • 修改CMakeLists.txt,添加opengl es的依赖, 以及NativeActivityNDK中的依赖

下面依次介绍各个流程,在Andrid Studio中创建一个新项目就不用多说了,着很简单

注册NativeActivity

打开AndroidManifest.xml,注释掉创建项目自动生成的MainActivity,然后添加一下代码


        
        
        
           
           
        

拷贝示例代码到cpp下

下面是书籍代码示例目录


Android Studio通过NativeActivity运行Opengl ES程序_第1张图片
snipaste_20180310_220631.png

拷贝commonChapter_2目录下的c文件和头文件到cpp目录下,主要包括以下文件:

Android Studio通过NativeActivity运行Opengl ES程序_第2张图片
snipaste_20180310_220813.png

可能在这几个拷贝的文件中有些头文件没有加, 按照提示自己添加

添加库的依赖

Android Studio中,NDK的编译和eclipse不太一样,在eclipse中是依靠Android.mkApplication.mk编译, 而Android Studio中是用CMake去编译

下面是几个需要添加的依赖:

  • NativeActivity所需要的依赖,也就是静态库android_native_app_glue
  • opengl es所需的几个库
  • NativeWindow所需的android.so

下面是修改后的CMakeLists.txt

# 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.4.1)

# 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.

include_directories(
            ${ANDROID_NDK}/sources/android/native_app_glue)

add_library( # Sets the name of the library.
             Hello_Triangle

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/esUtil_Android.c
             src/main/cpp/esShader.c
             src/main/cpp/esTransform.c
             src/main/cpp/esUtil.c
             src/main/cpp/Hello_Triangle.c)

add_library(
            app-glue
            STATIC
            ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c )

find_library( OPENGLES3_LIBRARY GLESv3 "OpenGL ES v3.0 library")
find_library( EGL_LIBRARY EGL "EGL 1.4 library" )

# 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.

find_library( # Sets the name of the path variable.
              log-lib
              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       Hello_Triangle

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib}
                       app-glue
                       ${OPENGLES3_LIBRARY}
                       ${EGL_LIBRARY}
                       android.so
                       )

下面就是运行之后的结果,只要NDKSDK版本没有问题,基本上就没问题了

Android Studio通过NativeActivity运行Opengl ES程序_第3张图片
S80310-215810.jpg

你可能感兴趣的:(Android Studio通过NativeActivity运行Opengl ES程序)