From f8b2ed3650adf176fe29662504b171adca255c00 Mon Sep 17 00:00:00 2001 From: vsr Date: Tue, 6 Mar 2012 14:23:19 +0400 Subject: [PATCH] 0023000: Improve the way the gradient and textured background is managed in 3d viewer --- src/OpenGl/OpenGl_View_2.cxx | 143 +++++++++---------- src/V3d/V3d_View.cdl | 15 +- src/V3d/V3d_View.cxx | 7 +- src/ViewerTest/ViewerTest_ViewerCommands.cxx | 130 ++++++++++++++++- 4 files changed, 210 insertions(+), 85 deletions(-) diff --git a/src/OpenGl/OpenGl_View_2.cxx b/src/OpenGl/OpenGl_View_2.cxx index 885c467f3d..1834b0fcf7 100644 --- a/src/OpenGl/OpenGl_View_2.cxx +++ b/src/OpenGl/OpenGl_View_2.cxx @@ -692,46 +692,12 @@ void OpenGl_View::Render (const Handle(OpenGl_Workspace) &AWorkspace, if ( glIsEnabled( GL_DEPTH_TEST ) ) glDisable( GL_DEPTH_TEST ); //push GL_ENABLE_BIT - // drawing bg image if defined - if ( myBgTexture.TexId != 0 ) - { - GLfloat texX_range = 1.F; // texture coordinate - GLfloat texY_range = 1.F; // texture coordinate - - // Set up for stretching or tiling - GLfloat x_offset, y_offset; - if ( myBgTexture.Style == Aspect_FM_CENTERED ) - { - x_offset = (GLfloat)myBgTexture.Width / (GLfloat)aViewWidth; - y_offset = (GLfloat)myBgTexture.Height / (GLfloat)aViewHeight; - } - else - { - x_offset = 1.F; - y_offset = 1.F; - if ( myBgTexture.Style == Aspect_FM_TILED ) - { - texX_range = (GLfloat)aViewWidth / (GLfloat)myBgTexture.Width; - texY_range = (GLfloat)aViewHeight / (GLfloat)myBgTexture.Height; - } - } - - glEnable( GL_TEXTURE_2D ); //push GL_ENABLE_BIT - glBindTexture( GL_TEXTURE_2D, myBgTexture.TexId ); //push GL_TEXTURE_BIT - - glDisable( GL_BLEND ); //push GL_ENABLE_BIT - - glColor3fv( AWorkspace->BackgroundColor().rgb ); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); //push GL_TEXTURE_BIT - - glBegin( GL_QUADS ); - glTexCoord2f(0.F, 0.F); glVertex2f( -x_offset, -y_offset ); - glTexCoord2f(texX_range, 0.F); glVertex2f( x_offset, -y_offset ); - glTexCoord2f(texX_range, texY_range); glVertex2f( x_offset, y_offset ); - glTexCoord2f(0.F, texY_range); glVertex2f( -x_offset, y_offset ); - glEnd(); - } - else //if( myBgGradient.type != Aspect_GFM_NONE ) + // drawing bg gradient if: + // - gradient fill type is not Aspect_GFM_NONE and + // - either background texture is no specified or it is drawn in Aspect_FM_CENTERED mode + if ( ( myBgGradient.type != Aspect_GFM_NONE ) && + ( myBgTexture.TexId == 0 || myBgTexture.Style == Aspect_FM_CENTERED || + myBgTexture.Style == Aspect_FM_NONE ) ) { Tfloat* corner1 = 0;/* -1,-1*/ Tfloat* corner2 = 0;/* 1,-1*/ @@ -743,16 +709,16 @@ void OpenGl_View::Render (const Handle(OpenGl_Workspace) &AWorkspace, switch( myBgGradient.type ) { case Aspect_GFM_HOR: - corner1 = myBgGradient.color2.rgb; + corner1 = myBgGradient.color1.rgb; corner2 = myBgGradient.color2.rgb; - corner3 = myBgGradient.color1.rgb; + corner3 = myBgGradient.color2.rgb; corner4 = myBgGradient.color1.rgb; break; case Aspect_GFM_VER: corner1 = myBgGradient.color2.rgb; - corner2 = myBgGradient.color1.rgb; + corner2 = myBgGradient.color2.rgb; corner3 = myBgGradient.color1.rgb; - corner4 = myBgGradient.color2.rgb; + corner4 = myBgGradient.color1.rgb; break; case Aspect_GFM_DIAG1: corner2 = myBgGradient.color2.rgb; @@ -773,28 +739,28 @@ void OpenGl_View::Render (const Handle(OpenGl_Workspace) &AWorkspace, corner4 = dcorner2; break; case Aspect_GFM_CORNER1: - corner1 = myBgGradient.color1.rgb; + corner1 = myBgGradient.color2.rgb; corner2 = myBgGradient.color2.rgb; corner3 = myBgGradient.color2.rgb; - corner4 = myBgGradient.color2.rgb; + corner4 = myBgGradient.color1.rgb; break; case Aspect_GFM_CORNER2: corner1 = myBgGradient.color2.rgb; - corner2 = myBgGradient.color1.rgb; - corner3 = myBgGradient.color2.rgb; + corner2 = myBgGradient.color2.rgb; + corner3 = myBgGradient.color1.rgb; corner4 = myBgGradient.color2.rgb; break; case Aspect_GFM_CORNER3: corner1 = myBgGradient.color2.rgb; - corner2 = myBgGradient.color2.rgb; - corner3 = myBgGradient.color1.rgb; + corner2 = myBgGradient.color1.rgb; + corner3 = myBgGradient.color2.rgb; corner4 = myBgGradient.color2.rgb; break; case Aspect_GFM_CORNER4: - corner1 = myBgGradient.color2.rgb; + corner1 = myBgGradient.color1.rgb; corner2 = myBgGradient.color2.rgb; corner3 = myBgGradient.color2.rgb; - corner4 = myBgGradient.color1.rgb; + corner4 = myBgGradient.color2.rgb; break; default: //printf("gradient background type not right\n"); @@ -810,14 +776,14 @@ void OpenGl_View::Render (const Handle(OpenGl_Workspace) &AWorkspace, glShadeModel( GL_SMOOTH ); //push GL_LIGHTING_BIT glBegin(GL_TRIANGLE_FAN); - if( myBgGradient.type != Aspect_GFM_CORNER2 && myBgGradient.type != Aspect_GFM_CORNER4 ) + if( myBgGradient.type != Aspect_GFM_CORNER1 && myBgGradient.type != Aspect_GFM_CORNER3 ) { glColor3f(corner1[0],corner1[1],corner1[2]); glVertex2f(-1.,-1.); glColor3f(corner2[0],corner2[1],corner2[2]); glVertex2f( 1.,-1.); glColor3f(corner3[0],corner3[1],corner3[2]); glVertex2f( 1., 1.); glColor3f(corner4[0],corner4[1],corner4[2]); glVertex2f(-1., 1.); } - else //if ( myBgGradient.type == Aspect_GFM_CORNER2 || myBgGradient.type == Aspect_GFM_CORNER4 ) + else //if ( myBgGradient.type == Aspect_GFM_CORNER1 || myBgGradient.type == Aspect_GFM_CORNER3 ) { glColor3f(corner2[0],corner2[1],corner2[2]); glVertex2f( 1.,-1.); glColor3f(corner3[0],corner3[1],corner3[2]); glVertex2f( 1., 1.); @@ -830,6 +796,47 @@ void OpenGl_View::Render (const Handle(OpenGl_Workspace) &AWorkspace, if ( curSM != GL_SMOOTH ) glShadeModel( curSM ); } + // drawing bg image if: + // - it is defined and + // - fill type is not Aspect_FM_NONE + if ( myBgTexture.TexId != 0 && myBgTexture.Style != Aspect_FM_NONE ) + { + GLfloat texX_range = 1.F; // texture coordinate + GLfloat texY_range = 1.F; // texture coordinate + + // Set up for stretching or tiling + GLfloat x_offset, y_offset; + if ( myBgTexture.Style == Aspect_FM_CENTERED ) + { + x_offset = (GLfloat)myBgTexture.Width / (GLfloat)aViewWidth; + y_offset = (GLfloat)myBgTexture.Height / (GLfloat)aViewHeight; + } + else + { + x_offset = 1.F; + y_offset = 1.F; + if ( myBgTexture.Style == Aspect_FM_TILED ) + { + texX_range = (GLfloat)aViewWidth / (GLfloat)myBgTexture.Width; + texY_range = (GLfloat)aViewHeight / (GLfloat)myBgTexture.Height; + } + } + + glEnable( GL_TEXTURE_2D ); //push GL_ENABLE_BIT + glBindTexture( GL_TEXTURE_2D, myBgTexture.TexId ); //push GL_TEXTURE_BIT + + glDisable( GL_BLEND ); //push GL_ENABLE_BIT + + glColor3fv( AWorkspace->BackgroundColor().rgb ); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); //push GL_TEXTURE_BIT + + glBegin( GL_QUADS ); + glTexCoord2f(0.F, 0.F); glVertex2f( -x_offset, -y_offset ); + glTexCoord2f(texX_range, 0.F); glVertex2f( x_offset, -y_offset ); + glTexCoord2f(texX_range, texY_range); glVertex2f( x_offset, y_offset ); + glTexCoord2f(0.F, texY_range); glVertex2f( -x_offset, y_offset ); + glEnd(); + } glPopMatrix(); glMatrixMode( GL_PROJECTION ); @@ -1461,15 +1468,7 @@ void OpenGl_View::CreateBackgroundTexture (const Standard_CString AFileName, con myBgTexture.TexId = texture; myBgTexture.Width = width; myBgTexture.Height = height; - switch ( AFillStyle ) - { - case Aspect_FM_NONE : - myBgTexture.Style = Aspect_FM_CENTERED; - break; - default : - myBgTexture.Style = AFillStyle; - break; - } + myBgTexture.Style = AFillStyle; } } @@ -1478,19 +1477,7 @@ void OpenGl_View::CreateBackgroundTexture (const Standard_CString AFileName, con //call_togl_set_bg_texture_style void OpenGl_View::SetBackgroundTextureStyle (const Aspect_FillMethod AFillStyle) { - /* check if background texture is already created */ - if ( myBgTexture.TexId != 0 ) - { - switch ( AFillStyle ) - { - case Aspect_FM_NONE : - myBgTexture.Style = Aspect_FM_CENTERED; - break; - default : - myBgTexture.Style = AFillStyle; - break; - } - } + myBgTexture.Style = AFillStyle; } /*----------------------------------------------------------------------*/ @@ -1521,9 +1508,7 @@ void OpenGl_View::SetBackgroundGradient (const Quantity_Color& AColor1, //call_togl_set_gradient_type void OpenGl_View::SetBackgroundGradientType (const Aspect_GradientFillMethod AType) { - // check if gradient background is already created - if ( myBgGradient.type != Aspect_GFM_NONE ) - myBgGradient.type = AType; + myBgGradient.type = AType; } /*----------------------------------------------------------------------*/ diff --git a/src/V3d/V3d_View.cdl b/src/V3d/V3d_View.cdl index 5e4a6089e1..35a1de9de7 100755 --- a/src/V3d/V3d_View.cdl +++ b/src/V3d/V3d_View.cdl @@ -279,7 +279,8 @@ is ---Level: Public ---Purpose: Defines the gradient background colours of the view -- by supplying : - -- the colour objects. + -- two colour objects, + -- and fill method (horizontal by default) SetBgGradientColors ( me : mutable ; Color1 : NameOfColor; @@ -289,9 +290,10 @@ is ---Level: Public ---Purpose: Defines the gradient background colours of the view -- by supplying : - -- the colour names in the form Quantity_NOC_xxxx . + -- two colour names in the form Quantity_NOC_xxxx, + -- and fill method (horizontal by default) - SetBgGradientStyle( me ; + SetBgGradientStyle( me : mutable ; AMethod : GradientFillMethod from Aspect = Aspect_GFM_HOR; update : Boolean from Standard = Standard_False); ---Level: Public @@ -300,9 +302,16 @@ is SetBackgroundImage( me : mutable; FileName : CString from Standard; FillStyle : FillMethod from Aspect = Aspect_FM_CENTERED; update : Boolean from Standard = Standard_False ); + ---Level: Public + ---Purpose: Defines the background texture of the view + --- by supplying : + --- texture image file name, + --- and fill method (centered by default) SetBgImageStyle( me : mutable; FillStyle : FillMethod from Aspect; update : Boolean from Standard = Standard_False ); + ---Level: Public + ---Purpose: Defines the textured background fill method of the view SetAxis ( me : mutable; X,Y,Z : Coordinate ; Vx,Vy,Vz : Parameter ) diff --git a/src/V3d/V3d_View.cxx b/src/V3d/V3d_View.cxx index 8798e4c020..864f7aaa53 100755 --- a/src/V3d/V3d_View.cxx +++ b/src/V3d/V3d_View.cxx @@ -696,10 +696,13 @@ void V3d_View::SetBgGradientColors( const Quantity_NameOfColor Color1, /*----------------------------------------------------------------------*/ void V3d_View::SetBgGradientStyle( const Aspect_GradientFillMethod FillStyle, - const Standard_Boolean update) const + const Standard_Boolean update) { + Quantity_Color Color1, Color2; + MyGradientBackground.Colors( Color1, Color2 ); + MyGradientBackground.SetColors( Color1, Color2, FillStyle ); if( MyView->IsDefined() ) - MyView->SetBgGradientStyle( FillStyle , update ) ; + MyView->SetBgGradientStyle( FillStyle, update ) ; } /*----------------------------------------------------------------------*/ diff --git a/src/ViewerTest/ViewerTest_ViewerCommands.cxx b/src/ViewerTest/ViewerTest_ViewerCommands.cxx index 5f40457d3e..e8a7808ac0 100755 --- a/src/ViewerTest/ViewerTest_ViewerCommands.cxx +++ b/src/ViewerTest/ViewerTest_ViewerCommands.cxx @@ -1376,6 +1376,7 @@ static int VSetBg(Draw_Interpretor& di, Standard_Integer argc, const char** argv if (argc < 2 || argc > 3) { di << "Usage : " << argv[0] << " imagefile [filltype] : Load image as background" << "\n"; + di << "filltype can be one of CENTERED, TILED, STRETCH, NONE" << "\n"; return 1; } @@ -1408,6 +1409,49 @@ static int VSetBg(Draw_Interpretor& di, Standard_Integer argc, const char** argv return 0; } +//============================================================================== +//function : VSetBgMode +//purpose : Change background image fill type +//============================================================================== + +static int VSetBgMode(Draw_Interpretor& di, Standard_Integer argc, const char** argv) +{ + if (argc != 2) + { + di << "Usage : " << argv[0] << " filltype : Change background image mode" << "\n"; + di << "filltype must be one of CENTERED, TILED, STRETCH, NONE" << "\n"; + return 1; + } + + Handle(AIS_InteractiveContext) AISContext = ViewerTest::GetAISContext(); + if(AISContext.IsNull()) + { + di << "use 'vinit' command before " << argv[0] << "\n"; + return 1; + } + + Aspect_FillMethod aFillType; + if (argc == 2) + { + const char* szType = argv[1]; + if (strcmp(szType, "NONE" ) == 0) aFillType = Aspect_FM_NONE; + else if (strcmp(szType, "CENTERED") == 0) aFillType = Aspect_FM_CENTERED; + else if (strcmp(szType, "TILED" ) == 0) aFillType = Aspect_FM_TILED; + else if (strcmp(szType, "STRETCH" ) == 0) aFillType = Aspect_FM_STRETCH; + else + { + di << "Wrong fill type : " << szType << "\n"; + di << "Must be one of CENTERED, TILED, STRETCH, NONE" << "\n"; + return 1; + } + } + + Handle(V3d_View) V3dView = ViewerTest::CurrentView(); + V3dView->SetBgImageStyle(aFillType, Standard_True); + + return 0; +} + //============================================================================== //function : VSetGradientBg //purpose : Mount gradient background @@ -1460,6 +1504,81 @@ static int VSetGradientBg(Draw_Interpretor& di, Standard_Integer argc, const cha return 0; } +//============================================================================== +//function : VSetGradientBgMode +//purpose : Change gradient background fill style +//============================================================================== +static int VSetGradientBgMode(Draw_Interpretor& di, Standard_Integer argc, const char** argv) +{ + if (argc != 2 ) + { + di << "Usage : " << argv[0] << " Type : Change gradient background fill type" << "\n"; + di << "Type must be one of 0 = NONE, 1 = HOR, 2 = VER, 3 = DIAG1, 4 = DIAG2" << "\n"; + di << " 5 = CORNER1, 6 = CORNER2, 7 = CORNER3, 8 = CORNER4" << "\n"; + return 1; + } + + Handle(AIS_InteractiveContext) AISContext = ViewerTest::GetAISContext(); + if(AISContext.IsNull()) + { + di << "use 'vinit' command before " << argv[0] << "\n"; + return 1; + } + if (argc == 2) + { + int aType = atoi(argv[1]); + if( aType < 0 || aType > 8 ) + { + di << "Wrong fill type " << "\n"; + di << "Must be one of 0 = NONE, 1 = HOR, 2 = VER, 3 = DIAG1, 4 = DIAG2" << "\n"; + di << " 5 = CORNER1, 6 = CORNER2, 7 = CORNER3, 8 = CORNER4" << "\n"; + return 1; + } + + Aspect_GradientFillMethod aMethod = Aspect_GradientFillMethod(aType); + + Handle(V3d_View) V3dView = ViewerTest::CurrentView(); + V3dView->SetBgGradientStyle( aMethod, 1 ); + } + + return 0; +} + +//============================================================================== +//function : VSetColorBg +//purpose : Set color background +//============================================================================== +static int VSetColorBg(Draw_Interpretor& di, Standard_Integer argc, const char** argv) +{ + if (argc != 4 ) + { + di << "Usage : " << argv[0] << " R G B : Set color background" << "\n"; + di << "R,G,B = [0..255]" << "\n"; + return 1; + } + + Handle(AIS_InteractiveContext) AISContext = ViewerTest::GetAISContext(); + if(AISContext.IsNull()) + { + di << "use 'vinit' command before " << argv[0] << "\n"; + return 1; + } + if (argc == 4) + { + + Standard_Real R = atof(argv[1])/255.; + Standard_Real G = atof(argv[2])/255.; + Standard_Real B = atof(argv[3])/255.; + Quantity_Color aColor(R,G,B,Quantity_TOC_RGB); + + Handle(V3d_View) V3dView = ViewerTest::CurrentView(); + V3dView->SetBackgroundColor( aColor ); + V3dView->Update(); + } + + return 0; +} + //============================================================================== //function : VScale //purpose : View Scaling @@ -1980,9 +2099,18 @@ void ViewerTest::ViewerCommands(Draw_Interpretor& theCommands) theCommands.Add("vsetbg", "vsetbg : vsetbg imagefile [filltype] : Load image as background", __FILE__,VSetBg,group); + theCommands.Add("vsetbgmode", + "vsetbgmode : vsetbgmode filltype : Change background image fill type", + __FILE__,VSetBgMode,group); theCommands.Add("vsetgradientbg", - "vsetgradientbg : vsetgradientbg r1 g1 b1 r2 g2 b2 filltype : Mount gradient background", + "vsetgradientbg : vsetgradientbg r1 g1 b1 r2 g2 b2 filltype : Mount gradient background", __FILE__,VSetGradientBg,group); + theCommands.Add("vsetgrbgmode", + "vsetgrbgmode : vsetgrbgmode filltype : Change gradient background fill type", + __FILE__,VSetGradientBgMode,group); + theCommands.Add("vsetcolorbg", + "vsetcolorbg : vsetcolorbg r g b : Set background color", + __FILE__,VSetColorBg,group); theCommands.Add("vscale", "vscale : vscale X Y Z", __FILE__,VScale,group); -- 2.39.5