增加一些日志输出.修改对EGLCore的初始化

This commit is contained in:
JackLee 2026-03-02 14:17:43 +08:00
parent 5c2e32b0d9
commit 1aaf8cfa89
6 changed files with 38 additions and 32 deletions

View File

@ -37,8 +37,7 @@ add_library(opencax SHARED
NativeEGLOCCT/NativeRender.cpp NativeEGLOCCT/NativeRender.cpp
NativeEGLOCCT/NativeRenderThread.cpp NativeEGLOCCT/NativeRenderThread.cpp
NativeEGLOCCT/NativeManager.cpp NativeEGLOCCT/NativeManager.cpp
napi_init.cpp napi_init.cpp)
)
# #
find_library(EGL-lib EGL) find_library(EGL-lib EGL)

View File

@ -1,6 +1,5 @@
#include "EGLCore.h" #include "EGLCore.h"
#include <cstdio> #include <cstdio>
#include <cstring>
#include "NativeEGLOCCT/common.h" #include "NativeEGLOCCT/common.h"
namespace NativeOpenCAX { namespace NativeOpenCAX {
@ -20,14 +19,13 @@ bool EGLCore::init(OHNativeWindow* window) {
printf("Native window is null\n"); printf("Native window is null\n");
return false; return false;
} }
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore","Current Thread ID: %{public}d",std::this_thread::get_id());
nativeWindow_ = window; nativeWindow_ = window;
// 获取EGL display // 获取EGL display
eglDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY); eglDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (eglDisplay_ == EGL_NO_DISPLAY) { if (eglDisplay_ == EGL_NO_DISPLAY) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","eglGetDisplay failed:%{public}d",std::this_thread::get_id()); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","eglGetDisplay failed:%{public}d",std::this_thread::get_id());
return false; return false;
} }
@ -38,6 +36,7 @@ bool EGLCore::init(OHNativeWindow* window) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","eglInitialize failed:%{public}d",std::this_thread::get_id()); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","eglInitialize failed:%{public}d",std::this_thread::get_id());
return false; return false;
} }
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","EGLDisplay version:%{public}d",majorVersion+"."+minorVersion);
// 配置EGL // 配置EGL
EGLint attribs[] = { EGLint attribs[] = {
@ -62,7 +61,6 @@ bool EGLCore::init(OHNativeWindow* window) {
// 创建EGL surface // 创建EGL surface
eglSurface_ = eglCreateWindowSurface(eglDisplay_, eglConfig_, (NativeWindowType)nativeWindow_, nullptr); eglSurface_ = eglCreateWindowSurface(eglDisplay_, eglConfig_, (NativeWindowType)nativeWindow_, nullptr);
if (eglSurface_ == EGL_NO_SURFACE) { if (eglSurface_ == EGL_NO_SURFACE) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","eglCreateWindowSurface failed:%{public}d",std::this_thread::get_id());
return false; return false;
} }
@ -74,23 +72,20 @@ bool EGLCore::init(OHNativeWindow* window) {
eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, contextAttribs); eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, contextAttribs);
if (eglContext_ == EGL_NO_CONTEXT) { if (eglContext_ == EGL_NO_CONTEXT) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","eglCreateContext failed:%{public}d",std::this_thread::get_id());
return false; return false;
} }
// 激活上下文 // 激活上下文
makeCurrent(); makeCurrent();
// 检查GL错误 // 检查GL错误
GLenum glError = glGetError(); GLenum glError = glGetError();
if (glError != GL_NO_ERROR) { if (glError != GL_NO_ERROR) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","OpenGL error after initialization:%{public}d",glError);
return false; return false;
} }
// 设置GL视口 // 设置GL视口
glViewport(0, 0, 1280, 720); // 默认大小,实际会随窗口变化 glViewport(0, 0, 1280, 720); // 默认大小,实际会随窗口变化
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInitDone","Thread ID: %{public}d",std::this_thread::get_id());
return true; return true;
} }

View File

@ -31,6 +31,8 @@ public:
EGLSurface getSurface() const { return eglSurface_; } EGLSurface getSurface() const { return eglSurface_; }
EGLContext getContext() const { return eglContext_; } EGLContext getContext() const { return eglContext_; }
EGLDisplay getDisplay() const { return eglDisplay_; } EGLDisplay getDisplay() const { return eglDisplay_; }
EGLConfig getConfig() const { return eglConfig_; }
OHNativeWindow* getOHNativeWindow()const{return nativeWindow_;}
private: private:
EGLDisplay eglDisplay_; EGLDisplay eglDisplay_;
EGLContext eglContext_; EGLContext eglContext_;

