Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1. Fixed out of bounds memory accesses in surfaceNormal
2. World can have other than 256x256 dims
3. Fixed compilation errors and warings
msokalski committed Jun 22, 2021
commit 6b75ec1c2d7a771356e74996130fc39a94c5c6fb
4 changes: 2 additions & 2 deletions SimpleHydrology.cpp
Original file line number Diff line number Diff line change
@@ -166,8 +166,8 @@ int main( int argc, char* args[] ) {
model.construct(constructor); //Reconstruct Updated Model

//Redraw the Path and Death Image
if(viewmap)
map.raw(image::make<double>(world.dim, world.waterpath, world.waterpool, hydromap));
if(viewmap) // make map transposed
map.raw(image::makeT<double>(world.dim, world.waterpath, world.waterpool, hydromap));
}
});

2 changes: 2 additions & 0 deletions TinyEngine/TinyEngine.h
Original file line number Diff line number Diff line change
@@ -79,6 +79,8 @@ bool init(std::string windowName, int width, int height){
std::cout<<"Failed to launch audio interface."<<std::endl;
return false;
}

return true;
}

void quit(){
24 changes: 24 additions & 0 deletions TinyEngine/include/helpers/image.h
Original file line number Diff line number Diff line change
@@ -50,4 +50,28 @@ namespace image {
SDL_UnlockSurface(s);
return s;
}


template<typename T>
SDL_Surface* makeT(glm::ivec2 size, T* data1, T* data2, std::function<glm::vec4(T, T)> handle){
SDL_Surface *s = SDL_CreateRGBSurface(0, size.x, size.y, 32, 0, 0, 0, 0);
SDL_LockSurface(s);

unsigned char* img_raw = (unsigned char*)s->pixels; //Raw Data

// transpose!
for (int x=0, i=0; x<size.x; x++)
for (int y=0; y<size.y; y++, i++)
{
int j = x+y*size.x;
glm::vec4 color = handle(data1[i], data2[i]); //Construct from Algorithm
*(img_raw+4*j) = (unsigned char)(255*color.x);
*(img_raw+4*j+1) = (unsigned char)(255*color.y);
*(img_raw+4*j+2) = (unsigned char)(255*color.z);
*(img_raw+4*j+3) = (unsigned char)(255*color.w);
}

SDL_UnlockSurface(s);
return s;
}
};
Binary file modified hydrology
Binary file not shown.
1 change: 1 addition & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ TINYLINK = -lX11 -lpthread -lSDL2 -lnoise -lSDL2_image -lSDL2_mixer -lSDL2_ttf -

CC = g++ -std=c++17
COMPILER_FLAGS = -Wfatal-errors -O3
#COMPILER_FLAGS = -Wfatal-errors -g -fsanitize=address
LINKER_FLAGS = -I/usr/local/include -L/usr/local/lib -lnoise
OBJ_NAME = hydrology
all: $(OBJS)
22 changes: 12 additions & 10 deletions source/vegetation.h
Original file line number Diff line number Diff line change
@@ -33,36 +33,38 @@ void Plant::grow(){
size += rate*(maxsize-size);
};


