Otclient  14/8/2020
shaderprogram.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef SHADERPROGRAM_H
24 #define SHADERPROGRAM_H
25 
26 #include "shader.h"
28 
29 // @bindclass
30 class ShaderProgram : public LuaObject
31 {
32  enum {
33  MAX_UNIFORM_LOCATIONS = 30
34  };
35 
36 public:
37  ShaderProgram();
39 
40  bool addShader(const ShaderPtr& shader);
41  bool addShaderFromSourceCode(Shader::ShaderType shaderType, const std::string& sourceCode);
42  bool addShaderFromSourceFile(Shader::ShaderType shaderType, const std::string& sourceFile);
43  void removeShader(const ShaderPtr& shader);
44  void removeAllShaders();
45  virtual bool link();
46  bool bind();
47  static void release();
48  std::string log();
49 
50  static void disableAttributeArray(int location) { glDisableVertexAttribArray(location); }
51  static void enableAttributeArray(int location) { glEnableVertexAttribArray(location); }
52  void disableAttributeArray(const char *name) { glDisableVertexAttribArray(getAttributeLocation(name)); }
53  void enableAttributeArray(const char *name) { glEnableVertexAttribArray(getAttributeLocation(name)); }
54 
55  int getAttributeLocation(const char *name);
56  void bindAttributeLocation(int location, const char *name);
57  void bindUniformLocation(int location, const char *name);
58 
59  void setAttributeArray(int location, const float *values, int size, int stride = 0) { glVertexAttribPointer(location, size, GL_FLOAT, GL_FALSE, stride, values); }
60  void setAttributeValue(int location, float value) { glVertexAttrib1f(location, value); }
61  void setAttributeValue(int location, float x, float y) { glVertexAttrib2f(location, x, y); }
62  void setAttributeValue(int location, float x, float y, float z) { glVertexAttrib3f(location, x, y, z); }
63  void setAttributeArray(const char *name, const float *values, int size, int stride = 0) { glVertexAttribPointer(getAttributeLocation(name), size, GL_FLOAT, GL_FALSE, stride, values); }
64  void setAttributeValue(const char *name, float value) { glVertexAttrib1f(getAttributeLocation(name), value); }
65  void setAttributeValue(const char *name, float x, float y) { glVertexAttrib2f(getAttributeLocation(name), x, y); }
66  void setAttributeValue(const char *name, float x, float y, float z) { glVertexAttrib3f(getAttributeLocation(name), x, y, z); }
67 
68  void setUniformValue(int location, const Color& color) { glUniform4f(m_uniformLocations[location], color.rF(), color.gF(), color.bF(), color.aF()); }
69  void setUniformValue(int location, int value) { glUniform1i(m_uniformLocations[location], value); }
70  void setUniformValue(int location, float value) { glUniform1f(m_uniformLocations[location], value); }
71  void setUniformValue(int location, float x, float y) { glUniform2f(m_uniformLocations[location], x, y); }
72  void setUniformValue(int location, float x, float y, float z) { glUniform3f(m_uniformLocations[location], x, y, z); }
73  void setUniformValue(int location, float x, float y, float z, float w) { glUniform4f(m_uniformLocations[location], x, y, z, w); }
74  void setUniformValue(int location, const Matrix2& mat) { glUniformMatrix2fv(m_uniformLocations[location], 1, GL_FALSE, mat.data()); }
75  void setUniformValue(int location, const Matrix3& mat) { glUniformMatrix3fv(m_uniformLocations[location], 1, GL_FALSE, mat.data()); }
76  void setUniformValue(const char *name, const Color& color) { glUniform4f(glGetUniformLocation(m_programId, name), color.rF(), color.gF(), color.bF(), color.aF()); }
77  void setUniformValue(const char *name, int value) { glUniform1i(glGetUniformLocation(m_programId, name), value); }
78  void setUniformValue(const char *name, float value) { glUniform1f(glGetUniformLocation(m_programId, name), value); }
79  void setUniformValue(const char *name, float x, float y) { glUniform2f(glGetUniformLocation(m_programId, name), x, y); }
80  void setUniformValue(const char *name, float x, float y, float z) { glUniform3f(glGetUniformLocation(m_programId, name), x, y, z); }
81  void setUniformValue(const char *name, float x, float y, float z, float w) { glUniform4f(glGetUniformLocation(m_programId, name), x, y, z, w); }
82  void setUniformValue(const char *name, const Matrix2& mat) { glUniformMatrix2fv(glGetUniformLocation(m_programId, name), 1, GL_FALSE, mat.data()); }
83  void setUniformValue(const char *name, const Matrix3& mat) { glUniformMatrix3fv(glGetUniformLocation(m_programId, name), 1, GL_FALSE, mat.data()); }
84  // TODO: Point, PointF, Color, Size, SizeF ?
85 
86  bool isLinked() { return m_linked; }
87  uint getProgramId() { return m_programId; }
88  ShaderList getShaders() { return m_shaders; }
89 
90 private:
91  bool m_linked;
92  uint m_programId;
93  static uint m_currentProgram;
94  ShaderList m_shaders;
95  std::array<int, MAX_UNIFORM_LOCATIONS> m_uniformLocations;
96 };
97 
98 #endif
ShaderProgram::~ShaderProgram
~ShaderProgram()
Definition: shaderprogram.cpp:39
ShaderProgram::setUniformValue
void setUniformValue(const char *name, float x, float y, float z)
Definition: shaderprogram.h:80
ShaderProgram::setAttributeValue
void setAttributeValue(const char *name, float x, float y, float z)
Definition: shaderprogram.h:66
ShaderProgram::setUniformValue
void setUniformValue(const char *name, float value)
Definition: shaderprogram.h:78
ShaderList
std::vector< ShaderPtr > ShaderList
Definition: declarations.h:64
Color
Definition: color.h:32
ShaderProgram::setAttributeValue
void setAttributeValue(const char *name, float x, float y)
Definition: shaderprogram.h:65
ShaderProgram::setUniformValue
void setUniformValue(int location, float value)
Definition: shaderprogram.h:70
z
gc sort z
Definition: CMakeLists.txt:176
ShaderProgram::disableAttributeArray
void disableAttributeArray(const char *name)
Definition: shaderprogram.h:52
ShaderProgram::setUniformValue
void setUniformValue(int location, float x, float y)
Definition: shaderprogram.h:71
Color::rF
float rF() const
Definition: color.h:52
ShaderProgram::addShaderFromSourceFile
bool addShaderFromSourceFile(Shader::ShaderType shaderType, const std::string &sourceFile)
Definition: shaderprogram.cpp:64
Color::gF
float gF() const
Definition: color.h:51
ShaderProgram::release
static void release()
Definition: shaderprogram.cpp:117
ShaderProgram::enableAttributeArray
void enableAttributeArray(const char *name)
Definition: shaderprogram.h:53
ShaderProgram::addShader
bool addShader(const ShaderPtr &shader)
Definition: shaderprogram.cpp:48
ShaderProgram::setUniformValue
void setUniformValue(int location, float x, float y, float z, float w)
Definition: shaderprogram.h:73
Shader::ShaderType
ShaderType
Definition: shader.h:31
ShaderProgram::setUniformValue
void setUniformValue(int location, const Color &color)
Definition: shaderprogram.h:68
ShaderProgram::setUniformValue
void setUniformValue(int location, float x, float y, float z)
Definition: shaderprogram.h:72
ShaderProgram::disableAttributeArray
static void disableAttributeArray(int location)
Definition: shaderprogram.h:50
luaobject.h
ShaderProgram::link
virtual bool link()
Definition: shaderprogram.cpp:90
ShaderProgram::setAttributeValue
void setAttributeValue(int location, float value)
Definition: shaderprogram.h:60
uint
unsigned int uint
Definition: types.h:31
ShaderProgram::setUniformValue
void setUniformValue(const char *name, const Color &color)
Definition: shaderprogram.h:76
ShaderProgram::setAttributeValue
void setAttributeValue(int location, float x, float y, float z)
Definition: shaderprogram.h:62
ShaderProgram::setUniformValue
void setUniformValue(const char *name, int value)
Definition: shaderprogram.h:77
ShaderProgram::bind
bool bind()
Definition: shaderprogram.cpp:106
ShaderProgram::setUniformValue
void setUniformValue(const char *name, float x, float y)
Definition: shaderprogram.h:79
ShaderProgram::setUniformValue
void setUniformValue(const char *name, float x, float y, float z, float w)
Definition: shaderprogram.h:81
ShaderProgram::removeAllShaders
void removeAllShaders()
Definition: shaderprogram.cpp:84
ShaderProgram::setUniformValue
void setUniformValue(int location, const Matrix2 &mat)
Definition: shaderprogram.h:74
Color::aF
float aF() const
Definition: color.h:49
ShaderProgram::setAttributeValue
void setAttributeValue(const char *name, float value)
Definition: shaderprogram.h:64
ShaderProgram::setUniformValue
void setUniformValue(const char *name, const Matrix2 &mat)
Definition: shaderprogram.h:82
ShaderProgram::bindAttributeLocation
void bindAttributeLocation(int location, const char *name)
Definition: shaderprogram.cpp:143
Color::bF
float bF() const
Definition: color.h:50
ShaderProgram::log
std::string log()
Definition: shaderprogram.cpp:125
ShaderProgram::setAttributeArray
void setAttributeArray(const char *name, const float *values, int size, int stride=0)
Definition: shaderprogram.h:63
ShaderProgram::setAttributeValue
void setAttributeValue(int location, float x, float y)
Definition: shaderprogram.h:61
stdext::shared_object_ptr
Definition: shared_object.h:39
ShaderProgram::enableAttributeArray
static void enableAttributeArray(int location)
Definition: shaderprogram.h:51
shader.h
ShaderProgram::getShaders
ShaderList getShaders()
Definition: shaderprogram.h:88
ShaderProgram::setUniformValue
void setUniformValue(const char *name, const Matrix3 &mat)
Definition: shaderprogram.h:83
ShaderProgram::bindUniformLocation
void bindUniformLocation(int location, const char *name)
Definition: shaderprogram.cpp:148
ShaderProgram::setUniformValue
void setUniformValue(int location, int value)
Definition: shaderprogram.h:69
ShaderProgram::isLinked
bool isLinked()
Definition: shaderprogram.h:86
ShaderProgram::getProgramId
uint getProgramId()
Definition: shaderprogram.h:87
Matrix
Definition: matrix.h:32
ShaderProgram::setUniformValue
void setUniformValue(int location, const Matrix3 &mat)
Definition: shaderprogram.h:75
ShaderProgram::getAttributeLocation
int getAttributeLocation(const char *name)
Definition: shaderprogram.cpp:138
LuaObject
LuaObject, all script-able classes have it as base.
Definition: luaobject.h:30
ShaderProgram::setAttributeArray
void setAttributeArray(int location, const float *values, int size, int stride=0)
Definition: shaderprogram.h:59
ShaderProgram::addShaderFromSourceCode
bool addShaderFromSourceCode(Shader::ShaderType shaderType, const std::string &sourceCode)
Definition: shaderprogram.cpp:55
Matrix::data
T * data()
Definition: matrix.h:50
ShaderProgram
Definition: shaderprogram.h:30
ShaderProgram::removeShader
void removeShader(const ShaderPtr &shader)
Definition: shaderprogram.cpp:73
ShaderProgram::ShaderProgram
ShaderProgram()
Definition: shaderprogram.cpp:30