#include #include #include "cube.h" struct shrub shrubmaker(float sx, float sy, float sz, int level){ struct shrub ns; ns.n = 0; size_t max_size = pow(2, level+2) * 3 * 2; printf("Using max size: %d\n", max_size); ns.vertices = (float*)malloc(max_size * sizeof(float)); void addvertex(float x, float y, float z){ ns.vertices[ns.n++] = x; ns.vertices[ns.n++] = y; ns.vertices[ns.n++] = z; } void ssrecur(float* start, int level){ float x = start[0], y = start[1], z = start[2]; // Optimizer should take this out for us float nx1, ny1, nz1, nx2, ny2, nz2; nx1 = x + (random()%100)/1000.0f * level/2.0; nx2 = x - (random()%100)/1000.0f * level/2.0; nz1 = z + (random()%100)/1000.0f * level/2.0; nz2 = z - (random()%100)/1000.0f * level/2.0; ny1 = y + (random()%100)/500.0f * level/2.0; ny2 = y + (random()%100)/500.0f * level/2.0; addvertex(x, y, z); addvertex(nx1, ny1, nz1); if(level) ssrecur(ns.vertices + ns.n - 3, level-1); addvertex(x, y, z); addvertex(nx2, ny2, nz2); if(level) ssrecur(ns.vertices + ns.n - 3, level-1); } float start[3] = {sx, sy, sz}; ssrecur(start, level); return ns; }