I'm trying to create a light source like in this image:
(http://i.imgur.com/eqzHBrz.png)
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
}
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.