Port UI shaders.

This commit is contained in:
Vincent Lejeune 2014-08-13 20:25:56 +02:00
parent 7a9e77da01
commit 22137eee88
6 changed files with 97 additions and 216 deletions

View File

@ -1,14 +1,14 @@
uniform vec2 center;
uniform vec2 size;
#if __VERSION__ >= 130
in vec2 position;
#if __VERSION__ >= 330
layout(location = 0) in vec2 Position;
#else
attribute vec2 position;
in vec2 Position;
#endif
void main()
{
gl_Position = vec4(position * size + center, 0., 1.);
gl_Position = vec4(Position * size + center, 0., 1.);
}

View File

@ -3,24 +3,22 @@ uniform vec2 size;
uniform vec2 texcenter;
uniform vec2 texsize;
#if __VERSION__ >= 130
in vec2 position;
in vec2 texcoord;
in uvec4 color;
out vec2 uv;
out vec4 col;
#if __VERSION__ >= 330
layout(location=0) in vec2 Position;
layout(location=3) in vec2 Texcoord;
layout(location=2) in uvec4 Color;
#else
attribute vec2 position;
attribute vec2 texcoord;
attribute uvec4 color;
varying vec2 uv;
varying vec4 col;
in vec2 Position;
in vec2 Texcoord;
in uvec4 Color;
#endif
out vec2 uv;
out vec4 col;
void main()
{
col = vec4(color) / 255.;
uv = texcoord * texsize + texcenter;
gl_Position = vec4(position * size + center, 0., 1.);
col = vec4(Color) / 255.;
uv = Texcoord * texsize + texcenter;
gl_Position = vec4(Position * size + center, 0., 1.);
}

View File

@ -3,19 +3,18 @@ uniform vec2 size;
uniform vec2 texcenter;
uniform vec2 texsize;
#if __VERSION__ >= 130
in vec2 position;
in vec2 texcoord;
out vec2 uv;
#if __VERSION__ >= 330
layout(location=0) in vec2 Position;
layout(location=3) in vec2 Texcoord;
#else
attribute vec2 position;
attribute vec2 texcoord;
varying vec2 uv;
in vec2 Position;
in vec2 Texcoord;
#endif
out vec2 uv;
void main()
{
uv = texcoord * texsize + texcenter;
gl_Position = vec4(position * size + center, 0., 1.);
uv = Texcoord * texsize + texcenter;
gl_Position = vec4(Position * size + center, 0., 1.);
}

View File

