【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;
}

 

 

 

 

 

 

 

 

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/336109.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

水印相机定位不准确怎么办_禄来的广角双反相机(2020版)

点击上方胶卷迷俱乐部快速关注&#xff0c;胶卷迷们坚实的阵地内容主要原创&#xff0c;配图来自群友和网络&#xff0c;所有水印保留最下方为微信自带广告&#xff0c;支持请点击(本号可获微量收入)4.0德国禄来公司以双反相机名震天下&#xff0c;其中的2.8&#xff0c;3.5两个…

【Android OpenGL ES 开发 (二)】渲染管线与Shader

计算出每一帧耗费的时间 1.在头文件中加入time.h,cpp中实现如下计算时间接口 float GetFrameTime(){static unsigned long long lastTime0,currentTime0;timeval current;gettimeofday(&current, nullptr);//取当前时间currentTime current.tv_sec * 1000 current.tv…

tibco汉化包6.3.0_TIBCO BusinessWorks 6和Container Edition与BW5的比较

tibco汉化包6.3.0[本文已于一段时间前发布在TIBCO博客上 。 我还在适当的地方添加了一些有关BusinessWorks Container Edition&#xff08;BW CE&#xff09;的信息 。 下面定义的大多数特征对于BW6和BW CE都是正确的] TIBCO ActiveMatrix BusinessWorks 6&#xff08;BW6&…

python地图散点图_Python中基于Basemap的三维散点图

我正在尝试绘制一个三维散点图&#xff0c;图中是Python身上的烟羽&#xff0c;下面是北美的地图。我试过各种方法&#xff0c;但似乎都有缺点。我现在的代码是&#xff1a;fig plt.figure(figsize(22,4))ax Axes3D(fig)M Basemap(llcrnrlon-140,llcrnrlat10,urcrnrlon-40,u…

【Android OpenGL ES 开发 (三)】Shader 扩展

编译Shader代码 1.封装一个编译shader的接口 GLuint CompileShader(GLenum shaderType,const char *shaderCode){GLuint shaderglCreateShader(shaderType);glShaderSource(shader,1,&shaderCode,NULL);glCompileShader(shader);GLint compileResultGL_TRUE;glGetShaderi…

css阴影属性_第三场阴影场与属性访问器接口

css阴影属性这是“ 影子字段与属性访问器”界面的 第3轮 。 如果您是新手&#xff0c;但不确定要怎么做&#xff0c;请查看我以前的文章或关于开发JavaFX应用程序时节省内存的第一篇文章 。 作为Java开发人员&#xff0c;我主要关心的是在开发JavaFX域模型时在性能 &#xff0c…

js if判断多个条件_JS条件判断小技巧(一)

经常code review&#xff0c;我发现JS newbie很容易写出一堆冗长的代码。今天就列几个比较常见的“解决之道”&#xff0c;看看如何减少JS里的条件判断。提前返回&#xff0c;少用if...else“if...else是编程语言的精华。——鲁迅”但是过多的嵌套&#xff0c;还是挺令人抓狂的…

【Android OpenGL ES 开发 (四)】纹理相关(一)

纹理贴图的原理 1.作用&#xff1a;可以用来渲染视频。 2.纹理坐标 生成OpenGL中的纹理对象 1.像素数据想要绘制出来需要先变成纹理 2.创建纹理放在GPU上 GLuint CreateTexture2D(unsigned char *pixelData,int width,int height,GLenum type) {GLuint texture;glGenTextu…

python selenium循环判断元素是否存在_检查Python Selenium是否存在元素

我有一个问题-我正在使用Selenium(firefox)Web驱动程序打开网页&#xff0c;单击一些链接等&#xff0c;然后捕获屏幕截图。我的脚本可以从CLI正常运行&#xff0c;但是通过cronjob运行时&#xff0c;它并没有通过第一个find_element()测试。我需要添加一些调试&#xff0c;或一…

jmx 替代_使用JMX作为Ganglia的现代替代品进行CLDB监视

jmx 替代有许多选项可用于监视MapR集群的性能和运行状况。 在本文中&#xff0c;我将介绍使用Java管理扩展&#xff08;JMX&#xff09;监视CLDB的鲜为人知的方法。 据最受尊敬的MapR数据工程师之一&#xff0c;Akihiko Kusanagi称&#xff0c;与使用Ganglia相比&#xff0c;使…

WPScan安全建议和防护

加固WordPress安全性 保护WordPress网站的安全性至关重要&#xff0c;因为它是全球最受欢迎的内容管理系统之一&#xff0c;也是攻击者经常瞄准的目标。本文将深入探讨如何加固WordPress安全性&#xff0c;包括实施强密码策略、保持更新、使用可靠的主题和插件、限制登录尝试、…

red hat安装宝塔_如何在几分钟内安装Red Hat Container Development Kit(CDK)

red hat安装宝塔作为负责开发容器化应用程序提供的可能性的应用程序开发人员或架构师&#xff0c;将所有工具组合在一起以帮助您入门时几乎没有帮助。 到现在。 红帽容器开发套件&#xff08;CDK&#xff09; 安装变得简单&#xff01; 红帽提供了一个容器开发套件&#xf…

python收集数据程序_用Python挖掘Twitter数据:数据采集

原标题&#xff1a;用Python挖掘Twitter数据&#xff1a;数据采集作者&#xff1a;Marco Bonzanini 翻译&#xff1a;数盟这是7部系列中的第1部分&#xff0c;注重挖掘Twitter数据以用于各种案例。这是第一篇文章&#xff0c;专注于数据采集&#xff0c;起到奠定基础的作用。Tw…

【FFMPEG中PTS与DTS统一转换为毫秒】

对于PTS和DTS 是两个非常重要的参数&#xff0c;在音视频同步时是必要的&#xff0c;为了方便使用&#xff0c;将二者统一为毫秒级别 static double r2d(AVRational r) {return r.den 0 ? 0 : (double)r.num / (double)r.den; } //转换为毫秒&#xff0c;方便做同步 AVPacke…

threejs 影子属性_影子场vs.属性访问器接口第2轮

threejs 影子属性如果你们还没有注意到Dirk Lemmerman和我之间的&#xff08;轻松&#xff09; 摊牌 &#xff0c;那么让我快速提及一下我们是如何做到这一点的。 首先&#xff0c;Dirk创建了JavaFX技巧23&#xff1a;“ 为属性保存内存阴影字段 ”&#xff0c;以帮助应用程序开…

【OpenGL从入门到精通】Shader专题

详解GPU的工作流程 1.shader通常称为着色器&#xff0c;作用是把CPU上的点渲染出来。 2.shader是并行的。 3.流程&#xff1a;数据data (顶点数据) ----->VS(输入&#xff1a;data的顶点数据&#xff0c;输出&#xff1a;gl_Position的 vec4 顶点数据)----->光栅化处理…

python内存管理可以使用del_Python深入学习之内存管理

语言的内存管理是语言设计的一个重要方面。它是决定语言性能的重要因素。无论是C语言的手工管理&#xff0c;还是Java的垃圾回收&#xff0c;都成为语言最重要的特征。这里以Python语言为例子&#xff0c;说明一门动态类型的、面向对象的语言的内存管理方式。对象的内存使用赋值…

【OpenGL从入门到精通(六)】纹理对象与纹理坐标

1.在OpenGL想要显示一张图片&#xff0c;需要先绘制一个自定义的几何体。 2.把图片加载到纹理对象中 3.当进行纹理贴图时候&#xff0c;使用纹理坐标来设置纹理对象。 2.

yeoman_具有Spring Boot和Yeoman的单页Angularjs应用程序

yeoman我非常感谢yeoman之类的工具&#xff0c;这些工具提供了一种非常快速的方法来将不同的javascript库组合在一起成为一个一致的应用程序。 Yeoman提供了UI层&#xff0c;如果您需要开发服务层和静态资产的Web层&#xff0c;则打包的一种好方法是使用Spring Boot 。 我知道有…