void Plant::root(double* density, glm::ivec2 dim, double f){

//Can always do this one
density[index] += f*1.0;

if(pos.x > 0){
//
density[index - 256] += f*0.6; //(-1, 0)
density[index - dim.y] += f*0.6; //(-1, 0)

if(pos.y > 0)
density[index - 257] += f*0.4; //(-1, -1)
density[index - dim.y - 1] += f*0.4; //(-1, -1)

if(pos.y < 256-1)
density[index - 255] += f*0.4; //(-1, 1)
if(pos.y < dim.y - 1)
density[index - dim.y + 1] += f*0.4; //(-1, 1)
}

if(pos.x < 256-1){
if(pos.x < dim.x - 1){
//
density[index + 256] += f*0.6; //(1, 0)
density[index + dim.y] += f*0.6; //(1, 0)

if(pos.y > 0)
density[index + 255] += f*0.4; //(1, -1)
density[index + dim.y - 1] += f*0.4; //(1, -1)

if(pos.y < 256-1)
density[index + 257] += f*0.4; //(1, 1)
if(pos.y < dim.y - 1)
density[index + dim.y + 1] += f*0.4; //(1, 1)
}

if(pos.y > 0)
density[index - 1] += f*0.6; //(0, -1)

if(pos.y < 256-1)
if(pos.y < dim.y - 1)
density[index + 1] += f*0.6; //(0, 1)
}

22 changes: 18 additions & 4 deletions source/water.h
Original file line number Diff line number Diff line change
@@ -31,12 +31,26 @@ struct Drop{
glm::vec3 surfaceNormal(int index, double* h, glm::ivec2 dim, double scale){

//Two large triangels adjacent to the plane (+Y -> +X) (-Y -> -X)
glm::vec3 n = glm::cross(glm::vec3(0.0, scale*(h[index+1]-h[index]), 1.0), glm::vec3(1.0, scale*(h[index+dim.y]-h[index]), 0.0));
n += glm::cross(glm::vec3(0.0, scale*(h[index-1]-h[index]), -1.0), glm::vec3(-1.0, scale*(h[index-dim.y]-h[index]), 0.0));
glm::vec3 n = {0,0,0};

// would be better to get x,y
// right in the arguments instead of index
int x = index / dim.y;
int y = index % dim.y;

if (x<dim.x-1 && y<dim.y-1)
n += glm::cross(glm::vec3(0.0, scale*(h[index+1]-h[index]), 1.0), glm::vec3(1.0, scale*(h[index+dim.y]-h[index]), 0.0));

if (x>0 && y>0)
n += glm::cross(glm::vec3(0.0, scale*(h[index-1]-h[index]), -1.0), glm::vec3(-1.0, scale*(h[index-dim.y]-h[index]), 0.0));

//Two Alternative Planes (+X -> -Y) (-X -> +Y)
n += glm::cross(glm::vec3(1.0, scale*(h[index+dim.y]-h[index]), 0.0), glm::vec3(0.0, scale*(h[index-1]-h[index]), -1.0));
n += glm::cross(glm::vec3(-1.0, scale*(h[index-dim.y]-h[index]), 0.0), glm::vec3(0.0, scale*(h[index+1]-h[index]), 1.0));

if (x<dim.x-1 && y>0)
n += glm::cross(glm::vec3(1.0, scale*(h[index+dim.y]-h[index]), 0.0), glm::vec3(0.0, scale*(h[index-1]-h[index]), -1.0));

if (x>0 && y<dim.y-1)
n += glm::cross(glm::vec3(-1.0, scale*(h[index-dim.y]-h[index]), 0.0), glm::vec3(0.0, scale*(h[index+1]-h[index]), 1.0));

return glm::normalize(n);
}
27 changes: 19 additions & 8 deletions source/world.h
Original file line number Diff line number Diff line change
@@ -8,18 +8,22 @@ class World{
void erode(int cycles); //Erode with N Particles
void grow();

static const int width = 256;
static const int height = 256;
static const int size = width*height;

int SEED = 0;
glm::ivec2 dim = glm::vec2(256, 256); //Size of the heightmap array
glm::ivec2 dim = glm::vec2(width, height); //Size of the heightmap array

double scale = 40.0; //"Physical" Height scaling of the map
double heightmap[256*256] = {0.0}; //Flat Array
double heightmap[size] = {0.0}; //Flat Array

double waterpath[256*256] = {0.0}; //Water Path Storage (Rivers)
double waterpool[256*256] = {0.0}; //Water Pool Storage (Lakes / Ponds)
double waterpath[size] = {0.0}; //Water Path Storage (Rivers)
double waterpool[size] = {0.0}; //Water Pool Storage (Lakes / Ponds)

//Trees
std::vector<Plant> trees;
double plantdensity[256*256] = {0.0}; //Density for Plants
double plantdensity[size] = {0.0}; //Density for Plants

//Erosion Process
bool active = false;
@@ -50,8 +54,14 @@ void World::generate(){
perlin.SetPersistence(0.5);

double min, max = 0.0;

for(int i = 0; i < dim.x*dim.y; i++){
heightmap[i] = perlin.GetValue((i/dim.y)*(1.0/dim.x), (i%dim.y)*(1.0/dim.y), SEED);

double x = i / dim.y;
double y = i % dim.y;

heightmap[i] = perlin.GetValue(x/dim.x, y/dim.y, SEED);

if(heightmap[i] > max) max = heightmap[i];
if(heightmap[i] < min) min = heightmap[i];
}
@@ -205,7 +215,7 @@ glm::mat4 biasMatrix = glm::mat4(
0.5, 0.5, 0.5, 1.0
);

std::function<void(Model* m)> constructor = [&](Model* m){
std::function<void(Model* m)> constructor = [](Model* m){
//Clear the Containers
m->indices.clear();
m->positions.clear();
@@ -331,7 +341,7 @@ std::function<void(Model* m)> constructor = [&](Model* m){
}
};

std::function<void()> eventHandler = [&](){
std::function<void()> eventHandler = [](){

if(!Tiny::event.scroll.empty()){

@@ -353,6 +363,7 @@ std::function<void()> eventHandler = [&](){
}

//Adjust Stuff

if(rotation < 0.0) rotation = 360.0 + rotation;
else if(rotation > 360.0) rotation = rotation - 360.0;
camera = glm::rotate(glm::lookAt(cameraPos, lookPos, glm::vec3(0,1,0)), glm::radians(rotation), glm::vec3(0,1,0));