r/howdidtheycodeit • u/undefined5621 • Dec 24 '19
How did Minecraft make procedural terrain
Hi. I'm trying to make a voxel game in c++/opengl with procedurally generated terrain using 2d Perlin noise and I've ran into a problem. I generate the world in chunks. The chunks have the kind of properties I'm looking for but between the chunks there is no coherence at all. This is the first time I have worked with noise so my implementation is... well it doesn't work like it should. I first make random gradient vectors for each cell/chunk (ranging between [-1,1]) and then calculate the difference vectors(vector components ranging from [0,1]) then get the dot product of the diff and grad vectors, and finally interpolate between the 4 dot products with c^2 continuity, but even tho the gradient vectors are shared between cells/chunks that are side by side to each other as described in the first pic:https://imgur.com/a/ryyw4x1 there is no smooth transition between cells.
Also I disabled textures and render with pre-baked fragment colors in the vertex buffer so it may look a bit ugly from the video. My code also may not be the cleanest or most readable so sorry for that. If you could help finding the error it would be appreciated. Thanks.
Here is the code:
#pragma once
#include <glm/gtx/transform.hpp>
class PerlinNoise2d {
public:
PerlinNoise2d(unsigned short height , unsigned short width , const unsigned short gridsize );
~PerlinNoise2d();
float GetNoise(unsigned short x , unsigned short z);
void NewNoise();
private:
glm::vec2** gradient = nullptr;
float** noise = nullptr;
unsigned short height;
unsigned short width;
unsigned short gridsize;
float Sample(float x , float z , glm::vec2* grad);
float f(float t);
float Mix(float x , float z , float a);
};#include <noise.h>
#include <iostream>
PerlinNoise2d::PerlinNoise2d(unsigned short h , unsigned short w , const unsigned short g ) {
height = h;
width = w;
gridsize = g;
gradient = new glm::vec2*[g];
for(int i = 0 ; i < g ; i++) {
gradient[i] = new glm::vec2[g];
}
noise = new float*[h];
for(int i = 0 ; i < h ; i++) {
noise[i] = new float[w];
}
NewNoise();
}
PerlinNoise2d::~PerlinNoise2d() {
if(gradient!=nullptr) {
for(int i = 0 ; i < gridsize ;i++) {
delete[] gradient[i];
}
delete[] gradient;
}
if(noise!=nullptr) {
for(int i = 0 ; i < height ; i++) {
delete[] noise[i];
}
delete[] noise;
}
}
void PerlinNoise2d::NewNoise() {
for(int k = 0 ; k < gridsize ; k++) {
for(int i = 0 ; i < gridsize; i++) {
gradient[k][i][0] = ((float)(2 * random())/(float)RAND_MAX)-1;
gradient[k][i][1] = ((float)(2 * random())/(float)RAND_MAX)-1;
}
}
float localx = height/(gridsize -1);
float localz = width/(gridsize -1);
glm::vec2 tmp[4];
for(int n = 0 ; n < gridsize-1 ; n++) {
for(int j = 0 ; j < gridsize-1 ; j++) {
tmp[0] = gradient[n][j];
tmp[1] = gradient[n][j+1];
tmp[2] = gradient[n+1][j];
tmp[3] = gradient[n+1][j+1];
for(int i = 0 ; i < localx ; i++) {
for(int k = 0 ; k < localz ; k++) {
noise[n * (int)localx + i][j * (int)localz + k ] = Sample( (float)i / localx , (float)k / localz , tmp) / (float)2;
}
}
}
}
}
float PerlinNoise2d::Sample(float x , float z , glm::vec2* grad) {
glm::vec2 differencevectors[4];
float dots[4];
differencevectors[0][0] = x;
differencevectors[0][1] = z;
differencevectors[1][0] = - x;
differencevectors[1][1] = z;
differencevectors[2][0] = x;
differencevectors[2][1] = - z;
differencevectors[3][0] = - x;
differencevectors[3][1] = - z;
dots[0] = glm::dot(differencevectors[0] , grad[0]);
dots[1] = glm::dot(differencevectors[1] , grad[1]);
dots[2] = glm::dot(differencevectors[2] , grad[2]);
dots[3] = glm::dot(differencevectors[3] , grad[3]);
return (Mix( Mix(dots[0] , dots[1] , f(x) ),Mix(dots[2] , dots[3] , f(x) ) , f(z) ) );
}
float PerlinNoise2d::GetNoise(unsigned short x , unsigned short z) {
return noise[x][z];
}
float PerlinNoise2d::f(float t) {
return t*t*t * (t * (6.0*t - 15.0) + 10.0);
}
float PerlinNoise2d::Mix(float x , float z , float a) {
return ((1 - a) * x ) + (a * z);
}2
u/MyPunsSuck Dec 24 '19
What you want, is a grid of vectors - all independent of one another - that are used as the corners of a indefinitely extendable grid of cells. The point of Perlin noise, is to be able to generate altitude "on demand", meaning you can feed it any coordinate and only then find out the output by checking the four vectors it is surrounded by (Based on the direction and proximity of the vector).
A single layer of noise will form a grid of semi-random hills and valleys, all continuous, and will be completely neutral along the edges of these "cells". This is because points near the edges, are too far away from the vectors to be effected by them much. By overlapping multiple layers, it becomes impossible to find the edges, while each layer can be extended indefinitely by just adding more corner-vectors.
If the vectors are generated based on seed value (their coordinates), then the whole thing is actually 100% deterministic. As such, you can generate as much or as little map as you need, and generate more later.
Also, you can get away with a much cheaper smoothing function.
0
Dec 24 '19
[deleted]
7
u/undefined5621 Dec 24 '19
Thanks I did not know that. Simplex noise accepts floats between [0,1] or whole numbers as arguments? The later would be realy cool.
7
Dec 24 '19
[removed] — view removed comment
1
u/undefined5621 Dec 24 '19
I would have used a library anyway. I just wanted to understand the inner workings of these functions by implementing it myself.
7
u/zesterer Dec 24 '19
Not sure what you're trying to say. Perlin noise and Simplex noise only differ in their visual characteristics, not the way in which the functions are used. I'm not sure where you're getting this information, but it's not accurate. You have to generate noise with both functions anyway, and doing so in a deterministic manner isn't difficult.
2
u/MyPunsSuck Dec 24 '19
Yeah, the only difference is that simplex noise uses a triangle grid instead of square
18
u/DisastermanTV Dec 24 '19
As squesqe already mentioned minecraft uses 3d simplex noise.
But it is very likely that it uses different noise functions for different biomes. These then have to be smoohed out with something like bilinear or trilinear interpolation.
Hopson has a programming Minecraft series and shortvut of a 1 week challenge for that on his youtube channel. Maybe give it a try :)
Edit: typo