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

Author Topic: Rotate images while batching  (Read 3495 times)

0 Members and 1 Guest are viewing this topic.

Chris12

  • Newbie
  • *
  • Posts: 20
    • View Profile
Rotate images while batching
« on: March 02, 2012, 04:10:33 pm »
Hi,

im using the new vertexarray introduced in SFML2 to batch all the tiles from my tile map.
I add all the quads to the vertex array and set the texture coordinates accordingly.
This works good.

I can rotate the tiles in steps of 90°, or mirror them by swapping the texture coordinates.
But how can I rotate the image by an arbitrary value ? For example 15°

Do I have to rotate my vertices by the required value myself ?
Applying a series of trigonometric functions to every vertex doesnt seem to be the correct way, because its relatively expensive to do those calculations every frame.
Can I somehow utilize the GPU to do that work for me ?
If so, how ?

I know I can use Sprite and set its Rotation to the desired value, but will using "Sprite" still make use of batching ?? I think not, but correct me if I'm wrong

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Rotate images while batching
« Reply #1 on: March 02, 2012, 05:01:58 pm »
Quote
Applying a series of trigonometric functions to every vertex doesnt seem to be the correct way, because its relatively expensive to do those calculations every frame.

Is it really too slow? How many tiles to you need to rotate? What code do you use to transform the vertices?

Arbitrary rotations doesn't seem to be compatible with the concept of a tile map (which is a grid), can you explain what you do exactly?
Laurent Gomila - SFML developer

Chris12

  • Newbie
  • *
  • Posts: 20
    • View Profile
Rotate images while batching
« Reply #2 on: March 02, 2012, 05:28:02 pm »
I didn't make myself clear sorry.
I used a tile map only as an example for rotations of 90 degrees.
You are right it doesnt make any sense to rotate tiles by steps other than 90°.

I want arbitrary rotation for something else.
For my particle system I need to rotate the particles arbitrarily.

Currently I'm taking the angle and get the sine and cosine value of it to offset the position of each vertex.

That works, but I wonder if it isn't possible to just give the angle to the GPU, and let the it to the trigonometric functions while its rendering.

Because one call to Math.Cos is nearly as slow as a Dictionary lookup...

Without batching performance is horrible, but thanks to the new access to vertexarrays stuff became MUCH faster.

After the number of drawcalls (that problem is resolved thanks to vertex arrays), the transformation / rotation seems to be the biggest resource hog.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Rotate images while batching
« Reply #3 on: March 02, 2012, 06:28:50 pm »
You didn't answer my main question:
Quote
Is it really too slow?


But anyway, rotating the vertices will still be the best solution. Especially if the heavy operation is the Math.Cos call: it would still be computed on the CPU with individual sprites, the GPU can only apply the resulting transform to the vertices. It will not compute the cosine for you.
Laurent Gomila - SFML developer

Chris12

  • Newbie
  • *
  • Posts: 20
    • View Profile
Rotate images while batching
« Reply #4 on: March 02, 2012, 07:38:24 pm »
Ah, sorry.
Yes it is a bit slow. I tested it without calculating Cos/Sin and performance increases by 10%.

Quote

It will not compute the cosine for you.

Some NVidia GPUs can calculate both sine and cosine of an angle in one single hardware tick. "__sincosf"

Shaders (min SM1.1) can compute cos and sin too, but I don't know if it's as fast.

Can't I use that somehow ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Rotate images while batching
« Reply #5 on: March 02, 2012, 09:06:54 pm »
Yeah, shaders can do it of course. But it's not suitable in this case.
Laurent Gomila - SFML developer

 

anything