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

Author Topic: sf::VertexArray: Help Generating Verticies of This Object  (Read 1937 times)

0 Members and 1 Guest are viewing this topic.

JGrieco42

  • Newbie
  • *
  • Posts: 6
    • View Profile
sf::VertexArray: Help Generating Verticies of This Object
« on: August 09, 2014, 02:06:55 am »
I'm trying to create a light source like in this image:


I see the implementation looking something like this, but I'm stuck as to how to achieve the radius fall-off effect.  Generating a full circle is a trivial case, the case where the angle is not a full circle is interesting, and to be clear, the radius fall-off is the most interesting part of this problem.  I'm looking ideally to use a for loop and create this look for an arbitrarily-sized vertex array, thanks.

struct Light : public sf::Drawable {
        Light( );


        sf::Vector2f    position;       // center of the triangle fan
        sf::Vector2f    direction;      // values clamped [-1, 1]
        float                   radius;
        float                   angle;          // equals pi radians in the image
        sf::Color               color;
        sf::VertexArray verticies;      // TriangleFan

        void                    draw( sf::RenderTarget& target, sf::RenderStates states ) const final override { target.draw( verticies, states ); }
        void                    calculateVerticies( );
};

void Light::calculateVerticies( ) {
        // amazing code, wow so good
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: sf::VertexArray: Help Generating Verticies of This Object
« Reply #1 on: August 10, 2014, 09:54:26 pm »
If the radius and general shape doesn't change, you could also draw it up and save as an image, from there you could rotate and alpha blend it with a half transparent render texture of the size of your game screen.

If needs to be dynamic, I suggest to sit down with paper, pen and maybe some math formula booklet and think about it. The math shouldn't be extremely complicated. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

docwild

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: sf::VertexArray: Help Generating Verticies of This Object
« Reply #2 on: August 11, 2014, 01:27:20 am »
You want to describe the outer rim of a circle?

x = radius * cos(angle)
y = radius * sin(angle)

Where angle is in radians.

So:

#include <SFML/Graphics.hpp>
const float DEGTORAD = 0.0174532925199432957f;

int main()
{
    const int SWIDTH = 1024;
    const int SHEIGHT = 768;
    std::vector<sf::Vertex> vertices(32);
    float radius = 100;
    float TOTALANGLE = 180.f;
    vertices[0]= sf::Vertex({0,0});
    vertices[0].color = sf::Color::White;
    for (int i = 0; i < 31; i+=2)
    {
          float angle = i / 30.f * TOTALANGLE * DEGTORAD;
          vertices[i+1] = sf::Vertex( {radius * cos(angle), radius * sin(angle)} );
          vertices[i+1].color = sf::Color::White;
    }
    for(auto &a: vertices)
    {
        a.position += sf::Vector2f(100,100);

    }
    sf::RenderWindow win(sf::VideoMode(SWIDTH,SHEIGHT),"Cossin",sf::Style::Close);
    while(win.isOpen())
    {
        sf::Event e;
        while (win.pollEvent(e))
        {
            if(e.type == sf::Event::Closed)
            {

                win.close();
            }
        }
        win.clear();
        //drawing
        win.draw(&vertices[0],vertices.size(),sf::PrimitiveType::Lines);
        win.display();
    }
    return 0;
}


 
NOTE: This describes a 180 degree half-circle in 32 x,y points starting at 0,0 and stretching out to 100,100. What you do with those is up to you.
« Last Edit: August 11, 2014, 01:47:03 am by docwild »

 

anything