0027105: Make code ISO-compliant [-Wpedantic fixes]
[occt.git] / src / OpenGl / OpenGl_AspectFace.cxx
index 41f72e2..bd45199 100644 (file)
@@ -1,55 +1,54 @@
 // Created on: 2011-07-13
 // Created by: Sergey ZERCHANINOV
-// Copyright (c) 2011-2012 OPEN CASCADE SAS
+// Copyright (c) 2011-2014 OPEN CASCADE SAS
 //
-// The content of this file is subject to the Open CASCADE Technology Public
-// License Version 6.5 (the "License"). You may not use the content of this file
-// except in compliance with the License. Please obtain a copy of the License
-// at http://www.opencascade.org and read it completely before using this file.
+// This file is part of Open CASCADE Technology software library.
 //
-// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
-// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
 //
-// The Original Code and all software distributed under the License is
-// distributed on an "AS IS" basis, without warranty of any kind, and the
-// Initial Developer hereby disclaims all such warranties, including without
-// limitation, any warranties of merchantability, fitness for a particular
-// purpose or non-infringement. Please see the License for the specific terms
-// and conditions governing the rights and limitations under the License.
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
+
+#include <Aspect_PolygonOffsetMode.hxx>
+#include <NCollection_Vec3.hxx>
 
 #include <OpenGl_AspectFace.hxx>
+#include <OpenGl_Context.hxx>
+#include <OpenGl_ShaderManager.hxx>
+#include <OpenGl_ShaderProgram.hxx>
 #include <OpenGl_Texture.hxx>
 #include <OpenGl_Workspace.hxx>
-#include <OpenGl_Context.hxx>
 
-#include <Aspect_PolygonOffsetMode.hxx>
-#include <Graphic3d_CGroup.hxx>
+#include <Graphic3d_ShaderProgram.hxx>
 #include <Graphic3d_TextureMap.hxx>
+#include <Graphic3d_TextureParams.hxx>
 #include <Graphic3d_TypeOfReflection.hxx>
 #include <Graphic3d_MaterialAspect.hxx>
 
