1 // Created on: 2012-04-09
2 // Created by: Sergey ANIKIN
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <ViewerTest.hxx>
18 #include <AIS_InteractiveContext.hxx>
19 #include <AIS_InteractiveObject.hxx>
21 #include <Draw_Interpretor.hxx>
22 #include <Graphic3d_Group.hxx>
23 #include <Graphic3d_ShaderObject.hxx>
24 #include <Graphic3d_ShaderProgram.hxx>
25 #include <OpenGl_AspectFace.hxx>
26 #include <OpenGl_AspectLine.hxx>
27 #include <OpenGl_AspectMarker.hxx>
28 #include <OpenGl_AspectText.hxx>
29 #include <OpenGl_Context.hxx>
30 #include <OpenGl_Element.hxx>
31 #include <OpenGl_GlCore20.hxx>
32 #include <OpenGl_GraphicDriver.hxx>
33 #include <OpenGl_Workspace.hxx>
34 #include <OSD_Environment.hxx>
35 #include <OSD_File.hxx>
36 #include <Prs3d_Drawer.hxx>
37 #include <Prs3d_Presentation.hxx>
38 #include <Prs3d_Root.hxx>
39 #include <Prs3d_ShadingAspect.hxx>
40 #include <Select3D_SensitiveCurve.hxx>
41 #include <SelectMgr_EntityOwner.hxx>
42 #include <SelectMgr_Selection.hxx>
43 #include <TCollection_AsciiString.hxx>
44 #include <V3d_View.hxx>
45 #include <V3d_Viewer.hxx>
46 #include <Visual3d_View.hxx>
47 #include <ViewerTest_DoubleMapOfInteractiveAndName.hxx>
48 #include <ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName.hxx>
50 extern Standard_Boolean VDisplayAISObject (const TCollection_AsciiString& theName,
51 const Handle(AIS_InteractiveObject)& theAISObj,
52 Standard_Boolean theReplaceIfExists = Standard_True);
53 extern ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
55 //=======================================================================
56 //function : VUserDraw
57 //purpose : Checks availability and operation of UserDraw feature
58 //=======================================================================
59 DEFINE_STANDARD_HANDLE(VUserDrawObj, AIS_InteractiveObject)
61 class VUserDrawObj : public AIS_InteractiveObject
65 DEFINE_STANDARD_RTTI(VUserDrawObj);
78 class Element : public OpenGl_Element
81 Handle(VUserDrawObj) myIObj;
84 Element (const Handle(VUserDrawObj)& theIObj,
85 Graphic3d_BndBox4f* theBounds)
89 myIObj->GetBounds(theBounds);
96 virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const
99 myIObj->Render(theWorkspace);
102 virtual void Release (OpenGl_Context*)
108 DEFINE_STANDARD_ALLOC
112 // Virtual methods implementation
113 void Compute (const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
114 const Handle(Prs3d_Presentation)& thePresentation,
115 const Standard_Integer theMode);
117 void ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
118 const Standard_Integer theMode);
120 // Called by VUserDrawElement
121 void Render(const Handle(OpenGl_Workspace)& theWorkspace) const;
122 void GetBounds(Graphic3d_BndBox4f* theBounds);
126 friend class Element;
128 IMPLEMENT_STANDARD_HANDLE(VUserDrawObj, AIS_InteractiveObject)
129 IMPLEMENT_STANDARD_RTTIEXT(VUserDrawObj, AIS_InteractiveObject)
131 void VUserDrawObj::Compute(const Handle(PrsMgr_PresentationManager3d)& /*thePresentationManager*/,
132 const Handle(Prs3d_Presentation)& thePresentation,
133 const Standard_Integer /*theMode*/)
135 thePresentation->Clear();
137 Handle(Graphic3d_Group) aGrp = Prs3d_Root::CurrentGroup(thePresentation);
138 aGrp->UserDraw(this, Standard_True, Standard_True);
141 void VUserDrawObj::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
142 const Standard_Integer /*theMode*/)
144 Handle(SelectMgr_EntityOwner) anEntityOwner = new SelectMgr_EntityOwner(this);
145 Handle(TColgp_HArray1OfPnt) aPnts = new TColgp_HArray1OfPnt(1, 5);
146 aPnts->SetValue(1, gp_Pnt(myCoords[0], myCoords[1], myCoords[2]));
147 aPnts->SetValue(2, gp_Pnt(myCoords[3], myCoords[4], myCoords[2]));
148 aPnts->SetValue(3, gp_Pnt(myCoords[3], myCoords[4], myCoords[5]));
149 aPnts->SetValue(4, gp_Pnt(myCoords[0], myCoords[1], myCoords[5]));
150 aPnts->SetValue(5, gp_Pnt(myCoords[0], myCoords[1], myCoords[2]));
151 Handle(Select3D_SensitiveCurve) aSensitive = new Select3D_SensitiveCurve(anEntityOwner, aPnts);
152 theSelection->Add(aSensitive);
155 void VUserDrawObj::GetBounds(Graphic3d_BndBox4f* theBounds)
159 Graphic3d_Vec4 aMinPt (myCoords[0], myCoords[1], myCoords[2], 1.0f);
160 Graphic3d_Vec4 aMaxPt (myCoords[3], myCoords[4], myCoords[5], 1.0f);
161 if (!theBounds->IsValid())
163 theBounds->Combine (Graphic3d_BndBox4f (aMinPt, aMaxPt));
167 theBounds->CornerMin() = aMinPt;
168 theBounds->CornerMax() = aMaxPt;
173 void VUserDrawObj::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
175 // To test linking against OpenGl_Workspace and all aspect classes
176 const OpenGl_AspectLine* aLA = theWorkspace->AspectLine(0);
177 const OpenGl_AspectMarker* aMA = theWorkspace->AspectMarker(0);
179 const OpenGl_AspectText* aTA = theWorkspace->AspectText(0);
181 TEL_COLOUR aColor = theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT ?
182 *(theWorkspace->HighlightColor) : aLA->Color();
184 // To test OpenGl_Window
185 //Handle(OpenGl_Context) aCtx = theWorkspace->GetGlContext();
187 // Finally draw something to make sure UserDraw really works
188 glPushAttrib(GL_ENABLE_BIT);
189 glDisable(GL_LIGHTING);
190 glColor4fv(aColor.rgb);
191 glBegin(GL_LINE_LOOP);
192 glVertex3f(myCoords[0], myCoords[1], myCoords[2]);
193 glVertex3f(myCoords[3], myCoords[4], myCoords[2]);
194 glVertex3f(myCoords[3], myCoords[4], myCoords[5]);
195 glVertex3f(myCoords[0], myCoords[1], myCoords[5]);
200 OpenGl_Element* VUserDrawCallback(const CALL_DEF_USERDRAW * theUserDraw)
202 Handle(VUserDrawObj) anIObj = (VUserDrawObj*)theUserDraw->Data;
205 std::cout << "VUserDrawCallback error: null object passed, the custom scene element will not be rendered" << std::endl;
208 return new VUserDrawObj::Element(anIObj, theUserDraw->Bounds);
211 static Standard_Integer VUserDraw (Draw_Interpretor& di,
212 Standard_Integer argc,
215 Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
216 if (aContext.IsNull())
218 di << argv[0] << "Call 'vinit' before!\n";
222 Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast (aContext->CurrentViewer()->Driver());
223 if (aDriver.IsNull())
225 std::cerr << "Graphic driver not available.\n";
231 di << argv[0] << "Wrong number of arguments, only the object name expected\n";
235 TCollection_AsciiString aName (argv[1]);
236 VDisplayAISObject(aName, Handle(AIS_InteractiveObject)());
238 // register the custom element factory function
239 aDriver->UserDrawCallback() = VUserDrawCallback;
241 Handle(VUserDrawObj) anIObj = new VUserDrawObj();
242 VDisplayAISObject(aName, anIObj);
247 //==============================================================================
248 //function : VFeedback
250 //==============================================================================
252 static int VFeedback (Draw_Interpretor& theDI,
253 Standard_Integer /*theArgNb*/,
254 const char** /*theArgVec*/)
256 // get the active view
257 Handle(V3d_View) aView = ViewerTest::CurrentView();
260 std::cerr << "No active view. Please call vinit.\n";
264 unsigned int aBufferSize = 1024 * 1024;
267 size_t aBytes = (size_t )aBufferSize * sizeof(GLfloat);
268 if (aBytes / sizeof(GLfloat) != (size_t )aBufferSize)
270 // finito la commedia
271 std::cerr << "Can not allocate buffer - requested size ("
272 << (double(aBufferSize / (1024 * 1024)) * double(sizeof(GLfloat)))
273 << " MiB) is out of address space\n";
277 GLfloat* aBuffer = (GLfloat* )Standard::Allocate (aBytes);
280 // finito la commedia
281 std::cerr << "Can not allocate buffer with size ("
282 << (double(aBufferSize / (1024 * 1024)) * double(sizeof(GLfloat)))
287 glFeedbackBuffer ((GLsizei )aBufferSize, GL_2D, aBuffer);
288 glRenderMode (GL_FEEDBACK);
292 GLint aResult = glRenderMode (GL_RENDER);
297 void* aPtr = aBuffer;
298 Standard::Free (aPtr);
303 std::cout << "FeedBack result= " << aResult << "\n";
310 GLint aBitmapsNb = 0;
311 GLint aPassThrNb = 0;
312 GLint aUnknownNb = 0;
313 const GLint NODE_VALUES = 2; // GL_2D
314 for (GLint anIter = 0; anIter < aResult;)
316 const GLfloat aPos = aBuffer[anIter];
317 switch ((GLint )aPos)
323 anIter += 1 + NODE_VALUES;
326 case GL_LINE_RESET_TOKEN:
331 anIter += 1 + 2 * NODE_VALUES;
334 case GL_POLYGON_TOKEN:
336 const GLint aCount = (GLint )aBuffer[++anIter];
338 anIter += aCount * NODE_VALUES + 1;
343 else if (aCount == 4)
353 case GL_BITMAP_TOKEN:
354 case GL_DRAW_PIXEL_TOKEN:
355 case GL_COPY_PIXEL_TOKEN:
358 anIter += 1 + NODE_VALUES;
361 case GL_PASS_THROUGH_TOKEN:
364 anIter += 2; // header + value
375 void* aPtr = aBuffer;
376 Standard::Free (aPtr);
379 theDI << "Total nodes: " << aNodesNb << "\n"
380 << "Points: " << aPntNb << "\n"
381 << "Line segments: " << aLinesNb << "\n"
382 << "Triangles: " << aTriNb << "\n"
383 << "Quads: " << aQuadsNb << "\n"
384 << "Polygons: " << aPolyNb << "\n"
385 << "Bitmap tokens: " << aBitmapsNb << "\n"
386 << "Pass through: " << aPassThrNb << "\n"
387 << "UNKNOWN: " << aUnknownNb << "\n";
389 double aLen2D = double(aNodesNb * 2 + aPntNb + aLinesNb * 2 + (aTriNb + aQuadsNb + aPolyNb) * 2 + aBitmapsNb + aPassThrNb);
390 double aLen3D = double(aNodesNb * 3 + aPntNb + aLinesNb * 2 + (aTriNb + aQuadsNb + aPolyNb) * 2 + aBitmapsNb + aPassThrNb);
391 double aLen3D_rgba = double(aNodesNb * 7 + aPntNb + aLinesNb * 2 + (aTriNb + aQuadsNb + aPolyNb) * 2 + aBitmapsNb + aPassThrNb);
392 theDI << "Buffer size GL_2D: " << aLen2D * double(sizeof(GLfloat)) / double(1024 * 1024) << " MiB\n"
393 << "Buffer size GL_3D: " << aLen3D * double(sizeof(GLfloat)) / double(1024 * 1024) << " MiB\n"
394 << "Buffer size GL_3D_COLOR: " << aLen3D_rgba * double(sizeof(GLfloat)) / double(1024 * 1024) << " MiB\n";
399 //==============================================================================
400 //function : VImmediateFront
402 //==============================================================================
404 static int VImmediateFront (Draw_Interpretor& /*theDI*/,
405 Standard_Integer theArgNb,
406 const char** theArgVec)
409 Handle(AIS_InteractiveContext) aContextAIS = ViewerTest::GetAISContext();
410 if (aContextAIS.IsNull())
412 std::cerr << "No active view. Please call vinit.\n";
416 Handle(Graphic3d_GraphicDriver) aDriver = aContextAIS->CurrentViewer()->Driver();
418 if (aDriver.IsNull())
420 std::cerr << "Graphic driver not available.\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 Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast (aView->View()->GraphicDriver());
455 if (aDriver.IsNull())
457 std::cerr << "Error: view does not use OpenGL.\n";
460 Handle(OpenGl_Context) aCtx = aDriver->GetSharedContext();
461 Standard_CString aDebugInfo = !aCtx.IsNull() && aCtx->IsDebugContext()
464 theDI << "OpenGL info:\n"
465 << " GLvendor = '" << (const char* )glGetString(GL_VENDOR) << "'\n"
466 << " GLdevice = '" << (const char* )glGetString(GL_RENDERER) << "'\n"
467 << " GLversion = '" << (const char* )glGetString(GL_VERSION) << "'\n"
468 << " GLSLversion = '" << (const char* )glGetString(GL_SHADING_LANGUAGE_VERSION) << "'\n"
473 const Standard_Boolean isList = theArgNb >= 3;
474 for (Standard_Integer anIter = 1; anIter < theArgNb; ++anIter)
476 TCollection_AsciiString aName (theArgVec[anIter]);
478 const char* aValue = NULL;
479 if (aName.Search ("VENDOR") != -1)
481 aValue = (const char* )glGetString (GL_VENDOR);
483 else if (aName.Search ("RENDERER") != -1)
485 aValue = (const char* )glGetString (GL_RENDERER);
487 else if (aName.Search ("SHADING_LANGUAGE_VERSION") != -1
488 || aName.Search ("GLSL") != -1)
490 aValue = (const char* )glGetString (GL_SHADING_LANGUAGE_VERSION);
492 else if (aName.Search ("VERSION") != -1)
494 aValue = (const char* )glGetString (GL_VERSION);
496 else if (aName.Search ("EXTENSIONS") != -1)
498 aValue = (const char* )glGetString (GL_EXTENSIONS);
502 std::cerr << "Unknown key '" << aName.ToCString() << "'\n";
508 theDI << "{" << aValue << "} ";
520 //==============================================================================
521 //function : VShaderProg
522 //purpose : Sets the pair of vertex and fragment shaders for the object
523 //==============================================================================
524 static Standard_Integer VShaderProg (Draw_Interpretor& /*theDI*/,
525 Standard_Integer theArgNb,
526 const char** theArgVec)
528 Handle(AIS_InteractiveContext) aCtx = ViewerTest::GetAISContext();
531 std::cerr << "Use 'vinit' command before " << theArgVec[0] << "\n";
534 else if (theArgNb < 2)
536 std::cerr << theArgVec[0] << " syntax error: lack of arguments\n";
540 TCollection_AsciiString aLastArg (theArgVec[theArgNb - 1]);
541 aLastArg.UpperCase();
542 const Standard_Boolean toTurnOff = aLastArg == "OFF";
543 Standard_Integer anArgsNb = theArgNb - 1;
544 Handle(Graphic3d_ShaderProgram) aProgram;
546 && aLastArg == "PHONG")
548 aProgram = new Graphic3d_ShaderProgram (Graphic3d_ShaderProgram::ShaderName_Phong);
551 && aProgram.IsNull())
555 std::cerr << theArgVec[0] << " syntax error: lack of arguments\n";
559 const TCollection_AsciiString aSrcVert = theArgVec[theArgNb - 2];
560 const TCollection_AsciiString aSrcFrag = theArgVec[theArgNb - 1];
561 if (!aSrcVert.IsEmpty()
562 && !OSD_File (aSrcVert).Exists())
564 std::cerr << "Non-existing vertex shader source\n";
567 if (!aSrcFrag.IsEmpty()
568 && !OSD_File (aSrcFrag).Exists())
570 std::cerr << "Non-existing fragment shader source\n";
574 aProgram = new Graphic3d_ShaderProgram();
575 aProgram->AttachShader (Graphic3d_ShaderObject::CreateFromFile (Graphic3d_TOS_VERTEX, aSrcVert));
576 aProgram->AttachShader (Graphic3d_ShaderObject::CreateFromFile (Graphic3d_TOS_FRAGMENT, aSrcFrag));
577 anArgsNb = theArgNb - 2;
580 Handle(AIS_InteractiveObject) anIO;
582 || *theArgVec[1] == '*')
584 for (ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName anIter (GetMapOfAIS());
585 anIter.More(); anIter.Next())
587 anIO = Handle(AIS_InteractiveObject)::DownCast (anIter.Key1());
590 anIO->Attributes()->ShadingAspect()->Aspect()->SetShaderProgram (aProgram);
591 aCtx->Redisplay (anIO, Standard_False);
594 aCtx->UpdateCurrentViewer();
598 for (Standard_Integer anArgIter = 1; anArgIter < anArgsNb; ++anArgIter)
600 const TCollection_AsciiString aName (theArgVec[anArgIter]);
601 if (!GetMapOfAIS().IsBound2 (aName))
603 std::cerr << "Warning: " << aName.ToCString() << " is not displayed\n";
606 anIO = Handle(AIS_InteractiveObject)::DownCast (GetMapOfAIS().Find2 (aName));
609 std::cerr << "Warning: " << aName.ToCString() << " is not an AIS object\n";
612 anIO->Attributes()->ShadingAspect()->Aspect()->SetShaderProgram (aProgram);
613 aCtx->Redisplay (anIO, Standard_False);
616 aCtx->UpdateCurrentViewer();
620 //=======================================================================
621 //function : OpenGlCommands
623 //=======================================================================
625 void ViewerTest::OpenGlCommands(Draw_Interpretor& theCommands)
627 const char* aGroup ="Commands for low-level TKOpenGl features";
629 theCommands.Add("vuserdraw",
630 "vuserdraw : name - simulates drawing with help of UserDraw",
631 __FILE__, VUserDraw, aGroup);
632 theCommands.Add("vfeedback",
633 "vfeedback : perform test GL feedback rendering",
634 __FILE__, VFeedback, aGroup);
635 theCommands.Add("vimmediatefront",
636 "vimmediatefront : render immediate mode to front buffer or to back buffer",
637 __FILE__, VImmediateFront, aGroup);
638 theCommands.Add("vglinfo",
639 "vglinfo [GL_VENDOR] [GL_RENDERER] [GL_VERSION] [GL_SHADING_LANGUAGE_VERSION] [GL_EXTENSIONS]"
641 __FILE__, VGlInfo, aGroup);
642 theCommands.Add("vshaderprog",
643 " 'vshaderprog [name] pathToVertexShader pathToFragmentShader'"
644 "\n\t\t: or 'vshaderprog [name] off' to disable GLSL program"
645 "\n\t\t: or 'vshaderprog [name] phong' to enable per-pixel lighting calculations"
646 "\n\t\t: * might be used to specify all displayed objects",
647 __FILE__, VShaderProg, aGroup);