From a509aabe9cb23a5160974feabaa978bdd0341abc Mon Sep 17 00:00:00 2001 From: JackLee <809262979@qq.com> Date: Mon, 2 Mar 2026 18:11:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0view=5F->SetWindow(m=5FWindow?= =?UTF-8?q?,(Aspect=5FRenderingContext=20)eglGetCurrentContext());?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=97=A0=E6=B3=95=E6=B8=B2=E6=9F=93=E5=88=B0?= =?UTF-8?q?OHNativeWindow=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/cpp/NativeEGLOCCT/NativeManager.cpp | 7 ++- .../main/cpp/NativeEGLOCCT/NativeManager.h | 4 +- .../main/cpp/NativeEGLOCCT/NativeRender.cpp | 56 ++++++++++++------- .../src/main/cpp/NativeEGLOCCT/NativeRender.h | 8 ++- entry/src/main/ets/pages/modelView.ets | 6 +- 5 files changed, 50 insertions(+), 31 deletions(-) diff --git a/entry/src/main/cpp/NativeEGLOCCT/NativeManager.cpp b/entry/src/main/cpp/NativeEGLOCCT/NativeManager.cpp index 2d936e55..c9807995 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/NativeManager.cpp +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeManager.cpp @@ -46,6 +46,7 @@ int32_t NativeManager::hasDraw_ = 0; int32_t NativeManager::hasChangeColor_ = 0; static std::map> renderThreadMap; static std::mutex mapMutex; + NativeManager::~NativeManager() { OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "~NativeManager"); @@ -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组件属性 diff --git a/entry/src/main/cpp/NativeEGLOCCT/NativeManager.h b/entry/src/main/cpp/NativeEGLOCCT/NativeManager.h index b03fde13..356aa174 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/NativeManager.h +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeManager.h @@ -61,10 +61,10 @@ private: std::unordered_map 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 nodeHandleMap_; diff --git a/entry/src/main/cpp/NativeEGLOCCT/NativeRender.cpp b/entry/src/main/cpp/NativeEGLOCCT/NativeRender.cpp index 319e87d5..f961ed41 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/NativeRender.cpp +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeRender.cpp @@ -1,4 +1,5 @@ #include "NativeRender.h" +#include "Aspect_NeutralWindow.hxx" #include @@ -29,7 +30,7 @@ NativeRender::NativeRender() translationX_(0.0f), translationY_(0.0f) { - + } NativeRender::~NativeRender() { @@ -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(); diff --git a/entry/src/main/cpp/NativeEGLOCCT/NativeRender.h b/entry/src/main/cpp/NativeEGLOCCT/NativeRender.h index a5d3fa3b..b1eac616 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/NativeRender.h +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeRender.h @@ -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 shapes_; float rotationX_; diff --git a/entry/src/main/ets/pages/modelView.ets b/entry/src/main/ets/pages/modelView.ets index c5f7f7ba..05fba084 100644 --- a/entry/src/main/ets/pages/modelView.ets +++ b/entry/src/main/ets/pages/modelView.ets @@ -74,8 +74,10 @@ export struct ModelView { Button('模块&&so库加载测试').onClick(()=>{ NativeLoadModelTest(); }) - } - ContentSlot(this.nodeContent); + }.height('5%') + Row(){ + ContentSlot(this.nodeContent); + }.height('95%') } .width('100%') .height('100%');