terrain = maketerrain(10, 10, 0.75, 1.25) |

struct terr_struct { GLfloat *** verts; GLfloat *** cols; unsigned long width; unsigned long height; unsigned long vert_num; }; terr_struct terrain; double cos_interpol(float a, float b, float x) { //Why is this here? So I can triangulate my mesh //in multiple resolutions, eventually float ft = x * pi; float f = (1 - cos(ft)) * .5; return a * (1-f) + b * f; } void genmap(unsigned char ** maparry, unsigned long width, unsigned long height) { for(int y = 0; y < height + 3; y++) { for(int x = 0; x < width + 3; x++) { maparry[x][y] = rand() % 256; } } } float map(double x, double y, unsigned char ** maparry) { float ans; float x1, x2; float xfactor = (double)x - (double)((long)x); float yfactor = (double)y - (double)((long)y); x1 = cos_interpol(maparry[(long)x][(long)y], maparry[(long)x][(long)y + 1], xfactor); x1 = cos_interpol(maparry[(long)x + 1][(long)y], maparry[(long)x +1][(long)y + 1], xfactor); ans = x1 * (1 - yfactor) + x2 * yfactor; return (float)ans / (float)255; } void makecols(unsigned char ** maparry, GLfloat *** a_cols) { char a = 0; for(int y = 0; y < 10 - 1; y++) { for(int x = 0; x < 20; x++) { if(x%2 == 0){a=0;}else{a=1;} //Dumb place-filler function that looks sort of like //blue water and green hills. a_cols[y][x][0] = 0; a_cols[y][x][1] = pow((map(x/2, y + a, maparry))/(float)2, 2) * 2;//a_verts[y][x][1]; a_cols[y][x][2] = pow((1 - map(x/2, y + a, maparry))/(float)2, 2) * 2;//a_verts[y][x][1]; } } } unsigned long makegrid(float tilesize, float yscale, unsigned char ** maparry, GLfloat *** a_verts, unsigned long width, unsigned long height) { unsigned long vnum = 0; unsigned long total_vnum = 0; for(long y = 0; y < height - 1; y++) { for(long x = 0; x < width; x++) { a_verts[y][vnum][0] = (x - (float)(width / (float)2)) * (float)tilesize; a_verts[y][vnum][2] = (y - (float)(height / (float)2)) * (float)tilesize; a_verts[y][vnum][1] = map(x, y, maparry) * yscale; vnum++; total_vnum++; a_verts[y][vnum][0] = (x - (float)(width / (float)2)) * (float)tilesize; a_verts[y][vnum][2] = ((y + 1) - (float)(height / (float)2)) * (float)tilesize; a_verts[y][vnum][1] = map(x, (y+1), maparry) * yscale; vnum++; total_vnum++; } vnum = 0; } return total_vnum; } terr_struct maketerrain(unsigned long width, unsigned long height, float tilesize, float yscale) { unsigned char ** terr_map; GLfloat *** verts; GLfloat *** cols; terr_struct ret_val; terr_map = new unsigned char * [width + 3]; for(unsigned long i = 0; i < width + 3; i++) { terr_map[i] = new unsigned char[height + 3]; } verts = new GLfloat ** [height - 1]; for(unsigned long y = 0; y < height - 1; y++) { verts[y] = new GLfloat * [width * 2]; for(unsigned long x = 0; x < width * 2; x++) { verts[y][x] = new GLfloat[3]; } } cols = new GLfloat ** [height - 1]; for(y = 0; y < height - 1; y++) { cols[y] = new GLfloat * [width * 2]; for(unsigned long x = 0; x < width * 2; x++) { cols[y][x] = new GLfloat[3]; } } genmap(terr_map, width, height); ret_val.vert_num = makegrid(tilesize, yscale, terr_map, verts, width, height); makecols(terr_map, cols); for(i = 0; i < height + 3; i++) { delete [] terr_map[i]; } delete [] terr_map; ret_val.cols = cols; ret_val.verts = verts; ret_val.width = width; ret_val.height = height; return ret_val; } |

for(unsigned long i = 0; i < terrain.height - 1; i++) { glVertexPointer(3 , GL_FLOAT, 0, terrain.verts[i] ); glColorPointer(3, GL_FLOAT, 0, terrain.cols[i]); glDrawArrays(GL_TRIANGLE_STRIP, 0, terrain.vert_num); } |