This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <lua.hpp> | |
int main(int argc, char *argv[]) { | |
// create a new lua context to work with | |
lua_State *L = luaL_newstate(); | |
// open any library we may use | |
luaL_openlibs(L); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (int x = 0; x < w; x ++) { | |
for (int y = 0; y < h; y ++) { | |
float xx = (float)x / (float)this->width; | |
float yy = (float)y / (float)this->height; | |
map[x + (y * w)] = perlin::perlin2d( | |
xx, yy, | |
6, 1.02f | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float perlin2d(const float x, const float y, | |
const int octaves, const float persistence) { | |
float total = 0.0f; | |
for (int i = 0; i <= (octaves - 1); i ++) { | |
float frequency = powf(2, i); | |
float amplitude = powf(persistence, i); | |
total = total + interpolateNoise(x * frequency, y * frequency) * amplitude; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Linear interpolation */ | |
float lerp(float a, float b, float x) { | |
return a * (1 - x) + b * x; | |
} | |
/* Trigonometric interpolation */ | |
float terp(float a, float b, float x) { | |
float ft = x * 3.1415927f; | |
float f = (1 - cosf(ft)) * 0.5f; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float smoothNoise(const float x, const float y) { | |
int ix = (int)x; | |
int iy = (int)y; | |
// sample the corners | |
float corners = (noise(ix - 1, iy - 1) + | |
noise(ix + 1, iy - 1) + | |
noise(ix - 1, iy + 1) + | |
noise(ix + 1, iy + 1)) / 16; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float noise(const int x, const int y) { | |
int n = x + y * 57; | |
n = (n << 13) ^ n; | |
return (1.0f - ( ( n * (n * n * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0f); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void mouseMotion(int x, int y) { | |
// calculate the origin point (shifting right divides by two, remember!) | |
int halfWidth = windowWidth >> 1; | |
int halfHeight = windowHeight >> 1; | |
// calculate how far we deviated from the origin point and deaden this | |
// by a factor of 20 | |
float deltaX = (halfWidth - x) / 20.0f; | |
float deltaY = (halfHeight - y) / 20.0f; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void camera::place(void) { | |
glm::vec3 viewPoint = this->position + this->viewDir; | |
// setup opengl with gluLookAt | |
gluLookAt( | |
position[0], position[1], position[2], | |
viewPoint[0], viewPoint[1], viewPoint[2], | |
upVector[0], upVector[1], upVector[2] | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// z movement | |
void camera::advance(const float distance) { | |
this->position += (this->viewDir * -distance); | |
} | |
// y movement | |
void camera::ascend(const float distance) { | |
this->position += (this->upVector * distance); | |
} |