1 // Created on: 2012-04-09
2 // Created by: Sergey ANIKIN
3 // Copyright (c) 2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
24 #include <ViewerTest.hxx>
26 #include <AIS_InteractiveContext.hxx>
27 #include <AIS_InteractiveObject.hxx>
29 #include <Draw_Interpretor.hxx>
30 #include <Graphic3d_Group.hxx>
31 #include <OpenGl_ArbVBO.hxx>
32 #include <OpenGl_AspectFace.hxx>
33 #include <OpenGl_AspectLine.hxx>
34 #include <OpenGl_AspectMarker.hxx>
35 #include <OpenGl_AspectText.hxx>
36 #include <OpenGl_Context.hxx>
37 #include <OpenGl_Element.hxx>
38 #include <OpenGl_ExtFBO.hxx>
39 #include <OpenGl_GlCore20.hxx>
40 #include <OpenGl_GraphicDriver.hxx>
41 #include <OpenGl_Workspace.hxx>
42 #include <Prs3d_Presentation.hxx>
43 #include <Prs3d_Root.hxx>
44 #include <Select3D_SensitiveCurve.hxx>
45 #include <SelectMgr_EntityOwner.hxx>
46 #include <SelectMgr_Selection.hxx>
47 #include <V3d_Viewer.hxx>
48 #include <TCollection_AsciiString.hxx>
49 #include <V3d_View.hxx>
50 #include <Visual3d_View.hxx>
52 extern Standard_Boolean VDisplayAISObject (const TCollection_AsciiString& theName,
53 const Handle(AIS_InteractiveObject)& theAISObj,
54 Standard_Boolean theReplaceIfExists = Standard_True);
56 //=======================================================================
57 //function : VUserDraw
58 //purpose : Checks availability and operation of UserDraw feature
59 //=======================================================================
60 DEFINE_STANDARD_HANDLE(VUserDrawObj, AIS_InteractiveObject)
62 class VUserDrawObj : public AIS_InteractiveObject
66 DEFINE_STANDARD_RTTI(VUserDrawObj);
79 class Element : public OpenGl_Element
82 Handle(VUserDrawObj) myIObj;
85 Element (const Handle(VUserDrawObj)& theIObj,
86 CALL_DEF_BOUNDS* theBounds)
90 myIObj->GetBounds(theBounds);
97 virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const
100 myIObj->Render(theWorkspace);
103 virtual void Release (const Handle(OpenGl_Context)& theGlCtx)
109 DEFINE_STANDARD_ALLOC
113 // Virtual methods implementation
114 void Compute (const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
115 const Handle(Prs3d_Presentation)& thePresentation,
116 const Standard_Integer theMode);
118 void ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
119 const Standard_Integer theMode);
121 // Called by VUserDrawElement
122 void Render(const Handle(OpenGl_Workspace)& theWorkspace) const;
123 void GetBounds(CALL_DEF_BOUNDS* theBounds);
127 friend class Element;
129 IMPLEMENT_STANDARD_HANDLE(VUserDrawObj, AIS_InteractiveObject)
130 IMPLEMENT_STANDARD_RTTIEXT(VUserDrawObj, AIS_InteractiveObject)
132 void VUserDrawObj::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
133 const Handle(Prs3d_Presentation)& thePresentation,
134 const Standard_Integer theMode)
136 thePresentation->Clear();
138 Handle(Graphic3d_Group) aGrp = Prs3d_Root::CurrentGroup(thePresentation);
139 aGrp->UserDraw(this, Standard_True, Standard_True);
142 void VUserDrawObj::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
143 const Standard_Integer theMode)
145 Handle(SelectMgr_EntityOwner) anEntityOwner = new SelectMgr_EntityOwner(this);
146 Handle(TColgp_HArray1OfPnt) aPnts = new TColgp_HArray1OfPnt(1, 5);
147 aPnts->SetValue(1, gp_Pnt(myCoords[0], myCoords[1], myCoords[2]));
148 aPnts->SetValue(2, gp_Pnt(myCoords[3], myCoords[4], myCoords[2]));
149 aPnts->SetValue(3, gp_Pnt(myCoords[3], myCoords[4], myCoords[5]));
150 aPnts->SetValue(4, gp_Pnt(myCoords[0], myCoords[1], myCoords[5]));
151 aPnts->SetValue(5, gp_Pnt(myCoords[0], myCoords[1], myCoords[2]));
152 Handle(Select3D_SensitiveCurve) aSensitive = new Select3D_SensitiveCurve(anEntityOwner, aPnts);
153 theSelection->Add(aSensitive);
156 void VUserDrawObj::GetBounds(CALL_DEF_BOUNDS* theBounds)
160 theBounds->XMin = myCoords[0];
161 theBounds->YMin = myCoords[1];
162 theBounds->ZMin = myCoords[2];
163 theBounds->XMax = myCoords[3];
164 theBounds->YMax = myCoords[4];
165 theBounds->ZMax = myCoords[5];
169 void VUserDrawObj::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
171 // To test linking against OpenGl_Workspace and all aspect classes
172 const OpenGl_AspectLine* aLA = theWorkspace->AspectLine(0);
173 const OpenGl_AspectMarker* aMA = theWorkspace->AspectMarker(0);
175 const OpenGl_AspectText* aTA = theWorkspace->AspectText(0);
177 TEL_COLOUR aColor = theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT ?
178 *(theWorkspace->HighlightColor) : aLA->Color();
180 // To test OpenGl_Window
181 //Handle(OpenGl_Context) aCtx = theWorkspace->GetGlContext();
183 // Finally draw something to make sure UserDraw really works
184 glPushAttrib(GL_ENABLE_BIT);
185 glDisable(GL_LIGHTING);
186 glColor4fv(aColor.rgb);
187 glBegin(GL_LINE_LOOP);
188 glVertex3f(myCoords[0], myCoords[1], myCoords[2]);
189 glVertex3f(myCoords[3], myCoords[4], myCoords[2]);
190 glVertex3f(myCoords[3], myCoords[4], myCoords[5]);
191 glVertex3f(myCoords[0], myCoords[1], myCoords[5]);
196 OpenGl_Element* VUserDrawCallback(const CALL_DEF_USERDRAW * theUserDraw)
198 Handle(VUserDrawObj) anIObj = (VUserDrawObj*)theUserDraw->Data;
201 std::cout << "VUserDrawCallback error: null object passed, the custom scene element will not be rendered" << std::endl;
204 return new VUserDrawObj::Element(anIObj, theUserDraw->Bounds);
207 static Standard_Integer VUserDraw (Draw_Interpretor& di,
208 Standard_Integer argc,
211 Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
212 if (aContext.IsNull())
214 di << argv[0] << "Call 'vinit' before!\n";
218 Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast (aContext->CurrentViewer()->Driver());
219 if (aDriver.IsNull())
221 std::cerr << "Graphic driver not available.\n";
227 di << argv[0] << "Wrong number of arguments, only the object name expected\n";
231 TCollection_AsciiString aName (argv[1]);
232 VDisplayAISObject(aName, Handle(AIS_InteractiveObject)());
234 // register the custom element factory function
235 aDriver->UserDrawCallback() = VUserDrawCallback;
237 Handle(VUserDrawObj) anIObj = new VUserDrawObj();
238 VDisplayAISObject(aName, anIObj);
243 //==============================================================================
244 //function : VFeedback
246 //==============================================================================
248 static int VFeedback (Draw_Interpretor& theDI,
249 Standard_Integer theArgNb,
250 const char** theArgVec)
252 // get the active view
253 Handle(V3d_View) aView = ViewerTest::CurrentView();
256 std::cerr << "No active view. Please call vinit.\n";
260 unsigned int aBufferSize = 1024 * 1024;
263 size_t aBytes = (size_t )aBufferSize * sizeof(GLfloat);
264 if (aBytes / sizeof(GLfloat) != (size_t )aBufferSize)
266 // finito la commedia
267 std::cerr << "Can not allocate buffer - requested size ("
268 << (double(aBufferSize / (1024 * 1024)) * double(sizeof(GLfloat)))
269 << " MiB) is out of address space\n";
273 GLfloat* aBuffer = (GLfloat* )Standard::Allocate (aBytes);
276 // finito la commedia
277 std::cerr << "Can not allocate buffer with size ("
278 << (double(aBufferSize / (1024 * 1024)) * double(sizeof(GLfloat)))
283 glFeedbackBuffer ((GLsizei )aBufferSize, GL_2D, aBuffer);
284 glRenderMode (GL_FEEDBACK);
288 GLint aResult = glRenderMode (GL_RENDER);
293 void* aPtr = aBuffer;
294 Standard::Free (aPtr);
299 std::cout << "FeedBack result= " << aResult << "\n";
306 GLint aBitmapsNb = 0;
307 GLint aPassThrNb = 0;
308 GLint aUnknownNb = 0;
309 const GLint NODE_VALUES = 2; // GL_2D
310 for (GLint anIter = 0; anIter < aResult;)
312 const GLfloat aPos = aBuffer[anIter];
313 switch ((GLint )aPos)
319 anIter += 1 + NODE_VALUES;
322 case GL_LINE_RESET_TOKEN:
327 anIter += 1 + 2 * NODE_VALUES;
330 case GL_POLYGON_TOKEN:
332 const GLint aCount = (GLint )aBuffer[++anIter];
334 anIter += aCount * NODE_VALUES + 1;
339 else if (aCount == 4)
349 case GL_BITMAP_TOKEN:
350 case GL_DRAW_PIXEL_TOKEN:
351 case GL_COPY_PIXEL_TOKEN:
354 anIter += 1 + NODE_VALUES;
357 case GL_PASS_THROUGH_TOKEN:
360 anIter += 2; // header + value
371 void* aPtr = aBuffer;
372 Standard::Free (aPtr);
375 theDI << "Total nodes: " << aNodesNb << "\n"
376 << "Points: " << aPntNb << "\n"
377 << "Line segments: " << aLinesNb << "\n"
378 << "Triangles: " << aTriNb << "\n"
379 << "Quads: " << aQuadsNb << "\n"
380 << "Polygons: " << aPolyNb << "\n"
381 << "Bitmap tokens: " << aBitmapsNb << "\n"
382 << "Pass through: " << aPassThrNb << "\n"
383 << "UNKNOWN: " << aUnknownNb << "\n";
385 double aLen2D = double(aNodesNb * 2 + aPntNb + aLinesNb * 2 + (aTriNb + aQuadsNb + aPolyNb) * 2 + aBitmapsNb + aPassThrNb);
386 double aLen3D = double(aNodesNb * 3 + aPntNb + aLinesNb * 2 + (aTriNb + aQuadsNb + aPolyNb) * 2 + aBitmapsNb + aPassThrNb);
387 double aLen3D_rgba = double(aNodesNb * 7 + aPntNb + aLinesNb * 2 + (aTriNb + aQuadsNb + aPolyNb) * 2 + aBitmapsNb + aPassThrNb);
388 theDI << "Buffer size GL_2D: " << aLen2D * double(sizeof(GLfloat)) / double(1024 * 1024) << " MiB\n"
389 << "Buffer size GL_3D: " << aLen3D * double(sizeof(GLfloat)) / double(1024 * 1024) << " MiB\n"
390 << "Buffer size GL_3D_COLOR: " << aLen3D_rgba * double(sizeof(GLfloat)) / double(1024 * 1024) << " MiB\n";
395 //==============================================================================
396 //function : VImmediateFront
398 //==============================================================================
400 static int VImmediateFront (Draw_Interpretor& theDI,
401 Standard_Integer theArgNb,
402 const char** theArgVec)
405 Handle(AIS_InteractiveContext) aContextAIS = ViewerTest::GetAISContext();
406 if (aContextAIS.IsNull())
408 std::cerr << "No active view. Please call vinit.\n";
412 Handle(Graphic3d_GraphicDriver) aDriver = aContextAIS->CurrentViewer()->Driver();
414 if (aDriver.IsNull())
416 std::cerr << "Graphic driver not available.\n";
422 //theDI << "VBO: " << aDriver->ToUseVBO() << "\n";
424 std::cerr << "Wrong number of arguments.\n";
428 Graphic3d_CView* aCView = (Graphic3d_CView* )(ViewerTest::CurrentView()->View()->CView());
429 aDriver->SetImmediateModeDrawToFront (*aCView, atoi(theArgVec[1]) != 0);
433 //==============================================================================
436 //==============================================================================
438 static int VGlInfo (Draw_Interpretor& theDI,
439 Standard_Integer theArgNb,
440 const char** theArgVec)
442 // get the active view
443 Handle(V3d_View) aView = ViewerTest::CurrentView();
446 std::cerr << "No active view. Please call vinit.\n";
452 theDI << "OpenGL info:\n"
453 << " GLvendor = '" << (const char* )glGetString(GL_VENDOR) << "'\n"
454 << " GLdevice = '" << (const char* )glGetString(GL_RENDERER) << "'\n"
455 << " GLversion = '" << (const char* )glGetString(GL_VERSION) << "'\n"
456 << " GLSLversion = '" << (const char* )glGetString(GL_SHADING_LANGUAGE_VERSION) << "'\n";
460 const Standard_Boolean isList = theArgNb >= 3;
461 for (Standard_Integer anIter = 1; anIter < theArgNb; ++anIter)
463 TCollection_AsciiString aName (theArgVec[anIter]);
465 const char* aValue = NULL;
466 if (aName.Search ("VENDOR") != -1)
468 aValue = (const char* )glGetString (GL_VENDOR);
470 else if (aName.Search ("RENDERER") != -1)
472 aValue = (const char* )glGetString (GL_RENDERER);
474 else if (aName.Search ("SHADING_LANGUAGE_VERSION") != -1
475 || aName.Search ("GLSL") != -1)
477 aValue = (const char* )glGetString (GL_SHADING_LANGUAGE_VERSION);
479 else if (aName.Search ("VERSION") != -1)
481 aValue = (const char* )glGetString (GL_VERSION);
483 else if (aName.Search ("EXTENSIONS") != -1)
485 aValue = (const char* )glGetString (GL_EXTENSIONS);
489 std::cerr << "Unknown key '" << aName.ToCString() << "'\n";
495 theDI << "{" << aValue << "} ";
506 //=======================================================================
507 //function : OpenGlCommands
509 //=======================================================================
511 void ViewerTest::OpenGlCommands(Draw_Interpretor& theCommands)
513 const char* aGroup ="Commands for low-level TKOpenGl features";
515 theCommands.Add("vuserdraw",
516 "vuserdraw : name - simulates drawing with help of UserDraw",
517 __FILE__, VUserDraw, aGroup);
518 theCommands.Add("vfeedback",
519 "vfeedback : perform test GL feedback rendering",
520 __FILE__, VFeedback, aGroup);
521 theCommands.Add("vimmediatefront",
522 "vimmediatefront : render immediate mode to front buffer or to back buffer",
523 __FILE__, VImmediateFront, aGroup);
525 theCommands.Add("vglinfo",
526 "vglinfo [GL_VENDOR] [GL_RENDERER] [GL_VERSION] [GL_SHADING_LANGUAGE_VERSION] [GL_EXTENSIONS]"
528 __FILE__, VGlInfo, aGroup);