0030700: Visualization, TKOpenGl - support PBR Metallic-Roughness shading model
[occt.git] / src / Graphic3d / Graphic3d_ShaderObject.cxx
1 // Created on: 2013-09-20
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <OSD_File.hxx>
17 #include <OSD_Protection.hxx>
18 #include <Standard_Atomic.hxx>
19 #include <Graphic3d_ShaderObject.hxx>
20 #include <Graphic3d_GraphicDriver.hxx>
21
22 IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_ShaderObject,Standard_Transient)
23
24 namespace
25 {
26   static volatile Standard_Integer THE_SHADER_OBJECT_COUNTER = 0;
27 }
28
29
30 // =======================================================================
31 // function : Graphic3d_ShaderObject
32 // purpose  : Creates a shader object from specified file
33 // =======================================================================
34 Graphic3d_ShaderObject::Graphic3d_ShaderObject (const Graphic3d_TypeOfShaderObject theType)
35 : myType (theType)
36 {
37   myID = TCollection_AsciiString ("Graphic3d_ShaderObject_")
38        + TCollection_AsciiString (Standard_Atomic_Increment (&THE_SHADER_OBJECT_COUNTER));
39 }
40
41 // =======================================================================
42 // function : CreatFromFile
43 // purpose  : Creates new shader object from specified file
44 // =======================================================================
45 Handle(Graphic3d_ShaderObject) Graphic3d_ShaderObject::CreateFromFile (const Graphic3d_TypeOfShaderObject theType,
46                                                                        const TCollection_AsciiString&     thePath)
47 {
48   Handle(Graphic3d_ShaderObject) aShader = new Graphic3d_ShaderObject (theType);
49   aShader->myPath = thePath;
50
51   OSD_File aFile (thePath);
52   if (!aFile.Exists())
53   {
54     return NULL;
55   }
56
57   aFile.Open (OSD_ReadOnly, OSD_Protection());
58   aFile.Read (aShader->mySource, (int)aFile.Size());
59   aFile.Close();
60
61   return aShader;
62 }
63
64 // =======================================================================
65 // function : CreatFromSource
66 // purpose  : Creates new shader object from specified source
67 // =======================================================================
68 Handle(Graphic3d_ShaderObject) Graphic3d_ShaderObject::CreateFromSource (const Graphic3d_TypeOfShaderObject theType,
69                                                                          const TCollection_AsciiString&     theSource)
70 {
71   Handle(Graphic3d_ShaderObject) aShader = new Graphic3d_ShaderObject (theType);
72   aShader->mySource = theSource;
73   return aShader;
74 }
75
76 // =======================================================================
77 // function : ~Graphic3d_ShaderObject
78 // purpose  : Releases resources of shader object
79 // =======================================================================
80 Graphic3d_ShaderObject::~Graphic3d_ShaderObject()
81 {
82   //
83 }
84
85 // =======================================================================
86 // function : IsDone
87 // purpose  : Checks if the shader object is valid or not
88 // =======================================================================
89 Standard_Boolean Graphic3d_ShaderObject::IsDone() const
90 {
91   return !mySource.IsEmpty();
92 }