0
\$\begingroup\$

I'm creating a lightning geometry shader using hlsl that creates an array of points based on an arbitrary number of generations. The gist of what I want to accomplish is here: http://drilian.com/2009/02/25/lightning-bolts/

The problem is the fact that the pseudo-code uses a dynamic array which deletes the current generations line segment and then resizes to add new line segments. I can only use fixed sized arrays in hlsl.

My current code is as stands:

int generations = 4;
int pointsarraysize = pow(2, generations) + 1;
float3 points[pointsarraysize];
points[0] = startpoint;
points[pointsarraysize - 1] = endpoint;

for(int currentgeneration = 0; currentgeneration < genetations; ++currentgeneration)
{
    for(int i = 0; i < pointsarraysize; ++i)
    {
        if (i == pow(2, currentgeneration))
        {

        }
    }
}

So what i've got creates an array of the size of points I want to end up with and sets the start point and end point.

How do I fill the array so that each iteration over the array creates the points in the right place in the array? I can't simply iterate linearly and calculate points depending on the index position because calculating, for example, the first point in an array with 4 generations requires the start point point[0] and a generation 3 point, point[2].

Is this even the best way to fill the array or am I missing something that could simplify the problem? I want to be able to change the number of generations rather than hard coding a certain amount.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The whole way I was going about it is wrong. I'll answer my own question in case anybody else comes here from google.

Instead of generating a whole tonne of points and appending them in one geometry shader to create lightning, the answer is to only generate each iteration of the lightning per geometry shader invocation to remove the need for having a dynamic array at all.

So, first you take a line into the geometry shader. Then you output only 2 or 3 line segments from the geometry shader (depending on if the lightning arcs or not, as per the second code section on http://drilian.com/2009/02/25/lightning-bolts/). Then use the stream output stage of the graphics pipeline to then invoke the geometry shader for the new line segments you just made.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .