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

Author Topic: Help with vertex arrays!  (Read 5211 times)

0 Members and 2 Guests are viewing this topic.

cooldog99

  • Jr. Member
  • **
  • Posts: 95
    • View Profile
    • Perilous Game Studios
Help with vertex arrays!
« on: February 15, 2012, 05:22:51 pm »
Since laurent got rid of the old shape styles and added vertex arrays (not saying this is bad, but it did help)

I've had an issue with trying to make shapes that aren't quads.

basically if I try to do anything above 3(tri), or 4(quad) vertices it gets screwy.

I have a "DrawHexagon" function that draws 6 vertices successfully, as a triangle fan, however the first vertice of this is always 0,0 with a white color.
all others are fine.
here is code

Code: [Select]
void DrawHexagon(float X, float Y, float W, float H, sf::Color Col){
sf::VertexArray Hex(sf::TrianglesFan, 6);

float HalfWidth = W / 2;
float HalfHeight = H / 2;
Hex.Append(sf::Vertex(sf::Vector2f(X, Y + HalfHeight), Col));
Hex.Append(sf::Vertex(sf::Vector2f(X + (HalfWidth / 2), Y), Col));
Hex.Append(sf::Vertex(sf::Vector2f(X + HalfWidth + (HalfWidth / 2), Y), Col));
Hex.Append(sf::Vertex(sf::Vector2f(X + W, Y + HalfHeight), Col));
Hex.Append(sf::Vertex(sf::Vector2f(X + HalfWidth + (HalfWidth / 2), Y + H), Col));
Hex.Append(sf::Vertex(sf::Vector2f(X + (HalfWidth / 2), Y + H), Col));
App.Draw(Hex);
}


Is there something wrong? O_o
Am I using wrong primitive type? :?
Why is there no Polygon primitive type, like openGL?
I'm a noob to vertex's :/

I'm trying to make a bunch of custom shape functions for a new game we're developing  :wink:

So much new stuff lately in sfml2, i'm trying to keep up!
I feel dumb, can anyone help? :(

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Help with vertex arrays!
« Reply #1 on: February 15, 2012, 05:30:29 pm »
Shapes still exist. For convex polygons you should take a look here.

I can't help you with your problem though, because I never used the vertex array.
TGUI: C++ SFML GUI

cooldog99

  • Jr. Member
  • **
  • Posts: 95
    • View Profile
    • Perilous Game Studios
Help with vertex arrays!
« Reply #2 on: February 15, 2012, 08:16:44 pm »
Quote from: "texus"
Shapes still exist. For convex polygons you should take a look here.

I can't help you with your problem though, because I never used the vertex array.


That does help for what I need, thanks.

But i'd still like some info on vertex arrays :)

also, is there a crash course on using shaders in sfml I can find?
not just implementing, but writing the actual shader too?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Help with vertex arrays!
« Reply #3 on: February 15, 2012, 08:42:24 pm »
You create your vertex array with an initial number of 6 points, but with Append you add other points instead of filling the 6 initial points. So you end up with 12 points: 6 empty + 6 of your hexagon.

So either you set an initial number of points and use operator[] to access them, or you only use Append.

By the way, your function would be much more efficient (no dynamic allocation/deallocation) with a fixed-size array of vertices allocated on the stack:
Code: [Select]
void DrawHexagon(float X, float Y, float W, float H, sf::Color Col)
{
   float HalfWidth = W / 2;
   float HalfHeight = H / 2;
   sf::Vertex Hex[6] =
   {
      sf::Vertex(sf::Vector2f(X, Y + HalfHeight), Col),
      sf::Vertex(sf::Vector2f(X + (HalfWidth / 2), Y), Col),
      sf::Vertex(sf::Vector2f(X + HalfWidth + (HalfWidth / 2), Y), Col),
      sf::Vertex(sf::Vector2f(X + W, Y + HalfHeight), Col),
      sf::Vertex(sf::Vector2f(X + HalfWidth + (HalfWidth / 2), Y + H), Col),
      sf::Vertex(sf::Vector2f(X + (HalfWidth / 2), Y + H), Col)
   }
   App.Draw(Hex, 6, sf::TrianglesFan);
}


sf::VertexArray is really just a std::vector<sf::Vertex>. Don't overuse it.
Laurent Gomila - SFML developer

cooldog99

  • Jr. Member
  • **
  • Posts: 95
    • View Profile
    • Perilous Game Studios
Help with vertex arrays!
« Reply #4 on: February 15, 2012, 09:08:00 pm »
Quote from: "Laurent"
You create your vertex array with an initial number of 6 points, but with Append you add other points instead of filling the 6 initial points. So you end up with 12 points: 6 empty + 6 of your hexagon.

So either you set an initial number of points and use operator[] to access them, or you only use Append.

By the way, your function would be much more efficient (no dynamic allocation/deallocation) with a fixed-size array of vertices allocated on the stack:
Code: [Select]
void DrawHexagon(float X, float Y, float W, float H, sf::Color Col)
{
   float HalfWidth = W / 2;
   float HalfHeight = H / 2;
   sf::Vertex Hex[6] =
   {
      sf::Vertex(sf::Vector2f(X, Y + HalfHeight), Col),
      sf::Vertex(sf::Vector2f(X + (HalfWidth / 2), Y), Col),
      sf::Vertex(sf::Vector2f(X + HalfWidth + (HalfWidth / 2), Y), Col),
      sf::Vertex(sf::Vector2f(X + W, Y + HalfHeight), Col),
      sf::Vertex(sf::Vector2f(X + HalfWidth + (HalfWidth / 2), Y + H), Col),
      sf::Vertex(sf::Vector2f(X + (HalfWidth / 2), Y + H), Col)
   }
   App.Draw(Hex, 6, sf::TrianglesFan);
}


sf::VertexArray is really just a std::vector<sf::Vertex>. Don't overuse it.


Ah, that makes sense  :wink:

thanks.

is there a detailed shader documentation anywhere? for the newest builds?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Help with vertex arrays!
« Reply #5 on: February 15, 2012, 10:22:43 pm »
Quote
is there a detailed shader documentation anywhere? for the newest builds?

Not yet. But the old tutorial + the 2.0 doc + a GLSL reference should be enough.
Laurent Gomila - SFML developer

 

anything