#include #include #include #include "cube.h" struct shrub shrubmaker(float sx, float sy, float sz, int level, float scale){ srand(time()); struct shrub ns; ns.n = 0; ns.fn = 0; size_t max_size = pow(2, level+2) * 4 * 2; ns.line_vertices = (float*)malloc(max_size * sizeof(float)); ns.fruit_vertices = (float*)malloc(4 * pow(2, level+3) * sizeof(float)); void addvertex(float x, float y, float z){ ns.line_vertices[ns.n++] = x; ns.line_vertices[ns.n++] = y; ns.line_vertices[ns.n++] = z; ns.line_vertices[ns.n++] = 0; // for padding } void addfruit(float x, float y, float z){ ns.fruit_vertices[ns.fn++] = x; ns.fruit_vertices[ns.fn++] = y; ns.fruit_vertices[ns.fn++] = z; ns.fruit_vertices[ns.fn++] = 0; // for padding } 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; float lsc = (level + 1) * scale; float rdp(){ return (100.0f - (float)(random()%200))/1000.0 * lsc; } nx1 = x + rdp(); nx2 = x + rdp(); nz1 = z + rdp(); nz2 = z + rdp(); ny1 = y + (random()%100)/500.0f * lsc; ny2 = y + (random()%100)/500.0f * lsc; if(nx1 == nx2){ printf("Oops! %f %f %f %f\n", x, nx1, nx2, rdp()); } addvertex(x, y, z); addvertex(nx1, ny1, nz1); if(level) ssrecur(ns.line_vertices + ns.n - 4, level-1); else addfruit(nx1, ny1, nz1); addvertex(x, y, z); addvertex(nx2, ny2, nz2); if(level) ssrecur(ns.line_vertices + ns.n - 4, level-1); else addfruit(nx2, ny2, nz2); } float start[4] = {sx, sy, sz, 0}; ssrecur(start, level); printf("Tree Vertices: %d\n", ns.n/4); printf("Fruits: %d\n", ns.fn/4); return ns; }