### Author Topic: Vertex Arrays - How to draw multiple shapes with 1 call to draw?  (Read 396 times)

0 Members and 1 Guest are viewing this topic.

#### JohnD

• Newbie
• Posts: 5
##### Vertex Arrays - How to draw multiple shapes with 1 call to draw?
« on: September 17, 2019, 06:55:58 am »
I've been looking at vertex arrays and I just can't wrap my head around how to draw multiple vertex arrays with just a single call to the "draw" function? Am I even saying that right? I've looked at a ton of guides and tutorials but they all seem to be just teaching me how to draw a single line, triangle or rectangle. I have seen tilemap sample codes but with little to no explanation to what's going on.

I'm sorry I don't have a sample code because I don't even know where to begin. Basically, I want to draw 2 or more triangles with differing sizes and textures. I feel like if I just have an example of drawing two triangles using vertex array using 1 instance of window.draw, I'd be ok. Any clarification to my confusion would be very much appreciated.

#### Laurent

• Hero Member
• Posts: 32397
##### Re: Vertex Arrays - How to draw multiple shapes with 1 call to draw?
« Reply #1 on: September 17, 2019, 07:49:17 am »
sf::VertexArray triangles(sf::Triangles);

// first triangle
triangles.append(sf::Vertex(...));
triangles.append(sf::Vertex(...));
triangles.append(sf::Vertex(...));

// second triangle
triangles.append(sf::Vertex(...));
triangles.append(sf::Vertex(...));
triangles.append(sf::Vertex(...));

// etc.

window.draw(triangles);

You won't be able to use multiple textures with a single draw call, though.
Laurent Gomila - SFML developer

#### JohnD

• Newbie
• Posts: 5
##### Re: Vertex Arrays - How to draw multiple shapes with 1 call to draw?
« Reply #2 on: September 18, 2019, 03:47:55 am »
Cool thanks! I now have a clear understanding of how this works! So a vertex array is basically an array of vertices (duh) drawn as a series of triangles (or whatever shape I choose). So I guess I'm limited to one type of shape for each vertexarray but does it also limit me to one texture? Can I not set texCoords for each triangle added via "append"? I'll go try these things out. In any case my problem's solved! Cheers!

#### Laurent

• Hero Member
• Posts: 32397
##### Re: Vertex Arrays - How to draw multiple shapes with 1 call to draw?
« Reply #3 on: September 18, 2019, 08:16:51 am »
Quote
does it also limit me to one texture?
Yes, SFML doesn't support multi-texturing in that case, so you'll need one draw call for each texture.

Quote
Can I not set texCoords for each triangle added via "append"?
Yes of course you can. But that will still be coordinates in the same texture.

A typical optimization is to use/build texture atlases: multiple small images inside a single big texture, and then play with texture coordinates to assign one or the other to your shapes/sprites.
Laurent Gomila - SFML developer

#### JohnD

• Newbie
• Posts: 5
##### Re: Vertex Arrays - How to draw multiple shapes with 1 call to draw?
« Reply #4 on: September 20, 2019, 02:34:02 am »
Yes of course you can. But that will still be coordinates in the same texture.

My bad! When I said differing textures, I meant "graphic" textures. So yep, giving my triangles different images from one big texture works.

anything