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 <Aspect_GraphicDevice.hxx>
27 #include <AIS_InteractiveContext.hxx>
28 #include <AIS_InteractiveObject.hxx>
30 #include <Draw_Interpretor.hxx>
31 #include <Graphic3d_Group.hxx>
32 #include <OpenGl_ArbVBO.hxx>
33 #include <OpenGl_AspectFace.hxx>
34 #include <OpenGl_AspectLine.hxx>
35 #include <OpenGl_AspectMarker.hxx>
36 #include <OpenGl_AspectText.hxx>
37 #include <OpenGl_Context.hxx>
38 #include <OpenGl_Element.hxx>
39 #include <OpenGl_ExtFBO.hxx>
40 #include <OpenGl_GlCore20.hxx>
41 #include <OpenGl_GraphicDriver.hxx>
42 #include <OpenGl_Workspace.hxx>
43 #include <Prs3d_Presentation.hxx>
44 #include <Prs3d_Root.hxx>
45 #include <Select3D_SensitiveCurve.hxx>
46 #include <SelectMgr_EntityOwner.hxx>
47 #include <SelectMgr_Selection.hxx>
48 #include <V3d_Viewer.hxx>
49 #include <TCollection_AsciiString.hxx>
50 #include <V3d_View.hxx>
51 #include <Visual3d_View.hxx>
53 extern Standard_Boolean VDisplayAISObject (const TCollection_AsciiString& theName,
54 const Handle(AIS_InteractiveObject)& theAISObj,
55 Standard_Boolean theReplaceIfExists = Standard_True);
57 //=======================================================================
58 //function : VUserDraw
59 //purpose : Checks availability and operation of UserDraw feature
60 //=======================================================================
61 DEFINE_STANDARD_HANDLE(VUserDrawObj, AIS_InteractiveObject)
63 class VUserDrawObj : public AIS_InteractiveObject
67 DEFINE_STANDARD_RTTI(VUserDrawObj);
80 class Element : public OpenGl_Element
83 Handle(VUserDrawObj) myIObj;
86 Element (const Handle(VUserDrawObj)& theIObj,
87 CALL_DEF_BOUNDS* theBounds)
91 myIObj->GetBounds(theBounds);
98 virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const
100 if (!myIObj.IsNull())
101 myIObj->Render(theWorkspace);
104 virtual void Release (const Handle(OpenGl_Context)& theGlCtx)
110 DEFINE_STANDARD_ALLOC
114 // Virtual methods implementation
115 void Compute (const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
116 const Handle(Prs3d_Presentation)& thePresentation,
117 const Standard_Integer theMode);
119 void ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
120 const Standard_Integer theMode);
122 // Called by VUserDrawElement
123 void Render(const Handle(OpenGl_Workspace)& theWorkspace) const;
124 void GetBounds(CALL_DEF_BOUNDS* theBounds);
128 friend class Element;
130 IMPLEMENT_STANDARD_HANDLE(VUserDrawObj, AIS_InteractiveObject)
131 IMPLEMENT_STANDARD_RTTIEXT(VUserDrawObj, AIS_InteractiveObject)
133 void VUserDrawObj::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
134 const Handle(Prs3d_Presentation)& thePresentation,
135 const Standard_Integer theMode)
137 thePresentation->Clear();
139 Handle(Graphic3d_Group) aGrp = Prs3d_Root::CurrentGroup(thePresentation);
140 aGrp->UserDraw(this, Standard_True, Standard_True);
143 void VUserDrawObj::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
144 const Standard_Integer theMode)
146 Handle(SelectMgr_EntityOwner) anEntityOwner = new SelectMgr_EntityOwner(this);
147 Handle(TColgp_HArray1OfPnt) aPnts = new TColgp_HArray1OfPnt(1, 5);
148 aPnts->SetValue(1, gp_Pnt(myCoords[0], myCoords[1], myCoords[2]));
149 aPnts->SetValue(2, gp_Pnt(myCoords[3], myCoords[4], myCoords[2]));
150 aPnts->SetValue(3, gp_Pnt(myCoords[3], myCoords[4], myCoords[5]));
151 aPnts->SetValue(4, gp_Pnt(myCoords[0], myCoords[1], myCoords[5]));
152 aPnts->SetValue(5, gp_Pnt(myCoords[0], myCoords[1], myCoords[2]));
153 Handle(Select3D_SensitiveCurve) aSensitive = new Select3D_SensitiveCurve(anEntityOwner, aPnts);
154 theSelection->Add(aSensitive);
157 void VUserDrawObj::GetBounds(CALL_DEF_BOUNDS* theBounds)
161 theBounds->XMin = myCoords[0];
162 theBounds->YMin = myCoords[1];
163 theBounds->ZMin = myCoords[2];
164 theBounds->XMax = myCoords[3];
165 theBounds->YMax = myCoords[4];
166 theBounds->ZMax = myCoords[5];
170 void VUserDrawObj::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
172 // To test linking against OpenGl_Workspace and all aspect classes
173 const OpenGl_AspectLine* aLA = theWorkspace->AspectLine(0);
174 const OpenGl_AspectFace* aFA = theWorkspace->AspectFace(0);
175 const OpenGl_AspectMarker* aMA = theWorkspace->AspectMarker(0);
177 const OpenGl_AspectText* aTA = theWorkspace->AspectText(0);
179 TEL_COLOUR aColor = theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT ?
180 *(theWorkspace->HighlightColor) : aLA->Color();
182 // To test OpenGl_Window
183 //Handle(OpenGl_Context) aCtx = theWorkspace->GetGlContext();
185 // Finally draw something to make sure UserDraw really works
186 glPushAttrib(GL_ENABLE_BIT);
187 glDisable(GL_LIGHTING);
188 glColor4fv(aColor.rgb);
189 glBegin(GL_LINE_LOOP);
190 glVertex3f(myCoords[0], myCoords[1], myCoords[2]);
191 glVertex3f(myCoords[3], myCoords[4], myCoords[2]);
192 glVertex3f(myCoords[3], myCoords[4], myCoords[5]);
193 glVertex3f(myCoords[0], myCoords[1], myCoords[5]);
198 OpenGl_Element* VUserDrawCallback(const CALL_DEF_USERDRAW * theUserDraw)
200 Handle(VUserDrawObj) anIObj = (VUserDrawObj*)theUserDraw->Data;
203 std::cout << "VUserDrawCallback error: null object passed, the custom scene element will not be rendered" << std::endl;
206 return new VUserDrawObj::Element(anIObj, theUserDraw->Bounds);
209 static Standard_Integer VUserDraw (Draw_Interpretor& di,
210 Standard_Integer argc,
213 Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
214 if (aContext.IsNull())
216 di << argv[0] << "Call 'vinit' before!\n";
220 Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast (aContext->CurrentViewer()->Device()->GraphicDriver());
221 if (aDriver.IsNull())
223 std::cerr << "Graphic driver not available.\n";
229 di << argv[0] << "Wrong number of arguments, only the object name expected\n";
233 TCollection_AsciiString aName (argv[1]);
234 VDisplayAISObject(aName, Handle(AIS_InteractiveObject)());
236 // register the custom element factory function
237 aDriver->UserDrawCallback() = VUserDrawCallback;
239 Handle(VUserDrawObj) anIObj = new VUserDrawObj();
240 VDisplayAISObject(aName, anIObj);
245 //==============================================================================
246 //function : VFeedback
248 //==============================================================================
250 static int VFeedback (Draw_Interpretor& theDI,
251 Standard_Integer theArgNb,
252 const char** theArgVec)
254 // get the active view
255 Handle(V3d_View) aView = ViewerTest::CurrentView();
258 std::cerr << "No active view. Please call vinit.\n";
262 unsigned int aBufferSize = 1024 * 1024;
265 size_t aBytes = (size_t )aBufferSize * sizeof(GLfloat);
266 if (aBytes / sizeof(GLfloat) != (size_t )aBufferSize)
268 // finito la commedia
269 std::cerr << "Can not allocate buffer - requested size ("
270 << (double(aBufferSize / (1024 * 1024)) * double(sizeof(GLfloat)))
271 << " MiB) is out of address space\n";
275 GLfloat* aBuffer = (GLfloat* )Standard::Allocate (aBytes);
278 // finito la commedia
279 std::cerr << "Can not allocate buffer with size ("
280 << (double(aBufferSize / (1024 * 1024)) * double(sizeof(GLfloat)))
285 glFeedbackBuffer ((GLsizei )aBufferSize, GL_2D, aBuffer);
286 glRenderMode (GL_FEEDBACK);
290 GLint aResult = glRenderMode (GL_RENDER);
295 void* aPtr = aBuffer;
296 Standard::Free (aPtr);
301 std::cout << "FeedBack result= " << aResult << "\n";
308 GLint aBitmapsNb = 0;
309 GLint aPassThrNb = 0;
310 GLint aUnknownNb = 0;
311 const GLint NODE_VALUES = 2; // GL_2D
312 for (GLint anIter = 0; anIter < aResult;)
314 const GLfloat aPos = aBuffer[anIter];
315 switch ((GLint )aPos)
321 anIter += 1 + NODE_VALUES;
324 case GL_LINE_RESET_TOKEN:
329 anIter += 1 + 2 * NODE_VALUES;
332 case GL_POLYGON_TOKEN:
334 const GLint aCount = (GLint )aBuffer[++anIter];
336 anIter += aCount * NODE_VALUES + 1;
341 else if (aCount == 4)
351 case GL_BITMAP_TOKEN:
352 case GL_DRAW_PIXEL_TOKEN:
353 case GL_COPY_PIXEL_TOKEN:
356 anIter += 1 + NODE_VALUES;
359 case GL_PASS_THROUGH_TOKEN:
362 anIter += 2; // header + value
373 void* aPtr = aBuffer;
374 Standard::Free (aPtr);
377 theDI << "Total nodes: " << aNodesNb << "\n"
378 << "Points: " << aPntNb << "\n"
379 << "Line segments: " << aLinesNb << "\n"
380 << "Triangles: " << aTriNb << "\n"
381 << "Quads: " << aQuadsNb << "\n"
382 << "Polygons: " << aPolyNb << "\n"
383 << "Bitmap tokens: " << aBitmapsNb << "\n"
384 << "Pass through: " << aPassThrNb << "\n"
385 << "UNKNOWN: " << aUnknownNb << "\n";
387 double aLen2D = double(aNodesNb * 2 + aPntNb + aLinesNb * 2 + (aTriNb + aQuadsNb + aPolyNb) * 2 + aBitmapsNb + aPassThrNb);
388 double aLen3D = double(aNodesNb * 3 + aPntNb + aLinesNb * 2 + (aTriNb + aQuadsNb + aPolyNb) * 2 + aBitmapsNb + aPassThrNb);
389 double aLen3D_rgba = double(aNodesNb * 7 + aPntNb + aLinesNb * 2 + (aTriNb + aQuadsNb + aPolyNb) * 2 + aBitmapsNb + aPassThrNb);
390 theDI << "Buffer size GL_2D: " << aLen2D * double(sizeof(GLfloat)) / double(1024 * 1024) << " MiB\n"
391 << "Buffer size GL_3D: " << aLen3D * double(sizeof(GLfloat)) / double(1024 * 1024) << " MiB\n"
392 << "Buffer size GL_3D_COLOR: " << aLen3D_rgba * double(sizeof(GLfloat)) / double(1024 * 1024) << " MiB\n";
397 //==============================================================================
398 //function : VImmediateFront
400 //==============================================================================
402 static int VImmediateFront (Draw_Interpretor& theDI,
403 Standard_Integer theArgNb,
404 const char** theArgVec)
407 Handle(AIS_InteractiveContext) aContextAIS = ViewerTest::GetAISContext();
408 if (aContextAIS.IsNull())
410 std::cerr << "No active view. Please call vinit.\n";
414 Handle(Graphic3d_GraphicDriver) aDriver =
415 Handle(Graphic3d_GraphicDriver)::DownCast (aContextAIS->CurrentViewer()->Device()->GraphicDriver());
416 if (aDriver.IsNull())
418 std::cerr << "Graphic driver not available.\n";
424 //theDI << "VBO: " << aDriver->ToUseVBO() << "\n";
426 std::cerr << "Wrong number of arguments.\n";
430 Graphic3d_CView* aCView = (Graphic3d_CView* )(ViewerTest::CurrentView()->View()->CView());
431 aDriver->SetImmediateModeDrawToFront (*aCView, atoi(theArgVec[1]) != 0);
435 //==============================================================================
438 //==============================================================================
440 static int VGlInfo (Draw_Interpretor& theDI,
441 Standard_Integer theArgNb,
442 const char** theArgVec)
444 // get the active view
445 Handle(V3d_View) aView = ViewerTest::CurrentView();
448 std::cerr << "No active view. Please call vinit.\n";
454 theDI << "OpenGL info:\n"
455 << " GLvendor = '" << (const char* )glGetString(GL_VENDOR) << "'\n"
456 << " GLdevice = '" << (const char* )glGetString(GL_RENDERER) << "'\n"
457 << " GLversion = '" << (const char* )glGetString(GL_VERSION) << "'\n"
458 << " GLSLversion = '" << (const char* )glGetString(GL_SHADING_LANGUAGE_VERSION) << "'\n";
462 const Standard_Boolean isList = theArgNb >= 3;
463 for (Standard_Integer anIter = 1; anIter < theArgNb; ++anIter)
465 TCollection_AsciiString aName (theArgVec[anIter]);
467 const char* aValue = NULL;
468 if (aName.Search ("VENDOR") != -1)
470 aValue = (const char* )glGetString (GL_VENDOR);
472 else if (aName.Search ("RENDERER") != -1)
474 aValue = (const char* )glGetString (GL_RENDERER);
476 else if (aName.Search ("SHADING_LANGUAGE_VERSION") != -1
477 || aName.Search ("GLSL") != -1)
479 aValue = (const char* )glGetString (GL_SHADING_LANGUAGE_VERSION);
481 else if (aName.Search ("VERSION") != -1)
483 aValue = (const char* )glGetString (GL_VERSION);
485 else if (aName.Search ("EXTENSIONS") != -1)
487 aValue = (const char* )glGetString (GL_EXTENSIONS);
491 std::cerr << "Unknown key '" << aName.ToCString() << "'\n";
497 theDI << "{" << aValue << "} ";
508 //=======================================================================
509 //function : OpenGlCommands
511 //=======================================================================
513 void ViewerTest::OpenGlCommands(Draw_Interpretor& theCommands)
515 const char* aGroup ="Commands for low-level TKOpenGl features";
517 theCommands.Add("vuserdraw",
518 "vuserdraw : name - simulates drawing with help of UserDraw",
519 __FILE__, VUserDraw, aGroup);
520 theCommands.Add("vfeedback",
521 "vfeedback : perform test GL feedback rendering",
522 __FILE__, VFeedback, aGroup);
523 theCommands.Add("vimmediatefront",
524 "vimmediatefront : render immediate mode to front buffer or to back buffer",
525 __FILE__, VImmediateFront, aGroup);
527 theCommands.Add("vglinfo",
528 "vglinfo [GL_VENDOR] [GL_RENDERER] [GL_VERSION] [GL_SHADING_LANGUAGE_VERSION] [GL_EXTENSIONS]"
530 __FILE__, VGlInfo, aGroup);