View File

@ -10,10 +10,10 @@
#include <Prs3d_ShadingAspect.hxx> #include <Prs3d_ShadingAspect.hxx>
#include <Graphic3d_MaterialAspect.hxx> #include <Graphic3d_MaterialAspect.hxx>
#include <Graphic3d_NameOfMaterial.hxx> #include <Graphic3d_NameOfMaterial.hxx>
#include <Graphic3d_TextureEnv.hxx>
#include <BRepPrimAPI_MakeBox.hxx> #include <BRepPrimAPI_MakeBox.hxx>
#include <BRepPrimAPI_MakeSphere.hxx> #include <BRepPrimAPI_MakeSphere.hxx>
#include <BRepPrimAPI_MakeCylinder.hxx> #include <BRepPrimAPI_MakeCylinder.hxx>
#include <Graphic3d_TextureEnv.hxx>
#include <V3d_TypeOfOrientation.hxx> #include <V3d_TypeOfOrientation.hxx>
#include <Aspect_TypeOfTriedronPosition.hxx> #include <Aspect_TypeOfTriedronPosition.hxx>
@ -23,8 +23,8 @@ NativeRender::NativeRender()
: rotationX_(0.0f), : rotationX_(0.0f),
rotationY_(0.0f), rotationY_(0.0f),
zoomLevel_(1.0f), zoomLevel_(1.0f),
width_(0), width_(1280),
height_(0), height_(720),
clearColor_(Quantity_NOC_BLACK), clearColor_(Quantity_NOC_BLACK),
translationX_(0.0f), translationX_(0.0f),
translationY_(0.0f) translationY_(0.0f)
@ -36,7 +36,8 @@ NativeRender::~NativeRender() {
shapes_.clear(); shapes_.clear();
} }
bool NativeRender::init(int width, int height) { bool NativeRender::init(int width, int height,EGLCore* eglCore) {
eglCore_=eglCore;
setupViewer(); setupViewer();
setupContext(); setupContext();
setupView(); setupView();
@ -47,7 +48,8 @@ bool NativeRender::init(int width, int height) {
} }
view_->SetBackgroundColor(clearColor_); view_->SetBackgroundColor(clearColor_);
view_->MustBeResized(); view_->MustBeResized();
view_->Redraw(); //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","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_);
@ -55,11 +57,15 @@ bool NativeRender::init(int width, int height) {
} }
void NativeRender::setupViewer() { void NativeRender::setupViewer() {
Handle(Aspect_DisplayConnection) displayConnection; 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();
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());
} }
// 创建V3d_Viewer // 创建V3d_Viewer
viewer_ = new V3d_Viewer(graphicDriver_); viewer_ = new V3d_Viewer(graphicDriver_);
@ -80,7 +86,6 @@ void NativeRender::setupView() {
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_->SetBgGradientColors( view_->SetBgGradientColors(
Quantity_Color(Quantity_NOC_GRAY), Quantity_Color(Quantity_NOC_GRAY),
@ -105,7 +110,7 @@ bool NativeRender::loadModel(const std::string& filePath) {
context_->Remove(shape, Standard_False); context_->Remove(shape, Standard_False);
} }
shapes_.clear(); shapes_.clear();
// 加载STEP文件 // 加载STEP文件
STEPControl_Reader reader; STEPControl_Reader reader;
IFSelect_ReturnStatus status = reader.ReadFile(filePath.c_str()); IFSelect_ReturnStatus status = reader.ReadFile(filePath.c_str());
@ -114,7 +119,7 @@ bool NativeRender::loadModel(const std::string& filePath) {
loadDefaultModel(); loadDefaultModel();
return false; return false;
} }
// 将文件内容转换为OCCT形状 // 将文件内容转换为OCCT形状
reader.TransferRoots(); reader.TransferRoots();
int numShapes = reader.NbShapes(); int numShapes = reader.NbShapes();
@ -123,17 +128,17 @@ bool NativeRender::loadModel(const std::string& filePath) {
loadDefaultModel(); loadDefaultModel();
return false; return false;
} }
// 加载所有形状 // 加载所有形状
for (Standard_Integer i = 1; i <= numShapes; i++) { for (Standard_Integer i = 1; i <= numShapes; i++) {
TopoDS_Shape shape = reader.Shape(i); TopoDS_Shape shape = reader.Shape(i);
if (!shape.IsNull()) { if (!shape.IsNull()) {
Handle(AIS_Shape) aisShape = new AIS_Shape(shape); Handle(AIS_Shape) aisShape = new AIS_Shape(shape);
// 设置材质 // 设置材质
Handle(Prs3d_Drawer) drawer = aisShape->Attributes(); Handle(Prs3d_Drawer) drawer = aisShape->Attributes();
Handle(Prs3d_ShadingAspect) shadingAspect = new Prs3d_ShadingAspect(); Handle(Prs3d_ShadingAspect) shadingAspect = new Prs3d_ShadingAspect();
// 随机颜色 // 随机颜色
int colorIndex = i % 7; int colorIndex = i % 7;
Quantity_Color color; Quantity_Color color;
@ -147,16 +152,15 @@ bool NativeRender::loadModel(const std::string& filePath) {
case 6: color = Quantity_NOC_ORANGE; break; case 6: color = Quantity_NOC_ORANGE; break;
default: color = Quantity_NOC_WHITE; default: color = Quantity_NOC_WHITE;
} }
shadingAspect->SetColor(color); shadingAspect->SetColor(color);
shadingAspect->SetMaterial(Graphic3d_NOM_PLASTIC); shadingAspect->SetMaterial(Graphic3d_NOM_PLASTIC);
drawer->SetShadingAspect(shadingAspect); drawer->SetShadingAspect(shadingAspect);
context_->Display(aisShape, Standard_True); context_->Display(aisShape, Standard_True);
shapes_.push_back(aisShape); shapes_.push_back(aisShape);
} }
} }
// 调整相机到合适位置 // 调整相机到合适位置
view_->FitAll(0.05, Standard_True); view_->FitAll(0.05, Standard_True);
view_->ZFitAll(); view_->ZFitAll();
@ -221,8 +225,9 @@ void NativeRender::render() {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Render","View Is Null"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Render","View Is Null");
return; 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_); glViewport(0, 0, width_, height_);
// 应用旋转 // 应用旋转
Handle(Graphic3d_Camera) camera = view_->Camera(); Handle(Graphic3d_Camera) camera = view_->Camera();

