Skip to content

Instantly share code, notes, and snippets.

@tuttlem
tuttlem / lua1.cpp
Last active January 2, 2016 13:29
Lua varaible reader
#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);
@tuttlem
tuttlem / test.lua
Created January 8, 2014 02:21
Testing Lua script
x = 10
@tuttlem
tuttlem / noise.cpp
Created December 31, 2013 06:34
Noise 5
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
);
}
@tuttlem
tuttlem / noise.cpp
Created December 31, 2013 06:32
Noise 4
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;
}
@tuttlem
tuttlem / noise.cpp
Created December 31, 2013 06:26
Noise 3
/* 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;
@tuttlem
tuttlem / noise.cpp
Last active January 1, 2016 19:48
Noise 2
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;
@tuttlem
tuttlem / noise.cpp
Created December 31, 2013 03:51
Noise 1
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);
}
@tuttlem
tuttlem / mouse.cpp
Created December 30, 2013 09:42
Camera mouse
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;
@tuttlem
tuttlem / place.cpp
Created December 30, 2013 06:56
Camera place
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]
);
}
@tuttlem
tuttlem / move.cpp
Created December 30, 2013 06:52
Camera movement
// 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);
}