增加view_->SetWindow(m_Window,(Aspect_RenderingContext )eglGetCurrentContext());解决无法渲染到OHNativeWindow的问题

This commit is contained in:
JackLee 2026-03-02 18:11:45 +08:00
parent 1aaf8cfa89
commit a509aabe9c
5 changed files with 50 additions and 31 deletions

View File

@ -47,6 +47,7 @@ int32_t NativeManager::hasChangeColor_ = 0;
static std::map<int64_t, std::shared_ptr<NativeRenderThread>> renderThreadMap;
static std::mutex mapMutex;
NativeManager::~NativeManager() {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "~NativeManager");
nativeXComponentMap_.clear();
@ -216,7 +217,7 @@ ArkUI_NodeHandle CreateNodeHandle(const std::string &tag) {
ArkUI_NodeHandle nodeHandel = nodeAPI->createNode(ARKUI_NODE_RELATIVE_CONTAINER);
// 节点默认宽度or高度
ArkUI_NumberValue nodeSizeData[] = {1280, 720};
ArkUI_NumberValue nodeMarginData[] = {{.u32 = 15}, {.f32 = 15}};
ArkUI_NumberValue nodeMarginData[] = {{.u32 = 0}, {.f32 = 0}};
// ArkUI_AttributeItem
// 参数1:指向数值数组
// 参数2:数组元素个数
@ -233,8 +234,8 @@ ArkUI_NodeHandle CreateNodeHandle(const std::string &tag) {
// 组件ID Item
ArkUI_AttributeItem comIdItem = {.string = tag.c_str(), .size = 1};
// 组件Surface Size
ArkUI_NumberValue surfaceSizeData[] = {1280, 720};
ArkUI_AttributeItem surfaceSizeItem = {surfaceSizeData, 2};
//ArkUI_NumberValue surfaceSizeData[] = {NAN, NAN};
ArkUI_AttributeItem surfaceSizeItem = {nodeSizeData, 2};
// 创建组件
xc = nodeAPI->createNode(ARKUI_NODE_XCOMPONENT);
// 设置XComponent组件属性

View File

@ -61,10 +61,10 @@ private:
std::unordered_map<std::string, NativeManager*> pluginManagerMap_;
public:
// [StartExclude plugin_manager_h_part]
uint64_t width_;
uint64_t height_;
OH_NativeXComponent_TouchEvent touchEvent_;
std::string modelPath_;
uint64_t width_;
uint64_t height_;
static int32_t hasDraw_;
static int32_t hasChangeColor_;
static std::unordered_map<std::string, ArkUI_NodeHandle> nodeHandleMap_;

View File

@ -1,4 +1,5 @@
#include "NativeRender.h"
#include "Aspect_NeutralWindow.hxx"
#include <GLES3/gl3.h>
@ -38,54 +39,55 @@ NativeRender::~NativeRender() {
bool NativeRender::init(int width, int height,EGLCore* eglCore) {
eglCore_=eglCore;
setupViewer();
setupContext();
setupView();
width_ = width;
height_ = height;
initTextStyle();
initViewer();
initContext();
initView();
if (view_.IsNull()) {
return false;
}
view_->SetBackgroundColor(clearColor_);
view_->MustBeResized();
//view_->Redraw();
view_->RedrawImmediate();
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRenderInit","NativeRender Init Done");
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRenderInit","width: %{public}d",width_);
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRenderInit","height: %{public}d",height_);
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Render", "GL Error before render: 0x%{public}x", glGetError());
return true;
}
void NativeRender::setupViewer() {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRender setupViewer","Current Thread ID: %{public}d",std::this_thread::get_id());
void NativeRender::initViewer(){
Handle(Aspect_DisplayConnection) displayConnection=new Aspect_DisplayConnection();
displayConnection->Init((Aspect_XDisplay*)eglGetCurrentDisplay());
// 创建图形驱动
if (graphicDriver_.IsNull()) {
graphicDriver_ = new OpenGl_GraphicDriver(displayConnection,Standard_False);
graphicDriver_->ChangeOptions().buffersNoSwap = Standard_True;
graphicDriver_->InitEglContext(eglCore_->getDisplay(), eglCore_->getContext(), eglCore_->getConfig());
graphicDriver_->InitEglContext(eglGetCurrentDisplay(), eglGetCurrentContext(), eglCore_->getConfig());
}
// 创建V3d_Viewer
viewer_ = new V3d_Viewer(graphicDriver_);
viewer_->SetDefaultBackgroundColor (Quantity_NOC_BLACK);
viewer_->SetDefaultLights();
viewer_->SetLightOn();
}
void NativeRender::setupContext() {
void NativeRender::initContext(){
context_ = new AIS_InteractiveContext(viewer_);
context_->SetDisplayMode(AIS_Shaded, Standard_False); // 默认使用着色模式
//context_->SetPixelTolerance (int(myDevicePixelRatio * 6.0)); // increase tolerance and adjust to hi-dpi screens
}
void NativeRender::setupView() {
void NativeRender::initView() {
Handle(Aspect_NeutralWindow) m_Window = new Aspect_NeutralWindow();
m_Window->SetSize (width_, height_);
view_ = viewer_->CreateView();
// 设置渲染参数
view_->SetImmediateUpdate (false);
//view_->ChangeRenderingParams().ToShowStats = true;
view_->ChangeRenderingParams().Method = Graphic3d_RM_RASTERIZATION;
view_->ChangeRenderingParams().IsShadowEnabled = Standard_False;
view_->ChangeRenderingParams().IsReflectionEnabled = Standard_False;
view_->ChangeRenderingParams().IsAntialiasingEnabled = Standard_True;
view_->ChangeRenderingParams().Resolution = 2;
view_->ChangeRenderingParams().StatsTextAspect = text_->Aspect();
view_->ChangeRenderingParams().StatsTextHeight = (int )text_->Height();
// 设置背景渐变
view_->SetBgGradientColors(
Quantity_Color(Quantity_NOC_GRAY),
@ -101,9 +103,21 @@ void NativeRender::setupView() {
// 设置相机参数
Handle(Graphic3d_Camera) camera = view_->Camera();
camera->SetFOVy(45.0); // 使用正确的FOV设置API
camera->SetZRange(1.0, 10000.0);
camera->SetZRange(1.0, 1000.0);
view_->SetWindow(m_Window,(Aspect_RenderingContext )eglGetCurrentContext());
}
void NativeRender::initTextStyle(){
text_ = new Prs3d_TextAspect();
text_->SetFont (Font_NOF_ASCII_MONO);
text_->SetHeight (12);
text_->Aspect()->SetColor (Quantity_NOC_GRAY95);
text_->Aspect()->SetColorSubTitle (Quantity_NOC_BLACK);
text_->Aspect()->SetDisplayType (Aspect_TODT_SHADOW);
text_->Aspect()->SetTextFontAspect (Font_FA_Bold);
text_->Aspect()->SetTextZoomable (false);
text_->SetHorizontalJustification (Graphic3d_HTA_LEFT);
text_->SetVerticalJustification (Graphic3d_VTA_BOTTOM);
}
bool NativeRender::loadModel(const std::string& filePath) {
// 清除现有模型
for (auto& shape : shapes_) {
@ -226,8 +240,8 @@ void NativeRender::render() {
return;
}
glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glClearColor(1, 0, 0, 1);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, width_, height_);
// 应用旋转
Handle(Graphic3d_Camera) camera = view_->Camera();

View File

@ -45,9 +45,10 @@ public:
void setZoomLevel(float zoom);
void setTranslation(float x, float y);
private:
void setupViewer();
void setupContext();
void setupView();
void initViewer();
void initContext();
void initView();
void initTextStyle();
// Bind existing EGL display/context to OCCT OpenGl driver
//void loadDefaultModel();
EGLCore* eglCore_;
@ -55,6 +56,7 @@ private:
Handle(V3d_Viewer) viewer_;
Handle(V3d_View) view_;
Handle(AIS_InteractiveContext) context_;
Handle(Prs3d_TextAspect) text_;
std::vector<Handle(AIS_Shape)> shapes_;
float rotationX_;

View File

@ -74,8 +74,10 @@ export struct ModelView {
Button('模块&&so库加载测试').onClick(()=>{
NativeLoadModelTest();
})
}
}.height('5%')
Row(){
ContentSlot(this.nodeContent);
}.height('95%')
}
.width('100%')
.height('100%');