mirror of
https://git.dev.opencascade.org/repos/occt.git
synced 2025-08-04 13:13:25 +03:00
0024250: TKOpenGl - per-pixel lighting using GLSL program (Phong shading)
This commit is contained in:
@@ -32,9 +32,6 @@
|
||||
#include <TopLoc_Location.hxx>
|
||||
#include <TopTools_HArray1OfShape.hxx>
|
||||
#include <TColStd_HArray1OfTransient.hxx>
|
||||
#include <OSD_Directory.hxx>
|
||||
#include <OSD_File.hxx>
|
||||
#include <OSD_Path.hxx>
|
||||
#include <OSD_Timer.hxx>
|
||||
#include <Geom_Axis2Placement.hxx>
|
||||
#include <Geom_Axis1Placement.hxx>
|
||||
@@ -54,10 +51,8 @@
|
||||
#include <AIS_ListIteratorOfListOfInteractive.hxx>
|
||||
#include <Aspect_InteriorStyle.hxx>
|
||||
#include <Graphic3d_AspectFillArea3d.hxx>
|
||||
#include <Graphic3d_TextureRoot.hxx>
|
||||
#include <Graphic3d_AspectLine3d.hxx>
|
||||
#include <Graphic3d_ShaderObject.hxx>
|
||||
#include <Graphic3d_ShaderProgram.hxx>
|
||||
#include <Graphic3d_TextureRoot.hxx>
|
||||
#include <Image_AlienPixMap.hxx>
|
||||
#include <Prs3d_ShadingAspect.hxx>
|
||||
#include <Prs3d_IsoAspect.hxx>
|
||||
@@ -1899,64 +1894,6 @@ Standard_Integer VTexture (Draw_Interpretor& di,Standard_Integer argc, const cha
|
||||
return 0;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
//function : VShaderProg
|
||||
//purpose : Sets the pair of vertex and fragment shaders for the object
|
||||
//==============================================================================
|
||||
static Standard_Integer VShaderProg (Draw_Interpretor& /*theDI*/,
|
||||
Standard_Integer theArgNb,
|
||||
const char** theArgVec)
|
||||
{
|
||||
Handle(AIS_InteractiveContext) anAISContext = ViewerTest::GetAISContext();
|
||||
if (anAISContext.IsNull())
|
||||
{
|
||||
std::cerr << "Use 'vinit' command before " << theArgVec[0] << "\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (theArgNb < 3)
|
||||
{
|
||||
std::cerr << theArgVec[0] <<" syntax error: lack of arguments - Type 'help vshaderprog\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
const TCollection_AsciiString aShapeName = theArgVec[1];
|
||||
if (!GetMapOfAIS().IsBound2 (aShapeName))
|
||||
{
|
||||
std::cerr << theArgVec[0] << ": Use 'vdisplay' before\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (GetMapOfAIS().Find2 (aShapeName));
|
||||
if (anIO.IsNull())
|
||||
{
|
||||
std::cerr << "Shape " << aShapeName.ToCString() << " does not exist\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
Handle(Graphic3d_ShaderProgram) aProgram = new Graphic3d_ShaderProgram();
|
||||
const TCollection_AsciiString aVertexSource (theArgVec[2]);
|
||||
if (!aVertexSource.IsEmpty() && !OSD_File(aVertexSource).Exists())
|
||||
{
|
||||
std::cerr << "Non-existing vertex shader source\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
TCollection_AsciiString aFragmentSource (theArgVec[3]);
|
||||
if (!aFragmentSource.IsEmpty() && !OSD_File(aFragmentSource).Exists())
|
||||
{
|
||||
std::cerr << "Non-existing fragment shader source\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
aProgram->AttachShader (Graphic3d_ShaderObject::CreateFromFile (Graphic3d_TOS_VERTEX, aVertexSource));
|
||||
aProgram->AttachShader (Graphic3d_ShaderObject::CreateFromFile (Graphic3d_TOS_FRAGMENT, aFragmentSource));
|
||||
anIO->Attributes()->ShadingAspect()->Aspect()->SetShaderProgram (aProgram);
|
||||
|
||||
anAISContext->Redisplay (anIO);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
//function : VDisplay2
|
||||
//author : ege
|
||||
@@ -3341,10 +3278,6 @@ void ViewerTest::Commands(Draw_Interpretor& theCommands)
|
||||
or 'vtexture NameOfShape IdOfTexture' (0<=IdOfTexture<=20)' to use predefined textures\n ",
|
||||
__FILE__,VTexture,group);
|
||||
|
||||
theCommands.Add("vshaderprog",
|
||||
"'vshaderprog NameOfShape VertexShaderFile FragmentShaderFile'",
|
||||
__FILE__,VShaderProg,group);
|
||||
|
||||
theCommands.Add("vtexscale",
|
||||
"'vtexscale NameOfShape ScaleU ScaleV' \n \
|
||||
or 'vtexscale NameOfShape ScaleUV' \n \
|
||||
|
@@ -23,11 +23,14 @@
|
||||
|
||||
#include <ViewerTest.hxx>
|
||||
|
||||
#include <AIS_Drawer.hxx>
|
||||
#include <AIS_InteractiveContext.hxx>
|
||||
#include <AIS_InteractiveObject.hxx>
|
||||
#include <Draw.hxx>
|
||||
#include <Draw_Interpretor.hxx>
|
||||
#include <Graphic3d_Group.hxx>
|
||||
#include <Graphic3d_ShaderObject.hxx>
|
||||
#include <Graphic3d_ShaderProgram.hxx>
|
||||
#include <OpenGl_ArbVBO.hxx>
|
||||
#include <OpenGl_AspectFace.hxx>
|
||||
#include <OpenGl_AspectLine.hxx>
|
||||
@@ -39,19 +42,25 @@
|
||||
#include <OpenGl_GlCore20.hxx>
|
||||
#include <OpenGl_GraphicDriver.hxx>
|
||||
#include <OpenGl_Workspace.hxx>
|
||||
#include <OSD_Environment.hxx>
|
||||
#include <OSD_File.hxx>
|
||||
#include <Prs3d_Presentation.hxx>
|
||||
#include <Prs3d_Root.hxx>
|
||||
#include <Prs3d_ShadingAspect.hxx>
|
||||
#include <Select3D_SensitiveCurve.hxx>
|
||||
#include <SelectMgr_EntityOwner.hxx>
|
||||
#include <SelectMgr_Selection.hxx>
|
||||
#include <V3d_Viewer.hxx>
|
||||
#include <TCollection_AsciiString.hxx>
|
||||
#include <V3d_View.hxx>
|
||||
#include <V3d_Viewer.hxx>
|
||||
#include <Visual3d_View.hxx>
|
||||
#include <ViewerTest_DoubleMapOfInteractiveAndName.hxx>
|
||||
#include <ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName.hxx>
|
||||
|
||||
extern Standard_Boolean VDisplayAISObject (const TCollection_AsciiString& theName,
|
||||
const Handle(AIS_InteractiveObject)& theAISObj,
|
||||
Standard_Boolean theReplaceIfExists = Standard_True);
|
||||
extern ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
|
||||
|
||||
//=======================================================================
|
||||
//function : VUserDraw
|
||||
@@ -505,6 +514,107 @@ static int VGlInfo (Draw_Interpretor& theDI,
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
//==============================================================================
|
||||
//function : VShaderProg
|
||||
//purpose : Sets the pair of vertex and fragment shaders for the object
|
||||
//==============================================================================
|
||||
static Standard_Integer VShaderProg (Draw_Interpretor& /*theDI*/,
|
||||
Standard_Integer theArgNb,
|
||||
const char** theArgVec)
|
||||
{
|
||||
Handle(AIS_InteractiveContext) aCtx = ViewerTest::GetAISContext();
|
||||
if (aCtx.IsNull())
|
||||
{
|
||||
std::cerr << "Use 'vinit' command before " << theArgVec[0] << "\n";
|
||||
return 1;
|
||||
}
|
||||
else if (theArgNb < 2)
|
||||
{
|
||||
std::cerr << theArgVec[0] << " syntax error: lack of arguments\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
TCollection_AsciiString aLastArg (theArgVec[theArgNb - 1]);
|
||||
aLastArg.UpperCase();
|
||||
const Standard_Boolean toTurnOff = aLastArg == "OFF";
|
||||
Standard_Integer anArgsNb = theArgNb - 1;
|
||||
Handle(Graphic3d_ShaderProgram) aProgram;
|
||||
if (!toTurnOff
|
||||
&& aLastArg == "PHONG")
|
||||
{
|
||||
aProgram = new Graphic3d_ShaderProgram (Graphic3d_ShaderProgram::ShaderName_Phong);
|
||||
}
|
||||
if (!toTurnOff
|
||||
&& aProgram.IsNull())
|
||||
{
|
||||
if (theArgNb < 3)
|
||||
{
|
||||
std::cerr << theArgVec[0] << " syntax error: lack of arguments\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
const TCollection_AsciiString aSrcVert = theArgVec[theArgNb - 2];
|
||||
const TCollection_AsciiString aSrcFrag = theArgVec[theArgNb - 1];
|
||||
if (!aSrcVert.IsEmpty()
|
||||
&& !OSD_File (aSrcVert).Exists())
|
||||
{
|
||||
std::cerr << "Non-existing vertex shader source\n";
|
||||
return 1;
|
||||
}
|
||||
if (!aSrcFrag.IsEmpty()
|
||||
&& !OSD_File (aSrcFrag).Exists())
|
||||
{
|
||||
std::cerr << "Non-existing fragment shader source\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
aProgram = new Graphic3d_ShaderProgram();
|
||||
aProgram->AttachShader (Graphic3d_ShaderObject::CreateFromFile (Graphic3d_TOS_VERTEX, aSrcVert));
|
||||
aProgram->AttachShader (Graphic3d_ShaderObject::CreateFromFile (Graphic3d_TOS_FRAGMENT, aSrcFrag));
|
||||
anArgsNb = theArgNb - 2;
|
||||
}
|
||||
|
||||
Handle(AIS_InteractiveObject) anIO;
|
||||
if (anArgsNb <= 1
|
||||
|| *theArgVec[1] == '*')
|
||||
{
|
||||
for (ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName anIter (GetMapOfAIS());
|
||||
anIter.More(); anIter.Next())
|
||||
{
|
||||
anIO = Handle(AIS_InteractiveObject)::DownCast (anIter.Key1());
|
||||
if (!anIO.IsNull())
|
||||
{
|
||||
anIO->Attributes()->ShadingAspect()->Aspect()->SetShaderProgram (aProgram);
|
||||
aCtx->Redisplay (anIO, Standard_False);
|
||||
}
|
||||
}
|
||||
aCtx->UpdateCurrentViewer();
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (Standard_Integer anArgIter = 1; anArgIter < anArgsNb; ++anArgIter)
|
||||
{
|
||||
const TCollection_AsciiString aName (theArgVec[anArgIter]);
|
||||
if (!GetMapOfAIS().IsBound2 (aName))
|
||||
{
|
||||
std::cerr << "Warning: " << aName.ToCString() << " is not displayed\n";
|
||||
continue;
|
||||
}
|
||||
anIO = Handle(AIS_InteractiveObject)::DownCast (GetMapOfAIS().Find2 (aName));
|
||||
if (anIO.IsNull())
|
||||
{
|
||||
std::cerr << "Warning: " << aName.ToCString() << " is not an AIS object\n";
|
||||
continue;
|
||||
}
|
||||
anIO->Attributes()->ShadingAspect()->Aspect()->SetShaderProgram (aProgram);
|
||||
aCtx->Redisplay (anIO, Standard_False);
|
||||
}
|
||||
|
||||
aCtx->UpdateCurrentViewer();
|
||||
return 0;
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
//function : OpenGlCommands
|
||||
//purpose :
|
||||
@@ -523,9 +633,14 @@ void ViewerTest::OpenGlCommands(Draw_Interpretor& theCommands)
|
||||
theCommands.Add("vimmediatefront",
|
||||
"vimmediatefront : render immediate mode to front buffer or to back buffer",
|
||||
__FILE__, VImmediateFront, aGroup);
|
||||
|
||||
theCommands.Add("vglinfo",
|
||||
"vglinfo [GL_VENDOR] [GL_RENDERER] [GL_VERSION] [GL_SHADING_LANGUAGE_VERSION] [GL_EXTENSIONS]"
|
||||
" : prints GL info",
|
||||
__FILE__, VGlInfo, aGroup);
|
||||
theCommands.Add("vshaderprog",
|
||||
" 'vshaderprog [name] pathToVertexShader pathToFragmentShader'"
|
||||
"\n\t\t: or 'vshaderprog [name] off' to disable GLSL program"
|
||||
"\n\t\t: or 'vshaderprog [name] phong' to enable per-pixel lighting calculations"
|
||||
"\n\t\t: * might be used to specify all displayed objects",
|
||||
__FILE__, VShaderProg, aGroup);
|
||||
}
|
||||
|
@@ -724,12 +724,12 @@ void ViewerTest::RedrawAllViews()
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
//function : SplitParameter
|
||||
//purpose : Split parameter string to parameter name an patameter value
|
||||
//function : splitParameter
|
||||
//purpose : Split parameter string to parameter name an parameter value
|
||||
//==============================================================================
|
||||
Standard_Boolean SplitParameter (const TCollection_AsciiString& theString,
|
||||
TCollection_AsciiString& theName,
|
||||
TCollection_AsciiString& theValue)
|
||||
Standard_Boolean splitParameter (const TCollection_AsciiString& theString,
|
||||
TCollection_AsciiString& theName,
|
||||
TCollection_AsciiString& theValue)
|
||||
{
|
||||
Standard_Integer aParamNameEnd = theString.FirstLocationInSet("=",1, theString.Length());
|
||||
if (aParamNameEnd == 0)
|
||||
@@ -768,7 +768,7 @@ if (theArgsNb > 9)
|
||||
for (Standard_Integer i = 1; i < theArgsNb; ++i)
|
||||
{
|
||||
TCollection_AsciiString aName = "", aValue = "";
|
||||
if(!SplitParameter (TCollection_AsciiString(theArgVec[i]),aName,aValue) && theArgsNb == 2)
|
||||
if(!splitParameter (TCollection_AsciiString(theArgVec[i]),aName,aValue) && theArgsNb == 2)
|
||||
{
|
||||
// In case of syntax: vinit ViewName
|
||||
aViewName = theArgVec[1];
|
||||
@@ -5276,6 +5276,83 @@ static int VSetTextureMode (Draw_Interpretor& theDi, Standard_Integer theArgsNb,
|
||||
return 0;
|
||||
}
|
||||
|
||||
//===============================================================================================
|
||||
//function : VDefaults
|
||||
//purpose :
|
||||
//===============================================================================================
|
||||
static int VDefaults (Draw_Interpretor& theDi,
|
||||
Standard_Integer theArgsNb,
|
||||
const char** theArgVec)
|
||||
{
|
||||
const Handle(AIS_InteractiveContext)& aCtx = ViewerTest::GetAISContext();
|
||||
if (aCtx.IsNull())
|
||||
{
|
||||
std::cerr << "No active viewer!\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
Handle(Prs3d_Drawer) aDefParams = aCtx->DefaultDrawer();
|
||||
if (theArgsNb < 2)
|
||||
{
|
||||
if (aDefParams->TypeOfDeflection() == Aspect_TOD_RELATIVE)
|
||||
{
|
||||
theDi << "DeflType: relative\n"
|
||||
<< "DeviationCoeff: " << aDefParams->DeviationCoefficient() << "\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
theDi << "DeflType: absolute\n"
|
||||
<< "AbsoluteDeflection: " << aDefParams->MaximalChordialDeviation() << "\n";
|
||||
}
|
||||
theDi << "AngularDeflection: " << (180.0 * aDefParams->HLRAngle() / M_PI) << "\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (Standard_Integer anArgIter = 1; anArgIter < theArgsNb; ++anArgIter)
|
||||
{
|
||||
TCollection_AsciiString anArg (theArgVec[anArgIter]);
|
||||
TCollection_AsciiString aKey, aValue;
|
||||
if (!splitParameter (anArg, aKey, aValue)
|
||||
|| aValue.IsEmpty())
|
||||
{
|
||||
std::cerr << "Error, wrong syntax at: '" << anArg.ToCString() << "'!\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
aKey.UpperCase();
|
||||
if (aKey == "ABSDEFL"
|
||||
|| aKey == "ABSOLUTEDEFLECTION"
|
||||
|| aKey == "DEFL"
|
||||
|| aKey == "DEFLECTION")
|
||||
{
|
||||
aDefParams->SetTypeOfDeflection (Aspect_TOD_ABSOLUTE);
|
||||
aDefParams->SetMaximalChordialDeviation (aValue.RealValue());
|
||||
}
|
||||
else if (aKey == "RELDEFL"
|
||||
|| aKey == "RELATIVEDEFLECTION"
|
||||
|| aKey == "DEVCOEFF"
|
||||
|| aKey == "DEVIATIONCOEFF"
|
||||
|| aKey == "DEVIATIONCOEFFICIENT")
|
||||
{
|
||||
aDefParams->SetTypeOfDeflection (Aspect_TOD_RELATIVE);
|
||||
aDefParams->SetDeviationCoefficient (aValue.RealValue());
|
||||
}
|
||||
else if (aKey == "ANGDEFL"
|
||||
|| aKey == "ANGULARDEFL"
|
||||
|| aKey == "ANGULARDEFLECTION")
|
||||
{
|
||||
// currently HLRDeviationAngle is used instead of DeviationAngle in most places
|
||||
aDefParams->SetHLRAngle (M_PI * aValue.RealValue() / 180.0);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Warning, unknown argument '" << anArg.ToCString() << "'\n";
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
//function : VClInfo
|
||||
//purpose : Prints info about active OpenCL device
|
||||
@@ -5726,6 +5803,9 @@ void ViewerTest::ViewerCommands(Draw_Interpretor& theCommands)
|
||||
" 2 - all textures enabled.\n"
|
||||
" this command sets texture details mode for the specified view.\n"
|
||||
, __FILE__, VSetTextureMode, group);
|
||||
theCommands.Add("vdefaults",
|
||||
"vdefaults [absDefl=value] [devCoeff=value] [angDefl=value]",
|
||||
__FILE__, VDefaults, group);
|
||||
theCommands.Add("vraytrace",
|
||||
"vraytrace 0|1",
|
||||
__FILE__,VRaytrace,group);
|
||||
|
Reference in New Issue
Block a user