• Home
Name Date Size #Lines LOC

..--

AppScope/22-Mar-2025-3532

entry/22-Mar-2025-4,0323,193

hvigor/22-Mar-2025-88

screenshots/device/22-Mar-2025-

.gitignoreD22-Mar-2025119 1111

README.mdD22-Mar-2025335 177

README_zh.mdD22-Mar-20256.2 KiB9975

build-profile.json5D22-Mar-20251.1 KiB4342

hvigorfile.tsD22-Mar-2025159 21

hvigorwD22-Mar-20251.4 KiB4928

hvigorw.batD22-Mar-20252 KiB7356

oh-package-lock.json5D22-Mar-20251 KiB2827

oh-package.json5D22-Mar-2025868 2826

README.md

1# Native OpenGL 3D Graphics
2
3
4
5### Introduction
6
7This sample mainly introduces how developers can use the Native window interface and XComponent to create 3D Graphics.
8
9### Usage
10
11The application page displays call to the Native window API and OpenGL(ES) API.
12
13
14### Constraints
15
16This sample can only be run on standard-system devices.
17

README_zh.md

1# OpenGL三棱椎
2
3### 介绍
4
5XComponent控件常用于相机预览流的显示和游戏画面的绘制,在OpenHarmony上,可以配合Native Window创建OpenGL开发环境,并最终将OpenGL绘制的图形显示到XComponent控件。本示例基于"Native C++"模板,调用OpenGL(OpenGL ES)图形库相关API绘制3D图形(三棱锥),并将结果渲染到页面的XComponent控件中进行展示。同时,还可以在屏幕上通过触摸滑动手势对三棱锥进行旋转,最终得到不同角度的图形并显示到页面。
6
7### 效果预览
8
9| 首页                                     | 滑动屏幕旋转变换                                 |
10| -------------------------------------- | ---------------------------------------- |
11| ![index](screenshots/device/index.png) | ![rotate](screenshots/device/rotate.png) |
12
13使用说明
14
15应用界面中展示了XComponent相关控件的使用,及采用OpenGL (OpenGL ES)相关标准API绘制3D图形(三棱锥,3D渲染的光源用的是简单的线性光源)。此外,可在屏幕触摸滑动,以使三棱锥进行旋转,其中主要采用了napi接口来更新3D图形的旋转角度。
16
17### 工程目录
18
19```
20entry/src/main/
21|---cpp
22|   |---CMakeLists.txt                       // cmake编译配置
23|   |---app_napi.cpp                         // 调用native接口
24|   |---include
25|   |   |---app_napi.h
26|   |   |---tetrahedron.h
27|   |   |---util
28|   |       |---log.h
29|   |       |---napi_manager.h
30|   |       |---napi_util.h
31|   |       |---native_common.h
32|   |       |---native_interface_xcomponent.h
33|   |---module.cpp                           // napi模块注册
34|   |---napi_manager.cpp
35|   |---napi_util.cpp
36|   |---tetrahedron.cpp                      // OpenGL (ES) 三棱锥实现
37|   |---type
38|       |---libentry
39|           |---oh-package.json5
40|           |---tetrahedron_napi.d.ts        // 接口导出
41|---ets
42|   |---entryability
43|   |   |---EntryAbility.ts
44|   |---pages
45|   |   |---Index.ets                        // 首页
46|   |---utils
47|       |---Logger.ets                       // 日志工具
48|
49```
50
51### 具体实现
52
53通过在IDE中创建Native C++工程,在C++代码中定义接口为Init和Update用于3D图形绘制环境的初始化和图形渲染更新,并映射NAPI相关接口UpdateAngle。ArkTS侧主要利用XComponent控件实现Index.ets,C++侧主要采用OpenGL ES相关标准API实现三棱锥的绘制流程相关代码,并可与ArkTS进行交互。
54
55应用启动时,NAPI模块也相应进行初始化,此时可通过C++侧的OH_NativeXComponent_GetXComponentId()接口,获取到当前XComponent控件的控件指针,并给到C++侧三棱锥绘制相关的Init和Update函数,实现3D图形显示。同时,为实现三棱锥的触摸屏滑动旋转效果,在C++代码中映射的NAPI接口UpdateAngle给到ArkTS侧调用。ArkTS侧需在导入NAPI模块"libtetrahedron_napi.so"正确的前提下,通过调用src/main/cpp/type/libentry/tetrahedron_napi.d.ts中声明的UpdateAngle接口更新三棱锥旋转角度。
56主要源码参考:[napi_manager.cpp](entry/src/main/cpp/napi_manager.cpp)、[app_napi.cpp](entry/src/main/cpp/app_napi.cpp)和[tetrahedron.cpp](entry/src/main/cpp/tetrahedron.cpp)。
57
58Native XComponent相关函数如下:
59
60| 函数名称                                                                                                                                             | 描述                               |
61| ------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------- |
62| OH_NativeXComponent_GetXComponentId(OH_NativeXComponent *component, char *id, uint64_t *size)                                                    | 获取ArkUI XComponent的id            |
63| OH_NativeXComponent_GetXComponentSize(OH_NativeXComponent *component, const void *window, uint64_t *width, uint64_t *height)                     | 获取ArkUI XComponent持有的surface的大小  |
64| OH_NativeXComponent_GetXComponentOffset(OH_NativeXComponent *component, const void *window, double *x, double *y)                                | 获取ArkUI XComponent组件相对屏幕左上顶点的偏移量 |
65| OH_NativeXComponent_GetTouchEvent(OH_NativeXComponent *component, const void *window, OH_NativeXComponent_TouchEvent *touchEvent)                | 获取ArkUI XComponent调度的触摸事件        |
66| OH_NativeXComponent_GetTouchPointToolType(OH_NativeXComponent *component, uint32_t pointIndex, OH_NativeXComponent_TouchPointToolType *toolType) | 获取ArkUI XComponent触摸点工具类型        |
67| OH_NativeXComponent_GetTouchPointTiltX(OH_NativeXComponent *component, uint32_t pointIndex, float *tiltX)                                       | 获取ArkUI XComponent触摸点倾斜与X轴角度     |
68| OH_NativeXComponent_GetTouchPointTiltY(OH_NativeXComponent *component, uint32_t pointIndex, float *tiltY)                                        | 获取ArkUI XComponent触摸点倾斜与Y轴角度     |
69| OH_NativeXComponent_GetMouseEvent(OH_NativeXComponent *component, const void *window, OH_NativeXComponent_MouseEvent *mouseEvent)                | 获取ArkUI XComponent调度的鼠标事件        |
70| OH_NativeXComponent_RegisterCallback(OH_NativeXComponent *component, OH_NativeXComponent_Callback *callback)                                     | 实例注册回调                           |
71| OH_NativeXComponent_RegisterMouseEventCallback(OH_NativeXComponent *component, OH_NativeXComponent_MouseEvent_Callback *callback)                | 实例注册鼠标事件回调                       |
72
73### 相关权限
74
75不涉及。
76
77### 依赖
78
79不涉及。
80
81### 约束与限制
82
831. 本示例仅支持标准系统上运行。
84
852. 本示例已适配API version 10版本SDK,SDK版本号(API Version 10 Release),镜像版本号(4.0Release) 。
86
873. 本示例需要使用DevEco Studio 版本号(4.0Release)及以上版本才可编译运行。
88
89### 下载
90
91如需单独下载本工程,执行如下命令:
92
93```
94git init
95git config core.sparsecheckout true
96echo code/BasicFeature/Native/NdkOpenGL/ > .git/info/sparse-checkout
97git remote add origin https://gitee.com/openharmony/applications_app_samples.git
98git pull origin master
99```