View File

@ -16,20 +16,25 @@
#include <Message.hxx> #include <Message.hxx>
#include <Quantity_Color.hxx> #include <Quantity_Color.hxx>
#include <gp_Quaternion.hxx> #include <gp_Quaternion.hxx>
#include <OpenGl_Window.hxx>
#include <gp_EulerSequence.hxx> #include <gp_EulerSequence.hxx>
#include <Aspect_Handle.hxx>
#include <ace/xcomponent/native_interface_xcomponent.h> #include <ace/xcomponent/native_interface_xcomponent.h>
#include "common.h" #include "common.h"
#include <EGL/egl.h>
#include "EGLCore.h"
class Aspect_Window; class Aspect_Window;
class gp_Quaternion; class gp_Quaternion;
class Graphic3d_Camera; class Graphic3d_Camera;
namespace NativeOpenCAX { namespace NativeOpenCAX {
class NativeRender { class NativeRender {
public: public:
NativeRender(); NativeRender();
~NativeRender(); ~NativeRender();
bool init(int width, int height); bool init(int width, int height,EGLCore* eglCore);
bool loadModel(const std::string& filePath); bool loadModel(const std::string& filePath);
void loadDefaultModel(); void loadDefaultModel();
void render(); void render();
@ -39,13 +44,13 @@ public:
void setClearColor(float r, float g, float b, float a); void setClearColor(float r, float g, float b, float a);
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 setupViewer();
void setupContext(); void setupContext();
void setupView(); void setupView();
// Bind existing EGL display/context to OCCT OpenGl driver
//void loadDefaultModel(); //void loadDefaultModel();
EGLCore* eglCore_;
Handle(OpenGl_GraphicDriver) graphicDriver_; Handle(OpenGl_GraphicDriver) graphicDriver_;
Handle(V3d_Viewer) viewer_; Handle(V3d_Viewer) viewer_;
Handle(V3d_View) view_; Handle(V3d_View) view_;

View File

@ -57,7 +57,7 @@ void NativeRenderThread::renderLoop() {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","Failed to initialize EGL"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","Failed to initialize EGL");
} }
// 初始化OCCT渲染器 // 初始化OCCT渲染器
if (!renderer_.init(windowWidth_, windowHeight_)) { if (!renderer_.init(windowWidth_, windowHeight_,eglCore_)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRenderInit","Failed to initialize OCCT renderer"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRenderInit","Failed to initialize OCCT renderer");
eglCore_->destroy(); eglCore_->destroy();
} }