Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Rotate sf::Texture with GLSL Shader  (Read 1952 times)

0 Members and 1 Guest are viewing this topic.

CaffeineAddict

  • Newbie
  • *
  • Posts: 2
    • View Profile
Rotate sf::Texture with GLSL Shader
« on: October 26, 2013, 05:28:26 pm »
Hello, I am currently using the SFML library for a project of mine (a small 2D game engine) and I've run into the following problem. I am trying to create a brush system where the level designer is able to assemble geometry based on convex shapes (think Hammer editor). Part of that system is texturing. The texture can be moved, scaled and rotated to provide a way for creating impressive geometry and objects without external tools. The SFML part of the brush is represented by a sf::ConvexShape and the texture by a sf::Texture. Scaling and offsetting the texture can be done by modifying the texture rectangle, but rotation can't, since sf::Rect<T> is axis aligned.
This is what I'm trying to do:



I've had multiple ideas how to work around this. One of them was to load an sf::Image first, apply a rotation algorithm to it and then create 360 instances of the texture, for one degree each. I quickly discarded this approach since it's ugly, slow and wastefull of resources. However, I came up with another possible solution: Shaders! I have very little experience with graphics programming and have therefore never written a shader by myself. I've worked with the UDK a while back, which provides a node editor that generates HLSL code. Using this editor it was trivial to rotate materials, even in real time. Can someone help me how to archive this using SFML and GLSL? I literally have no idea where to start. For instance, should this be done in the vertex or in the fragment shader? I really don't a a clue how to proceed. Usually I would tackle this problem by learning more about the issue at hand (i.e. how to write shaders), but I am on a very tight deadline since this is a graduation project. I would appreciate if someone could help me out, or at least give me a pointer how to archive my goal.

Thanks for the help.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Rotate sf::Texture with GLSL Shader
« Reply #1 on: October 26, 2013, 07:43:42 pm »
Quote
but rotation can't, since sf::Rect<T> is axis aligned.
Just use a vertex array instead of a sf::ConvexShape, to get rid of the axis-aligned texture rect limitation.
Laurent Gomila - SFML developer

CaffeineAddict

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Rotate sf::Texture with GLSL Shader
« Reply #2 on: October 26, 2013, 08:03:30 pm »
I was planing on moving to vertex arrays for optimization reasons anyways. Thanks for the tip.