增加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

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

View File

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

View File

@ -1,4 +1,5 @@
#include "NativeRender.h" #include "NativeRender.h"
#include "Aspect_NeutralWindow.hxx"
#include <GLES3/gl3.h> #include <GLES3/gl3.h>
@ -29,7 +30,7 @@ NativeRender::NativeRender()
translationX_(0.0f), translationX_(0.0f),
translationY_(0.0f) translationY_(0.0f)
{ {
} }
NativeRender::~NativeRender() { NativeRender::~NativeRender() {
@ -38,54 +39,55 @@ NativeRender::~NativeRender() {
bool NativeRender::init(int width, int height,EGLCore* eglCore) { bool NativeRender::init(int width, int height,EGLCore* eglCore) {
eglCore_=eglCore; eglCore_=eglCore;
setupViewer(); initTextStyle();
setupContext(); initViewer();
setupView(); initContext();
width_ = width; initView();
height_ = height;
if (view_.IsNull()) { if (view_.IsNull()) {
return false; return false;
} }
view_->SetBackgroundColor(clearColor_); view_->SetBackgroundColor(clearColor_);
view_->MustBeResized(); view_->MustBeResized();
//view_->Redraw();
view_->RedrawImmediate(); 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","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","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, "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; return true;
} }
void NativeRender::initViewer(){
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());
Handle(Aspect_DisplayConnection) displayConnection=new Aspect_DisplayConnection(); Handle(Aspect_DisplayConnection) displayConnection=new Aspect_DisplayConnection();
displayConnection->Init((Aspect_XDisplay*)eglGetCurrentDisplay());
// 创建图形驱动 // 创建图形驱动
if (graphicDriver_.IsNull()) { if (graphicDriver_.IsNull()) {
graphicDriver_ = new OpenGl_GraphicDriver(displayConnection,Standard_False); graphicDriver_ = new OpenGl_GraphicDriver(displayConnection,Standard_False);
graphicDriver_->ChangeOptions().buffersNoSwap = Standard_True; graphicDriver_->ChangeOptions().buffersNoSwap = Standard_True;
graphicDriver_->InitEglContext(eglCore_->getDisplay(), eglCore_->getContext(), eglCore_->getConfig()); graphicDriver_->InitEglContext(eglGetCurrentDisplay(), eglGetCurrentContext(), eglCore_->getConfig());
} }
// 创建V3d_Viewer // 创建V3d_Viewer
viewer_ = new V3d_Viewer(graphicDriver_); viewer_ = new V3d_Viewer(graphicDriver_);
viewer_->SetDefaultBackgroundColor (Quantity_NOC_BLACK);
viewer_->SetDefaultLights(); viewer_->SetDefaultLights();
viewer_->SetLightOn(); viewer_->SetLightOn();
} }
void NativeRender::initContext(){
void NativeRender::setupContext() {
context_ = new AIS_InteractiveContext(viewer_); context_ = new AIS_InteractiveContext(viewer_);
context_->SetDisplayMode(AIS_Shaded, Standard_False); // 默认使用着色模式 context_->SetDisplayMode(AIS_Shaded, Standard_False); // 默认使用着色模式
//context_->SetPixelTolerance (int(myDevicePixelRatio * 6.0)); // increase tolerance and adjust to hi-dpi screens
} }
void NativeRender::initView() {
void NativeRender::setupView() { Handle(Aspect_NeutralWindow) m_Window = new Aspect_NeutralWindow();
m_Window->SetSize (width_, height_);
view_ = viewer_->CreateView(); view_ = viewer_->CreateView();
// 设置渲染参数 // 设置渲染参数
view_->SetImmediateUpdate (false);
//view_->ChangeRenderingParams().ToShowStats = true;
view_->ChangeRenderingParams().Method = Graphic3d_RM_RASTERIZATION; view_->ChangeRenderingParams().Method = Graphic3d_RM_RASTERIZATION;
view_->ChangeRenderingParams().IsShadowEnabled = Standard_False; view_->ChangeRenderingParams().IsShadowEnabled = Standard_False;
view_->ChangeRenderingParams().IsReflectionEnabled = Standard_False; view_->ChangeRenderingParams().IsReflectionEnabled = Standard_False;
view_->ChangeRenderingParams().IsAntialiasingEnabled = Standard_True; view_->ChangeRenderingParams().IsAntialiasingEnabled = Standard_True;
view_->ChangeRenderingParams().Resolution = 2; view_->ChangeRenderingParams().Resolution = 2;
view_->ChangeRenderingParams().StatsTextAspect = text_->Aspect();
view_->ChangeRenderingParams().StatsTextHeight = (int )text_->Height();
// 设置背景渐变 // 设置背景渐变
view_->SetBgGradientColors( view_->SetBgGradientColors(
Quantity_Color(Quantity_NOC_GRAY), Quantity_Color(Quantity_NOC_GRAY),
@ -101,9 +103,21 @@ void NativeRender::setupView() {
// 设置相机参数 // 设置相机参数
Handle(Graphic3d_Camera) camera = view_->Camera(); Handle(Graphic3d_Camera) camera = view_->Camera();
camera->SetFOVy(45.0); // 使用正确的FOV设置API 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) { bool NativeRender::loadModel(const std::string& filePath) {
// 清除现有模型 // 清除现有模型
for (auto& shape : shapes_) { for (auto& shape : shapes_) {
@ -226,8 +240,8 @@ void NativeRender::render() {
return; return;
} }
glClearColor(1, 0, 0, 1); //glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, width_, height_); glViewport(0, 0, width_, height_);
// 应用旋转 // 应用旋转
Handle(Graphic3d_Camera) camera = view_->Camera(); Handle(Graphic3d_Camera) camera = view_->Camera();

View File

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

View File

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