how to do texture mapping on Visual3d_Layer

how to do texture mapping on Visual3d_Layer?

I mean that I draw a rectangle by using Visual3d_Layer,and this rectangle don't change it's position and orientation when rotation and panning.

Now I want to map a texture on the rectangle, how can I do it? who can help me?

Thank you!

Roman Lygin's picture

You might want to check the Sexy Background post to see what Open CASCADE offers. If it's not exactly what you need you might want to check the source code and derive yours.

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

zhangzhigang824's picture

Hi Roman,

thanks a lot!

I have seen the site, and it gives examples to change the color of the background.
Can you give me a example to map a texture on a Layer Geometry.

thanks again!

Roman Lygin's picture

Hi,

The first chapter of the post (*Adding an image as background*) is about setting a texture into the under-layer.
Perhaps the V3d_View::SetBackgroundImage() method will be just enough for you (it accepts the image and location/stretch parameter). If not, dive into it and see what it does and mimic in your code.

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

zhangzhigang824's picture

Hi Roman,

Thanks for your reply!

I need to map a texture on a rectangle but not on the whole view. I have solved this problem by using OpenGL texture mapping functions. the code is followed:

Standard_Integer w,h;
Handle(Aspect_Window) hWin = myView->Window();
hWin->Size(w,h);
Handle(Visual3d_Layer) myLayer=new Visual3d_Layer(myView->View()->ViewManager(),
Aspect_TOL_OVERLAY, Standard_False);

myLayer->Clear();
myLayer->SetViewport(w,h);
myLayer->SetOrtho(0,w,h,0,(Aspect_TypeOfConstraint)-1);

// then you could draw on the given layer
myLayer->Begin();

glEnable(GL_TEXTURE_2D);
LoadGLTextures();
Standard_Integer X,Y;
myView->Convert (200,140,200,X,Y) ;
// draw a polyline
myLayer->BeginPolygon();

glTexCoord2f(0.0f, 1.0f); myLayer->AddVertex(X,Y, Standard_False);
glTexCoord2f(1.0f, 1.0f); myLayer->AddVertex(X,Y+20);
glTexCoord2f(1.0f, 0.0f); myLayer->AddVertex(X+100,Y+20);
glTexCoord2f(0.0f, 0.0f); myLayer->AddVertex(X+100,Y);

myLayer->ClosePrimitive();
glDisable(GL_TEXTURE_2D);
// and close the layer and redraw
myLayer->End();
myView->Redraw();

*********************************************************

We can use OpenGL functions in OpenCasCade directly.

Francois Lauzon's picture

Hello Roman,
I have implemented the gradient background, it's quite nice! It's work fine under most case, but I have one case which cause flicker with dynamic selection:

1- Open local context
2- set vertex filter
3- when I move the mouse, it's switch between the gradient background and the full color background... once I close the local context, I don't have this problem (neutral context dynamic selection works fine)..

Have you notice that behavior.
Francois.

Francois Lauzon's picture

Ok, I have found a way to make it work, I guess it's a bug. In the file Visual3d_TransientManager.cxx, everytime the Graphic3d_CView is used, the pointer to the overlay and underlay are not set, so the opengl layer don't know they exist (in some case only, like the one I mention in an earlier post). So I just initialize those pointers and everything seems to work just fine.

It's the same things for all those methods:
Visual3d_TransientManager::BeginDraw
Visual3d_TransientManager::ClearDraw
Visual3d_TransientManager::BeginAddDraw
after the line:

theCView = *(CALL_DEF_VIEW *)AView->CView ();

I added those lines:

if (!AView->UnderLayer().IsNull()) {
theCView.ptrUnderLayer=(CALL_DEF_LAYER *)&(AView->UnderLayer()->CLayer());
}
if (!AView->OverLayer().IsNull()) {
theCView.ptrOverLayer=(CALL_DEF_LAYER *)&(AView->OverLayer()->CLayer());
}

Paul Jimenez's picture

Nice finding. It was one of those annoying issues I wanted to solve, but I couldn't find the right file to look for it. I have already applied it to my local copy, and it works just fine. Thanks for sharing.

OCCPATCH

Francois Lauzon's picture

I glad it's working for you too! I just saw the Visal3D_Layer posting, I didn't remember it, I have been using Layers for some time now, but it's the first time I use them with local context open.

Roman Lygin's picture

Hi Francois,

Yes, I faced this bug too though fortunately it does not affect my app yet.
The bug itself was first reported here - http://www.opencascade.org/org/forum/thread_15167/. Glad you have fixed it. Kudos !

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