scalar_t fbm2(scalar_t x, scalar_t y, int octaves) { int i; scalar_t res = 0.0f, freq = 1.0f; for(i=0; i<octaves; i++) { res += noise2(x * freq, y * freq) / freq; freq *= 2.0f; } return res; }
static int perlin_lua(lua_State *L) { int top = lua_gettop(L); if(top == 2) { lua_pushnumber(L, noise2(lua_tonumber(L, 1), lua_tonumber(L, 2))); } else if(top == 3) { lua_pushnumber(L, noise3(lua_tonumber(L, 1), lua_tonumber(L, 2), lua_tonumber(L, 3))); } else { return luaL_error(L, "expected 2 or 3 arguments but got %d", top); } return 1; }
int main(int argc, char **argv){ time_t t; srand((unsigned) time(&t)); float seed = rand()%1000/100.0; strcat(path, filename); FILE *file = fopen(path, "w"); fprintf(file, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); fprintf(file, "<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" "); fprintf(file, "x=\"0px\" y=\"0px\" width=\"%dpx\" height=\"%d", width, height); fprintf(file, "px\" viewBox=\"0 0 %d %d", width, height); fprintf(file, "\" xml:space=\"preserve\">\n<g>\n"); for(int i = 0; i < COLUMNS; i++){ fprintf(file, "<polyline fill=\"none\" stroke=\"#000000\" stroke-miterlimit=\"10\" points=\""); // hanging open quote for(float j = 0; j < height; j++){ int halfI = floor(i*.5); int one = 1.5*floor((i+0)*.25); int three = 1.5*floor((i+2)*.25); float vec[2]; vec[0] = i*.33; vec[1] = j/(float)height*3; float xShift = 30 * noise2(vec); float x = 70 + 40*i + 30*one + 30*three + waveMag*sinf(j*.023) * posNeg(halfI%2) + xShift; float y = j; fprintf(file, "%.2f,%.2f ", x, y); } fprintf(file, "\"/>\n"); // closing quote } // fprintf(file, "\"/>\n"); // closing quote // fprintf(file, "<polyline fill=\"none\" stroke=\"#000000\" stroke-miterlimit=\"10\" points=\""); // hanging open quote // float turn = 0; // for(int i = 0; i < COLUMNS; i+=2){ // for(float j = 0; j < TWOPI; j += TWOPI/divider){ // x = 0; // y = 0; // fprintf(file, "%.2f,%.2f ", x, y); // } // } fprintf(file, "\"/>\n"); // closing quote fprintf(file, "</g>\n"); fprintf(file, "</svg>"); fclose(file); return 0; }
F32 LLPerlinNoise::turbulence2(F32 x, F32 y, F32 freq) { F32 t, lx, ly; for (t = 0.f ; freq >= 1.f ; freq *= 0.5f) { lx = freq * x; ly = freq * y; t += noise2(lx, ly)/freq; } return t; }
static PyObject * py_noise2(PyObject *self, PyObject *args, PyObject *kwargs) { float x, y; int octaves = 1; float persistence = 0.5f; float lacunarity = 2.0f; float repeatx = 1024; // arbitrary float repeaty = 1024; // arbitrary int base = 0; static char *kwlist[] = {"x", "y", "octaves", "persistence", "lacunarity", "repeatx", "repeaty", "base", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ff|iffffi:noise2", kwlist, &x, &y, &octaves, &persistence, &lacunarity, &repeatx, &repeaty, &base)) return NULL; if (octaves == 1) { // Single octave, return simple noise return (PyObject *) PyFloat_FromDouble((double) noise2(x, y, repeatx, repeaty, base)); } else if (octaves > 1) { int i; float freq = 1.0f; float amp = 1.0f; float max = 0.0f; float total = 0.0f; for (i = 0; i < octaves; i++) { total += noise2(x * freq, y * freq, repeatx * freq, repeaty * freq, base) * amp; max += amp; freq *= lacunarity; amp *= persistence; } return (PyObject *) PyFloat_FromDouble((double) (total / max)); } else { PyErr_SetString(PyExc_ValueError, "Expected octaves value > 0"); return NULL; } }
float CTerrain::getAltitude(float vec[]) { float harmovec[2]; float f=0; for(float h=4.0f; h<=NBHARMONIC; h*=2.0f) { harmovec[0] = vec[0]*h; harmovec[1] = vec[1]*h; f+=noise2(harmovec)/h; //*2.9f /*bordel factor*/; } f=(f+0.1f /*more or less sea*/ )*6000.0f; /* top height */ return f; }
void noise2D::init() { int i, j; for (i=0; i<MAXKNOTS; i++) { for (j=0; j<MAXKNOTS; j++) { knots2[i][j]=noise2(i, j); knots2[i][j]=(knots2[i][j]*SHINESS); } } return; }
// To wrap on a sphere: use theta and phi for x and y // Brushed metal: vary only ONE of x,y // (eg PerlinNoise2D( uv.x, _1_, 2, 2, 8 ) // (the 1 stays constant across the 2d face) // (OR you could just generate a line of 1d noise and copy it) double PerlinGenerator::PerlinNoise2D( double x, double y, double divisionFactor, double freqMult, int numFreqs ) { double sum = 0; double p[2],scale = 1; p[0] = x; p[1] = y; for( int i=0 ; i < numFreqs ; i++ ) { sum += noise2(p) / scale; scale *= divisionFactor ; p[0] *= freqMult ; p[1] *= freqMult ; } return sum ; }
double PerlinNoise2D(double x,double y,double alpha,double beta,int n) { int i; double val,sum = 0; double p[2],scale = 1; p[0] = x; p[1] = y; for (i=0;i<n;i++) { val = noise2(p); sum += val / scale; scale *= alpha; p[0] *= beta; p[1] *= beta; } return(sum); }
float PerlinNoise::noise2(const Vector2& vec, int octaves, float persistence) { octaves += 1; float amplitude = 1.f; float total_amp = 0; float ret = 0.f; Vector2 v = vec; for (int i=0; i<octaves; i++) { ret += noise2(v.x, v.y) * amplitude; total_amp += amplitude; amplitude *= persistence; v *= 2.0f; } return ret / total_amp; }
float Perlin::perlin_noise_2D(float vec[2]) { int terms = mOctaves; float freq = mFrequency; float result = 0.0f; float amp = mAmplitude; vec[0]*=mFrequency; vec[1]*=mFrequency; for(int i=0; i < terms; i++) { result += noise2(vec)*amp; vec[0] *= 2.0f; vec[1] *= 2.0f; amp*=0.5f; } return result; }
void* fill_image(void *datap) { struct fill_image_data *data = datap; size_t i; // Fill each pixel in the image for (i = data->start; i < data->end; i++) { int t, x, y; // There is one channel per pixel, so divide by 1 t = i / 1; x = t % data->image->width; y = t / data->image->width; // noise2 calculates the noise at integer x,y data->buffer[i] = 255 * noise2(x, y); } // Print completion message and flush output to make it show (because we did not print a \n) printf("%c", data->finish_message); fflush(stdout); }
void Map::rockgen(float resolution) { const float max = static_cast<float>(std::max(width_, height_)); for(int y = 0; y < height_; ++y) { for(int x = 0; x < width_; ++x) { float vec[2] = {(x + 0.5f) / max * resolution, (y + 0.5f) / max * resolution}; float per = noise2(vec); if(per < -0.4) { tiles_[y][x] = Tile(x, y , ROCK); } } } }
double Perlin::perlin_noise_2D(double vec[2]) { int terms = mOctaves; double freq = mFrequency; double result = 0.0f; double amp = mAmplitude; vec[0]*=mFrequency; vec[1]*=mFrequency; for( int i=0; i<terms; i++ ) { result += noise2(vec)*amp; vec[0] *= 2.0f; vec[1] *= 2.0f; amp*=0.5f; } return result; }
void MapGenerator::make_cells(Cell *cells) { SimplexNoise noise1(random); SimplexNoise noise2(random); unsigned int x = 0; while (x < cells_x) { unsigned int y = 0; while (y < cells_y) { float fx = x * 0.1f; float fy = y * 0.1f; bool wall; wall = noise1.raw_noise_2d(fx, fy) > 0.0; wall &= noise2.raw_noise_2d(fx, fy) > 0.0; wall &= x != 1; wall &= x != cells_x - 2; wall &= y != 1; wall &= y != cells_y - 2; wall |= x == 0; wall |= x == cells_x - 1; wall |= y == 0; wall |= y == cells_y - 1; cells[x + y * cells_x].type = wall ? Cell::steel : Cell::empty; y++; } x++; } unsigned int base_x = cells_x / 16; unsigned int base_y = cells_y / 16; cells[base_x + cells_y / 2 * cells_x].type = Cell::base; cells[(cells_x - base_x - 1) + cells_y / 2 * cells_x].type = Cell::base; cells[cells_x / 2 + base_y * cells_x].type = Cell::base; cells[cells_x / 2 + (cells_y - base_y - 1) * cells_x].type = Cell::base; }
T noise_2D ( T vec[2]) { T amp = mAmplitude; T result = 0.0; vec[0] *= mFrequency; vec[1] *= mFrequency; for( int i = 0; i < mOctaves; i++ ) { result += noise2(vec) * amp; vec[0] *= 2.0; vec[1] *= 2.0; amp *= 0.5; } return result; }
void LLSurfacePatch::eval(const U32 x, const U32 y, const U32 stride, LLVector3 *vertex, LLVector3 *normal, LLVector2 *tex0, LLVector2 *tex1) { if (!mSurfacep || !mSurfacep->getRegion() || !mSurfacep->getGridsPerEdge()) { return; // failsafe } llassert_always(vertex && normal && tex0 && tex1); U32 surface_stride = mSurfacep->getGridsPerEdge(); U32 point_offset = x + y*surface_stride; *normal = getNormal(x, y); LLVector3 pos_agent = getOriginAgent(); pos_agent.mV[VX] += x * mSurfacep->getMetersPerGrid(); pos_agent.mV[VY] += y * mSurfacep->getMetersPerGrid(); pos_agent.mV[VZ] = *(mDataZ + point_offset); *vertex = pos_agent; LLVector3 rel_pos = pos_agent - mSurfacep->getOriginAgent(); LLVector3 tex_pos = rel_pos * (1.f/surface_stride); tex0->mV[0] = tex_pos.mV[0]; tex0->mV[1] = tex_pos.mV[1]; tex1->mV[0] = mSurfacep->getRegion()->getCompositionXY(llfloor(mOriginRegion.mV[0])+x, llfloor(mOriginRegion.mV[1])+y); const F32 xyScale = 4.9215f*7.f; //0.93284f; const F32 xyScaleInv = (1.f / xyScale)*(0.2222222222f); F32 vec[3] = { fmod((F32)(mOriginGlobal.mdV[0] + x)*xyScaleInv, 256.f), fmod((F32)(mOriginGlobal.mdV[1] + y)*xyScaleInv, 256.f), 0.f }; F32 rand_val = llclamp(noise2(vec)* 0.75f + 0.5f, 0.f, 1.f); tex1->mV[1] = rand_val; }
void Map::swampgrassgen(float resolution) { const float max = static_cast<float>(std::max(width_, height_)); for(int y = 0; y < height_; ++y) { for(int x = 0; x < width_; ++x) { float vec[2] = {(x + 0.5f) / max * resolution, (y + 0.5f) / max * resolution}; float per = noise2(vec); if(per < -0.2) { tiles_[y][x] = Tile(x, y, SWAMP); } else { tiles_[y][x] = Tile(x, y , GRASS); } } } }
BOOL LLVLComposition::generateHeights(const F32 x, const F32 y, const F32 width, const F32 height) { if (!mParamsReady) { // All the parameters haven't been set yet (we haven't gotten the message from the sim) return FALSE; } llassert(mSurfacep); if (!mSurfacep || !mSurfacep->getRegion()) { // We don't always have the region yet here.... return FALSE; } S32 x_begin, y_begin, x_end, y_end; x_begin = llround( x * mScaleInv ); y_begin = llround( y * mScaleInv ); x_end = llround( (x + width) * mScaleInv ); y_end = llround( (y + width) * mScaleInv ); if (x_end > mWidth) { x_end = mWidth; } if (y_end > mWidth) { y_end = mWidth; } LLVector3d origin_global = from_region_handle(mSurfacep->getRegion()->getHandle()); // For perlin noise generation... const F32 slope_squared = 1.5f*1.5f; const F32 xyScale = 4.9215f; //0.93284f; const F32 zScale = 4; //0.92165f; const F32 z_offset = 0.f; const F32 noise_magnitude = 2.f; // Degree to which noise modulates composition layer (versus // simple height) // Heights map into textures as 0-1 = first, 1-2 = second, etc. // So we need to compress heights into this range. const S32 NUM_TEXTURES = 4; const F32 xyScaleInv = (1.f / xyScale); const F32 zScaleInv = (1.f / zScale); const F32 inv_width = 1.f/mWidth; // OK, for now, just have the composition value equal the height at the point. for (S32 j = y_begin; j < y_end; j++) { for (S32 i = x_begin; i < x_end; i++) { F32 vec[3]; F32 vec1[3]; F32 twiddle; // Bilinearly interpolate the start height and height range of the textures F32 start_height = bilinear(mStartHeight[SOUTHWEST], mStartHeight[SOUTHEAST], mStartHeight[NORTHWEST], mStartHeight[NORTHEAST], i*inv_width, j*inv_width); // These will be bilinearly interpolated F32 height_range = bilinear(mHeightRange[SOUTHWEST], mHeightRange[SOUTHEAST], mHeightRange[NORTHWEST], mHeightRange[NORTHEAST], i*inv_width, j*inv_width); // These will be bilinearly interpolated LLVector3 location(i*mScale, j*mScale, 0.f); F32 height = mSurfacep->resolveHeightRegion(location) + z_offset; // Step 0: Measure the exact height at this texel vec[0] = (F32)(origin_global.mdV[VX]+location.mV[VX])*xyScaleInv; // Adjust to non-integer lattice vec[1] = (F32)(origin_global.mdV[VY]+location.mV[VY])*xyScaleInv; vec[2] = height*zScaleInv; // // Choose material value by adding to the exact height a random value // vec1[0] = vec[0]*(0.2222222222f); vec1[1] = vec[1]*(0.2222222222f); vec1[2] = vec[2]*(0.2222222222f); twiddle = noise2(vec1)*6.5f; // Low freq component for large divisions twiddle += turbulence2(vec, 2)*slope_squared; // High frequency component twiddle *= noise_magnitude; F32 scaled_noisy_height = (height + twiddle - start_height) * F32(NUM_TEXTURES) / height_range; scaled_noisy_height = llmax(0.f, scaled_noisy_height); scaled_noisy_height = llmin(3.f, scaled_noisy_height); *(mDatap + i + j*mWidth) = scaled_noisy_height; } } return TRUE; }
void Controller::update() { checkBounds(); vector<Boid2*> new_particles; float pf; for(vector<Boid2*>::iterator it = flock_ps.particles.begin(); it != flock_ps.particles.end(); ++it) { Boid2& b = **it; Vec2& pos = b.position; // PERLIN if(settings.flocking_apply_perlin) { pf = noise2(pos.x * settings.flocking_perlin_influence, pos.y * settings.flocking_perlin_influence); pf *= settings.flocking_perlin_scale; b.addForce(Vec2(pf, pf)); } // TRAIL if(b.grow_trail_end != 0) { if(Timer::now() > b.grow_trail_end) { b.grow_trail_end = 0; } else { b.trail.push_back(b.position); if(b.trail.size() > 20) { b.trail.pop_front(); } } } else if(b.trail.size() > 0){ b.trail.push_back(b.position); b.trail.pop_front(); b.trail.pop_front(); if(b.trail.size() == 0) { b.glow_end = Timer::now() + (uint64_t)settings.boid_glow_duration_millis; } } // GLOW if(b.glow_end != 0 && b.glow_end < Timer::now()) { b.glow_end = 0; // CREATE EXPLOSION PARTICLES int num = 10; float range = 20.0f; for(int i = 0; i < num; ++i) { Vec2 npos = b.position + randomVec2()*range; Boid2* exp = new Boid2(npos); exp->lifespan = random(settings.explosion_min_lifespan, settings.explosion_max_lifespan); exp->velocity = -b.velocity ; // exp->velocity.x *= random(-settings.explosion_random_x_vel, settings.explosion_random_x_vel); // exp->velocity.y *= random(-settings.explosion_random_y_vel, settings.explosion_random_y_vel); //exp->disable(); exp->size = random(1.0f, 3.5f); fx_ps.addParticle(exp); } } } // EXPLOSIONS float t = Timer::now() * 0.001; for(Boids2::iterator it = fx_ps.begin(); it != fx_ps.end(); ++it) { Boid2& b = **it; Vec2& pos = b.position; // apply perlin. float nx1 = noise2(pos.x * settings.explosion_perlin_influence, t); float nx2 = noise2(pos.x * settings.explosion_perlin_influence, t * 1.1); float nx = nx1-nx2; float ny1 = noise2(pos.y * settings.explosion_perlin_influence, t); float ny2 = noise2(pos.y * settings.explosion_perlin_influence, t * 1.1); float ny = ny1-ny2; pf *= settings.explosion_perlin_scale; b.addForce(Vec2(nx * settings.explosion_perlin_scale, ny * settings.explosion_perlin_scale)); // grow trail? if(settings.explosion_trail_length > 0) { if(b.trail.size() < settings.explosion_trail_length) { b.trail.push_back(pos); } else { b.trail.pop_front(); } } } for(vector<Player*>::iterator it = players.begin(); it != players.end(); ++it) { (*it)->update(); } //player1.update(); }
real shPerlin::Noise2D(const vec2& v)const{ real items[2] = {v.x, v.y}; return noise2(items); }
float tileableNoise2(const float x, const float y, const float w, const float h){ return (noise2(x, y) * (w - x) * (h - y) + noise2(x - w, y) * x * (h - y) + noise2(x, y - h) * (w - x) * y + noise2(x - w, y - h) * x * y) / (w * h); }
float noise( float x, float y ){ float vec[2]; vec[0] = x; vec[1] = y; return (noise2(vec) + 1) / 2; }
void ViBenchMarker3::process2() { int time = mTime.elapsed(); QObject::disconnect(mCurrentObject.data(), SIGNAL(noiseGenerated()), this, SLOT(process2())); /*QObject::connect(mCurrentObject.data(), SIGNAL(encoded()), this, SLOT(quit())); mCurrentObject->encode(ViAudio::Noise); return;*/ qreal maxMAT = 0; qint64 maxTP = 0, maxTN = 0, maxFP = 0, maxFN = 0; qint64 i, lengthIndex, offset1 = 0, offset2 = 0; int noChange = 0; ViAudioReadData corrupted(mCurrentObject->buffer(ViAudio::Noise)); ViAudioReadData realMask(mCurrentObject->buffer(ViAudio::CustomMask)); ViAudioReadData length(mCurrentObject->buffer(ViAudio::Custom)); corrupted.setSampleCount(WINDOW_SIZE); realMask.setSampleCount(WINDOW_SIZE); length.setSampleCount(WINDOW_SIZE); ViSampleChunk *nData1 = new ViSampleChunk(corrupted.bufferSamples() / 2); ViSampleChunk *nData2 = new ViSampleChunk(corrupted.bufferSamples() / 2); ViSampleChunk *nMask1 = new ViSampleChunk(corrupted.bufferSamples() / 2); ViSampleChunk *nMask2 = new ViSampleChunk(corrupted.bufferSamples() / 2); ViSampleChunk *nRealMask1 = new ViSampleChunk(realMask.bufferSamples() / 2); ViSampleChunk *nRealMask2 = new ViSampleChunk(realMask.bufferSamples() / 2); ViSampleChunk *nLength1 = new ViSampleChunk(corrupted.bufferSamples() / 2); ViSampleChunk *nLength2 = new ViSampleChunk(corrupted.bufferSamples() / 2); while(corrupted.hasData() && realMask.hasData()) { corrupted.read(); ViSampleChunk &corrupted1 = corrupted.splitSamples(0); ViSampleChunk &corrupted2 = corrupted.splitSamples(1); realMask.read(); ViSampleChunk &realMask1 = realMask.splitSamples(0); ViSampleChunk &realMask2 = realMask.splitSamples(1); length.read(); ViSampleChunk &length1 = length.splitSamples(0); ViSampleChunk &length2 = length.splitSamples(1); for(i = 0; i < corrupted1.size(); ++i) { (*nData1)[i + offset1] = corrupted1[i]; (*nRealMask1)[i + offset1] = realMask1[i]; (*nLength1)[i + offset1] = length1[i]; } offset1 += corrupted1.size(); for(i = 0; i < corrupted2.size(); ++i) { (*nData2)[i + offset2] = corrupted2[i]; (*nRealMask2)[i + offset2] = realMask2[i]; (*nLength2)[i + offset2] = length2[i]; } offset2 += corrupted2.size(); } mCurrentObject->clearBuffer(ViAudio::Target); mCurrentObject->clearBuffer(ViAudio::Noise); mCurrentObject->clearBuffer(ViAudio::NoiseMask); ViNoise noise1(nData1, nMask1, mCurrentThreshold); ViNoise noise2(nData2, nMask2, mCurrentThreshold); QVector<qreal> lengthTP(ViNoiseCreator::noiseSizeCount()); QVector<qreal> lengthFN(ViNoiseCreator::noiseSizeCount()); lengthTP.fill(0); lengthFN.fill(0); QVector<qreal> maxLengthTP, maxLengthFN; for(mCurrentThreshold = MASK_START; mCurrentThreshold <= MASK_END; mCurrentThreshold += MASK_INTERVAL) { noise1.setThreshold(mCurrentThreshold); noise1.generateMask(ViNoise::NOISE_TYPE); noise2.setThreshold(mCurrentThreshold); noise2.generateMask(ViNoise::NOISE_TYPE); qint64 truePositives = 0, falsePositives = 0, trueNegatives = 0, falseNegatives = 0; for(i = 0; i < nRealMask1->size(); ++i) { if((*nRealMask1)[i] == 1) { lengthIndex = ViNoiseCreator::fromSizeMask((*nLength1)[i]) - 1; if((*nMask1)[i] == 1) { ++truePositives; lengthTP[lengthIndex] += 1; } else { ++falseNegatives; lengthFN[lengthIndex] += 1; } } else if((*nRealMask1)[i] == 0) { if((*nMask1)[i] == 1) ++falsePositives; else ++trueNegatives; } } for(i = 0; i < nRealMask2->size(); ++i) { if((*nRealMask2)[i] == 1) { lengthIndex = ViNoiseCreator::fromSizeMask((*nLength2)[i]) - 1; if((*nMask2)[i] == 1) { ++truePositives; lengthTP[lengthIndex] += 1; } else { ++falseNegatives; lengthFN[lengthIndex] += 1; } } else if((*nRealMask2)[i] == 0) { if((*nMask2)[i] == 1) ++falsePositives; else ++trueNegatives; } } qreal math = matthews(truePositives, trueNegatives, falsePositives, falseNegatives); if(math > maxMAT) { maxMAT = math; maxTP = truePositives; maxTN = trueNegatives; maxFP = falsePositives; maxFN = falseNegatives; maxLengthTP = lengthTP; maxLengthFN = lengthFN; noChange = 0; } ++noChange; if(noChange > NO_CHANGE) break; } delete nRealMask1; delete nRealMask2; delete nLength1; delete nLength2; ++mDoneParamIterations; if(maxMAT > mBestMatthews) mBestMatthews = maxMAT; printFileData(time, maxTP, maxTN, maxFP, maxFN, maxLengthTP, maxLengthFN); printTerminal(time, maxTP, maxTN, maxFP, maxFN, mBestMatthews); if(nextParam()) process1(false); else nextFile(); }
int main(int argc, char *argv[]) { glutInit(&argc, argv); // glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH); glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH); glutInitWindowSize(windowWidth, windowHeight); glutCreateWindow("Square"); // set up world space to screen mapping glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0f, (GLfloat) windowWidth, 0.0f, (GLfloat) windowHeight, -1.0f, 1.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0, 0, windowWidth, windowHeight); glClearColor(1.0,.95,.85,0); glutDisplayFunc(Redraw); glutReshapeFunc(Reshape); glutMouseFunc(Button); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); curve . add(100,100); curve . add(200,150); curve . add(300,100); curve . add(400,300); curve . add(100,100); curve.radius = 10; curve.curveType = CUBIC_BSPLINE; // curve.curveType = FOUR_POINT; // make a texture map texWidth = texHeight = 512; texture = new GLubyte[4*texWidth*texHeight]; for(int x=0;x<texWidth;x++) for(int y=0;y<texHeight;y++) { float n = noise2(x/100.,y/100.); GLubyte v = n*255; // printf("%f(%d) ",n,v); texture[4*(x + y*texWidth)] = v; texture[4*(x + y*texWidth)+1] = v; texture[4*(x + y*texWidth)+2] = v; texture[4*(x + y*texWidth)+3] = v; } // initialize texturing glTexImage2D(GL_TEXTURE_2D,0,4,texHeight,texWidth,0,GL_RGBA, GL_UNSIGNED_BYTE,texture); // glTexImage2D(GL_TEXTURE_2D,0,1,texHeight,texWidth,0,GL_LUMINANCE, // GL_UNSIGNED_BYTE,texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // filtering glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glutCreateMenu(menuSelect); glutAddMenuEntry("Cubic B-Spline",0); glutAddMenuEntry("Four Point",1); glutAddMenuEntry("Control Polygon",2); glutAddMenuEntry("Opaque/Transparent",3); glutAddMenuEntry("Thicker",4); glutAddMenuEntry("Thinner",5); glutAddMenuEntry("Toggle Tapering",6); glutAddMenuEntry("Toggle Texture",7); glutAddMenuEntry("Quit",8); glutAttachMenu(GLUT_MIDDLE_BUTTON); glutMainLoop(); return 0; }
extern "C" int main() { uint32_t last = systick_millis_count; uint32_t lastRender = 0; uint16_t i = 0; uint16_t j = 0; uint8_t index = 0; uint8_t startIndex = 0; pinMode(LED_BUILTIN, OUTPUT); for (i = 0; i < LUT_TOTAL_SIZE; i++) { buffers.lutCurrent.entries[i] = defaultLUT[i] >> 1;// (i % LUT_CH_SIZE) << 7; } #ifdef RAINBOW_LANTERNS int16_t numSteps = 64; #endif leds.begin(); rainbow10[0] = rainbow7[0] = color(0xFF, 0, 0); rainbow10[1] = rainbow7[1] = color(0xFF, 0xA5, 0); rainbow10[2] = rainbow7[2] = color(0xFF, 0xFF, 0); rainbow10[3] = rainbow7[3] = color(0, 0x80, 0); rainbow10[4] = color(0, 0xFF, 0); rainbow10[5] = color(0, 0xA5, 0x80); rainbow10[6] = rainbow7[4] = color(0, 0, 0xFF); rainbow10[7] = rainbow7[5] = color(0x4B, 0, 0x82); rainbow10[8] = rainbow7[6] = color(0xFF, 0, 0xFF); rainbow10[9] = color(0xEE, 0x82, 0xEE); buffers.flags = CFLAG_NO_ACTIVITY_LED; //buffers.flags |= CFLAG_LED_CONTROL; // Announce firmware version serial_begin(BAUD2DIV(115200)); serial_print("Fadecandy v" DEVICE_VER_STRING "\r\n"); //usb_serial_printf("test\n"); // Application main loop while (usb_dfu_state == DFU_appIDLE) { watchdog_refresh(); // Select a different drawing loop based on our firmware config flags switch (buffers.flags & (CFLAG_NO_INTERPOLATION | CFLAG_NO_DITHERING)) { case 0: default: updateDrawBuffer_I1_D1(calculateInterpCoefficient()); break; case CFLAG_NO_INTERPOLATION: updateDrawBuffer_I0_D1(0x10000); break; case CFLAG_NO_DITHERING: updateDrawBuffer_I1_D0(calculateInterpCoefficient()); break; case CFLAG_NO_INTERPOLATION | CFLAG_NO_DITHERING: updateDrawBuffer_I0_D0(0x10000); break; } //buffers.flags |= CFLAG_NO_ACTIVITY_LED; // Start sending the next frame over DMA leds.show(); if ((systick_millis_count - last) > 500) { //buffers.flags ^= CFLAG_LED_CONTROL; last = systick_millis_count; } #ifdef RAINBOW_CIRCLE if ((systick_millis_count - lastRender) > 500) { buffers.flags ^= CFLAG_LED_CONTROL; lastRender = systick_millis_count; index = startIndex; for (i = 0; i < 52/*32*/; i++) { uint8_t* pixel = buffers.fbNew->pixel(LEDS_PER_STRIP * 7 + i); uint32_t color = rainbow7[index]; pixel[0] = RED(color); pixel[1] = GREEN(color); pixel[2] = BLUE(color); index = (index + 1) % 7; } for (i = 0; i < 20; i++) { uint8_t* pixel = buffers.fbNew->pixel(LEDS_PER_STRIP * 6 + i); uint32_t color = rainbow7[index]; pixel[0] = RED(color); pixel[1] = GREEN(color); pixel[2] = BLUE(color); index = (index + 1) % 7; } startIndex = (startIndex + 1) % 7; buffers.finalizeFramebuffer(); } #endif #ifdef RAINBOW_TOPHAT (void)j; if ((systick_millis_count - lastRender) > 500) { buffers.flags ^= CFLAG_LED_CONTROL; lastRender = systick_millis_count; index = startIndex; for (i = 0; i < 52/*32*/; i++) { uint8_t* pixel = buffers.fbNew->pixel(LEDS_PER_STRIP * 7 + i); uint32_t color = rainbow7[index]; pixel[0] = RED(color); pixel[1] = GREEN(color); pixel[2] = BLUE(color); if (i>0 && i % 5 == 0) { index = (index + 1) % 7; } } for (i = 0; i < 20; i++) { uint8_t* pixel = buffers.fbNew->pixel(LEDS_PER_STRIP * 6 + i); uint32_t color = rainbow7[index]; pixel[0] = RED(color); pixel[1] = GREEN(color); pixel[2] = BLUE(color); index = (index + 1) % 7; } startIndex = (startIndex + 1) % 7; buffers.finalizeFramebuffer(); } #endif #ifdef RAINBOW_LANTERNS /*(void)j; (void)index; (void)startIndex; if ((systick_millis_count - lastRender) > 500) { buffers.flags ^= CFLAG_LED_CONTROL; lastRender = systick_millis_count; for (i = 0; i < 20; i++) { uint8_t* pixel = buffers.fbNew->pixel(LEDS_PER_STRIP * 7 + i); int16_t direction = directions[i] < 0 ? -1 : 1; uint32_t color1 = rainbow7[indexes[i]]; int16_t index2 = indexes[i] + direction; if (index2 < 0) { index2 = 6; } else if (index2 >= 7) { index2 = 0; } uint32_t color2 = rainbow7[index2]; int32_t reddiff = RED(color2) - RED(color1); int32_t bluediff = BLUE(color2) - BLUE(color1); int32_t greendiff = GREEN(color2) - BLUE(color1); reddiff *= steps[i]; reddiff /= numSteps; bluediff *= steps[i]; bluediff /= numSteps; greendiff *= steps[i]; greendiff /= numSteps; pixel[0] = RED(color1) + reddiff; pixel[1] = GREEN(color1) + greendiff; pixel[2] = BLUE(color1) + bluediff; steps[i] += abs(directions[i]); if (steps[i] >= numSteps) { steps[i] = 0; indexes[i] = index2; } } buffers.finalizeFramebuffer(); }*/ (void)j; (void)numSteps; if ((systick_millis_count - lastRender) > 5000) { buffers.flags ^= CFLAG_LED_CONTROL; lastRender = systick_millis_count; index = startIndex; for (i = 0; i < 20/*32*/; i++) { uint8_t* pixel = buffers.fbNew->pixel(LEDS_PER_STRIP * 7 + i); uint32_t color = rainbow7[index]; pixel[0] = RED(color); pixel[1] = GREEN(color); pixel[2] = BLUE(color); if (i>0 && i % 2 == 0) { index = (index + 1) % 7; } } for (i = 0; i < 20; i++) { uint8_t* pixel = buffers.fbNew->pixel(LEDS_PER_STRIP * 6 + i); uint32_t color = rainbow7[index]; pixel[0] = RED(color); pixel[1] = GREEN(color); pixel[2] = BLUE(color); index = (index + 1) % 7; } startIndex = (startIndex + 1) % 7; buffers.finalizeFramebuffer(); } #endif #ifdef PARASOL if ((systick_millis_count - lastRender) > 500) { buffers.flags ^= CFLAG_LED_CONTROL; lastRender = systick_millis_count; index = startIndex; for (i = 0; i < 8; i++) { for (j = 0; j < 3; j++) { uint8_t* pixel = buffers.fbNew->pixel(LEDS_PER_STRIP * i + j); uint32_t color = rainbow7[index]; pixel[0] = RED(color); pixel[1] = GREEN(color); pixel[2] = BLUE(color); } index = (index + 1) % 7; } startIndex = (startIndex + 1) % 7; buffers.finalizeFramebuffer(); } #endif #ifdef SMOOTH_RAINBOW (void)startIndex; (void)j; if ((systick_millis_count - lastRender) > 30) { lastRender = systick_millis_count; uint32_t ledIndex = 0; if (index >= 16) { buffers.flags ^= CFLAG_LED_CONTROL; index = 0; } index++; float color1[3]; float color2[3]; for (i = 0; i < 32; i++) { uint8_t* pixel = buffers.fbNew->pixel(LEDS_PER_STRIP * 7 + i); pixel[0] = 0xFF; pixel[1] = 0; pixel[2] = 0; float scaled = ledIndex / 44.0f; //fix16_t noise = 32768; (void)scaled; float noise = .5f + noise2(scaled, time); //float noise = .5f + fbm_noise3(scaled, time, .5f, 4, .25f, 2.0f); noise = fmax(fmin(1, noise), 0); float mult = 9 * noise; float index1 = floor(mult); float index2 = ceil(mult); float percent = mult - index1; uint32_t color = rainbow10[(int)index1]; color1[0] = RED(color); color1[1] = GREEN(color); color1[2] = BLUE(color); color = rainbow10[(int)index2]; color2[0] = RED(color); color2[1] = GREEN(color); color2[2] = BLUE(color); pixel[0] = (uint8_t)((int)(color1[0] + ((color2[0] - color1[0]) * percent)) & 0xFF); pixel[1] = (uint8_t)((int)(color1[1] + ((color2[1] - color1[1]) * percent)) & 0xFF); pixel[2] = (uint8_t)((int)(color1[2] + ((color2[2] - color1[2]) * percent)) & 0xFF); ledIndex++; } /* for (i = 0; i < 12; i++) { uint8_t* pixel = buffers.fbNew->pixel(LEDS_PER_STRIP * 6 + i); pixel[0] = 0xFF; pixel[1] = 0; pixel[2] = 0; float scaled = ledIndex / 44.0f; (void)scaled; float noise = 32768; //noise2(scaled, time); //fix16_t noise = 32768 + noise2(scaled, time); //fix16_t noise = 32768 + fbm_noise3(scaled, time, 0, 4, 16384, 131072); noise = fmax(fmin(noise, 1), 0); float mult = 9 * noise; float index1 = floor(mult); float index2 = ceil(mult); float percent = mult - index1; (void)index2; (void)percent; uint32_t color = rainbow10[1];// fix16_to_int(index1)]; color1[0] = RED(color); color1[1] = GREEN(color); color1[2] = BLUE(color); color = rainbow10[2];// fix16_to_int(index2)]; color2[0] = RED(color); color2[1] = GREEN(color); color2[2] = BLUE(color); pixel[0] = (uint8_t)((int)(color1[0] + ((color2[0] - color1[0]) * percent)) & 0xFF); pixel[1] = (uint8_t)((int)(color1[1] + ((color2[1] - color1[1]) * percent)) & 0xFF); pixel[2] = (uint8_t)((int)(color1[2] + ((color2[2] - color1[2]) * percent)) & 0xFF); ledIndex++; } */ time = (time + timestep); buffers.finalizeFramebuffer(); } #endif // We can switch to the next frame's buffer now. buffers.finalizeFrame(); // Performance counter, for monitoring frame rate externally perf_frameCounter++; } // Reboot into DFU bootloader dfu_reboot(); }
/* * 2D noise */ float Noise::Noise2D(float x, float y) { float vec[2] = { x, y }; return noise2(vec); }
static PyObject * py_noise2(PyObject *self, PyObject *args, PyObject *kwargs) { float x, y; int octaves = 1; float persistence = 0.5f; float lacunarity = 2.0f; float repeatx = FLT_MAX; float repeaty = FLT_MAX; float z = 0.0f; static char *kwlist[] = {"x", "y", "octaves", "persistence", "lacunarity", "repeatx", "repeaty", "base", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ff|ifffff:snoise2", kwlist, &x, &y, &octaves, &persistence, &lacunarity, &repeatx, &repeaty, &z)) { return NULL; } if (octaves <= 0) { PyErr_SetString(PyExc_ValueError, "Expected octaves value > 0"); return NULL; } if (repeatx == FLT_MAX && repeaty == FLT_MAX) { // Flat noise, no tiling float freq = 1.0f; float amp = 1.0f; float max = 1.0f; float total = noise2(x + z, y + z); int i; for (i = 1; i < octaves; i++) { freq *= lacunarity; amp *= persistence; max += amp; total += noise2(x * freq + z, y * freq + z) * amp; } return (PyObject *) PyFloat_FromDouble((double) (total / max)); } else { // Tiled noise float w = z; if (repeaty != FLT_MAX) { float yf = y * 2.0 / repeaty; float yr = repeaty * M_1_PI * 0.5; float vy = fast_sin(yf); float vyz = fast_cos(yf); y = vy * yr; w += vyz * yr; if (repeatx == FLT_MAX) { return (PyObject *) PyFloat_FromDouble( (double) fbm_noise3(x, y, w, octaves, persistence, lacunarity)); } } if (repeatx != FLT_MAX) { float xf = x * 2.0 / repeatx; float xr = repeatx * M_1_PI * 0.5; float vx = fast_sin(xf); float vxz = fast_cos(xf); x = vx * xr; z += vxz * xr; if (repeaty == FLT_MAX) { return (PyObject *) PyFloat_FromDouble( (double) fbm_noise3(x, y, z, octaves, persistence, lacunarity)); } } return (PyObject *) PyFloat_FromDouble( (double) fbm_noise4(x, y, z, w, octaves, persistence, lacunarity)); } }