-#include <NCollection_Vec3.hxx>
+#include <Image_PixMap.hxx>
 
 namespace
 {
-
   static OPENGL_SURF_PROP THE_DEFAULT_MATERIAL =
   {
     0.2F,  0.8F, 0.1F, 0.0F, // amb, diff, spec, emsv
-    1.0F, 10.0F, 0.0F,       // trans, shine, env_reflexion
+    1.0F, 10.0F, 1.0F, 0.0F, // trans, shine, index, env_reflexion
     0, // isphysic
     (OPENGL_AMBIENT_MASK | OPENGL_DIFFUSE_MASK | OPENGL_SPECULAR_MASK), // color_mask
     {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // ambient color
     {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // diffuse color
     {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // specular color
     {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // emissive color
-    {{ 1.0F, 1.0F, 1.0F, 1.0F }}  // material color
+    {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // material color
+    Graphic3d_BSDF()
   };
 
-  static TEL_POFFSET_PARAM THE_DEFAULT_POFFSET = { Aspect_POM_Fill, 1.0F, 0.0F };
   static const TCollection_AsciiString THE_EMPTY_KEY;
-
-};
+}
 
 // =======================================================================
 // function : convertMaterial
@@ -115,6 +114,10 @@ void OpenGl_AspectFace::convertMaterial (const CALL_DEF_MATERIAL& theMat,
   // trans = 1. => transparent
   // in OpenGl it is opposite.
   theSurf.trans = 1.0f - theMat.Transparency;
+  theSurf.index = theMat.RefractionIndex;
+
+  // material BSDF (for physically-based rendering)
+  theSurf.BSDF = theMat.BSDF;
 }
 
 // =======================================================================
@@ -130,11 +133,7 @@ OpenGl_AspectFace::OpenGl_AspectFace()
   myIntFront (THE_DEFAULT_MATERIAL),
   myIntBack (THE_DEFAULT_MATERIAL),
   myPolygonOffset (THE_DEFAULT_POFFSET),
-  myDoTextureMap (false),
-  myTextureMap(),
-  myIsTextureInit (Standard_False),
-  myTextureRes(),
-  myTextureId()
+  myDoTextureMap (false)
 {}
 
 // =======================================================================
@@ -208,12 +207,6 @@ void OpenGl_AspectFace::SetAspect (const CALL_DEF_CONTEXTFILLAREA& theAspect)
   myIntBack.matcol.rgb[2] = (float )theAspect.BackIntColor.b;
   myIntBack.matcol.rgb[3] = 1.0f;
 
-  myDoTextureMap  = (theAspect.Texture.doTextureMap != 0);
-  myTextureMap    = theAspect.Texture.TextureMap;
-
-  const TCollection_AsciiString& aNewKey = myTextureMap.IsNull() ? THE_EMPTY_KEY : myTextureMap->GetId();
-  myIsTextureInit = (!aNewKey.IsEmpty() && myTextureId == aNewKey);
-
   //TelPolygonOffset
   myPolygonOffset.mode   = (Aspect_PolygonOffsetMode )theAspect.PolygonOffsetMode;
   myPolygonOffset.factor = theAspect.PolygonOffsetFactor;
@@ -226,6 +219,26 @@ void OpenGl_AspectFace::SetAspect (const CALL_DEF_CONTEXTFILLAREA& theAspect)
   anEdgeAspect.LineType = (Aspect_TypeOfLine )theAspect.LineType;
   anEdgeAspect.Width    = (float )theAspect.Width;
   myAspectEdge.SetAspect (anEdgeAspect);
+
+  myDoTextureMap = (theAspect.Texture.doTextureMap != 0);
+
+  // update texture binding
+  myTexture = theAspect.Texture.TextureMap;
+
+  const TCollection_AsciiString& aTextureKey = myTexture.IsNull() ? THE_EMPTY_KEY : myTexture->GetId();
+  if (aTextureKey.IsEmpty() || myResources.TextureId != aTextureKey)
+  {
+    myResources.ResetTextureReadiness();
+  }
+
+  // update shader program binding
+  myShaderProgram = theAspect.ShaderProgram;
+
+  const TCollection_AsciiString& aShaderKey = myShaderProgram.IsNull() ? THE_EMPTY_KEY : myShaderProgram->GetId();
+  if (aShaderKey.IsEmpty() || myResources.ShaderProgramId != aShaderKey)
+  {
+    myResources.ResetShaderReadiness();
+  }
 }
 
 // =======================================================================
@@ -363,6 +376,8 @@ void OpenGl_AspectFace::SetAspect (const Handle(Graphic3d_AspectFillArea3d)& the
   aFaceContext.PolygonOffsetFactor = (Standard_ShortReal)aPolyFactor;
   aFaceContext.PolygonOffsetUnits  = (Standard_ShortReal)aPolyUnits;
 
+  aFaceContext.ShaderProgram = theAspect->ShaderProgram();
+
   SetAspect (aFaceContext);
 }
 
@@ -379,68 +394,102 @@ void OpenGl_AspectFace::Render (const Handle(OpenGl_Workspace)& theWorkspace) co
 // function : Release
 // purpose  :
 // =======================================================================
-void OpenGl_AspectFace::Release (const Handle(OpenGl_Context)& theContext)
+void OpenGl_AspectFace::Release (OpenGl_Context* theContext)
 {
-  if (!myTextureRes.IsNull())
+  if (!myResources.Texture.IsNull())
   {
-    if (!theContext.IsNull())
+    if (theContext)
     {
-      if (myTextureId.IsEmpty())
+      if (myResources.TextureId.IsEmpty())
       {
-        theContext->DelayedRelease (myTextureRes);
+        theContext->DelayedRelease (myResources.Texture);
       }
       else
       {
-        myTextureRes.Nullify(); // we need nullify all handles before ReleaseResource() call
-        theContext->ReleaseResource (myTextureId);
+        myResources.Texture.Nullify(); // we need nullify all handles before ReleaseResource() call
+        theContext->ReleaseResource (myResources.TextureId, Standard_True);
       }
     }
-    myTextureRes.Nullify();
+    myResources.Texture.Nullify();
+  }
+  myResources.TextureId.Clear();
+  myResources.ResetTextureReadiness();
+
+  if (!myResources.ShaderProgram.IsNull()
+   && theContext)
+  {
+    theContext->ShaderManager()->Unregister (myResources.ShaderProgramId,
+                                             myResources.ShaderProgram);
   }
-  myTextureId.Clear();
+  myResources.ShaderProgramId.Clear();
+  myResources.ResetShaderReadiness();
 }
 
 // =======================================================================
-// function : buildTexure
+// function : BuildTexture
 // purpose  :
 // =======================================================================
-void OpenGl_AspectFace::buildTexture (const Handle(OpenGl_Workspace)& theWorkspace) const
+void OpenGl_AspectFace::Resources::BuildTexture (const Handle(OpenGl_Context)&       theCtx,
+                                                 const Handle(Graphic3d_TextureMap)& theTexture)
 {
-  const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
+  // release old texture resource
+  if (!Texture.IsNull())
+  {
+    if (TextureId.IsEmpty())
+    {
+      theCtx->DelayedRelease (Texture);
+      Texture.Nullify();
+    }
+    else
+    {
+      Texture.Nullify(); // we need nullify all handles before ReleaseResource() call
+      theCtx->ReleaseResource (TextureId, Standard_True);
+    }
+  }
+
+  TextureId = theTexture.IsNull() ? THE_EMPTY_KEY : theTexture->GetId();
 
-  const TCollection_AsciiString& aNewKey = myTextureMap.IsNull() ? THE_EMPTY_KEY : myTextureMap->GetId();
-  if (aNewKey.IsEmpty() || myTextureId != aNewKey)
+  if (!theTexture.IsNull())
   {
-    if (!myTextureRes.IsNull())
+    if (TextureId.IsEmpty() || !theCtx->GetResource<Handle(OpenGl_Texture)> (TextureId, Texture))
     {
-      if (myTextureId.IsEmpty())
+      Texture = new OpenGl_Texture (theTexture->GetParams());
+      Handle(Image_PixMap) anImage = theTexture->GetImage();
+      if (!anImage.IsNull())
       {
-        aContext->DelayedRelease (myTextureRes);
-        myTextureRes.Nullify();
+        Texture->Init (theCtx, *anImage.operator->(), theTexture->Type());
       }
-      else
+      if (!TextureId.IsEmpty())
       {
-        myTextureRes.Nullify(); // we need nullify all handles before ReleaseResource() call
-        aContext->ReleaseResource (myTextureId);
+        theCtx->ShareResource (TextureId, Texture);
       }
     }
-    myTextureId = aNewKey;
+  }
+}
 
-    if (!myTextureMap.IsNull())
-    {
-      if (aNewKey.IsEmpty() || !aContext->GetResource<Handle(OpenGl_Texture)> (aNewKey, myTextureRes))
-      {
-        myTextureRes = new OpenGl_Texture (myTextureMap->GetParams());
-        Handle(Image_PixMap) anImage = myTextureMap->GetImage();
-        if (!anImage.IsNull())
-        {
-          myTextureRes->Init (aContext, *anImage.operator->(), myTextureMap->Type());
-        }
-        if (!aNewKey.IsEmpty())
-        {
-          aContext->ShareResource (aNewKey, myTextureRes);
-        }
-      }
-    }
+// =======================================================================
+// function : BuildShader
+// purpose  :
+// =======================================================================
+void OpenGl_AspectFace::Resources::BuildShader (const Handle(OpenGl_Context)&          theCtx,
+                                                const Handle(Graphic3d_ShaderProgram)& theShader)
+{
+  if (theCtx->core20fwd == NULL)
+  {
+    return;
   }
+
+  // release old shader program resources
+  if (!ShaderProgram.IsNull())
+  {
+    theCtx->ShaderManager()->Unregister (ShaderProgramId, ShaderProgram);
+    ShaderProgramId.Clear();
+    ShaderProgram.Nullify();
+  }
+  if (theShader.IsNull())
+  {
+    return;
+  }
+
+  theCtx->ShaderManager()->Create (theShader, ShaderProgramId, ShaderProgram);
 }