Skip to content

Instantly share code, notes, and snippets.

@shybovycha
Created March 26, 2017 17:34
Show Gist options
  • Save shybovycha/28bf7f0474b6cb9690c73a6fc4e4e015 to your computer and use it in GitHub Desktop.
Save shybovycha/28bf7f0474b6cb9690c73a6fc4e4e015 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
#include <SFML/Window.hpp>
#define Persistence 1.f / 8.f
#define Number_Of_Octaves 7
inline float interpolate(float a, float b, float x)
{
float ft = x * 3.1415927;
float f = (1.f - cos(ft)) * 0.5f;
return (a * (1.f - f)) + (b * f);
//return (a * (1.f - x)) + (b * x);
}
inline double findnoise(int x)
{
x = (x<<13) ^ x;
return (double)( 1.0 - ( (x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
}
inline double findnoise2(double x,double y)
{
int n=(int)x+(int)y*57;
n=(n<<13)^n;
int nn=(n*(n*n*60493+19990303)+1376312589)&0x7fffffff;
return 1.0-((double)nn/1073741824.0);
}
double noise2(double x,double y)
{
double floorx=(double)((int)x);//This is kinda a cheap way to floor a double integer.
double floory=(double)((int)y);
double s,t,u,v;//Integer declaration
s=findnoise2(floorx,floory);
t=findnoise2(floorx+1,floory);
u=findnoise2(floorx,floory+1);//Get the surrounding pixels to calculate the transition.
v=findnoise2(floorx+1,floory+1);
double int1=interpolate(s,t,x-floorx);//Interpolate between the values.
double int2=interpolate(u,v,x-floorx);//Here we use x-floorx, to get 1st dimension. Don't mind the x-floorx thingie, it's part of the cosine formula.
return interpolate(int1,int2,y-floory);//Here we use y-floory, to get the 2nd dimension.
}
double perlin(double x, double y)
{
double getnoise =0, p = Persistence, octaves = Number_Of_Octaves;
for(int a=0;a<octaves-1;a++)//This loops trough the octaves.
{
double frequency = pow(2,a);//This increases the frequency with every loop of the octave.
double amplitude = pow(p,a);//This decreases the amplitude with every loop of the octave.
getnoise += noise2(((double)x)*frequency,((double)y)*frequency)*amplitude;//This uses our perlin noise functions. It calculates all our zoom and frequency and amplitude
}// It gives a decimal value, you know, between the pixels. Like 4.2 or 5.1
//int color= (int)((getnoise*128.0)+128.0);//Convert to 0-256 values.
getnoise = (getnoise < 0) ? 0 : getnoise;
return getnoise;
}
int main()
{
// Create the main window
sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL");
// Set color and depth clear value
glClearDepth(1.f);
glClearColor(0.f, 0.f, 0.f, 0.f);
// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 500.f);
// Start game loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
// Escape key : exit
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
// Resize event : adjust viewport
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
}
// Set the active window before using OpenGL commands
// It's useless here because active window is always the same,
// but don't forget it if you use multiple windows or controls
App.SetActive();
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Apply some transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.f, 0.f, -150.f);
glPointSize(2.5f);
// Draw a cube
glBegin(GL_POINTS);
for (int x = -100; x < 100; x++)
for (int y = -100; y < 100; y++)
{
float n = perlin(x, y);
//glColor3f(n, n, n);
glColor3f(n, n, n);
glVertex2f(x, y);
}
glEnd();
// Finally, display rendered frame on screen
App.Display();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment