bool Background02::Init() { scene = new Scene(); for (int i = 0; i < BACKGROUND2_NRPLANES; i++) { scene->addObject(genPlane(1, 1, "env3")); scene->object[i]->cull = CULL_NONE; scene->object[i]->transMode = UNI_ADD; scene->object[i]->scaleUV(1, 8); for (int j = 0; j < scene->object[i]->nrVertices; j++) { scene->object[i]->vertex[j].x *= 2.f; scene->object[i]->vertex[j].z *= 8.f; } scene->object[i]->finish(false); bgrnd2Timers[i][0] = uniRand() * 10000.f; bgrnd2Timers[i][1] = uniRand() * 3000.f + 10000.f; bgrnd2Timers[i][2] = uniRand() * 3000.f + 10000.f; bgrnd2Timers[i][3] = uniRand() * 500.f + 1000.f; } return true; }
double randn() { double res = 0.0; for (int i=0; i<10; i++) { res += (uniRand() * 2.0 - 1.0); } res = res / 10; return res; }
bool Starfield::Init() { scene = new Scene(); scene->fogSprites = true; scene->addSpriteGroup(new SpriteGroup(STARFIELD_NRSTARS, 0.14f, 0x1a1a1a, "sprite01")); for (int i = 0; i < STARFIELD_NRSTARS; i++) { scene->spriteGroup[0]->sprite[i].setLoc((uniRand() * 20.f) - 10.f, (uniRand() * 20.f) - 10.f, (uniRand() * 30.f) - 5.f); } oldTimer = 0; mode = 1; return true; }
bool Sprites02::Init() { nrSprites = 2000; sprites = new SpriteInfo[nrSprites]; for (int i = 0; i < nrSprites; i++) { sprites[i].x = (uniRand() * 1000.f) - 100.f; sprites[i].y = uniRand() * 550.f + 80.f; sprites[i].speed = uniRand() * 15.f + 5.f; sprites[i].size = sprites[i].speed * 1.5f; } oldTime = 0; fadeOut = false; return true; }
void Field::InitDeployment(double gridSize) { UniformRandVar uniRand(2500, 7500); //define grids nGrid = maxX/gridSize; nGrid = nGrid*nGrid; gridPool = new Grid[nGrid]; for (int i = 0; i < nGrid; ++i) { int j = i*gridSize/maxX; if (j == 1) { bool stop = 1; } double offset = gridSize/2.0; int x = i%maxX; Point p(x*gridSize+offset, j*gridSize+offset); gridPool[i].init(i, p, RHO_0); //int index_, Point location_, int rho0_ gridPool[i].size = gridSize; gridPool[i].field = this; } //deploy human for (int i = 0; i < nHuman; ++i) { Point p(maxX/2.0, maxY/2.0); humanPool[i].init(i, p); humanPool[i].field = this; } //update the grid state that whether it has human int gR = humanPool[0].inGrid(gridSize); if (gR < 0 || gR >= 1000000) { cout << "Grid number is wrong" << endl; exit(2); } gridPool[gR].hasHuman = true; //init gridNum for humanPool for (int i = 0; i < nHuman; ++i) { humanPool[i].gridNum = gR; gridPool[gR].humanNum++; humanPool[i].grid = &gridPool[gR]; } //get neighbors for each grid for (int i = 0; i < nGrid; ++i) { gridPool[i].searchingNeighbors(); } }
void Starfield::Do(dword timer) { float timing = timer - oldTimer; oldTimer = timer; int fade = (timer / 1000.f) * 5.f; if (fade > 30) { fade = 30; } int color = (fade << 16) | (fade << 8) | (fade); for (int i = 0; i < STARFIELD_NRSTARS; i++) { float x = scene->spriteGroup[0]->sprite[i].x; float y = scene->spriteGroup[0]->sprite[i].y; float z = scene->spriteGroup[0]->sprite[i].z; scene->spriteGroup[0]->sprite[i].setLoc(x, y, z - (timing * STARFIELD_STARSPEED)); if (z < -5.0f) { float p = uniRand() * _PI2; // x = sin(p) * (uniRand() * 0.4f + 1.2f + (sin(timer / 1000.f) * 0.4f)); // y = cos(p) * (uniRand() * 0.4f + 1.2f + (cos(timer / 1300.f) * 0.4f)); // z = 25 + (uniRand() * 4.f); if (mode == 1) { x = (uniRand() * 20.f) - 10.f; y = (uniRand() * 20.f) - 10.f; } else { x = sin(p) * (uniRand() * 0.4f + 1.2f + (sin(timer / 1000.f) * 0.4f)); y = cos(p) * (uniRand() * 0.4f + 1.2f + (cos(timer / 1300.f) * 0.4f)); } z = 25 + (uniRand() * 4.f); scene->spriteGroup[0]->sprite[i].setLoc(x, y, z); } scene->spriteGroup[0]->sprite[i].diffuse = color; } scene->fog = true; scene->fogEnd = 30.f; scene->camera->setRoll(sin(timer / 5000.f) * 20.f); uniSetRenderState(D3DRS_RANGEFOGENABLE, TRUE); scene->render(); uniSetRenderState(D3DRS_RANGEFOGENABLE, FALSE); }
// sample from vector of probabilities int sampleFromVector(Vector& p) { double* cum = new double[p.m_]; cum[0] = p.get(0); for (int i=1; i<p.m_; i++) { cum[i] = cum[i-1] + p.get(i); } double r = uniRand(); int sample = 0; for (int i=0; i<p.m_; i++) { if (cum[i] >= r) { sample = i; break; } } delete[] cum; return sample; }
int randInt(int max){ double r = uniRand() * max; return (int)r; }