tibia-client/src/framework/graphics/shaderprogram.h

99 lines
5.8 KiB
C
Raw Normal View History

2011-12-07 01:31:55 +01:00
/*
2012-01-02 17:58:37 +01:00
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
2011-12-07 01:31:55 +01:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef SHADERPROGRAM_H
#define SHADERPROGRAM_H
#include "shader.h"
#include <framework/luaengine/luaobject.h>
2011-12-07 01:31:55 +01:00
2012-06-22 05:14:13 +02:00
// @bindclass
2012-06-14 20:26:55 +02:00
class ShaderProgram : public LuaObject
2011-12-07 01:31:55 +01:00
{
enum {
2011-12-08 18:28:29 +01:00
MAX_UNIFORM_LOCATIONS = 30
2011-12-07 01:31:55 +01:00
};
2012-06-14 20:26:55 +02:00
2011-12-07 01:31:55 +01:00
public:
ShaderProgram();
~ShaderProgram();
bool addShader(const ShaderPtr& shader);
bool addShaderFromSourceCode(Shader::ShaderType shaderType, const std::string& sourceCode);
bool addShaderFromSourceFile(Shader::ShaderType shaderType, const std::string& sourceFile);
void removeShader(const ShaderPtr& shader);
void removeAllShaders();
2011-12-08 00:43:12 +01:00
virtual bool link();
2011-12-07 01:31:55 +01:00
bool bind();
2012-04-08 21:28:03 +02:00
static void release();
2011-12-07 01:31:55 +01:00
std::string log();
static void disableAttributeArray(int location) { glDisableVertexAttribArray(location); }
static void enableAttributeArray(int location) { glEnableVertexAttribArray(location); }
2011-12-07 01:31:55 +01:00
void disableAttributeArray(const char *name) { glDisableVertexAttribArray(getAttributeLocation(name)); }
void enableAttributeArray(const char *name) { glEnableVertexAttribArray(getAttributeLocation(name)); }
int getAttributeLocation(const char *name);
void bindAttributeLocation(int location, const char *name);
void bindUniformLocation(int location, const char *name);
2011-12-25 00:14:12 +01:00
void setAttributeArray(int location, const float *values, int size, int stride = 0) { glVertexAttribPointer(location, size, GL_FLOAT, GL_FALSE, stride, values); }
void setAttributeValue(int location, float value) { glVertexAttrib1f(location, value); }
void setAttributeValue(int location, float x, float y) { glVertexAttrib2f(location, x, y); }
void setAttributeValue(int location, float x, float y, float z) { glVertexAttrib3f(location, x, y, z); }
void setAttributeArray(const char *name, const float *values, int size, int stride = 0) { glVertexAttribPointer(getAttributeLocation(name), size, GL_FLOAT, GL_FALSE, stride, values); }
void setAttributeValue(const char *name, float value) { glVertexAttrib1f(getAttributeLocation(name), value); }
void setAttributeValue(const char *name, float x, float y) { glVertexAttrib2f(getAttributeLocation(name), x, y); }
void setAttributeValue(const char *name, float x, float y, float z) { glVertexAttrib3f(getAttributeLocation(name), x, y, z); }
2011-12-07 01:31:55 +01:00
2012-03-19 22:56:07 +01:00
void setUniformValue(int location, const Color& color) { glUniform4f(m_uniformLocations[location], color.rF(), color.gF(), color.bF(), color.aF()); }
2012-06-22 05:14:13 +02:00
void setUniformValue(int location, int value) { glUniform1i(m_uniformLocations[location], value); }
2011-12-25 00:14:12 +01:00
void setUniformValue(int location, float value) { glUniform1f(m_uniformLocations[location], value); }
void setUniformValue(int location, float x, float y) { glUniform2f(m_uniformLocations[location], x, y); }
void setUniformValue(int location, float x, float y, float z) { glUniform3f(m_uniformLocations[location], x, y, z); }
void setUniformValue(int location, float x, float y, float z, float w) { glUniform4f(m_uniformLocations[location], x, y, z, w); }
void setUniformValue(int location, const Matrix2& mat) { glUniformMatrix2fv(m_uniformLocations[location], 1, GL_FALSE, mat.data()); }
void setUniformValue(int location, const Matrix3& mat) { glUniformMatrix3fv(m_uniformLocations[location], 1, GL_FALSE, mat.data()); }
2012-03-19 22:56:07 +01:00
void setUniformValue(const char *name, const Color& color) { glUniform4f(glGetUniformLocation(m_programId, name), color.rF(), color.gF(), color.bF(), color.aF()); }
2012-06-22 05:14:13 +02:00
void setUniformValue(const char *name, int value) { glUniform1i(glGetUniformLocation(m_programId, name), value); }
2011-12-25 00:14:12 +01:00
void setUniformValue(const char *name, float value) { glUniform1f(glGetUniformLocation(m_programId, name), value); }
void setUniformValue(const char *name, float x, float y) { glUniform2f(glGetUniformLocation(m_programId, name), x, y); }
void setUniformValue(const char *name, float x, float y, float z) { glUniform3f(glGetUniformLocation(m_programId, name), x, y, z); }
void setUniformValue(const char *name, float x, float y, float z, float w) { glUniform4f(glGetUniformLocation(m_programId, name), x, y, z, w); }
void setUniformValue(const char *name, const Matrix2& mat) { glUniformMatrix2fv(glGetUniformLocation(m_programId, name), 1, GL_FALSE, mat.data()); }
void setUniformValue(const char *name, const Matrix3& mat) { glUniformMatrix3fv(glGetUniformLocation(m_programId, name), 1, GL_FALSE, mat.data()); }
2012-06-22 05:14:13 +02:00
// TODO: Point, PointF, Color, Size, SizeF ?
2011-12-07 01:31:55 +01:00
bool isLinked() { return m_linked; }
2012-06-22 05:14:13 +02:00
uint getProgramId() { return m_programId; }
2011-12-07 01:31:55 +01:00
ShaderList getShaders() { return m_shaders; }
private:
bool m_linked;
2012-06-22 05:14:13 +02:00
uint m_programId;
static uint m_currentProgram;
2011-12-07 01:31:55 +01:00
ShaderList m_shaders;
2012-06-22 05:14:13 +02:00
std::array<int, MAX_UNIFORM_LOCATIONS> m_uniformLocations;
2011-12-07 01:31:55 +01:00
};
#endif