1 // Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details 2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt 3 4 #include "Uniform.h" 5 #include "TextureGL.h" 6 7 namespace Graphics { 8 namespace OGL { 9 Uniform()10 Uniform::Uniform() : 11 m_location(-1) 12 { 13 } 14 Init(const char * name,GLuint program)15 void Uniform::Init(const char *name, GLuint program) 16 { 17 m_location = glGetUniformLocation(program, name); 18 } 19 Set(int i)20 void Uniform::Set(int i) 21 { 22 if (m_location != -1) 23 glUniform1i(m_location, i); 24 } 25 Set(float f)26 void Uniform::Set(float f) 27 { 28 if (m_location != -1) 29 glUniform1f(m_location, f); 30 } 31 Set(const vector3f & v)32 void Uniform::Set(const vector3f &v) 33 { 34 if (m_location != -1) 35 glUniform3f(m_location, v.x, v.y, v.z); 36 } 37 Set(const vector3d & v)38 void Uniform::Set(const vector3d &v) 39 { 40 if (m_location != -1) 41 glUniform3f(m_location, static_cast<float>(v.x), static_cast<float>(v.y), static_cast<float>(v.z)); //yes, 3f 42 } 43 Set(const Color & c)44 void Uniform::Set(const Color &c) 45 { 46 Color4f c4f = c.ToColor4f(); 47 if (m_location != -1) 48 glUniform4f(m_location, c4f.r, c4f.g, c4f.b, c4f.a); 49 } 50 Set(const int v[3])51 void Uniform::Set(const int v[3]) 52 { 53 if (m_location != -1) 54 glUniform3i(m_location, v[0], v[1], v[2]); 55 } 56 Set(const float x,const float y,const float z,const float w)57 void Uniform::Set(const float x, const float y, const float z, const float w) 58 { 59 if (m_location != -1) 60 glUniform4f(m_location, x, y, z, w); 61 } 62 Set(const float m[9])63 void Uniform::Set(const float m[9]) 64 { 65 if (m_location != -1) 66 glUniformMatrix3fv(m_location, 1, GL_FALSE, m); 67 } 68 Set(const matrix3x3f & m)69 void Uniform::Set(const matrix3x3f &m) 70 { 71 if (m_location != -1) 72 glUniformMatrix3fv(m_location, 1, GL_FALSE, &m[0]); 73 } 74 Set(const matrix4x4f & m)75 void Uniform::Set(const matrix4x4f &m) 76 { 77 if (m_location != -1) 78 glUniformMatrix4fv(m_location, 1, GL_FALSE, &m[0]); 79 } 80 Set(Texture * tex,unsigned int unit)81 void Uniform::Set(Texture *tex, unsigned int unit) 82 { 83 if (m_location != -1 && tex) { 84 glActiveTexture(GL_TEXTURE0 + unit); 85 static_cast<TextureGL *>(tex)->Bind(); 86 glUniform1i(m_location, unit); 87 } 88 } 89 90 } // namespace OGL 91 } // namespace Graphics 92