#include #include #include #include float kernel[9]; float gravmap[YRES/CELL][XRES/CELL]; //Maps to be used by the main thread float gravx[YRES/CELL][XRES/CELL]; float gravy[YRES/CELL][XRES/CELL]; float gravp[YRES/CELL][XRES/CELL]; float *gravpf; float *gravyf; float *gravxf; unsigned gravmask[YRES/CELL][XRES/CELL]; float th_ogravmap[YRES/CELL][XRES/CELL]; // Maps to be processed by the gravity thread float th_gravmap[YRES/CELL][XRES/CELL]; float th_gravx[YRES/CELL][XRES/CELL]; float th_gravy[YRES/CELL][XRES/CELL]; float th_gravp[YRES/CELL][XRES/CELL]; float *th_gravpf; float *th_gravyf; float *th_gravxf; float vx[YRES/CELL][XRES/CELL], ovx[YRES/CELL][XRES/CELL]; float vy[YRES/CELL][XRES/CELL], ovy[YRES/CELL][XRES/CELL]; float pv[YRES/CELL][XRES/CELL], opv[YRES/CELL][XRES/CELL]; unsigned char bmap_blockair[YRES/CELL][XRES/CELL]; float cb_vx[YRES/CELL][XRES/CELL]; float cb_vy[YRES/CELL][XRES/CELL]; float cb_pv[YRES/CELL][XRES/CELL]; float cb_hv[YRES/CELL][XRES/CELL]; float fvx[YRES/CELL][XRES/CELL], fvy[YRES/CELL][XRES/CELL]; float hv[YRES/CELL][XRES/CELL], ohv[YRES/CELL][XRES/CELL]; // For Ambient Heat void make_kernel(void) //used for velocity { int i, j; float s = 0.0f; for (j=-1; j<2; j++) for (i=-1; i<2; i++) { kernel[(i+1)+3*(j+1)] = expf(-2.0f*(i*i+j*j)); s += kernel[(i+1)+3*(j+1)]; } s = 1.0f / s; for (j=-1; j<2; j++) for (i=-1; i<2; i++) kernel[(i+1)+3*(j+1)] *= s; } void update_airh(void) { int x, y, i, j; float dh, dx, dy, f, tx, ty; for (i=0; i0 && y+j0 && x+i=2 && i=2 && j0) vy[y][x] -= airdiff/5000.0f; } ohv[y][x] = dh; } } memcpy(hv, ohv, sizeof(hv)); } void bilinear_interpolation(float *src, float *dst, int sw, int sh, int rw, int rh) { int y, x, fxceil, fyceil; float fx, fy, fyc, fxc; double intp; float tr, tl, br, bl; //Bilinear interpolation for upscaling for (y=0; y=sw) fxceil = sw-1; if (fyceil>=sh) fyceil = sh-1; tr = src[sw*(int)floor(fy)+fxceil]; tl = src[sw*(int)floor(fy)+(int)floor(fx)]; br = src[sw*fyceil+fxceil]; bl = src[sw*fyceil+(int)floor(fx)]; dst[rw*y+x] = ((tl*(1.0f-fxc))+(tr*(fxc)))*(1.0f-fyc) + ((bl*(1.0f-fxc))+(br*(fxc)))*(fyc); } } void update_grav(void) { int x, y, i, j, changed = 0; float val, distance; #ifndef GRAV_DIFF //Find any changed cells for (i=0; i 0.0001f || th_gravmap[i][j]<-0.0001f) //Only calculate with populated or changed cells. { #endif for (y = 0; y < YRES / CELL; y++) { for (x = 0; x < XRES / CELL; x++) { if (x == j && y == i)//Ensure it doesn't calculate with itself continue; distance = sqrt(pow(j - x, 2) + pow(i - y, 2)); #ifdef GRAV_DIFF val = th_gravmap[i][j] - th_ogravmap[i][j]; #else val = th_gravmap[i][j]; #endif th_gravx[y][x] += M_GRAV * val * (j - x) / pow(distance, 3); th_gravy[y][x] += M_GRAV * val * (i - y) / pow(distance, 3); th_gravp[y][x] += M_GRAV * val / pow(distance, 2); } } } } } bilinear_interpolation(th_gravy, th_gravyf, XRES/CELL, YRES/CELL, XRES, YRES); bilinear_interpolation(th_gravx, th_gravxf, XRES/CELL, YRES/CELL, XRES, YRES); bilinear_interpolation(th_gravp, th_gravpf, XRES/CELL, YRES/CELL, XRES, YRES); fin: memcpy(th_ogravmap, th_gravmap, sizeof(th_gravmap)); memset(th_gravmap, 0, sizeof(th_gravmap)); } void update_air(void) { int x, y, i, j; float dp, dx, dy, f, tx, ty; for (y=0; y0 && y+j0 && x+i=2 && i=2 && j 256.0f) dp = 256.0f; if (dp < -256.0f) dp = -256.0f; if (dx > 256.0f) dx = 256.0f; if (dx < -256.0f) dx = -256.0f; if (dy > 256.0f) dy = 256.0f; if (dy < -256.0f) dy = -256.0f; switch (airMode) { default: case 0: //Default break; case 1: //0 Pressure dp = 0.0f; break; case 2: //0 Velocity dx = 0.0f; dy = 0.0f; break; case 3: //0 Air dx = 0.0f; dy = 0.0f; dp = 0.0f; break; case 4: //No Update break; } ovx[y][x] = dx; ovy[y][x] = dy; opv[y][x] = dp; } memcpy(vx, ovx, sizeof(vx)); memcpy(vy, ovy, sizeof(vy)); memcpy(pv, opv, sizeof(pv)); } }