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

Author Topic: How to draw a *single* vertex ?  (Read 14833 times)

0 Members and 1 Guest are viewing this topic.

ThomasAn.

  • Newbie
  • *
  • Posts: 18
    • View Profile
How to draw a *single* vertex ?
« on: May 18, 2013, 08:41:53 am »
I see a lot of documentation about vertex arrays ... but how can we draw a *single* vertex ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to draw a *single* vertex ?
« Reply #1 on: May 18, 2013, 08:48:28 am »
In the tutorial there's an example that shows how to draw two vertices. From there it's easy to deduce the code to draw one ;)

sf::Vertex v;
window.draw(&v, 1, sf::Points);

But I can't imagine why you would want to draw a single vertex.
Laurent Gomila - SFML developer

ThomasAn.

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to draw a *single* vertex ?
« Reply #2 on: May 18, 2013, 09:18:13 am »
Thank you. I will try that.

Reason being, I have a simple particle system that displays either points or circles (or other shapes). So I already have a defined array of single entities and it seems like a waste having to copy an existing array to a vertex array when points are to be shown.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to draw a *single* vertex ?
« Reply #3 on: May 18, 2013, 09:22:09 am »
Are you saying that you will draw each particle individually?

Expect the worst performances ever if you do so.
Laurent Gomila - SFML developer

ThomasAn.

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to draw a *single* vertex ?
« Reply #4 on: May 18, 2013, 09:27:18 am »
The worst performance ... is that due to repeated calls to Window.draw(....) ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to draw a *single* vertex ?
« Reply #5 on: May 18, 2013, 09:37:10 am »
Yes.

Vertex arrays were added exactly for this purpose: allowing to group multiple vertices into a single entity, which can be drawn with a single call.

There's an example of a particle system that draws particles as single points in the tutorial, can't you use a similar approach?
Laurent Gomila - SFML developer

ThomasAn.

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to draw a *single* vertex ?
« Reply #6 on: May 18, 2013, 10:41:11 am »
Thank you for this tip! I will modify my design to make use of vertex arrays and reduce calls to "draw".

ThomasAn.

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to draw a *single* vertex ?
« Reply #7 on: May 18, 2013, 11:15:26 am »
What would be the optimum way to handle a particle system wherein each particle has more properties than merely position, or texture (in my case there is a number of other properties; elasticity, surf friction, magnetic coef, mass, etc etc)

Should it be an array of class objects for the properties and a separate vertexArray ? ... and then try to keep their indices synchronized ? (It seems to me if I add a single vertex as a member within the particle class-object, then I'd have to iterate through the class-object array to create a new vertex array for drawing ... which likely creates memory overhead and too much allocation/deallocation, I think.)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to draw a *single* vertex ?
« Reply #8 on: May 18, 2013, 12:04:44 pm »
Should it be an array of class objects for the properties and a separate vertexArray ?
Exactly. You could look at the implementation of my thor::ParticleSystem here.

What I do, is rebuilding the whole vertex array everytime it needs to change. I don't think you get better performance when you remove or insert single vertices every time a particle disappears or appears, since std::vector (the data structure underlying sf::VertexArray) is rather merciless with respect to those operations.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

ThomasAn.

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to draw a *single* vertex ?
« Reply #9 on: May 19, 2013, 03:41:32 am »
There's an example of a particle system that draws particles as single points in the tutorial, can't you use a similar approach?

Laurent, I read your example and I feel I understand your approach on that simple particle system.

... the question now becomes is it possible to have a particle system where each particle is a simple shape (square, circle, triangle etc) ? Sounds like a vertexArray of vertexArrays.

As such, is it possible to have a single call to window.draw(....) that puts a cluster of circles on screen ? (assuming we want to do this with actual vertex geometry instead of using textures that represent circles or sprites)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to draw a *single* vertex ?
« Reply #10 on: May 19, 2013, 08:28:25 am »
Yes you can, but then you will have to use sf::Triangles, which requires many more vertices than sf::TriangleFan for a circle (but only allow to define a single shape). This should be a big problem though.

But I strongly suggest that you use a texture instead. Especially for circles, which require a lot of vertices to look good.
Laurent Gomila - SFML developer

ThomasAn.

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to draw a *single* vertex ?
« Reply #11 on: May 28, 2013, 08:18:48 pm »
Speaking of textures ... I don't see any examples about texturing a single vertex as an independent particle.
So far I know how to set the coordinates of displaying a texture on a vertex, but the method of actually showing a texture at those coordinates is rather mystical to me.

I see for vertex arrays, the texture is displayed from the Window.draw(vertices, &texture); but that would not work for particle vertices each having their own texture.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to draw a *single* vertex ?
« Reply #12 on: May 28, 2013, 08:57:36 pm »
Why would you texture a single vertex? It's just a single pixel.

And why would each particle have a different texture?
Laurent Gomila - SFML developer

ThomasAn.

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to draw a *single* vertex ?
« Reply #13 on: May 28, 2013, 09:04:09 pm »
I am going for something like this:


Being able to assign a changing texture to each particle depending on certain conditions of speed, or pressure... all without sacrificing performance (if possible). I just need to display a 20x20 texture (out of a pool of textures) by its middle assigned to each particle
« Last Edit: May 28, 2013, 09:09:24 pm by ThomasAn. »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to draw a *single* vertex ?
« Reply #14 on: May 28, 2013, 09:08:49 pm »
These particles are not points (single vertices), they are circles. And in this video only the color changes, so you can use the same white circle texture for all the particles, and only change their color.
Laurent Gomila - SFML developer