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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Garwin

Pages: 1 2 3 [4]
46
Graphics / Re: View zoom and text
« on: April 10, 2023, 12:56:34 am »
I used this for CircleShape. It works fine.

sf::Vector2f getPositionForText (const sf::CircleShape& shape,
                                                  const sf::RenderWindow& window,
                                                  const sf::View& viewFrom,
                                                  const sf::View& viewTo)
{
    float radius = shape.getRadius();
    sf::Vector2f center {shape.getPosition().x, shape.getPosition().y};
    sf::Vector2i positionInWindow = window.mapCoordsToPixel(center,viewFrom);
    center = window.mapPixelToCoords (positionInWindow, viewTo);
    center += sf::Vector2f {-radius * shape.getScale().x, -radius * shape.getScale().y}
                         * (viewTo.getSize().x / viewFrom.getSize().x);
    return center;
}

// and calling this function

text3.setPosition(getPositionForText(entity2, window, mainView, textView)
     + sf::Vector2f{0.f, -textSpaceHeight}); // text offset
 

Thinking about doing similar for any sf::Shape or VertexArray. I am thinking about 2 ways:
1. through bounding box
- getting sf::FloatRect bounding box of the entity
- transform between views using sf::Transform
- getting a new bounding box from a transformed bounding box
Advantages: relatively simple and probably reasonably fast
Disadvantages: position of text will be simplified as in the bounding box from the transformed bounding box could be large, especially if the transformed bounding box is at 45°.
2. through transforming entity
- copy entity
- transform entity between views
- finding the bounding box from the transformed entity
Advantages: Much more precise as the bounding box is found on top of transformed entity
Disadvantages: If there are many text labels over an entity it practically make no sense for the view and all these entities need to be copied transformed one by one

47
Graphics / Re: View zoom and text
« on: April 07, 2023, 02:40:42 pm »
Personally, I'd probably go with a separate view and calculate the position and size of each text to be perfectly matching the objects in the map. It gives complete control over the sizing of the text, and you don't have to adjust for world zoom/rotation etc.
Plus it also allows you to focus on texts that are currently visible.

I am trying to prototype this world's behavior. The moving entities work fine and the text is as it should be - placed over the top left corner with offset relating to the text size.

sf::Vector2f transformText (const sf::Vector2f& position, const sf::RenderWindow& window, const sf::View& viewFrom, const sf::View& viewTo)
{
    sf::Vector2i positionInWindow = window.mapCoordsToPixel (position, viewFrom);
    return window.mapPixelToCoords (positionInWindow, viewTo);
}

text2.setPosition(transformText(entity2.getPosition() + textOffset, window, mainView, textView));
 

When I tried to start zooming and rotating the view, the text follow the entity's left corner, however, as the entity is rotating the top left corner is not the top left anymore so with 180° rotation the text is under the entity.

I understand that I need to take the entity to apply the transformation of the view and get the bounding box of the entity and use the top left corner as a place for the text before offset.

Can SFML help me with that? The function transformText transforms a single coordinate but I need the whole entity to get global bounds. Can I achieve that without making a shadow copy of the entity to just have this entity with view transformation to achieve global bounds and that top left corner?

note: the whole prototype is in the attachment


48
Graphics / View zoom and text
« on: April 05, 2023, 10:36:08 pm »
I would like to create world map which has a lot of labels. This map can be rotated, moved and zoomed and for that view is perfect. However these labels should not be zoomed.

What can be best solution with SFML?

I am thinking to have the second view for these labels only. However it means, that view cannot use zoom. That zoom must be transferred to move relating to the position of center of the view and applied individually to the texts and view applied on the text can use same moving as world map view and rotation skipped.

Another idea is to use transform matrix on position of object with world map view to get pixel on screen and than render text on that position.

Or is there another better way to do it?


49
Window / Re: Precomputing and storing render-information efficiently
« on: April 05, 2023, 11:02:31 am »
How do you create and store these particles? What container do you use?
It seems to me quite strange that for just several thousand of particles, it is slow.

With 60 FPS you have about 17 ms for one frame so updating 10,000 particles one by one means about a little more than 1 microsecond for updating one particle - this is quite a huge time for that if particles are stored in the contiguous memory block.

50
Graphics / Re: VertexArray and TriangleFans
« on: August 02, 2022, 08:13:30 am »
Thanks a lot, now I understand and you confirm me, what I have fought, only primitives points, lines and triangles can have multiple ones in a single array, otherwise it needs to have

I have already implemented it through triangles, it is not so difficult. I have better performance than using sf::CircleShape but still will need some optimization. I would probably try std container (deque) to erase already faded triangles from the container so fewer triangles need to be managed.

51
Graphics / Re: VertexArray and TriangleFans
« on: August 02, 2022, 07:24:40 am »
Yes, you can just use a std::vector<sf::Vertex> and draw that to the window.
However you can't mix the primitive type per vertex "array", so it would need to all be triangle fans.

See also: https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php#primitive-types

This I have already tried and I have no issue drawing a single triangle fan however I cannot draw more triangle fans with one vertex array.

I have a triangle fan with 10 points (1 center, 8 points for the circle and 1 additional point at end of the circle).

I append vertex array to include several triangle fans but when I tried to draw it, it does draw only the first one.

m_game->m_window.draw(&m_tail[0], m_pointCount, sf::TriangleFan);

m_pointCount is 10 (one single triangle fan)

This just does not work (m_tail has dozens of points as it has dozens of triangle fans).


52
Graphics / VertexArray and TriangleFans
« on: August 01, 2022, 10:59:27 pm »
I am trying to do fireworks.
My idea is to draw circle shapes even on tails. However this means just too many shapes and too many drawing calls that sometimes fps drops just too low.

So I was thinking using vertex arrays and triangle fans so using 1 call will draw me firework with the whole tail.
note: I do not want to use lines or rectangles as I cannot get gradient I want.

Is it possible to use vertex array (or own container as deque or vector) for triangle fans and if so how it can  be done, how can I tell how many points is single triangle fan and how many it should draw from the array.

So far it seems to me that it is possible only for same primitives as points, lines and triangles or is there a way to achive it with more complex primitives?

Pages: 1 2 3 [4]
anything