Add rotation uniform to 2d drawing shader

This commit is contained in:
Benau 2019-08-16 10:31:24 +08:00
parent e20f6e3209
commit ce1c188b1a
3 changed files with 15 additions and 10 deletions

View File

@ -2,7 +2,7 @@ uniform vec2 center;
uniform vec2 size; uniform vec2 size;
uniform vec2 texcenter; uniform vec2 texcenter;
uniform vec2 texsize; uniform vec2 texsize;
uniform float rotation;
#ifdef Explicit_Attrib_Location_Usable #ifdef Explicit_Attrib_Location_Usable
layout(location=0) in vec2 Position; layout(location=0) in vec2 Position;
layout(location=3) in vec2 Texcoord; layout(location=3) in vec2 Texcoord;
@ -15,6 +15,9 @@ out vec2 uv;
void main() void main()
{ {
float s = sin(rotation);
float c = cos(rotation);
mat2 m = mat2(c, -s, s, c);
uv = Texcoord * texsize + texcenter; uv = Texcoord * texsize + texcenter;
gl_Position = vec4(Position * size + center, 0., 1.); gl_Position = vec4(m * Position * size + center, 0., 1.) ;
} }

View File

@ -60,14 +60,15 @@ public:
// ============================================================================ // ============================================================================
class TextureRectShader : public TextureShader<TextureRectShader, 1, class TextureRectShader : public TextureShader<TextureRectShader, 1,
core::vector2df, core::vector2df, core::vector2df, core::vector2df,
core::vector2df, core::vector2df> core::vector2df, core::vector2df,
float>
{ {
public: public:
TextureRectShader() TextureRectShader()
{ {
loadProgram(OBJECT, GL_VERTEX_SHADER, "texturedquad.vert", loadProgram(OBJECT, GL_VERTEX_SHADER, "texturedquad.vert",
GL_FRAGMENT_SHADER, "texturedquad.frag"); GL_FRAGMENT_SHADER, "texturedquad.frag");
assignUniforms("center", "size", "texcenter", "texsize"); assignUniforms("center", "size", "texcenter", "texsize", "rotation");
assignSamplerNames(0, "tex", ST_BILINEAR_CLAMPED_FILTERED); assignSamplerNames(0, "tex", ST_BILINEAR_CLAMPED_FILTERED);
} // TextureRectShader } // TextureRectShader
@ -161,7 +162,7 @@ static void drawTexColoredQuad(const video::ITexture *texture,
static void drawTexQuad(GLuint texture, float width, float height, static void drawTexQuad(GLuint texture, float width, float height,
float center_pos_x, float center_pos_y, float center_pos_x, float center_pos_y,
float tex_center_pos_x, float tex_center_pos_y, float tex_center_pos_x, float tex_center_pos_y,
float tex_width, float tex_height) float tex_width, float tex_height, float rotation)
{ {
TextureRectShader::getInstance()->use(); TextureRectShader::getInstance()->use();
glBindVertexArray(SharedGPUObjects::getUI_VAO()); glBindVertexArray(SharedGPUObjects::getUI_VAO());
@ -171,7 +172,7 @@ static void drawTexQuad(GLuint texture, float width, float height,
core::vector2df(center_pos_x, center_pos_y), core::vector2df(center_pos_x, center_pos_y),
core::vector2df(width, height), core::vector2df(width, height),
core::vector2df(tex_center_pos_x, tex_center_pos_y), core::vector2df(tex_center_pos_x, tex_center_pos_y),
core::vector2df(tex_width, tex_height) ); core::vector2df(tex_width, tex_height), rotation);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0); glBindVertexArray(0);
@ -456,7 +457,7 @@ void draw2DImage(const video::ITexture* texture,
const core::rect<s32>* clip_rect, const core::rect<s32>* clip_rect,
const video::SColor* const colors, const video::SColor* const colors,
bool use_alpha_channel_of_texture, bool use_alpha_channel_of_texture,
bool draw_translucently) bool draw_translucently, float rotation)
{ {
if (!CVS->isGLSL()) if (!CVS->isGLSL())
{ {
@ -511,7 +512,7 @@ void draw2DImage(const video::ITexture* texture,
{ {
drawTexQuad(texture->getOpenGLTextureName(), width, height, drawTexQuad(texture->getOpenGLTextureName(), width, height,
center_pos_x, center_pos_y, tex_center_pos_x, center_pos_x, center_pos_y, tex_center_pos_x,
tex_center_pos_y, tex_width, tex_height); tex_center_pos_y, tex_width, tex_height, rotation);
} }
if (clip_rect) if (clip_rect)
glDisable(GL_SCISSOR_TEST); glDisable(GL_SCISSOR_TEST);
@ -588,7 +589,7 @@ void draw2DImage(const video::ITexture* texture,
{ {
drawTexQuad(texture->getOpenGLTextureName(), width, height, drawTexQuad(texture->getOpenGLTextureName(), width, height,
center_pos_x, center_pos_y, tex_center_pos_x, center_pos_x, center_pos_y, tex_center_pos_x,
tex_center_pos_y, tex_width, tex_height); tex_center_pos_y, tex_width, tex_height, 0.0f/*rotation*/);
} }
if (clip_rect) if (clip_rect)
glDisable(GL_SCISSOR_TEST); glDisable(GL_SCISSOR_TEST);

View File

@ -56,7 +56,8 @@ void draw2DImage(const irr::video::ITexture* texture,
const irr::core::rect<irr::s32>& sourceRect, const irr::core::rect<irr::s32>& sourceRect,
const irr::core::rect<irr::s32>* clipRect, const irr::core::rect<irr::s32>* clipRect,
const irr::video::SColor* const colors, const irr::video::SColor* const colors,
bool useAlphaChannelOfTexture, bool draw_translucently = false); bool useAlphaChannelOfTexture, bool draw_translucently = false,
float rotation = 0.0f);
void draw2DImage(const irr::video::ITexture* texture, void draw2DImage(const irr::video::ITexture* texture,
const irr::core::rect<float>& destRect, const irr::core::rect<float>& destRect,