ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

shader.h

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifndef GRAPHICS_OPENGL_SHADER_H
00024 #define GRAPHICS_OPENGL_SHADER_H
00025 
00026 #include "common/file.h"
00027 #include "common/array.h"
00028 #include "common/ptr.h"
00029 
00030 #include "math/matrix3.h"
00031 #include "math/matrix4.h"
00032 #include "math/vector2d.h"
00033 #include "math/vector3d.h"
00034 #include "math/vector4d.h"
00035 
00036 #include "graphics/opengl/system_headers.h"
00037 
00038 namespace OpenGL {
00039 
00040 namespace BuiltinShaders {
00041     extern const char *boxVertex, *boxFragment;
00042     extern const char *compatVertex, *compatFragment;
00043     extern const char *controlVertex, *controlFragment;
00044 }
00045 
00046 struct VertexAttrib {
00047     VertexAttrib(uint32 idx, const char *name) :
00048         _enabled(false), _idx(idx), _name(name), _vbo(0), _size(0),
00049         _type(GL_FLOAT), _normalized(false), _stride(0), _offset(0) {}
00050     bool _enabled;
00051     uint32 _idx;
00052     Common::String _name;
00053     GLuint _vbo;
00054     GLint _size;
00055     GLenum _type;
00056     bool _normalized;
00057     GLsizei _stride;
00058     size_t _offset;
00059     float _const[4];
00060 };
00061 
00062 class Shader {
00063     typedef Common::HashMap<Common::String, GLint> UniformsMap;
00064 
00065 public:
00066     ~Shader();
00067     Shader* clone() {
00068         return new Shader(*this);
00069     }
00070 
00071     void use(bool forceReload = false);
00072 
00073     void setUniform(const char *uniform, const Math::Matrix4 &m) {
00074         GLint pos = getUniformLocation(uniform);
00075         if (pos != -1)
00076             glUniformMatrix4fv(pos, 1, GL_FALSE, m.getData());
00077     }
00078 
00079     void setUniform(const char* uniform, const Math::Matrix3 &m) {
00080         GLint pos = getUniformLocation(uniform);
00081         if (pos != -1)
00082             glUniformMatrix3fv(pos, 1, GL_FALSE, m.getData());
00083     }
00084 
00085     void setUniform(const char *uniform, const Math::Vector4d &v) {
00086         GLint pos = getUniformLocation(uniform);
00087         if (pos != -1)
00088             glUniform4fv(pos, 1, v.getData());
00089     }
00090 
00091     void setUniform(const char *uniform, const Math::Vector3d &v) {
00092         GLint pos = getUniformLocation(uniform);
00093         if (pos != -1)
00094             glUniform3fv(pos, 1, v.getData());
00095     }
00096 
00097     void setUniform(const char *uniform, const Math::Vector2d &v) {
00098         GLint pos = getUniformLocation(uniform);
00099         if (pos != -1)
00100             glUniform2fv(pos, 1, v.getData());
00101     }
00102 
00103     void setUniform(const char *uniform, unsigned int x) {
00104         GLint pos = getUniformLocation(uniform);
00105         if (pos != -1)
00106             glUniform1i(pos, x);
00107     }
00108 
00109     // Different name to avoid overload ambiguity
00110     void setUniform1f(const char *uniform, float f) {
00111         GLint pos = getUniformLocation(uniform);
00112         if (pos != -1)
00113             glUniform1f(pos, f);
00114     }
00115 
00116 
00117     GLint getUniformLocation(const char *uniform) const {
00118         UniformsMap::iterator kv = _uniforms->find(uniform);
00119         if (kv == _uniforms->end()) {
00120             GLint ret = glGetUniformLocation(*_shaderNo, uniform);
00121             _uniforms->setVal(uniform, ret);
00122             return ret;
00123         } else {
00124             return kv->_value;
00125         }
00126     }
00127 
00128     void enableVertexAttribute(const char *attrib, GLuint vbo, GLint size, GLenum type, GLboolean normalized, GLsizei stride, uint32 offset);
00129     void disableVertexAttribute(const char *attrib, int size, const float *data);
00130     template <int r>
00131     void disableVertexAttribute(const char *attrib, const Math::Matrix<r,1> &m) {
00132         disableVertexAttribute(attrib, r, m.getData());
00133     }
00134     VertexAttrib & getAttributeAt(uint32 idx);
00135     VertexAttrib & getAttribute(const char *attrib);
00136 
00137     static GLuint createBuffer(GLenum target, GLsizeiptr size, const GLvoid* data, GLenum usage = GL_STATIC_DRAW);
00138     static void freeBuffer(GLuint vbo);
00139 
00140     static Shader* fromFiles(const char *vertex, const char *fragment, const char **attributes);
00141     static Shader* fromFiles(const char *shared, const char **attributes) {
00142         return fromFiles(shared, shared, attributes);
00143     }
00144 
00145     static Shader* fromStrings(const Common::String &name, const char *vertex, const char *fragment, const char **attributes);
00146 
00147     void unbind();
00148 
00149 private:
00150     Shader(const Common::String &name, GLuint vertexShader, GLuint fragmentShader, const char **attributes);
00151 
00152     // Since this class is cloned using the implicit copy constructor,
00153     // a reference counting pointer is used to ensure deletion of the OpenGL
00154     // program upon destruction of the last clone.
00155     Common::SharedPtr<GLuint> _shaderNo;
00156 
00157     Common::String _name;
00158 
00159     Common::Array<VertexAttrib> _attributes;
00160     Common::SharedPtr<UniformsMap> _uniforms;
00161 
00162     static Shader *_previousShader;
00163 };
00164 
00165 } // End of namespace OpenGL
00166 
00167 #endif


Generated on Sat Mar 16 2019 05:01:54 for ResidualVM by doxygen 1.7.1
curved edge   curved edge