【Android OpenGL ES 开发 (一)】使用c++开发opengles 与 日志功能 及 加载assets
生活随笔
收集整理的這篇文章主要介紹了
【Android OpenGL ES 开发 (一)】使用c++开发opengles 与 日志功能 及 加载assets
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
創建OpenGLES視口
1.App窗口改成OpenGL窗口,是通過java調用C++,在以下位置修改如下內容
package com.example.learnogles;import androidx.appcompat.app.AppCompatActivity;import android.content.Context; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.util.Log; import android.widget.TextView;import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10;class HangyuGLSurfaceViewRenderer implements GLSurfaceView.Renderer{public void onSurfaceCreated(GL10 gl, EGLConfig config){//視口初始化時gl.glClearColor(0.1f,0.4f,0.6f,1.0f);}public void onSurfaceChanged(GL10 gl, int width, int height){//視口改變時gl.glViewport(0,0,width,height);//左下角是0,0,Viewport相當于畫布,需要擺放在視口的什么位置}public void onDrawFrame(GL10 gl){//繪制時候gl.glClear(gl.GL_COLOR_BUFFER_BIT|gl.GL_DEPTH_BUFFER_BIT|gl.GL_STENCIL_BUFFER_BIT);//擦除顏色緩沖區} }class HnagyuGLSurfaceView extends GLSurfaceView{ //重載GLSurfaceViewGLSurfaceView.Renderer mRenderer;//渲染器public HnagyuGLSurfaceView(Context context){super(context);setEGLContextClientVersion(2);//設置openGLES的版本號mRenderer = new HangyuGLSurfaceViewRenderer();//生成一個渲染器setRenderer(mRenderer);//設置渲染器} }public class MainActivity extends AppCompatActivity {// Used to load the 'native-lib' library on application startup.static {System.loadLibrary("baohangyu"); //會加載動態庫}HnagyuGLSurfaceView mGLview;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mGLview = new HnagyuGLSurfaceView(getApplication());//new一個視口setContentView(mGLview);//設置渲染視口}/*** A native method that is implemented by the 'native-lib' native library,* which is packaged with this application.*/public native String stringFromJNI();public native int Test();public native int TestLive(); }?
使用NDK來寫OpenGLES代碼
1.首先需要先把相關頭文件加入到指定位置
#pragma once#include <stdio.h> #include <string.h> #include <jni.h> #include <iostream> #include <GLES2/gl2.h> #include <GLES2/gl2ext.h> #include <GLES2/gl2platform.h>2.在 MainActivity.java 中調用C++API接口
package com.example.learnogles;import androidx.appcompat.app.AppCompatActivity;import android.content.Context; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.util.Log; import android.widget.TextView;import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10;class HangyuGLSurfaceViewRenderer implements GLSurfaceView.Renderer{public void onSurfaceCreated(GL10 gl, EGLConfig config){//視口初始化時MainActivity.Instance().Init();}public void onSurfaceChanged(GL10 gl, int width, int height){//視口改變時MainActivity.Instance().OnViewportChanged(width, height);}public void onDrawFrame(GL10 gl){//繪制時候MainActivity.Instance().Render();} }class HnagyuGLSurfaceView extends GLSurfaceView{ //重載GLSurfaceViewGLSurfaceView.Renderer mRenderer;//渲染器public HnagyuGLSurfaceView(Context context){super(context);setEGLContextClientVersion(2);//設置openGLES的版本號mRenderer = new HangyuGLSurfaceViewRenderer();//生成一個渲染器setRenderer(mRenderer);//設置渲染器} }public class MainActivity extends AppCompatActivity {// Used to load the 'native-lib' library on application startup.static {System.loadLibrary("baohangyu"); //會加載動態庫}static MainActivity mSelf;HnagyuGLSurfaceView mGLview;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mSelf = this;mGLview = new HnagyuGLSurfaceView(getApplication());//new一個視口setContentView(mGLview);//設置渲染視口}public static MainActivity Instance(){//實例化方法return mSelf;}/*** A native method that is implemented by the 'native-lib' native library,* which is packaged with this application.*///OpenGLES C++接口public native void Init();public native void OnViewportChanged(int width, int height);public native void Render();}3.cmake需要添加如下配置
# 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)# Declares and names the project.project("learnogles")# 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.baohangyu #baohangyu.so# Sets the library as a shared library.SHARED# Provides a relative path to your source file(s).Sence.cppnative-lib.cpp )# 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.baohangyu# Links the target library to the log library# included in the NDK.${log-lib} android GLESv2 ) #需要添加安卓相關和真正的OPENGL ES版本4.cpp中的實現
// // Created by 19691 on 2020/11/11. //#include "Sence.h"extern "C" JNIEXPORT void JNICALL Java_com_example_learnogles_MainActivity_Init(JNIEnv* env,jobject) {glClearColor(0.1f,0.4f,0.6f,1.0f); }extern "C" JNIEXPORT void JNICALL Java_com_example_learnogles_MainActivity_OnViewportChanged(JNIEnv* env,jobject,jint width,jint height) {glViewport(0,0,width,height); }extern "C" JNIEXPORT void JNICALL Java_com_example_learnogles_MainActivity_Render(JNIEnv* env,jobject /* this */) {glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); }?
創建Assets資源目錄
1.在項目目錄中app\src\main文件夾下創建assets文件夾,名字唯一
在CPP里打印日志
1.首先在頭文件AllHeader.h中加入安卓打印日志的頭文件,#include <android/log.h>
2.再加入 #define HANGYU_LOG_TAG "HANGYUOpenGLES"
3.在cpp中可以調用? __android_log_print(ANDROID_LOG_INFO,HANGYU_LOG_TAG,"Render");? 類似這條語句來打印日志,會在Logcat中顯示
在NDK層加載外部資源
1.先添加以下頭文件
//在NDK層加載外部文件相關 #include <android/asset_manager.h> #include <android/asset_manager_jni.h>2.cpp中初始化需要做如下增加
static AAssetManager *sAAssetManager = nullptr;//定義一個資源相關指針變量 extern "C" JNIEXPORT void JNICALL Java_com_example_learnogles_MainActivity_Init(JNIEnv* env,jobject ,jobject am) {sAAssetManager = AAssetManager_fromJava(env,am);//初始化資源指針__android_log_print(ANDROID_LOG_INFO,HANGYU_LOG_TAG,"Init");glClearColor(0.0f,1.0f,1.0f,0.0f); }?3.修改java層的C++API接口
public native void Init(AssetManager am);4.實現加載文件的C++方法?
先創建一個Utils類,并且將cpp文件加入cmake的編譯列表,以下為方法實現
unsigned char * LoadfileContent(const char *path, int&filesize) {unsigned char *filecontent= nullptr;filesize = 0;AAsset *asset = AAssetManager_open(sAAssetManager,path,AASSET_MODE_UNKNOWN); //打開資源,其中AAsset是真正的資源if(asset!= nullptr){filesize = AAsset_getLength(asset);filecontent = new unsigned char[filesize+1];AAsset_read(asset,filecontent,filesize);filecontent[filesize]=0;AAsset_close(asset);}return filecontent; }?
?
?
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的【Android OpenGL ES 开发 (一)】使用c++开发opengles 与 日志功能 及 加载assets的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 固态硬盘到底该怎么测试如何查看电脑固态硬
- 下一篇: C++ 11 深度学习(四)结构、权限修