[Blog] Sexy background

If you want to add a special touch to your application, here is a couple of hints on 'personalizing' your 3D view...

Read more at http://opencascade.blogspot.com/2008/12/sexy-background.html

Sharjith Naramparambath's picture

That looks great but does not work outright! I am sorry I do not know what else has to be done to get the gradient background. But simply using your code fragment, even with the Handle(Visual3d_Layer) aLayer taken as member variable in the appwizard generated code does not show the gradient. Only default flat color set while creating the viewer.

Regards
N. Sharjith

Roman Lygin's picture

Just in case, replied on the blog...

Sharjith Naramparambath's picture

A small shortcoming... When local context is opened, hilighting/selection of objects causes gradient overlay to disappear and default background to be exposed, but reappears after the view is refreshed.

Regards
N. Sharjith

Paul Jimenez's picture

I'm using a layer in my application to draw arrows as an overlay, and I have the same problem. The moment a shape is highlighted, the layer is not drawn. I already posted about it, and I even tried to look into it for a solution. Unfortunately, after jumping from function to function in many different files, I was unable to find a way to solve this issue. Constantly refreshing the view wouldn't be a good solution either.

Carlos's picture

Hi,

Have you solved this problem?.

I'm experiencing a similar problem with the gradient background explained in the Roman's Blog if a NIS object is created.

Some hints:

Everything works fine at startup or if only AIS-Objects are created
If a NIS object (lines) are created, the gradient dissapears and the background color imposed in aLayer->SetColor (aTopColor); remains.

I have tried to change the Aspect_TOL_UNDERLAY parameter in the definition of the layer to Aspect_TOL_OVERLAY. In this case, the gradient remain and the NIS Objects appears but no AIS-Objects are created (the trihedron also dissapears).

I'm pretty sure that there is not a bug as I have included the VSetBgColor function given in the Blog in Drawexe and it works fine.

I have adapted the C# sample of Opencascade and this is the init procedure:
bool OCCViewer::InitViewerNIS(void* wnd)
{
try {
myGraphicDevice = new Graphic3d_WNTGraphicDevice();
} catch (Standard_Failure) {
return false;
}
TCollection_ExtendedString a3DName("Visu3D");
Handle(WNT_Window) aWNTWindow = new WNT_Window(myGraphicDevice, reinterpret_cast (wnd));
myViewer = new V3d_Viewer( myGraphicDevice, a3DName.ToExtString(),"", 1000.0,
V3d_XposYnegZpos, Quantity_NOC_DARKGOLDENROD4,
V3d_ZBUFFER,V3d_GOURAUD,V3d_WAIT,
Standard_True, Standard_False);
if (!aWNTWindow->IsMapped())
aWNTWindow->Map();

myViewer->Init();
myViewer->SetDefaultLights();
myViewer->SetLightOn();
myViewer->SetDefaultBackgroundColor(Quantity_NOC_DARKGOLDENROD4);

nisView = new NIS_View(myViewer,aWNTWindow);

myNISContext = new NIS_InteractiveContext;
myNISContext->AttachView(nisView);
myNISContext->SetSelectionMode (NIS_InteractiveContext::Mode_Normal);
myNISContext->SetSelectionMode (NIS_InteractiveContext::Mode_Normal);

myAISContext = new AIS_InteractiveContext(myViewer);
myAISContext->SelectionColor(Quantity_NOC_DARKGOLDENROD4);

myView = Handle(V3d_View)::DownCast(nisView);

nisView->TriedronDisplay(Aspect_TOTP_LEFT_LOWER,
Quantity_NOC_WHITE,
0.1, V3d_ZBUFFER);
myNISContext->UpdateViews();

myView->Redraw();
myView->MustBeResized();

nisView->Redraw();
nisView->MustBeResized();

bool retbool;
retbool=OCCViewer::OnBackground(); //Hace un gradiente en la presentación

return true;

}

I have also tried to reply the init procedure given in Drawexe, but without success.

I'm thinking that it is a problem with the Z order of the layers and views or with the V3D_Viewer parameters, but I have not a deep knowledge of OCC so any suggestions will be welcomed.

Carlos

Roman Lygin's picture

Hi Carlos,

Hopefully the fix from Francois will help you. Check it here - http://www.opencascade.org/org/forum/thread_16172/. I bet NIS should not bring any specificity here. Please let us know if it worked.

Good luck.
Roman

---
opencascade.blogspot.com - the Open CASCADE blog

Carlos's picture

Hi Roman,

I have tried the patch on the thread, I have not seen it before, but not sucess.

I have made some trials:

