OGL32CTX: Factorize UI shaders

This should fix UI disappearing after resolution change.
This commit is contained in:
Vincent Lejeune
2014-01-28 21:26:16 +01:00
parent 9955aa6970
commit fd968a0acf
4 changed files with 204 additions and 141 deletions

View File

@@ -4,7 +4,6 @@ uniform vec2 size;
in vec2 position;
void main()
{
gl_Position = vec4(position * size + center, 0., 1.);

View File

@@ -55,8 +55,6 @@ PFNGLVERTEXATTRIBIPOINTERPROC glVertexAttribIPointer;
PFNGLDEBUGMESSAGECALLBACKARBPROC glDebugMessageCallbackARB;
#endif
static GLuint quad_buffer;
static GLuint ColoredVertex;
static bool is_gl_init = false;
//#define ARB_DEBUG_OUTPUT
@@ -185,27 +183,6 @@ void initGL()
#ifdef ARB_DEBUG_OUTPUT
glDebugMessageCallbackARB((GLDEBUGPROCARB)debugCallback, NULL);
#endif
const float quad_vertex[] = {
-1., -1., -1., 1., // UpperLeft
-1., 1., -1., -1., // LowerLeft
1., -1., 1., 1., // UpperRight
1., 1., 1., -1., // LowerRight
};
glGenBuffers(1, &quad_buffer);
glBindBuffer(GL_ARRAY_BUFFER, quad_buffer);
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(float), quad_vertex, GL_STATIC_DRAW);
const unsigned quad_color[] = {
0, 0, 0, 255,
255, 0, 0, 255,
0, 255, 0, 255,
0, 0, 255, 255,
};
glGenBuffers(1, &ColoredVertex);
glBindBuffer(GL_ARRAY_BUFFER, ColoredVertex);
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(unsigned), quad_color, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
// Mostly from shader tutorial
@@ -306,29 +283,6 @@ void setTexture(unsigned TextureUnit, GLuint TextureId, GLenum MagFilter, GLenum
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
static GLuint TexturedQuadShader;
static GLuint TexturedQuadAttribPosition;
static GLuint TexturedQuadAttribTexCoord;
static GLuint TexturedQuadUniformTex;
static GLuint TexturedQuadUniformCenter;
static GLuint TexturedQuadUniformSize;
static GLuint TexturedQuadUniformTexcenter;
static GLuint TexturedQuadUniformTexsize;
static GLuint TQvao;
static GLuint ColorTexturedQuadShader;
static GLuint ColorTexturedQuadAttribPosition;
static GLuint ColorTexturedQuadAttribTexCoord;
static GLuint ColorTexturedQuadAttribColor;
static GLuint ColorTexturedQuadUniformTex;
static GLuint ColorTexturedQuadUniformCenter;
static GLuint ColorTexturedQuadUniformSize;
static GLuint ColorTexturedQuadUniformTexcenter;
static GLuint ColorTexturedQuadUniformTexsize;
static GLuint CTQvao;
static void drawTexColoredQuad(const video::ITexture *texture, const video::SColor *col, 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)
@@ -340,42 +294,15 @@ static void drawTexColoredQuad(const video::ITexture *texture, const video::SCol
col[3].getRed(), col[3].getGreen(), col[3].getBlue(), col[3].getAlpha(),
};
if (!ColorTexturedQuadShader) {
ColorTexturedQuadShader = LoadProgram(file_manager->getAsset("shaders/colortexturedquad.vert").c_str(), file_manager->getAsset("shaders/colortexturedquad.frag").c_str());
ColorTexturedQuadAttribPosition = glGetAttribLocation(ColorTexturedQuadShader, "position");
ColorTexturedQuadAttribTexCoord = glGetAttribLocation(ColorTexturedQuadShader, "texcoord");
ColorTexturedQuadAttribColor = glGetAttribLocation(ColorTexturedQuadShader, "color");
ColorTexturedQuadUniformTex = glGetUniformLocation(ColorTexturedQuadShader, "tex");
ColorTexturedQuadUniformCenter = glGetUniformLocation(ColorTexturedQuadShader, "center");
ColorTexturedQuadUniformSize = glGetUniformLocation(ColorTexturedQuadShader, "size");
ColorTexturedQuadUniformTexcenter = glGetUniformLocation(ColorTexturedQuadShader, "texcenter");
ColorTexturedQuadUniformTexsize = glGetUniformLocation(ColorTexturedQuadShader, "texsize");
glGenVertexArrays(1, &CTQvao);
glBindVertexArray(CTQvao);
glEnableVertexAttribArray(ColorTexturedQuadAttribPosition);
glEnableVertexAttribArray(ColorTexturedQuadAttribTexCoord);
glEnableVertexAttribArray(ColorTexturedQuadAttribColor);
glBindBuffer(GL_ARRAY_BUFFER, quad_buffer);
glVertexAttribPointer(ColorTexturedQuadAttribPosition, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glVertexAttribPointer(ColorTexturedQuadAttribTexCoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid *)(2 * sizeof(float)));
glBindBuffer(GL_ARRAY_BUFFER, ColoredVertex);
glVertexAttribIPointer(ColorTexturedQuadAttribColor, 4, GL_UNSIGNED_INT, 4 * sizeof(unsigned), 0);
glBindVertexArray(0);
}
glBindBuffer(GL_ARRAY_BUFFER, ColoredVertex);
glBindBuffer(GL_ARRAY_BUFFER, UIShader::ColoredTextureRectShader::colorvbo);
glBufferSubData(GL_ARRAY_BUFFER, 0, 16 * sizeof(unsigned), colors);
glUseProgram(ColorTexturedQuadShader);
glBindVertexArray(CTQvao);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, static_cast<const irr::video::COpenGLTexture*>(texture)->getOpenGLTextureName());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glUniform1i(ColorTexturedQuadUniformTex, 0);
glUniform2f(ColorTexturedQuadUniformCenter, center_pos_x, center_pos_y);
glUniform2f(ColorTexturedQuadUniformSize, width, height);
glUniform2f(ColorTexturedQuadUniformTexcenter, tex_center_pos_x, tex_center_pos_y);
glUniform2f(ColorTexturedQuadUniformTexsize, tex_width, tex_height);
glUseProgram(UIShader::ColoredTextureRectShader::Program);
glBindVertexArray(UIShader::ColoredTextureRectShader::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);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
@@ -385,36 +312,12 @@ void drawTexQuad(const video::ITexture *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)
{
if (!TexturedQuadShader) {
TexturedQuadShader = LoadProgram(file_manager->getAsset("shaders/texturedquad.vert").c_str(), file_manager->getAsset("shaders/texturedquad.frag").c_str());
glUseProgram(UIShader::TextureRectShader::Program);
glBindVertexArray(UIShader::TextureRectShader::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);
TexturedQuadAttribPosition = glGetAttribLocation(TexturedQuadShader, "position");
TexturedQuadAttribTexCoord = glGetAttribLocation(TexturedQuadShader, "texcoord");
TexturedQuadUniformTex = glGetUniformLocation(TexturedQuadShader, "tex");
TexturedQuadUniformCenter = glGetUniformLocation(TexturedQuadShader, "center");
TexturedQuadUniformSize = glGetUniformLocation(TexturedQuadShader, "size");
TexturedQuadUniformTexcenter = glGetUniformLocation(TexturedQuadShader, "texcenter");
TexturedQuadUniformTexsize = glGetUniformLocation(TexturedQuadShader, "texsize");
glGenVertexArrays(1, &TQvao);
glBindVertexArray(TQvao);
glEnableVertexAttribArray(TexturedQuadAttribPosition);
glEnableVertexAttribArray(TexturedQuadAttribTexCoord);
glBindBuffer(GL_ARRAY_BUFFER, quad_buffer);
glVertexAttribPointer(TexturedQuadAttribPosition, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glVertexAttribPointer(TexturedQuadAttribTexCoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid *)(2 * sizeof(float)));
glBindVertexArray(0);
}
glUseProgram(TexturedQuadShader);
glBindVertexArray(TQvao);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, static_cast<const irr::video::COpenGLTexture*>(texture)->getOpenGLTextureName());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glUniform1i(TexturedQuadUniformTex, 0);
glUniform2f(TexturedQuadUniformCenter, center_pos_x, center_pos_y);
glUniform2f(TexturedQuadUniformSize, width, height);
glUniform2f(TexturedQuadUniformTexcenter, tex_center_pos_x, tex_center_pos_y);
glUniform2f(TexturedQuadUniformTexsize, tex_width, tex_height);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
@@ -470,8 +373,6 @@ void draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect
sourceRect.LowerRightCorner.X * invW,
sourceRect.LowerRightCorner.Y *invH);
initGL();
if (useAlphaChannelOfTexture)
{
glEnable(GL_BLEND);
@@ -490,12 +391,6 @@ void draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect
glUseProgram(0);
}
static GLuint ColoredQuadShader;
static GLuint ColoredQuadUniformCenter;
static GLuint ColoredQuadUniformSize;
static GLuint ColoredQuadUniformColor;
static GLuint CQvao;
void GL32_draw2DRectangle(video::SColor color, const core::rect<s32>& position,
const core::rect<s32>* clip)
{
@@ -521,8 +416,6 @@ void GL32_draw2DRectangle(video::SColor color, const core::rect<s32>& position,
float height = position.LowerRightCorner.Y - position.UpperLeftCorner.Y;
height /= screen_h;
initGL();
if (color.getAlpha() < 255)
{
glEnable(GL_BLEND);
@@ -533,24 +426,10 @@ void GL32_draw2DRectangle(video::SColor color, const core::rect<s32>& position,
glDisable(GL_BLEND);
}
if (!ColoredQuadShader)
{
ColoredQuadShader = LoadProgram(file_manager->getAsset("shaders/coloredquad.vert").c_str(), file_manager->getAsset("shaders/coloredquad.frag").c_str());
ColoredQuadUniformColor = glGetUniformLocation(ColoredQuadShader, "color");
ColoredQuadUniformCenter = glGetUniformLocation(ColoredQuadShader, "center");
ColoredQuadUniformSize = glGetUniformLocation(ColoredQuadShader, "size");
glGenVertexArrays(1, &CQvao);
glBindVertexArray(CQvao);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, quad_buffer);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glBindVertexArray(0);
}
glUseProgram(ColoredQuadShader);
glBindVertexArray(CQvao);
glUniform2f(ColoredQuadUniformCenter, center_pos_x, center_pos_y);
glUniform2f(ColoredQuadUniformSize, width, height);
glUniform4i(ColoredQuadUniformColor, color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
glUseProgram(UIShader::ColoredRectShader::Program);
glBindVertexArray(UIShader::ColoredRectShader::vao);
UIShader::ColoredRectShader::setUniforms(center_pos_x, center_pos_y, width, height, color);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

View File

@@ -61,11 +61,10 @@ Shaders::Shaders()
loadShaders();
}
GLuint quad_vbo = 0;
GLuint quad_vbo;
static void initQuadVBO()
{
initGL();
const float quad_vertex[] = {
-1., -1., 0., 0., // UpperLeft
-1., 1., 0., 1., // LowerLeft
@@ -78,6 +77,22 @@ static void initQuadVBO()
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
// It should be possible to merge it with previous one...
GLuint quad_buffer;
static void initQuadBuffer()
{
const float quad_vertex[] = {
-1., -1., -1., 1., // UpperLeft
-1., 1., -1., -1., // LowerLeft
1., -1., 1., 1., // UpperRight
1., 1., 1., -1., // LowerRight
};
glGenBuffers(1, &quad_buffer);
glBindBuffer(GL_ARRAY_BUFFER, quad_buffer);
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(float), quad_vertex, GL_STATIC_DRAW);
}
void Shaders::loadShaders()
{
const std::string &dir = file_manager->getAsset(FileManager::SHADER, "");
@@ -224,6 +239,7 @@ void Shaders::loadShaders()
initGL();
initQuadVBO();
initQuadBuffer();
FullScreenShader::BloomBlendShader::init();
FullScreenShader::BloomShader::init();
FullScreenShader::ColorLevelShader::init();
@@ -261,6 +277,9 @@ void Shaders::loadShaders()
ParticleShader::HeightmapSimulationShader::init();
ParticleShader::SimpleParticleRender::init();
ParticleShader::SimpleSimulationShader::init();
UIShader::ColoredRectShader::init();
UIShader::ColoredTextureRectShader::init();
UIShader::TextureRectShader::init();
}
Shaders::~Shaders()
@@ -1445,3 +1464,129 @@ namespace FullScreenShader
glUniform1i(FullScreenShader::FogShader::uniform_tex, TU_ntex);
}
}
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()
{
Program = LoadProgram(file_manager->getAsset("shaders/texturedquad.vert").c_str(), 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);
}
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 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()
{
Program = LoadProgram(file_manager->getAsset("shaders/colortexturedquad.vert").c_str(), file_manager->getAsset("shaders/colortexturedquad.frag").c_str());
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);
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)));
const unsigned quad_color[] = {
0, 0, 0, 255,
255, 0, 0, 255,
0, 255, 0, 255,
0, 0, 255, 255,
};
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);
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()
{
Program = LoadProgram(file_manager->getAsset("shaders/coloredquad.vert").c_str(), 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());
}
}

View File

@@ -436,6 +436,46 @@ public:
}
namespace UIShader
{
class TextureRectShader
{
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;
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);
};
class ColoredTextureRectShader
{
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;
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);
};
class ColoredRectShader
{
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);
};
}
#define FOREACH_SHADER(ACT) \
ACT(ES_NORMAL_MAP) \
ACT(ES_NORMAL_MAP_LIGHTMAP) \