@ -908,14 +908,16 @@ static void drawTexColoredQuad(const video::ITexture *texture, const video::SCol
col[3].getRed(), col[3].getGreen(), col[3].getBlue(), col[3].getAlpha(),
};
glBindBuffer(GL_ARRAY_BUFFER, UIShader::ColoredTextureRectShader::colorvbo);
glBindBuffer(GL_ARRAY_BUFFER, UIShader::ColoredTextureRectShader::getInstance()->colorvbo);
glBufferSubData(GL_ARRAY_BUFFER, 0, 16 * sizeof(unsigned), colors);
glUseProgram(UIShader::ColoredTextureRectShader::Program);
glBindVertexArray(UIShader::ColoredTextureRectShader::vao);
glUseProgram(UIShader::ColoredTextureRectShader::getInstance()->Program);
glBindVertexArray(UIShader::ColoredTextureRectShader::getInstance()->vao);
setTexture(0, static_cast<const irr::video::COpenGLTexture*>(texture)->getOpenGLTextureName(), GL_LINEAR, GL_LINEAR);
UIShader::TextureRectShader::setUniforms(center_pos_x, center_pos_y, width, height, tex_center_pos_x, tex_center_pos_y, tex_width, tex_height, 0);
setTexture(UIShader::ColoredTextureRectShader::getInstance()->TU_tex, static_cast<const irr::video::COpenGLTexture*>(texture)->getOpenGLTextureName(), GL_LINEAR, GL_LINEAR);
UIShader::ColoredTextureRectShader::getInstance()->setUniforms(
core::vector2df(center_pos_x, center_pos_y), core::vector2df(width, height),
core::vector2df(tex_center_pos_x, tex_center_pos_y), core::vector2df(tex_width, tex_height));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
@ -929,11 +931,14 @@ void drawTexQuad(GLuint texture, float width, float height,
float center_pos_x, float center_pos_y, float tex_center_pos_x, float tex_center_pos_y,
float tex_width, float tex_height)
{
glUseProgram(UIShader::TextureRectShader::Program);
glBindVertexArray(UIShader::TextureRectShader::vao);
glUseProgram(UIShader::TextureRectShader::getInstance()->Program);
glBindVertexArray(SharedObject::UIVAO);
setTexture(0, texture, GL_LINEAR, GL_LINEAR);
UIShader::TextureRectShader::setUniforms(center_pos_x, center_pos_y, width, height, tex_center_pos_x, tex_center_pos_y, tex_width, tex_height, 0);
setTexture(UIShader::TextureRectShader::getInstance()->TU_tex, texture, GL_LINEAR, GL_LINEAR);
UIShader::TextureRectShader::getInstance()->setUniforms(
core::vector2df(center_pos_x, center_pos_y), core::vector2df(width, height),
core::vector2df(tex_center_pos_x, tex_center_pos_y),
core::vector2df(tex_width, tex_height));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
@ -1029,11 +1034,12 @@ void draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect
clipRect->getWidth(), clipRect->getHeight());
}
glUseProgram(UIShader::UniformColoredTextureRectShader::Program);
glBindVertexArray(UIShader::UniformColoredTextureRectShader::vao);
glUseProgram(UIShader::UniformColoredTextureRectShader::getInstance()->Program);
glBindVertexArray(SharedObject::UIVAO);
setTexture(0, static_cast<const irr::video::COpenGLTexture*>(texture)->getOpenGLTextureName(), GL_LINEAR, GL_LINEAR);
UIShader::UniformColoredTextureRectShader::setUniforms(center_pos_x, center_pos_y, width, height, tex_center_pos_x, tex_center_pos_y, tex_width, tex_height,colors, 0);
setTexture(UIShader::UniformColoredTextureRectShader::getInstance()->TU_tex, static_cast<const irr::video::COpenGLTexture*>(texture)->getOpenGLTextureName(), GL_LINEAR, GL_LINEAR);
UIShader::UniformColoredTextureRectShader::getInstance()->setUniforms(
core::vector2df(center_pos_x, center_pos_y), core::vector2df(width, height), core::vector2df(tex_center_pos_x, tex_center_pos_y), core::vector2df(tex_width, tex_height), colors);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
@ -1064,11 +1070,14 @@ void draw2DImageFromRTT(GLuint texture, size_t texture_w, size_t texture_h,
destRect, sourceRect, width, height, center_pos_x, center_pos_y,
tex_width, tex_height, tex_center_pos_x, tex_center_pos_y);
glUseProgram(UIShader::UniformColoredTextureRectShader::Program);
glBindVertexArray(UIShader::UniformColoredTextureRectShader::vao);
glUseProgram(UIShader::UniformColoredTextureRectShader::getInstance()->Program);
glBindVertexArray(SharedObject::UIVAO);
setTexture(0, texture, GL_LINEAR, GL_LINEAR);
UIShader::UniformColoredTextureRectShader::setUniforms(center_pos_x, center_pos_y, width, height, tex_center_pos_x, tex_center_pos_y, tex_width, tex_height, colors, 0);
setTexture(UIShader::UniformColoredTextureRectShader::getInstance()->TU_tex, texture, GL_LINEAR, GL_LINEAR);
UIShader::UniformColoredTextureRectShader::getInstance()->setUniforms(
core::vector2df(center_pos_x, center_pos_y), core::vector2df(width, height),
core::vector2df(tex_center_pos_x, tex_center_pos_y), core::vector2df(tex_width, tex_height),
colors);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
@ -1172,9 +1181,9 @@ void GL32_draw2DRectangle(video::SColor color, const core::rect<s32>& position,
clip->getWidth(), clip->getHeight());
}
glUseProgram(UIShader::ColoredRectShader::Program);
glBindVertexArray(UIShader::ColoredRectShader::vao);
UIShader::ColoredRectShader::setUniforms(center_pos_x, center_pos_y, width, height, color);
glUseProgram(UIShader::ColoredRectShader::getInstance()->Program);
glBindVertexArray(SharedObject::UIVAO);
UIShader::ColoredRectShader::getInstance()->setUniforms(core::vector2df(center_pos_x, center_pos_y), core::vector2df(width, height), color);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindBuffer(GL_ARRAY_BUFFER, 0);

View File

@ -133,6 +133,7 @@ Shaders::Shaders()
GLuint quad_vbo, tri_vbo;
GLuint SharedObject::FullScreenQuadVAO = 0;
GLuint SharedObject::UIVAO = 0;
static void initQuadVBO()
{
@ -179,6 +180,15 @@ static void initQuadBuffer()
glGenBuffers(1, &quad_buffer);
glBindBuffer(GL_ARRAY_BUFFER, quad_buffer);
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(float), quad_vertex, GL_STATIC_DRAW);
glGenVertexArrays(1, &SharedObject::UIVAO);
glBindVertexArray(SharedObject::UIVAO);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(3);
glBindBuffer(GL_ARRAY_BUFFER, quad_buffer);
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);
}
GLuint SharedObject::billboardvbo = 0;
@ -391,10 +401,6 @@ void Shaders::loadShaders()
ParticleShader::HeightmapSimulationShader::init();
ParticleShader::SimpleParticleRender::init();
ParticleShader::SimpleSimulationShader::init();
UIShader::ColoredRectShader::init();
UIShader::ColoredTextureRectShader::init();
UIShader::TextureRectShader::init();
UIShader::UniformColoredTextureRectShader::init();
UtilShader::ColoredLine::init();
}
@ -1966,127 +1972,44 @@ namespace FullScreenShader
namespace UIShader
{
GLuint TextureRectShader::Program;
GLuint TextureRectShader::attrib_position;
GLuint TextureRectShader::attrib_texcoord;
GLuint TextureRectShader::uniform_tex;
GLuint TextureRectShader::uniform_center;
GLuint TextureRectShader::uniform_size;
GLuint TextureRectShader::uniform_texcenter;
GLuint TextureRectShader::uniform_texsize;
GLuint TextureRectShader::vao;
void TextureRectShader::init()
TextureRectShader::TextureRectShader()
{
Program = LoadProgram(
GL_VERTEX_SHADER, file_manager->getAsset("shaders/texturedquad.vert").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/texturedquad.frag").c_str());
attrib_position = glGetAttribLocation(Program, "position");
attrib_texcoord = glGetAttribLocation(Program, "texcoord");
uniform_tex = glGetUniformLocation(Program, "tex");
uniform_center = glGetUniformLocation(Program, "center");
uniform_size = glGetUniformLocation(Program, "size");
uniform_texcenter = glGetUniformLocation(Program, "texcenter");
uniform_texsize = glGetUniformLocation(Program, "texsize");
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(attrib_position);
glEnableVertexAttribArray(attrib_texcoord);
glBindBuffer(GL_ARRAY_BUFFER, quad_buffer);
glVertexAttribPointer(attrib_position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glVertexAttribPointer(attrib_texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid *)(2 * sizeof(float)));
glBindVertexArray(0);
AssignUniforms("center", "size", "texcenter", "texsize");
TU_tex = 0;
AssignTextureUnit(Program, TexUnit(TU_tex, "tex"));
}
void TextureRectShader::setUniforms(float center_pos_x, float center_pos_y, float width, float height, float tex_center_pos_x, float tex_center_pos_y, float tex_width, float tex_height, unsigned TU_tex)
{
glUniform1i(uniform_tex, TU_tex);
glUniform2f(uniform_center, center_pos_x, center_pos_y);
glUniform2f(uniform_size, width, height);
glUniform2f(uniform_texcenter, tex_center_pos_x, tex_center_pos_y);
glUniform2f(uniform_texsize, tex_width, tex_height);
}
GLuint UniformColoredTextureRectShader::Program;
GLuint UniformColoredTextureRectShader::attrib_position;
GLuint UniformColoredTextureRectShader::attrib_texcoord;
GLuint UniformColoredTextureRectShader::uniform_tex;
GLuint UniformColoredTextureRectShader::uniform_color;
GLuint UniformColoredTextureRectShader::uniform_center;
GLuint UniformColoredTextureRectShader::uniform_size;
GLuint UniformColoredTextureRectShader::uniform_texcenter;
GLuint UniformColoredTextureRectShader::uniform_texsize;
GLuint UniformColoredTextureRectShader::vao;
void UniformColoredTextureRectShader::init()
UniformColoredTextureRectShader::UniformColoredTextureRectShader()
{
Program = LoadProgram(
GL_VERTEX_SHADER, file_manager->getAsset("shaders/texturedquad.vert").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/uniformcolortexturedquad.frag").c_str());
attrib_position = glGetAttribLocation(Program, "position");
attrib_texcoord = glGetAttribLocation(Program, "texcoord");
uniform_tex = glGetUniformLocation(Program, "tex");
uniform_color = glGetUniformLocation(Program, "color");
uniform_center = glGetUniformLocation(Program, "center");
uniform_size = glGetUniformLocation(Program, "size");
uniform_texcenter = glGetUniformLocation(Program, "texcenter");
uniform_texsize = glGetUniformLocation(Program, "texsize");
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(attrib_position);
glEnableVertexAttribArray(attrib_texcoord);
glBindBuffer(GL_ARRAY_BUFFER, quad_buffer);
glVertexAttribPointer(attrib_position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glVertexAttribPointer(attrib_texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid *)(2 * sizeof(float)));
glBindVertexArray(0);
AssignUniforms("center", "size", "texcenter", "texsize", "color");
TU_tex = 0;
AssignTextureUnit(Program, TexUnit(TU_tex, "tex"));
}
void UniformColoredTextureRectShader::setUniforms(float center_pos_x, float center_pos_y, float width, float height, float tex_center_pos_x, float tex_center_pos_y, float tex_width, float tex_height, const SColor &color, unsigned TU_tex)
{
glUniform1i(uniform_tex, TU_tex);
glUniform2f(uniform_center, center_pos_x, center_pos_y);
glUniform2f(uniform_size, width, height);
glUniform2f(uniform_texcenter, tex_center_pos_x, tex_center_pos_y);
glUniform2f(uniform_texsize, tex_width, tex_height);
glUniform4i(uniform_color, color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
}
GLuint ColoredTextureRectShader::Program;
GLuint ColoredTextureRectShader::attrib_position;
GLuint ColoredTextureRectShader::attrib_texcoord;
GLuint ColoredTextureRectShader::attrib_color;
GLuint ColoredTextureRectShader::uniform_tex;
GLuint ColoredTextureRectShader::uniform_center;
GLuint ColoredTextureRectShader::uniform_size;
GLuint ColoredTextureRectShader::uniform_texcenter;
GLuint ColoredTextureRectShader::uniform_texsize;
GLuint ColoredTextureRectShader::colorvbo;
GLuint ColoredTextureRectShader::vao;
void ColoredTextureRectShader::init()
ColoredTextureRectShader::ColoredTextureRectShader()
{
Program = LoadProgram(
GL_VERTEX_SHADER, file_manager->getAsset("shaders/colortexturedquad.vert").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/colortexturedquad.frag").c_str());
AssignUniforms("center", "size", "texcenter", "texsize");
TU_tex = 0;
AssignTextureUnit(Program, TexUnit(TU_tex, "tex"));
attrib_position = glGetAttribLocation(Program, "position");
attrib_texcoord = glGetAttribLocation(Program, "texcoord");
attrib_color = glGetAttribLocation(Program, "color");
uniform_tex = glGetUniformLocation(Program, "tex");
uniform_center = glGetUniformLocation(Program, "center");
uniform_size = glGetUniformLocation(Program, "size");
uniform_texcenter = glGetUniformLocation(Program, "texcenter");
uniform_texsize = glGetUniformLocation(Program, "texsize");
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(attrib_position);
glEnableVertexAttribArray(attrib_texcoord);
glEnableVertexAttribArray(attrib_color);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(3);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, quad_buffer);
glVertexAttribPointer(attrib_position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glVertexAttribPointer(attrib_texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid *)(2 * sizeof(float)));
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)));
const unsigned quad_color[] = {
0, 0, 0, 255,
255, 0, 0, 255,
@ -2096,47 +2019,15 @@ namespace UIShader
glGenBuffers(1, &colorvbo);
glBindBuffer(GL_ARRAY_BUFFER, colorvbo);
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(unsigned), quad_color, GL_DYNAMIC_DRAW);
glVertexAttribIPointer(attrib_color, 4, GL_UNSIGNED_INT, 4 * sizeof(unsigned), 0);
glVertexAttribIPointer(2, 4, GL_UNSIGNED_INT, 4 * sizeof(unsigned), 0);
glBindVertexArray(0);
}
void ColoredTextureRectShader::setUniforms(float center_pos_x, float center_pos_y, float width, float height, float tex_center_pos_x, float tex_center_pos_y, float tex_width, float tex_height, unsigned TU_tex)
{
glUniform1i(uniform_tex, TU_tex);
glUniform2f(uniform_center, center_pos_x, center_pos_y);
glUniform2f(uniform_size, width, height);
glUniform2f(uniform_texcenter, tex_center_pos_x, tex_center_pos_y);
glUniform2f(uniform_texsize, tex_width, tex_height);
}
GLuint ColoredRectShader::Program;
GLuint ColoredRectShader::attrib_position;
GLuint ColoredRectShader::uniform_center;
GLuint ColoredRectShader::uniform_size;
GLuint ColoredRectShader::uniform_color;
GLuint ColoredRectShader::vao;
void ColoredRectShader::init()
ColoredRectShader::ColoredRectShader()
{
Program = LoadProgram(
GL_VERTEX_SHADER, file_manager->getAsset("shaders/coloredquad.vert").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/coloredquad.frag").c_str());
attrib_position = glGetAttribLocation(Program, "position");
uniform_color = glGetUniformLocation(Program, "color");
uniform_center = glGetUniformLocation(Program, "center");
uniform_size = glGetUniformLocation(Program, "size");
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(attrib_position);
glBindBuffer(GL_ARRAY_BUFFER, quad_buffer);
glVertexAttribPointer(attrib_position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glBindVertexArray(0);
}
void ColoredRectShader::setUniforms(float center_pos_x, float center_pos_y, float width, float height, const video::SColor &color)
{
glUniform2f(uniform_center, center_pos_x, center_pos_y);
glUniform2f(uniform_size, width, height);
glUniform4i(uniform_color, color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
AssignUniforms("center", "size", "color");
}
}

View File

@ -34,6 +34,7 @@ public:
static GLuint cubevbo, cubeindexes, frustrumvbo, frustrumindexes;
static GLuint ViewProjectionMatrixesUBO;
static GLuint FullScreenQuadVAO;
static GLuint UIVAO;
};
namespace UtilShader
@ -790,53 +791,36 @@ public:
namespace UIShader
{
class TextureRectShader
class TextureRectShader : public ShaderHelperSingleton<TextureRectShader, core::vector2df, core::vector2df, core::vector2df, core::vector2df>
{
public:
static GLuint Program;
static GLuint attrib_position, attrib_texcoord;
static GLuint uniform_tex, uniform_center, uniform_size, uniform_texcenter, uniform_texsize;
static GLuint vao;
GLuint TU_tex;
static void init();
static void setUniforms(float center_pos_x, float center_pos_y, float width, float height, float tex_center_pos_x, float tex_center_pos_y, float tex_width, float tex_height, unsigned TU_tex);
TextureRectShader();
};
class UniformColoredTextureRectShader
class UniformColoredTextureRectShader : public ShaderHelperSingleton<UniformColoredTextureRectShader, core::vector2df, core::vector2df, core::vector2df, core::vector2df, video::SColor>
{
public:
static GLuint Program;
static GLuint attrib_position, attrib_texcoord;
static GLuint uniform_tex, uniform_color, uniform_center, uniform_size, uniform_texcenter, uniform_texsize;
static GLuint vao;
GLuint TU_tex;
static void init();
static void setUniforms(float center_pos_x, float center_pos_y, float width, float height, float tex_center_pos_x, float tex_center_pos_y, float tex_width, float tex_height, const video::SColor &color, unsigned TU_tex);
UniformColoredTextureRectShader();
};
class ColoredTextureRectShader
class ColoredTextureRectShader : public ShaderHelperSingleton<ColoredTextureRectShader, core::vector2df, core::vector2df, core::vector2df, core::vector2df>
{
public:
static GLuint Program;
static GLuint attrib_position, attrib_texcoord, attrib_color;
static GLuint uniform_tex, uniform_center, uniform_size, uniform_texcenter, uniform_texsize;
static GLuint colorvbo;
static GLuint vao;
GLuint TU_tex;
GLuint colorvbo;
GLuint vao;
static void init();
static void setUniforms(float center_pos_x, float center_pos_y, float width, float height, float tex_center_pos_x, float tex_center_pos_y, float tex_width, float tex_height, unsigned TU_tex);
ColoredTextureRectShader();
};
class ColoredRectShader
class ColoredRectShader : public ShaderHelperSingleton<ColoredRectShader, core::vector2df, core::vector2df, video::SColor>
{
public:
static GLuint Program;
static GLuint attrib_position;
static GLuint uniform_center, uniform_size, uniform_color;
static GLuint vao;
static void init();
static void setUniforms(float center_pos_x, float center_pos_y, float width, float height, const video::SColor &color);
ColoredRectShader();
};
}