diff --git a/entry/src/main/cpp/CMakeLists.txt b/entry/src/main/cpp/CMakeLists.txt index e884a2e3..61ea1258 100644 --- a/entry/src/main/cpp/CMakeLists.txt +++ b/entry/src/main/cpp/CMakeLists.txt @@ -37,8 +37,7 @@ add_library(opencax SHARED NativeEGLOCCT/NativeRender.cpp NativeEGLOCCT/NativeRenderThread.cpp NativeEGLOCCT/NativeManager.cpp - napi_init.cpp - ) + napi_init.cpp) # 查找系统库 find_library(EGL-lib EGL) diff --git a/entry/src/main/cpp/NativeEGLOCCT/EGLCore.cpp b/entry/src/main/cpp/NativeEGLOCCT/EGLCore.cpp index 75e020ca..df828397 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/EGLCore.cpp +++ b/entry/src/main/cpp/NativeEGLOCCT/EGLCore.cpp @@ -1,6 +1,5 @@ #include "EGLCore.h" #include -#include #include "NativeEGLOCCT/common.h" namespace NativeOpenCAX { @@ -20,14 +19,13 @@ bool EGLCore::init(OHNativeWindow* window) { printf("Native window is null\n"); 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; // 获取EGL display eglDisplay_ = eglGetDisplay(EGL_DEFAULT_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()); - 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()); return false; } + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","EGLDisplay version:%{public}d",majorVersion+"."+minorVersion); // 配置EGL EGLint attribs[] = { @@ -62,7 +61,6 @@ bool EGLCore::init(OHNativeWindow* window) { // 创建EGL surface eglSurface_ = eglCreateWindowSurface(eglDisplay_, eglConfig_, (NativeWindowType)nativeWindow_, nullptr); 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; } @@ -74,23 +72,20 @@ bool EGLCore::init(OHNativeWindow* window) { eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, contextAttribs); 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; } // 激活上下文 makeCurrent(); - + // 检查GL错误 GLenum glError = glGetError(); 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; } // 设置GL视口 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; } diff --git a/entry/src/main/cpp/NativeEGLOCCT/EGLCore.h b/entry/src/main/cpp/NativeEGLOCCT/EGLCore.h index e5406dfd..cfd2f29a 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/EGLCore.h +++ b/entry/src/main/cpp/NativeEGLOCCT/EGLCore.h @@ -31,6 +31,8 @@ public: EGLSurface getSurface() const { return eglSurface_; } EGLContext getContext() const { return eglContext_; } EGLDisplay getDisplay() const { return eglDisplay_; } + EGLConfig getConfig() const { return eglConfig_; } + OHNativeWindow* getOHNativeWindow()const{return nativeWindow_;} private: EGLDisplay eglDisplay_; EGLContext eglContext_; diff --git a/entry/src/main/cpp/NativeEGLOCCT/NativeRender.cpp b/entry/src/main/cpp/NativeEGLOCCT/NativeRender.cpp index c834c27c..319e87d5 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/NativeRender.cpp +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeRender.cpp @@ -10,10 +10,10 @@ #include #include #include +#include #include #include #include -#include #include #include @@ -23,8 +23,8 @@ NativeRender::NativeRender() : rotationX_(0.0f), rotationY_(0.0f), zoomLevel_(1.0f), - width_(0), - height_(0), + width_(1280), + height_(720), clearColor_(Quantity_NOC_BLACK), translationX_(0.0f), translationY_(0.0f) @@ -36,7 +36,8 @@ NativeRender::~NativeRender() { shapes_.clear(); } -bool NativeRender::init(int width, int height) { +bool NativeRender::init(int width, int height,EGLCore* eglCore) { + eglCore_=eglCore; setupViewer(); setupContext(); setupView(); @@ -47,7 +48,8 @@ bool NativeRender::init(int width, int height) { } view_->SetBackgroundColor(clearColor_); 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","width: %{public}d",width_); 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() { - 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()) { - graphicDriver_ = new OpenGl_GraphicDriver(displayConnection, Standard_False); + graphicDriver_ = new OpenGl_GraphicDriver(displayConnection,Standard_False); graphicDriver_->ChangeOptions().buffersNoSwap = Standard_True; + graphicDriver_->InitEglContext(eglCore_->getDisplay(), eglCore_->getContext(), eglCore_->getConfig()); } // 创建V3d_Viewer viewer_ = new V3d_Viewer(graphicDriver_); @@ -80,7 +86,6 @@ void NativeRender::setupView() { view_->ChangeRenderingParams().IsReflectionEnabled = Standard_False; view_->ChangeRenderingParams().IsAntialiasingEnabled = Standard_True; view_->ChangeRenderingParams().Resolution = 2; - // 设置背景渐变 view_->SetBgGradientColors( Quantity_Color(Quantity_NOC_GRAY), @@ -105,7 +110,7 @@ bool NativeRender::loadModel(const std::string& filePath) { context_->Remove(shape, Standard_False); } shapes_.clear(); - + // 加载STEP文件 STEPControl_Reader reader; IFSelect_ReturnStatus status = reader.ReadFile(filePath.c_str()); @@ -114,7 +119,7 @@ bool NativeRender::loadModel(const std::string& filePath) { loadDefaultModel(); return false; } - + // 将文件内容转换为OCCT形状 reader.TransferRoots(); int numShapes = reader.NbShapes(); @@ -123,17 +128,17 @@ bool NativeRender::loadModel(const std::string& filePath) { loadDefaultModel(); return false; } - + // 加载所有形状 for (Standard_Integer i = 1; i <= numShapes; i++) { TopoDS_Shape shape = reader.Shape(i); if (!shape.IsNull()) { Handle(AIS_Shape) aisShape = new AIS_Shape(shape); - + // 设置材质 Handle(Prs3d_Drawer) drawer = aisShape->Attributes(); Handle(Prs3d_ShadingAspect) shadingAspect = new Prs3d_ShadingAspect(); - + // 随机颜色 int colorIndex = i % 7; Quantity_Color color; @@ -147,16 +152,15 @@ bool NativeRender::loadModel(const std::string& filePath) { case 6: color = Quantity_NOC_ORANGE; break; default: color = Quantity_NOC_WHITE; } - + shadingAspect->SetColor(color); shadingAspect->SetMaterial(Graphic3d_NOM_PLASTIC); drawer->SetShadingAspect(shadingAspect); - + context_->Display(aisShape, Standard_True); shapes_.push_back(aisShape); } } - // 调整相机到合适位置 view_->FitAll(0.05, Standard_True); view_->ZFitAll(); @@ -221,8 +225,9 @@ void NativeRender::render() { OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Render","View Is Null"); 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 3a9de177..a5d3fa3b 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/NativeRender.h +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeRender.h @@ -16,20 +16,25 @@ #include #include #include +#include #include +#include #include #include "common.h" +#include +#include "EGLCore.h" class Aspect_Window; class gp_Quaternion; class Graphic3d_Camera; + namespace NativeOpenCAX { class NativeRender { public: NativeRender(); ~NativeRender(); - bool init(int width, int height); + bool init(int width, int height,EGLCore* eglCore); bool loadModel(const std::string& filePath); void loadDefaultModel(); void render(); @@ -39,13 +44,13 @@ public: void setClearColor(float r, float g, float b, float a); void setZoomLevel(float zoom); void setTranslation(float x, float y); - private: void setupViewer(); void setupContext(); void setupView(); + // Bind existing EGL display/context to OCCT OpenGl driver //void loadDefaultModel(); - + EGLCore* eglCore_; Handle(OpenGl_GraphicDriver) graphicDriver_; Handle(V3d_Viewer) viewer_; Handle(V3d_View) view_; diff --git a/entry/src/main/cpp/NativeEGLOCCT/NativeRenderThread.cpp b/entry/src/main/cpp/NativeEGLOCCT/NativeRenderThread.cpp index 6d659b54..fcba0c42 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/NativeRenderThread.cpp +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeRenderThread.cpp @@ -57,7 +57,7 @@ void NativeRenderThread::renderLoop() { OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","Failed to initialize EGL"); } // 初始化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"); eglCore_->destroy(); }