PDA

View Full Version : Help With Recursion



Shakenbake158
02-17-2015, 06:58 PM
Hey guys, I am attempting to make a fractal mountain using OpenGL and recursion.

I have already wrote the code to create the Sierpinski Gasket, and I figures it wouldn't be that big of a deal to modify, but it is proving to be more difficult than I anticipated.

Here is my recursive function


void divide_triangle(const vec4 &a, const vec4 &b, const vec4 &c, int count) {
double random = (rand() % 5 + 1) / 100;
if (count > 0) {
// Midpoints of sides
vec4 v0 = (a + b) / 2.0;
vec4 v1 = (a + c) / 2.0;
vec4 v2 = (b + c) / 2.0;

divide_triangle(v0, v1, v2, count - 1); // This is the line I added
in order to also divide the middle triangle

divide_triangle(b, v2, v0, count - 1);
divide_triangle(a, v0, v1, count - 1);
divide_triangle(c, v1, v2, count - 1);
}

else {
triangle(a, b, c); // Draw triangle at the end of recursion
}
}


Have you guys got any ideas? Thanks!

GClements
02-18-2015, 03:45 PM
Have you guys got any ideas?
What's the problem, exactly?

One thing which I notice is that the middle triangle (v0,v1,v2) has the opposite direction to the parent and to the other three, i.e. if (a,b,c) is counter-clockwise, then the middle triangle will be clockwise while the other three are counter-clockwise. If you're culling back faces or using two-sided lighting, that matters.