From ce1c188b1a6d217b67d16b9614203e7d179264b8 Mon Sep 17 00:00:00 2001 From: Benau Date: Fri, 16 Aug 2019 10:31:24 +0800 Subject: [PATCH] Add rotation uniform to 2d drawing shader --- data/shaders/texturedquad.vert | 7 +++++-- src/graphics/2dutils.cpp | 15 ++++++++------- src/graphics/2dutils.hpp | 3 ++- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/data/shaders/texturedquad.vert b/data/shaders/texturedquad.vert index 9637d054a..49121bb71 100644 --- a/data/shaders/texturedquad.vert +++ b/data/shaders/texturedquad.vert @@ -2,7 +2,7 @@ uniform vec2 center; uniform vec2 size; uniform vec2 texcenter; uniform vec2 texsize; - +uniform float rotation; #ifdef Explicit_Attrib_Location_Usable layout(location=0) in vec2 Position; layout(location=3) in vec2 Texcoord; @@ -15,6 +15,9 @@ out vec2 uv; void main() { + float s = sin(rotation); + float c = cos(rotation); + mat2 m = mat2(c, -s, s, c); uv = Texcoord * texsize + texcenter; - gl_Position = vec4(Position * size + center, 0., 1.); + gl_Position = vec4(m * Position * size + center, 0., 1.) ; } diff --git a/src/graphics/2dutils.cpp b/src/graphics/2dutils.cpp index 03be97f2f..b46d48e42 100644 --- a/src/graphics/2dutils.cpp +++ b/src/graphics/2dutils.cpp @@ -60,14 +60,15 @@ public: // ============================================================================ class TextureRectShader : public TextureShader + core::vector2df, core::vector2df, + float> { public: TextureRectShader() { loadProgram(OBJECT, GL_VERTEX_SHADER, "texturedquad.vert", GL_FRAGMENT_SHADER, "texturedquad.frag"); - assignUniforms("center", "size", "texcenter", "texsize"); + assignUniforms("center", "size", "texcenter", "texsize", "rotation"); assignSamplerNames(0, "tex", ST_BILINEAR_CLAMPED_FILTERED); } // TextureRectShader @@ -161,7 +162,7 @@ static void drawTexColoredQuad(const video::ITexture *texture, static 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) + float tex_width, float tex_height, float rotation) { TextureRectShader::getInstance()->use(); 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(width, height), 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); glBindVertexArray(0); @@ -456,7 +457,7 @@ void draw2DImage(const video::ITexture* texture, const core::rect* clip_rect, const video::SColor* const colors, bool use_alpha_channel_of_texture, - bool draw_translucently) + bool draw_translucently, float rotation) { if (!CVS->isGLSL()) { @@ -511,7 +512,7 @@ void draw2DImage(const video::ITexture* texture, { drawTexQuad(texture->getOpenGLTextureName(), width, height, 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) glDisable(GL_SCISSOR_TEST); @@ -588,7 +589,7 @@ void draw2DImage(const video::ITexture* texture, { drawTexQuad(texture->getOpenGLTextureName(), width, height, 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) glDisable(GL_SCISSOR_TEST); diff --git a/src/graphics/2dutils.hpp b/src/graphics/2dutils.hpp index 081fe1f2c..e2fd31da0 100644 --- a/src/graphics/2dutils.hpp +++ b/src/graphics/2dutils.hpp @@ -56,7 +56,8 @@ void draw2DImage(const irr::video::ITexture* texture, const irr::core::rect& sourceRect, const irr::core::rect* clipRect, 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, const irr::core::rect& destRect,