1 // Copyright (c) 2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <AndroidQt_Window.h>
15 #include <AndroidQt.h>
16 #include <AndroidQt_UserInteractionParameters.h>
18 #include <AIS_Shape.hxx>
19 #include <BRepTools.hxx>
20 #include <BRep_Builder.hxx>
21 #include <OpenGl_GraphicDriver.hxx>
22 #include <Quantity_Color.hxx>
23 #include <Standard_ErrorHandler.hxx>
25 #include <TopoDS_Face.hxx>
26 #include <TopoDS_Shape.hxx>
27 #include <UnitsAPI.hxx>
32 // =======================================================================
33 // function : AndroidQt
35 // =======================================================================
36 AndroidQt::AndroidQt()
37 : myFitAllAction (false)
39 connect (this, SIGNAL(windowChanged(QQuickWindow*)), this, SLOT(handleWindowChanged(QQuickWindow*)));
41 // set shaders location variable
42 QByteArray aDataRoot = "/data/data/org.qtproject.example.AndroidQt/files/opencascade/shared";
43 qputenv ("CSF_ShadersDirectory", aDataRoot + "/Shaders");
46 // =======================================================================
47 // function : ReadShapeFromFile
49 // =======================================================================
50 bool AndroidQt::ReadShapeFromFile (QString theFilePath)
52 QUrl aFileUrl (theFilePath);
53 QString aFilePath = theFilePath;
54 if (aFileUrl.isLocalFile())
56 aFilePath = QUrl (theFilePath).toLocalFile();
59 if (!QFile (aFilePath).exists())
65 BRep_Builder aBuildTool;
70 if (!BRepTools::Read (aShape, aFilePath.toStdString().c_str(), aBuildTool))
75 if (!myContext.IsNull())
77 myContext->EraseAll (Standard_False);
79 Handle(AIS_Shape) aShapePrs = new AIS_Shape (aShape);
80 aShapePrs->SetColor (Quantity_Color(1.0, 0.73, 0.2, Quantity_TOC_RGB));
82 myContext->Display (aShapePrs, Standard_False);
83 myContext->SetDisplayMode (aShapePrs, AIS_Shaded, Standard_False);
87 myFitAllAction = true;
95 catch (Standard_Failure)
103 // =======================================================================
104 // function : InitTouch
106 // =======================================================================
107 void AndroidQt::InitTouch (const double theX,
111 myTouchPoint.SetStarts (theX, theY);
115 // =======================================================================
116 // function : UpdateTouch
118 // =======================================================================
119 void AndroidQt::UpdateTouch (const double theX,
123 myTouchPoint.SetEnds (theX, theY);
130 // =======================================================================
131 // function : handleWindowChanged
133 // =======================================================================
134 void AndroidQt::handleWindowChanged (QQuickWindow* theWin)
141 connect(theWin, SIGNAL(beforeSynchronizing()), this, SLOT(sync()), Qt::DirectConnection);
143 theWin->setClearBeforeRendering (false);
146 // =======================================================================
149 // =======================================================================
150 void AndroidQt::sync()
152 myViewportSize = window()->size() * window()->devicePixelRatio();
154 if (myViewer.IsNull())
157 connect (window(), SIGNAL(beforeRendering()), this, SLOT(paint()), Qt::DirectConnection);
161 Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast (myViewer->Driver());
162 if (aDriver->getRawGlContext() != eglGetCurrentContext())
168 Handle(AndroidQt_Window) aWindow = Handle(AndroidQt_Window)::DownCast (myView->Window());
169 aWindow->SetSize (myViewportSize.width(), myViewportSize.height());
170 //myView->MustBeResized(); // can be used instead of SetWindow() when EGLsurface has not been changed
172 EGLContext anEglContext = eglGetCurrentContext();
173 myView->SetWindow (aWindow, (Aspect_RenderingContext )anEglContext);
178 // =======================================================================
181 // =======================================================================
182 void AndroidQt::paint()
186 if (Abs(myTouchPoint.DevX()) + Abs(myTouchPoint.DevY()) > 1)
188 myView->StartRotation (myTouchPoint.X().first, myTouchPoint.Y().first);
189 myView->Rotation (myTouchPoint.X().second, myTouchPoint.Y().second);
191 myTouchPoint.ClearDev();
197 myFitAllAction = false;
205 // =======================================================================
206 // function : initViewer
208 // =======================================================================
209 bool AndroidQt::initViewer()
212 int aWidth = 0, aHeight = 0;
213 EGLDisplay anEglDisplay = eglGetCurrentDisplay();
214 EGLContext anEglContext = eglGetCurrentContext();
215 EGLSurface anEglSurf = eglGetCurrentSurface (EGL_DRAW);
217 if (anEglDisplay == EGL_NO_DISPLAY
218 || anEglContext == EGL_NO_CONTEXT
219 || anEglSurf == EGL_NO_SURFACE)
225 eglQuerySurface (anEglDisplay, anEglSurf, EGL_WIDTH, &aWidth);
226 eglQuerySurface (anEglDisplay, anEglSurf, EGL_HEIGHT, &aHeight);
227 eglQuerySurface (anEglDisplay, anEglSurf, EGL_CONFIG_ID, &aCfgId);
229 const EGLint aConfigAttribs[] = { EGL_CONFIG_ID, aCfgId, EGL_NONE };
230 EGLint aNbConfigs = 0;
231 void* anEglConfig = NULL;
233 if (eglChooseConfig (anEglDisplay, aConfigAttribs, &anEglConfig, 1, &aNbConfigs) != EGL_TRUE)
239 if (!myViewer.IsNull())
241 Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast (myViewer->Driver());
242 Handle(AndroidQt_Window) aWindow = Handle(AndroidQt_Window)::DownCast (myView->Window());
243 if (!aDriver->InitEglContext (anEglDisplay, anEglContext, anEglConfig))
249 aWindow->SetSize (aWidth, aHeight);
250 myView->SetWindow (aWindow, (Aspect_RenderingContext )anEglContext);
253 Handle(OpenGl_GraphicDriver) aDriver = new OpenGl_GraphicDriver (NULL, Standard_False);
254 aDriver->ChangeOptions().buffersNoSwap = Standard_True;
255 //aDriver->ChangeOptions().glslWarnings = Standard_True; // for GLSL shaders debug
257 if (!aDriver->InitEglContext (anEglDisplay, anEglContext, anEglConfig))
264 myViewer = new V3d_Viewer (aDriver);
265 myViewer->SetDefaultBackgroundColor (AndroidQt_UserInteractionParameters::BgColor.Name());
266 myViewer->SetDefaultLights();
267 myViewer->SetLightOn();
269 // create AIS context
270 myContext = new AIS_InteractiveContext (myViewer);
271 myContext->SetDisplayMode (AIS_Shaded, false);
273 Handle(AndroidQt_Window) aWindow = new AndroidQt_Window (aWidth, aHeight);
274 myView = myViewer->CreateView();
276 myView->SetWindow (aWindow, (Aspect_RenderingContext )anEglContext);
277 myView->TriedronDisplay (Aspect_TOTP_RIGHT_LOWER, Quantity_NOC_WHITE, 0.08, V3d_ZBUFFER);
282 // =======================================================================
283 // function : release
285 // =======================================================================
286 void AndroidQt::release()