Is it possible to draw a picture using OpenGL in the OpenCASCADE Viewer?

This question is similar to this:https://dev.opencascade.org/content/using-opencascade-opengl

I understand that the Viewer is typically used for handling three-dimensional objects, but I have a requirement to draw an image using OpenGL in the OpenCASCADE Viewer and place it at the bottom of the Viewer as a legend. While I am aware that it is possible to indirectly achieve this by using the SetBackgroundImage method to set the legend as the background, my boss seems not satisfied with this approach.
I have also found this discussion:https://dev.opencascade.org/content/running-custom-opengl-alongside-v3d-... , but as a beginner, I am unable to comprehend how to customize OpenGL in the viewer.
Therefore, I would like to know if there is any method to accomplish this goal. Thank you!

gkv311 n's picture

Dealing with OpenGL is over-complicated for this purpose.

If what you want is just displaying an image in 3D viewer - you may achieve this by displaying a texture, applying 2D transformation persistence to the presentation (Graphic3d_TMF_2d) and assigning desired Z-layer (Graphic3d_ZLayerId_BotOSD). You may do this even using standard AIS_Shape presentation or by defining own very simple AIS_InteractiveObject subclass.

pload MODELING VISUALIZATION
vinit View1 -width 512 -height 512
vbackground -gradient GRAY20 GRAY80
restore [locate_data_file Ball.brep] b
vdisplay -dispMode 1 b
vfit

set dx 220; set dy 74
set logoPath "$::env(CSF_OCCTResourcePath)/DrawResources/OCC_logo.png"
box logo $dx $dy 0 -preview
vdisplay -dispMode 1 logo -topmost -2d bottomRight [expr $dx+25] 25
vtexture logo "$logoPath"
vaspects logo -alphaMode BLEND -shadingModel UNLIT
vdisplay logo -underlay
vaspects logo -alphaMode MASKBLEND 0.001 -shadingModel UNLIT

TOM kom's picture

Thanks for your prompt reply, I followed your advice by using AIS_TexturedShape to achieve this. The result looks great.

// Create legend
{
    TopoDS_Shape aTopoBoxTop1 = BRepPrimAPI_MakeBox(1000.0, 120.0, 0.1);

    gp_Trsf translation;
    translation.SetTranslation(gp_Vec(0, 0.0, 0));

    // Apply translation to the top box
    BRepBuilderAPI_Transform myTransform(aTopoBoxTop1, translation);
    TopoDS_Shape aTopoBox = myTransform.Shape();
    Handle_AIS_TexturedShape texture = new AIS_TexturedShape(aTopoBox);

    // Texture-related settings
    texture->SetTextureFileName("./data/legend.png");
    texture->SetTextureMapOn();
    texture->SetTextureRepeat(true, 1, 1);
    texture->DisableTextureModulate();

    // Set Z value (depth)
    texture->SetZLayer(Graphic3d_ZLayerId_Topmost);

    // Add the textured shape to the graphic context
    aViewer.myContext->SetDisplayMode(texture, 3, false);
    aViewer.myContext->Display(texture, 3, 0, false);
    texture->SetTransformPersistence(new Graphic3d_TransformPers(Graphic3d_TMF_2d));
}

By the way, I still wanted to know if there are any articles or examples on creating OpenGL within the Open CASCADE viewer. Even though I can currently achieve the desired effect using AIS_TexturedShape, for the purpose of learning OpenGL and Open CASCADE, my boss might still prefer me to use OpenGL :-) thanks again for your help.

gkv311 n's picture

You may find VUserDraw sample that performs low-level OpenGL drawing within src/OpenGlTest/OpenGlTest_Commands.cxx in OCCT.

TOM kom's picture

Thanks, I will check out it☺️