1. I have rebuild the TKV3D dll with the modified code proposed by Francois.
2. Debugging the app. it seems that the application does not use this code. Creating a view, drawing a AIS ciyinder and a NIS line. This also happens if only V3_View (no NIS_VIEW) are created, so only drawing AIS Shapes.
3. If the parameter of the layer Aspect_TOL_UNDERLAY is changed to Aspect_TOL_OVERLAY the results are the same as described in the previous post, no influence of the patch.

I think that it is not a bug as it works properly in Drawexe, but I'm not able to fix it.

This is a modified AUXNIS function given in http://www.opencascade.org/org/forum/thread_14292/ that includes a AIS_Shape. It works combined with the VSetBgColor proposed by you.

static int auxNIS (Draw_Interpretor& dout, Standard_Integer n, const char** a)
{
// using the NIS view previously created by the command "vinit"
const Handle(V3d_View) aView = ViewerTest::CurrentView();
aView->TriedronDisplay(Aspect_TOTP_LEFT_LOWER,
Quantity_NOC_WHITE,
0.1, V3d_ZBUFFER);
aView->MustBeResized();

// Create the cylindrical shape (shell of 3 faces)
BRepPrim_Cylinder aCyl(gp_Ax2(gp_Pnt(0., 0., 0.), gp_Dir(0., 0., 1.)),
10., 20.);
TopoDS_Shape shape = aCyl.Shell();
for (TopExp_Explorer exp(shape, TopAbs_FACE); exp.More(); exp.Next())
{
const TopoDS_Face& face = TopoDS::Face(exp.Current());
BRepMesh::Mesh(face, 0.1);
Handle_NIS_Surface surface = new NIS_Surface(face);
TheNISContext()->Display(surface);
}

TopoDS_Shape S1 =BRepPrimAPI_MakeCylinder (gp_Ax2(gp_Pnt(10,10,10),
gp_Dir(1,0,0)),
10,20);

Handle(AIS_Shape) ais1 = new AIS_Shape(S1);
TheAISContext()->Display(ais1,Standard_True);

TheNISContext()->UpdateViews();

return 0;
}

All I have described also happens with AIS in wireframe mode. If a NIS line is superposed with AIS shape the Line prevails. I think that the order of the presentation is:

