Remove rotation uniform in gl shaders

This commit is contained in:
Benau 2023-01-08 08:08:35 +08:00
parent 9dd9363ab4
commit e798c1abee
7 changed files with 32 additions and 71 deletions

View File

@ -19,10 +19,7 @@ out vec4 color;
void main() void main()
{ {
float s = sin(rotation);
float c = cos(rotation);
mat2 m = mat2(c, -s, s, c);
color = Color.zyxw; color = Color.zyxw;
uv = Texcoord * texsize + texcenter; uv = Texcoord * texsize + texcenter;
gl_Position = vec4(m * Position * size + center, 0., 1.); gl_Position = vec4(Position * size + center, 0., 1.);
} }

View File

@ -2,7 +2,6 @@ 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,9 +14,6 @@ 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(m * Position * size + center, 0., 1.) ; gl_Position = vec4(Position * size + center, 0., 1.) ;
} }

View File

@ -1,10 +0,0 @@
uniform sampler2D tex;
uniform float custom_alpha;
in vec2 uv;
out vec4 FragColor;
void main()
{
FragColor = texture(tex, uv) * custom_alpha;
}

View File

@ -63,38 +63,19 @@ 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", "rotation"); assignUniforms("center", "size", "texcenter", "texsize");
assignSamplerNames(0, "tex", ST_BILINEAR_CLAMPED_FILTERED); assignSamplerNames(0, "tex", ST_BILINEAR_CLAMPED_FILTERED);
} // TextureRectShader } // TextureRectShader
}; // TextureRectShader }; // TextureRectShader
// ============================================================================
class TextureRectCustomAlphaShader : public TextureShader<TextureRectCustomAlphaShader, 1,
core::vector2df, core::vector2df,
core::vector2df, core::vector2df,
float, float>
{
public:
TextureRectCustomAlphaShader()
{
loadProgram(OBJECT, GL_VERTEX_SHADER, "texturedquad.vert",
GL_FRAGMENT_SHADER, "texturedquad_custom_alpha.frag");
assignUniforms("center", "size", "texcenter", "texsize", "rotation",
"custom_alpha");
assignSamplerNames(0, "tex", ST_BILINEAR_CLAMPED_FILTERED);
} // TextureRectCustomAlphaShader
}; // TextureRectCustomAlphaShader
// ============================================================================ // ============================================================================
class ColoredRectShader : public Shader<ColoredRectShader, core::vector2df, class ColoredRectShader : public Shader<ColoredRectShader, core::vector2df,
core::vector2df, video::SColor> core::vector2df, video::SColor>
@ -112,7 +93,7 @@ public:
class ColoredTextureRectShader : public TextureShader<ColoredTextureRectShader, 1, class ColoredTextureRectShader : public TextureShader<ColoredTextureRectShader, 1,
core::vector2df, core::vector2df, core::vector2df, core::vector2df,
core::vector2df, core::vector2df, float> core::vector2df, core::vector2df>
{ {
public: public:
GLuint m_color_vbo; GLuint m_color_vbo;
@ -122,7 +103,7 @@ public:
{ {
loadProgram(OBJECT, GL_VERTEX_SHADER, "colortexturedquad.vert", loadProgram(OBJECT, GL_VERTEX_SHADER, "colortexturedquad.vert",
GL_FRAGMENT_SHADER, "colortexturedquad.frag"); GL_FRAGMENT_SHADER, "colortexturedquad.frag");
assignUniforms("center", "size", "texcenter", "texsize", "rotation"); assignUniforms("center", "size", "texcenter", "texsize");
assignSamplerNames(0, "tex", ST_BILINEAR_FILTERED); assignSamplerNames(0, "tex", ST_BILINEAR_FILTERED);
@ -153,7 +134,7 @@ static void drawTexColoredQuad(const video::ITexture *texture,
float height, float center_pos_x, float height, float center_pos_x,
float center_pos_y, float tex_center_pos_x, float center_pos_y, float tex_center_pos_x,
float tex_center_pos_y, float tex_width, float tex_center_pos_y, float tex_width,
float tex_height, float rotation) float tex_height)
{ {
glBindVertexArray(ColoredTextureRectShader::getInstance()->m_vao); glBindVertexArray(ColoredTextureRectShader::getInstance()->m_vao);
glBindBuffer(GL_ARRAY_BUFFER, glBindBuffer(GL_ARRAY_BUFFER,
@ -169,7 +150,7 @@ static void drawTexColoredQuad(const video::ITexture *texture,
->setUniforms(core::vector2df(center_pos_x, center_pos_y), ->setUniforms(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), rotation); core::vector2df(tex_width, tex_height));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
@ -183,7 +164,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 rotation) float tex_width, float tex_height)
{ {
TextureRectShader::getInstance()->use(); TextureRectShader::getInstance()->use();
glBindVertexArray(SharedGPUObjects::getUI_VAO()); glBindVertexArray(SharedGPUObjects::getUI_VAO());
@ -193,7 +174,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), rotation); core::vector2df(tex_width, tex_height));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0); glBindVertexArray(0);
@ -358,7 +339,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, float rotation) bool draw_translucently)
{ {
video::SColor translucence[4] = video::SColor translucence[4] =
{ {
@ -414,13 +395,13 @@ void draw2DImage(const video::ITexture* texture,
drawTexColoredQuad(texture, draw_translucently ? translucence : colors, drawTexColoredQuad(texture, draw_translucently ? translucence : colors,
width, height, center_pos_x, width, height, center_pos_x,
center_pos_y, tex_center_pos_x, tex_center_pos_y, center_pos_y, tex_center_pos_x, tex_center_pos_y,
tex_width, tex_height, rotation); tex_width, tex_height);
} }
else else
{ {
drawTexQuad(texture->getTextureHandler(), width, height, drawTexQuad(texture->getTextureHandler(), 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, rotation); tex_center_pos_y, tex_width, tex_height);
} }
if (clip_rect) if (clip_rect)
glDisable(GL_SCISSOR_TEST); glDisable(GL_SCISSOR_TEST);
@ -430,15 +411,12 @@ void draw2DImage(const video::ITexture* texture,
} // draw2DImage } // draw2DImage
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void draw2DImageCustomAlpha(video::ITexture* texture, void draw2DImageRotationColor(video::ITexture* texture,
const irr::core::rect<irr::s32>& destRect, const irr::core::rect<irr::s32>& destRect,
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,
float rotation, float custom_alpha) float rotation, const irr::video::SColor& color)
{ {
video::SColor color((unsigned)-1);
color.setAlpha(core::clamp((int)(custom_alpha * 255.0f), 0, 255));
const core::dimension2d<u32>& ss = texture->getSize(); const core::dimension2d<u32>& ss = texture->getSize();
core::rect<f32> tcoords; core::rect<f32> tcoords;
tcoords.UpperLeftCorner.X = (f32)sourceRect.UpperLeftCorner.X / (f32)ss.Width; tcoords.UpperLeftCorner.X = (f32)sourceRect.UpperLeftCorner.X / (f32)ss.Width;
@ -505,7 +483,7 @@ void draw2DImageCustomAlpha(video::ITexture* texture,
} }
draw2DVertexPrimitiveList(texture, vtx, 4, indices, 2, video::EVT_STANDARD, draw2DVertexPrimitiveList(texture, vtx, 4, indices, 2, video::EVT_STANDARD,
scene::EPT_TRIANGLES, video::EIT_16BIT); scene::EPT_TRIANGLES, video::EIT_16BIT);
} // draw2DImageCustomAlpha } // draw2DImageRotationColor
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void draw2DVertexPrimitiveList(video::ITexture *tex, const void* vertices, void draw2DVertexPrimitiveList(video::ITexture *tex, const void* vertices,

View File

@ -49,14 +49,13 @@ 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 draw2DImageCustomAlpha(irr::video::ITexture* texture, void draw2DImageRotationColor(irr::video::ITexture* texture,
const irr::core::rect<irr::s32>& destRect, const irr::core::rect<irr::s32>& destRect,
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,
float rotation, float custom_alpha); float rotation, const irr::video::SColor& color);
void draw2DVertexPrimitiveList(irr::video::ITexture *t, const void* vertices, void draw2DVertexPrimitiveList(irr::video::ITexture *t, const void* vertices,
irr::u32 vertexCount, const void* indexList, irr::u32 vertexCount, const void* indexList,

View File

@ -717,7 +717,7 @@ void RaceGUI::drawGlobalMiniMap()
rotation = rotation + M_PI; rotation = rotation + M_PI;
} }
rotation = -1.0f * rotation + 0.25f * M_PI; // icons-frame_arrow.png was rotated by 45 degrees rotation = -1.0f * rotation + 0.25f * M_PI; // icons-frame_arrow.png was rotated by 45 degrees
draw2DImage(m_icons_frame, position, rect, NULL, colors, true, false, rotation); draw2DImageRotationColor(m_icons_frame, position, rect, NULL, rotation, color);
} }
else else
{ {

View File

@ -378,11 +378,11 @@ void RaceGUIMultitouch::draw(const AbstractKart* kart,
if (button->type == MultitouchButtonType::BUTTON_STEERING) if (button->type == MultitouchButtonType::BUTTON_STEERING)
{ {
video::SColor color((unsigned)-1);
video::ITexture* btn_texture = m_steering_wheel_tex; video::ITexture* btn_texture = m_steering_wheel_tex;
core::rect<s32> coords(pos_zero, btn_texture->getSize()); core::rect<s32> coords(pos_zero, btn_texture->getSize());
draw2DImage(btn_texture, btn_pos, coords, NULL, NULL, true, false/*draw_translucently*/, draw2DImageRotationColor(btn_texture, btn_pos, coords, NULL,
(button->axis_y >= 0 ? -1 : 1) * button->axis_x); (button->axis_y >= 0 ? -1 : 1) * button->axis_x, color);
#ifndef SERVER_ONLY
AbstractKart* k = NULL; AbstractKart* k = NULL;
Camera* c = Camera::getActiveCamera(); Camera* c = Camera::getActiveCamera();
if (c) if (c)
@ -391,12 +391,13 @@ void RaceGUIMultitouch::draw(const AbstractKart* kart,
{ {
float accel = k->getControls().getAccel(); float accel = k->getControls().getAccel();
core::rect<s32> mask_coords(pos_zero, m_steering_wheel_tex_mask_up->getSize()); core::rect<s32> mask_coords(pos_zero, m_steering_wheel_tex_mask_up->getSize());
draw2DImageCustomAlpha(m_steering_wheel_tex_mask_up, btn_pos, mask_coords, NULL, color.setAlpha(core::clamp((int)(accel >= 0.0f ? accel * 128.0f : 0), 0, 255));
(button->axis_y >= 0 ? -1 : 1) * button->axis_x, accel >= 0.0f ? accel * 0.5f : 0.0f); draw2DImageRotationColor(m_steering_wheel_tex_mask_up, btn_pos, mask_coords, NULL,
draw2DImageCustomAlpha(m_steering_wheel_tex_mask_down, btn_pos, mask_coords, NULL, (button->axis_y >= 0 ? -1 : 1) * button->axis_x, color);
(button->axis_y >= 0 ? -1 : 1) * button->axis_x, k->getControls().getBrake() ? 0.5f : 0.0f); color.setAlpha(k->getControls().getBrake() ? 128 : 0);
draw2DImageRotationColor(m_steering_wheel_tex_mask_down, btn_pos, mask_coords, NULL,
(button->axis_y >= 0 ? -1 : 1) * button->axis_x, color);
} }
#endif
// float x = (float)(button->x) + (float)(button->width) / 2.0f * // float x = (float)(button->x) + (float)(button->width) / 2.0f *
// (button->axis_x + 1.0f); // (button->axis_x + 1.0f);
// float y = (float)(button->y) + (float)(button->height) / 2.0f * // float y = (float)(button->y) + (float)(button->height) / 2.0f *