创建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相关推荐

  1. android自定义美颜相机完整程序,Android OpenGL ES从入门到进阶(一)—— 五分钟开发一款美颜相机...

    源码链接:https://github.com/smzhldr/AGLFramework 一.前言 商店里有数十款的美颜相机类产品,其实现原理基本上都是以OpenGL ES为核心的特效处理,大神可以忽 ...

  2. Android OpenGL ES 开发教程(20):颜色Color

    OpenGL ES 支持的颜色格式为RGBA模式(红,绿,蓝,透明度).颜色的定义通常使用Hex格式0xFF00FF 或十进制格式(255,0,255), 在OpenGL 中却是使用0-1之间的浮点数 ...

  3. OpenGl文章 Android OpenGL ES 简明开发教程

    Android OpenGL ES 简明开发教程 分类:android学习笔记2011-12-14 15:04375人阅读评论(0)收藏举报 ApiDemos 的Graphics示例中含有OpenGL ...

  4. android 美颜相机开发,Android OpenGL ES从入门到进阶(一)—— 五分钟开发一款美颜相机...

    源码链接:https://github.com/smzhldr/AGLFramework 一.前言 商店里有数十款的美颜相机类产品,以及像抖音,唱吧之类带有视频的软件,功能很强大,其实现原理基本上都是 ...

  5. Android OpenGL ES从入门到进阶(一)—— 五分钟开发一款美颜相机

    源码链接:https://github.com/smzhldr/AGLFramework 基础知识入门篇(Hello Triangle) 渲染纹理到屏幕 GLSurfaceView预览相机 简单易用的 ...

  6. Android OpenGL ES 开发教程(16):Viewing和Modeling(MODELVIEW) 变换

    Viewing和Modeling 变换关系紧密,对应到相机拍照为放置三角架和调整被拍物体位置及角度,通常将这两个变换使用一个modelview 变换矩阵来定义.对于同一个坐标变换,可以使用不同的方法来 ...

  7. Opengl ES 1.x NDK实例开发之七:旋转的纹理立方体

    开发框架介绍请参见:Opengl ES NDK实例开发之一:搭建开发框架 本章在第六章(Opengl ES 1.x NDK实例开发之六:纹理贴图)的基础上绘制一个旋转的纹理立方体,原理和纹理贴图一样, ...

  8. Opengl ES 1.x NDK实例开发之八:旋转的纹理金字塔

    开发框架介绍请参见:Opengl ES NDK实例开发之一:搭建开发框架 本章在第六章(Opengl ES 1.x NDK实例开发之六:纹理贴图)的基础上绘制一个旋转的纹理金字塔,原理和纹理贴图一样, ...

  9. Android OpenGL ES 从入门到精通系统性学习教程

    1 为什么要写这个教程 目前这个 OpenGL ES 极简教程的更新暂时告一段落,在此之前,很荣幸获得了阮一峰老师的推荐. 因为在工作中频繁使用 OpenGL ES 做一些特效.滤镜之类的效果,加上平 ...

最新文章

  1. 一个可以实现图片格式相互转化的工具convert_picture.exe
  2. php 5.6 新特性,PHP5.6新特性介绍
  3. 【LDA学习系列】MCMC之Metropolis-Hastings采样算法python代码理解
  4. 【面经】记一次字节跳动前端面试经历
  5. 不适合的任务:一个例子 启示:记住 我们选择做我们最擅长的事情,并且把事情做到最好
  6. 当知识图谱遇上文本摘要:保留抽象式文本摘要的事实性知识
  7. 关于DataFormWebPart中CreatedModifiedInfo信息的分开使用
  8. 【Java】字符串编程练习题
  9. JAVA 手机号正则 工具类
  10. cron每月1号_微信服务号按粉丝标签分组群发消息怎样实现?
  11. 电商BANNER灵感背景欣赏|平面设计中的极简风格
  12. 关机时长时间停留在”正在保存设置“的解决办法
  13. 《穷爸爸,富爸爸》读书笔记
  14. 访问艺术馆(codevs 1163)树形DP
  15. spring教程笔记4
  16. 高德地图上覆盖物polygon方法的使用
  17. dda算法c语言opengl实现
  18. P6035CDN打印机 kyocera_京瓷P7040cdn打印机驱动下载
  19. 25岁从零开始学习平面设计会不会晚
  20. autoware planning trajectory_smoother 模块解读

热门文章

  1. JUnit 5和Selenium –使用Selenium内置的`PageFactory`实现页面对象模式
  2. Java:如何创建轻量级数据库微服务
  3. 专业QA如何实施可靠的CI / CD管道?
  4. java中iterator_如何在Java中读取CSV文件-Iterator和Decorator的案例研究
  5. Java 9:ServiceLoader
  6. Java数组排序解码
  7. 用Lucene建立搜索索引
  8. 将数据库日志添加到JUnit3
  9. 带有Java 8,lambda表达式和Mockito-Java8附加组件的更紧凑的Mockito
  10. 仍不切换到Java 8的6个理由