NIS shapes.
Layer created with Aspect_TOL_OVERLAY (if aLayer->Destroy() is called, the AIS shape appears.
AIS Shapes (including the trihedron)
Layer created with Aspect_TOL_UNDERLAY
Background of the viewer

Thanks for your help.
Carlos

Carlos's picture

Hi,

some points to try to help this issue.

I have found one difference between my application and Drawexe that could explain this problem.

In drawexe, the sentence MyGraphicDriver->TriedronDisplay (MyCView,APosition, AColor, AScale, AsWireframe); called in Visual3d_View::TriedronDisplay goes to OpenGl_GraphicDriver::TriedronDisplay. In my application, based in the C# OCC example does not use OpenGL and the graphicdevice is created with myGraphicDevice = new Graphic3d_WNTGraphicDevice(); The debugger pass over the MyGraphicDriver->TriedronDisplay without calling any other function.

In the documentation, it is stated that NIS objects use OPENGL directly.

Regards,
Carlos

Carlos's picture

Hi,

To try to help to other people.

This is not the difference between these two applications. It was my mistake. I think it was related with the linkage of TKOpengl.dll.

In Drawexe is loaded with the TKViewerTest.dll as there are a call to OpenGL_AVi functions.

In the C# example it is not directly loaded. Instead it is loaded in Graphic3d_WNTGraphicDevice::SetGraphicDriver() based in the environment variable "CSF_GraphicShr", that was pointed to the release version.

I included the TKOpengl reference in OCC.dll and now it is loaded at the beginning of the process, being debuggeable. I think, but not sure, that is because the linkage is done with the /FORCE:MULTIPLE option.

Regard,

Carlos

Carlos's picture

Hi,

After some time playing with this problem, I have found these unforeseen behaivours.

1. The layer defined in the C# sample changes the limits Y (vertical) axis from (-1, 1) to (-1,0). I have been checked this trying to paint a small rectangle to see what happens. If the following function is used, the rectangle that appears covers almost the display, leaving about a 10% at each side. In the Drawexe, the limits are mantained to the standards one.

bool OCCViewer::OnBackground()

{
Quantity_Color aTopColor (0.9, 0.9, 0.9, Quantity_TOC_RGB);
Quantity_Color aBottomColor (1, 1, 1, Quantity_TOC_RGB);
Standard_Boolean aSizeDependant = Standard_True;

if (aLayer.IsNull())
aLayer = new Visual3d_Layer (myView->Viewer()->Viewer(),
Aspect_TOL_UNDERLAY, aSizeDependant);

int aWidth =10, aHeight = 10;
aLayer->Clear();
aLayer->Begin();
aLayer->SetViewport (aWidth, aHeight);
aLayer->BeginPolygon();
aLayer->SetColor (aTopColor);
aLayer->AddVertex (-0.9,-0.1); // Limits modified
aLayer->AddVertex (0.9,-0.1);

aLayer->SetColor (aBottomColor);

aLayer->AddVertex (0.9,-0.9);
aLayer->AddVertex (-0.9,-0.9);

aLayer->ClosePrimitive();
aLayer->End();

return 1;
}

The other behaivour that I have found is that it is possible to use NIS_SURFACES with the gradient background and AIS Shapes, but if a line is create, the gradient dissapears. Again in Drawexe the behaivour is correct.

I have modified the InitViewer function in the C# sample, as if someone wants to reply this problem. In this case it is also necessary to include the above OnBackground() function and include the include files and TKNIS.lib to the OCC project.

Regards,

Carlos

Carlos's picture

The modified InitViewer()

bool OCCViewer::InitViewer(void* wnd)
{
try {
myGraphicDevice = new Graphic3d_WNTGraphicDevice();
} catch (Standard_Failure) {
return false;
}
TCollection_ExtendedString a3DName("Visu3D");
myViewer = new V3d_Viewer( myGraphicDevice, a3DName.ToExtString(),"", 1000.0,
V3d_XposYnegZpos, Quantity_NOC_GRAY30,
V3d_ZBUFFER,V3d_GOURAUD,V3d_WAIT,
Standard_True, Standard_False);

Handle(WNT_Window) aWNTWindow = new WNT_Window(myGraphicDevice, reinterpret_cast (wnd));

myViewer->Init();
// myViewer->SetZBufferManagment( Standard_False ); // <- THIS LINE HELPS!! Probado y no funciona.
myViewer->SetDefaultLights();
myViewer->SetLightOn();
myViewer->SetDefaultBackgroundColor(Quantity_NOC_DARKGOLDENROD4);

nisView = new NIS_View(myViewer,aWNTWindow);

myNISContext = new NIS_InteractiveContext;
myNISContext->AttachView(nisView);
myNISContext->SetSelectionMode (NIS_InteractiveContext::Mode_Normal);
myNISContext->SetSelectionMode (NIS_InteractiveContext::Mode_Normal);

myAISContext = new AIS_InteractiveContext(myViewer);
myAISContext->SelectionColor(Quantity_NOC_DARKGOLDENROD4);

myView = Handle(V3d_View)::DownCast(nisView);

nisView->TriedronDisplay(Aspect_TOTP_LEFT_LOWER,
Quantity_NOC_DARKGOLDENROD4,
0.1, V3d_ZBUFFER);

OCCViewer::OnBackground();

// Create the cylindrical shape (shell of 3 faces)
BRepPrim_Cylinder aCyl(gp_Ax2(gp_Pnt(0., 0., 0.), gp_Dir(0., 0., 1.)),
10., 20.);
TopoDS_Shape shape = aCyl.Shell();
for (TopExp_Explorer exp(shape, TopAbs_FACE); exp.More(); exp.Next())
{
const TopoDS_Face& face = TopoDS::Face(exp.Current());
BRepMesh::Mesh(face, 0.1);
Handle_NIS_Surface surface = new NIS_Surface(face);
myNISContext->Display(surface);
}
/*
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DISPLAY A LINE
int IDnis=-1;
Handle(NCollection_BaseAllocator) theAlloc;
Handle_NIS_Triangulated miNIS=new NIS_Triangulated(2,theAlloc);
int nnodos=3;
int nodo=1;

miNIS->SetLinePrs(2,Standard_False,2);
nodo=0;
gp_XYZ Coord0(0, 0, 0);
miNIS->SetNode(nodo, Coord0);
nodo=1;
gp_XYZ Coord1(1000, 200, 300);
miNIS->SetNode(nodo, Coord1);
miNIS->SetLineNode(0,0);
miNIS->SetLineNode(1,1);
myNISContext->Display(miNIS,Standard_False);

Quantity_Color aColor (0.1, 0.7, 0.7, Quantity_TOC_RGB);
miNIS->SetColor(aColor,Standard_False);
IDnis=miNIS->ID();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
*/

myNISContext->UpdateViews();

myView->Redraw();
myView->MustBeResized();

nisView->Redraw();
nisView->MustBeResized();
return true;

}