NGL  6.5
The NCCA Graphics Library
Material.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Jon Macey
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include <fstream>
18 #include "NGLStream.h"
19 #include "Material.h"
20 #include "ShaderLib.h"
21 //----------------------------------------------------------------------------------------------------------------------
24 //----------------------------------------------------------------------------------------------------------------------
25 
26 namespace ngl
27 {
28 
29 //----------------------------------------------------------------------------------------------------------------------
30 void Material :: setDefault() noexcept
31 {
32  m_transparency = 0.0f;
33  m_specularExponent = 1.0f;
34  m_surfaceRoughness = 0.0f;
35  m_ambient.set(0.1f,0.1f,0.1f);
36  m_diffuse.set(0.8f,0.8f,0.8f);
37  m_specular.set(0,0,0);
38 }
39 //----------------------------------------------------------------------------------------------------------------------
40 Material::Material(STDMAT _mat) noexcept
41 {
42  m_specularExponent = s_materials[static_cast<int>(_mat)][9];
43  m_ambient.set(s_materials[static_cast<int>(_mat)][0],s_materials[static_cast<int>(_mat)][1],s_materials[static_cast<int>(_mat)][2]);
44  m_diffuse.set(s_materials[static_cast<int>(_mat)][3],s_materials[static_cast<int>(_mat)][4],s_materials[static_cast<int>(_mat)][5]);
45  m_specular.set(s_materials[static_cast<int>(_mat)][6],s_materials[static_cast<int>(_mat)][7],s_materials[static_cast<int>(_mat)][8]);
46  m_specularExponent = s_materials[static_cast<int>(_mat)][9];
47  m_surfaceRoughness = 0.0;
48 
49 }
50 
51 //----------------------------------------------------------------------------------------------------------------------
52 void Material :: change( STDMAT _mat) noexcept
53 {
54  m_specularExponent = s_materials[static_cast<int>(_mat)][9];
55  m_ambient.set(s_materials[static_cast<int>(_mat)][0],s_materials[static_cast<int>(_mat)][1],s_materials[static_cast<int>(_mat)][2]);
56  m_diffuse.set(s_materials[static_cast<int>(_mat)][3],s_materials[static_cast<int>(_mat)][4],s_materials[static_cast<int>(_mat)][5]);
57  m_specular.set(s_materials[static_cast<int>(_mat)][6],s_materials[static_cast<int>(_mat)][7],s_materials[static_cast<int>(_mat)][8]);
58 }
59 
60 //----------------------------------------------------------------------------------------------------------------------
61 Material::Material(int _mat) noexcept
62 {
63  m_specularExponent = s_materials[_mat][9];
64  m_ambient.set(s_materials[_mat][0],s_materials[_mat][1],s_materials[_mat][2]);
65  m_diffuse.set(s_materials[_mat][3],s_materials[_mat][4],s_materials[_mat][5]);
66  m_specular.set(s_materials[_mat][6],s_materials[_mat][7],s_materials[_mat][8]);
67  m_surfaceRoughness = 0.0;
68 
69 }
70 
71 //----------------------------------------------------------------------------------------------------------------------
72 void Material :: set( const Material &_m) noexcept
73 {
74  m_transparency = _m.m_transparency;
75  m_specularExponent = _m.m_specularExponent;
76  m_surfaceRoughness = _m.m_surfaceRoughness;
77  m_ambient.set(_m.m_ambient);
78  m_diffuse.set(_m.m_diffuse);
79  m_specular.set(_m.m_specular);
80 }
81 
82 
83 
84 //----------------------------------------------------------------------------------------------------------------------
85 Material::Material( const std::string &_fName) noexcept
86 {
87  std::ifstream FileIn;
88  FileIn.open(_fName.c_str(),std::ios::in);
89 
90  if (!FileIn.is_open())
91  {
92  std::cout << "File : "<< _fName <<" Not found Exiting "<<std::endl;
93  exit(EXIT_FAILURE);
94  }
95 
96  FileIn>>m_ambient;
97  FileIn>>m_diffuse;
98  FileIn>>m_specular;
99 
100  FileIn.close();
101 }
102 
103 //----------------------------------------------------------------------------------------------------------------------
104 void Material::load(const std::string &_fname) noexcept
105 {
107  std::cout<<"Load of "<<_fname <<" not yet implemented\n";
108 }
109 
110 
111 void Material::loadToShader( std::string _uniformName )const noexcept
112 {
113 
114  ShaderLib *shader=ShaderLib::instance();
115  /*
116  so if we use one of our standard shaders for NGL we can load these values
124  */
125 
126 
127  shader->setShaderParam4f(_uniformName+".ambient",m_ambient.m_r,m_ambient.m_g,m_ambient.m_b,m_ambient.m_a);
128  shader->setShaderParam4f(_uniformName+".diffuse",m_diffuse.m_r,m_diffuse.m_g,m_diffuse.m_b,m_diffuse.m_a);
129  shader->setShaderParam4f(_uniformName+".specular",m_specular.m_r,m_specular.m_g,m_specular.m_b,m_specular.m_a);
130  shader->setShaderParam1f(_uniformName+".shininess", m_specularExponent);
131 
132  // std::cout<<"block id = "<<blockID<<"\n";
133 
134 }
135 
136 
137 } // end ngl namespace
138 
139 
140 
main shader loader / manager class for GLSL shaders
implementation files for RibExport class
Definition: AABB.cpp:22
GLuint in
Definition: glew.h:11550
GLuint shader
Definition: glew.h:1816
stream helpers for ngl data types
static ShaderLib * instance()
Get the instance.
GLsizei const GLchar *const * string
Definition: glew.h:1847
GLclampf f
Definition: glew.h:3511