Port stkbillboard to singleton shader

This commit is contained in:
Vincent Lejeune 2014-08-07 00:52:44 +02:00
parent a0e457ac13
commit 79fba1779e
4 changed files with 29 additions and 48 deletions

View File

@ -2,16 +2,15 @@ uniform mat4 ModelViewMatrix;
uniform vec3 Position;
uniform vec2 Size;
#if __VERSION__ >= 130
#if __VERSION__ >= 330
layout(location = 0) in vec2 Corner;
layout(location = 3) in vec2 Texcoord;
#else
in vec2 Corner;
in vec2 Texcoord;
out vec2 uv;
#else
attribute vec2 Corner;
attribute vec2 Texcoord;
varying vec2 uv;
#endif
out vec2 uv;
void main(void)
{

View File

@ -406,7 +406,6 @@ void Shaders::loadShaders()
FullScreenShader::MLAAGatherSHader::init();
MeshShader::ColorizeShader::init();
MeshShader::BubbleShader::init();
MeshShader::BillboardShader::init();
LightShader::PointLightShader::init();
MeshShader::RSMShader::init();
MeshShader::SkyboxShader::init();
@ -960,40 +959,19 @@ namespace MeshShader
AssignTextureUnit(Program, TexUnit(TU_tex, "tex"));
}
GLuint BillboardShader::Program;
GLuint BillboardShader::attrib_corner;
GLuint BillboardShader::attrib_texcoord;
GLuint BillboardShader::uniform_MV;
GLuint BillboardShader::uniform_P;
GLuint BillboardShader::uniform_tex;
GLuint BillboardShader::uniform_Position;
GLuint BillboardShader::uniform_Size;
void BillboardShader::init()
BillboardShader::BillboardShader()
{
Program = LoadProgram(
GL_VERTEX_SHADER, file_manager->getAsset("shaders/billboard.vert").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/billboard.frag").c_str());
attrib_corner = glGetAttribLocation(Program, "Corner");
attrib_texcoord = glGetAttribLocation(Program, "Texcoord");
uniform_MV = glGetUniformLocation(Program, "ModelViewMatrix");
uniform_P = glGetUniformLocation(Program, "ProjectionMatrix");
uniform_Position = glGetUniformLocation(Program, "Position");
uniform_Size = glGetUniformLocation(Program, "Size");
uniform_tex = glGetUniformLocation(Program, "tex");
}
void BillboardShader::setUniforms(const core::matrix4 &ModelViewMatrix,
const core::matrix4 &ProjectionMatrix,
const core::vector3df &Position,
const core::dimension2d<float> &size,
unsigned TU_tex)
{
glUniformMatrix4fv(uniform_MV, 1, GL_FALSE, ModelViewMatrix.pointer());
glUniformMatrix4fv(uniform_P, 1, GL_FALSE, ProjectionMatrix.pointer());
glUniform3f(uniform_Position, Position.X, Position.Y, Position.Z);
glUniform2f(uniform_Size, size.Width, size.Height);
glUniform1i(uniform_tex, TU_tex);
GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData");
glUniformBlockBinding(Program, uniform_ViewProjectionMatrixesUBO, 0);
AssignUniforms("ModelViewMatrix", "ProjectionMatrix", "Position", "Size");
TU_tex = 0;
AssignTextureUnit(Program, TexUnit(TU_tex, "tex"));
}
GLuint ColorizeShader::Program;

View File

@ -94,6 +94,13 @@ struct UniformHelper
setUniformsHelper<N + 1>(uniforms, arg...);
}
template<unsigned N = 0, typename... Args>
static void setUniformsHelper(const std::vector<GLuint> &uniforms, const core::dimension2df &v, Args... arg)
{
glUniform2fWraper(uniforms[N], v.X, v.Y);
setUniformsHelper<N + 1>(uniforms, arg...);
}
template<unsigned N = 0, typename... Args>
static void setUniformsHelper(const std::vector<GLuint> &uniforms, float f, Args... arg)
{
@ -297,15 +304,12 @@ public:
TransparentFogShader();
};
class BillboardShader
class BillboardShader : public ShaderHelperSingleton<BillboardShader, core::matrix4, core::matrix4, core::vector3df, core::dimension2df>
{
public:
static GLuint Program;
static GLuint attrib_corner, attrib_texcoord;
static GLuint uniform_MV, uniform_P, uniform_tex, uniform_Position, uniform_Size;
GLuint TU_tex;
static void init();
static void setUniforms(const core::matrix4 &ModelViewMatrix, const core::matrix4 &ProjectionMatrix, const core::vector3df &Position, const core::dimension2d<float> &size, unsigned TU_tex);
BillboardShader();
};

View File

@ -13,10 +13,10 @@ static void createbillboardvao()
glGenVertexArrays(1, &billboardvao);
glBindVertexArray(billboardvao);
glBindBuffer(GL_ARRAY_BUFFER, SharedObject::billboardvbo);
glEnableVertexAttribArray(MeshShader::BillboardShader::attrib_corner);
glEnableVertexAttribArray(MeshShader::BillboardShader::attrib_texcoord);
glVertexAttribPointer(MeshShader::BillboardShader::attrib_corner, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glVertexAttribPointer(MeshShader::BillboardShader::attrib_texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid*) (2 * sizeof(float)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(3);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid*) (2 * sizeof(float)));
glBindVertexArray(0);
}
@ -50,8 +50,8 @@ void STKBillboard::render()
compressTexture(tex, true, true);
GLuint texid = getTextureGLuint(tex);
setTexture(0, texid, GL_LINEAR, GL_LINEAR);
glUseProgram(MeshShader::BillboardShader::Program);
MeshShader::BillboardShader::setUniforms(irr_driver->getViewMatrix(), irr_driver->getProjMatrix(), pos, Size, 0);
glUseProgram(MeshShader::BillboardShader::getInstance()->Program);
MeshShader::BillboardShader::getInstance()->setUniforms(irr_driver->getViewMatrix(), irr_driver->getProjMatrix(), pos, Size);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
return;