bingomanatee
YaBB Newbies
Offline
Posts: 4
CA
Terrain 3D (the source)
Reply #4 - Feb 15th , 2009, 10:12pm
This is the core terrain rendering class (uses openGL), shortened for brevity. class Terrain_3d { float t_width = 1500; float t_height = 500; float depth = 1500; float[] center = { 0.0, 500.0, -200.0 }; float [][] heights; // t_width, depth, t_height int width_points = 100; int depth_points = 100; int rolls = 3; // number of random walks the interpolation makes int hydration[][]; /**************** colors *****************************/ color ocean_color = color(240, 100, 10); color ocean_color_2 = color(210, 100, 33); float waterline = 0.5; color coast_color = color(0, 50, 15); color hills_color = color(30, 50, 45); float hills = 0.8; color mountains_color = color(60, 50, 75); float mountains = 1.2; color peaks_color = color(60, 25, 100); float peaks = 2; /*********************** constructor ***********************/ Terrain_3d(int pTWP, int pTDP, float pWidth, float pDepth) { width_points = pTWP; depth_points = pTDP; t_width = pWidth; depth = pDepth; t_height = (pWidth + pDepth) / 30; heights = new float[pTWP][pTDP]; hydration = new int[pTWP][pTDP]; for (int w = 0; w < pTWP; ++w) for (int d = 0; d < pTDP; ++d) { heights[w][d] = 0; hydration[w][d] = 0; } } // ******************* DIVIDE ********************** // doubles the terrain's resolution, interpolating the results and smoothing Terrain_3d subdivide() { ... } /****************** height accessors ********************** * "convenience" methods for height access */ void set_height(int w, int d, float height_percent){ if (w != constrain(w, 0, width_points )) return; if (d != constrain(d, 0, depth_points )) return; heights[w][d] = height_percent; } float get_height(int w, int d){ return heights[w][d]; } /************************ render *************************** * the basic 3d visualizer method; draws triangles */ void render() { for (int w = 0; w < width_points - 1; ++w) for (int d = 0; d < depth_points - 1; ++d) { float water_height = waterline * t_height * -1; float[] tri_heights = { (get_height(w, d) * t_height * -1), (get_height(w + 1, d) * t_height * -1), (get_height(w, d+1) * t_height * -1), (get_height(w+1, d+1) * t_height * -1) }; // if (max(tri_heights) > t_height * waterline * -1) continue; // float v = random(0, 100); // float v = constrain((100 * get_height(w, d)), 25, 100); // fill(map(w, 0, width_points, 0, 360), 100, v); fill(terrain_color(tri_heights)); for (int t = 0; t < tri_heights.length; ++t) tri_heights[t] = min(water_height, tri_heights[t]); // if (heights[w][d] < height/10) continue; beginShape(TRIANGLES); vertex( (w / float( width_points)) * t_width, tri_heights[0], ((d * -1 / float(depth_points)) * depth) ); vertex( ((w + 1) / float( width_points)) * t_width, tri_heights[1], ((d * -1/ float(depth_points)) * depth) ); vertex( (w / float( width_points)) * t_width, tri_heights[2], (((d + 1) * -1/ float(depth_points)) * depth) ); endShape(); beginShape(TRIANGLES); vertex( ((w + 1) / float( width_points)) * t_width, tri_heights[1], ((d * -1/ float(depth_points)) * depth) ); vertex( ((w + 1) / float( width_points)) * t_width, tri_heights[3], (((d + 1) * -1 / float(depth_points)) * depth) ); vertex( (w / float( width_points)) * t_width, tri_heights[2], (((d + 1) * -1/ float(depth_points)) * depth) ); endShape(); } } ...