Added terrain generation using FastNoiseLite
This commit is contained in:
parent
8365b148d0
commit
13828992d4
@ -11,7 +11,6 @@ extern NodeManager nodeManager;
|
|||||||
extern BlockManager blockManager;
|
extern BlockManager blockManager;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct Position2D
|
struct Position2D
|
||||||
{
|
{
|
||||||
int x;
|
int x;
|
||||||
|
2586
include/FastNoiseLite.h
Normal file
2586
include/FastNoiseLite.h
Normal file
File diff suppressed because it is too large
Load Diff
24
include/Utilities.h
Normal file
24
include/Utilities.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#ifndef UTILITIES
|
||||||
|
#define UTILITIES
|
||||||
|
|
||||||
|
|
||||||
|
class Utilities
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Utilities()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static float degToRad(int degrees)
|
||||||
|
{
|
||||||
|
return 3.14 / 180 * degrees; // Only the first three digits of pi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
53
main.cpp
53
main.cpp
@ -1,11 +1,13 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <GL/glut.h>
|
#include <GL/glut.h>
|
||||||
|
#include "Utilities.h"
|
||||||
#include "MapBlock.h"
|
#include "MapBlock.h"
|
||||||
#include "Base.h"
|
#include "Base.h"
|
||||||
#include "NodeRenderer.h"
|
#include "NodeRenderer.h"
|
||||||
#include "TextureHandler.h"
|
#include "TextureHandler.h"
|
||||||
|
#include <math.h>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include "FastNoiseLite.h"
|
||||||
#include <random>
|
#include <random>
|
||||||
//#include <SFML/System.hpp>
|
//#include <SFML/System.hpp>
|
||||||
//#include <SFML/Window.hpp>
|
//#include <SFML/Window.hpp>
|
||||||
@ -18,9 +20,10 @@ BlockManager blockManager;
|
|||||||
TextureHandler textureHandler;
|
TextureHandler textureHandler;
|
||||||
|
|
||||||
GLfloat playerX = 0;
|
GLfloat playerX = 0;
|
||||||
GLfloat playerY = -30;
|
GLfloat playerY = 30;
|
||||||
GLfloat playerZ = -100;
|
GLfloat playerZ = 100;
|
||||||
GLfloat playerRotX = 0;
|
GLfloat playerRotX = 0;
|
||||||
|
GLfloat playerRotY = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -29,9 +32,10 @@ void display()
|
|||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
|
||||||
glTranslatef(playerX, playerY, playerZ);
|
glTranslatef(-playerX, -playerY, -playerZ);
|
||||||
glRotatef(playerRotX, .0F, 1.0F, .0F);
|
glRotatef(playerRotX, 1.0F, .0F, .0F);
|
||||||
|
glRotatef(playerRotY, .0F, 1.0F, .0F);
|
||||||
|
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
glEnable(GL_TEXTURE_2D);
|
glEnable(GL_TEXTURE_2D);
|
||||||
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
|
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
|
||||||
|
|
||||||
@ -39,11 +43,11 @@ void display()
|
|||||||
glBegin(GL_QUADS);
|
glBegin(GL_QUADS);
|
||||||
|
|
||||||
|
|
||||||
for(int x = 0; x < 64; x++)
|
for(int x = 0; x < 128; x++)
|
||||||
{
|
{
|
||||||
for(int z = 0; z < 64; z++)
|
for(int z = 0; z < 128; z++)
|
||||||
{
|
{
|
||||||
for(int y = 0; y < 256; y++)
|
for(int y = 0; y < 64; y++)
|
||||||
{
|
{
|
||||||
Position2D block = BlockUtilities::getBlockFromNodeCoordinates(x, z);
|
Position2D block = BlockUtilities::getBlockFromNodeCoordinates(x, z);
|
||||||
|
|
||||||
@ -69,8 +73,8 @@ void display()
|
|||||||
const sf::Window& window = nullptr;
|
const sf::Window& window = nullptr;
|
||||||
sf::Vector2i mouseDelta = sf::Mouse::getPosition(window) - lastMousePos;
|
sf::Vector2i mouseDelta = sf::Mouse::getPosition(window) - lastMousePos;
|
||||||
lastMousePos = sf::Mouse::getPosition(window);*/
|
lastMousePos = sf::Mouse::getPosition(window);*/
|
||||||
|
|
||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -79,7 +83,7 @@ void reshape(int width, int height)
|
|||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
|
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
gluPerspective(20, width / (float) height, 5, 150);
|
gluPerspective(20, width / (float) height, 5, 512.0F);
|
||||||
glViewport(0, 0, width, height);
|
glViewport(0, 0, width, height);
|
||||||
|
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
@ -105,12 +109,14 @@ void KeyboardFunc(unsigned char key, int x, int y)
|
|||||||
|
|
||||||
if(key == 'w')
|
if(key == 'w')
|
||||||
{
|
{
|
||||||
playerZ += .8F;
|
playerX += .8;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(key == 's')
|
if(key == 's')
|
||||||
{
|
{
|
||||||
playerZ -= .8F;
|
playerX -= sin(Utilities::degToRad(playerRotY));
|
||||||
|
playerY += sin(Utilities::degToRad(playerRotX));
|
||||||
|
playerZ += cos(Utilities::degToRad(playerRotY));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(key == 'q')
|
if(key == 'q')
|
||||||
@ -123,16 +129,26 @@ void KeyboardFunc(unsigned char key, int x, int y)
|
|||||||
playerY -= .1F;
|
playerY -= .1F;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(key == 'f')
|
if(key == 't')
|
||||||
{
|
{
|
||||||
playerRotX -= .8F;
|
playerRotX -= .8F;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(key == 'h')
|
if(key == 'g')
|
||||||
{
|
{
|
||||||
playerRotX += .8F;
|
playerRotX += .8F;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(key == 'f')
|
||||||
|
{
|
||||||
|
playerRotY -= .8F;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(key == 'h')
|
||||||
|
{
|
||||||
|
playerRotY += .8F;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -165,6 +181,10 @@ int main(int argc, char **argv)
|
|||||||
textureHandler.loadAllTextures();
|
textureHandler.loadAllTextures();
|
||||||
|
|
||||||
|
|
||||||
|
FastNoiseLite fnl;
|
||||||
|
fnl.SetSeed(1337);
|
||||||
|
fnl.SetNoiseType(FastNoiseLite::NoiseType_Value);
|
||||||
|
fnl.SetFrequency(.01F);
|
||||||
|
|
||||||
for(int bx = 0; bx < 8; bx++)
|
for(int bx = 0; bx < 8; bx++)
|
||||||
{
|
{
|
||||||
@ -174,9 +194,10 @@ int main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
for(int z = 0; z < 16; z++)
|
for(int z = 0; z < 16; z++)
|
||||||
{
|
{
|
||||||
for(int y = 0; y < 16; y++)
|
for(int y = 0; y < 48 * abs(fnl.GetNoise((float)x + (16 * bx), (float)z + (16 * bz))) + 2; y++)
|
||||||
{
|
{
|
||||||
blockManager.mapBlocks[bx][bz].addNode(1, 0, x, y, z);
|
blockManager.mapBlocks[bx][bz].addNode(1, 0, x, y, z);
|
||||||
|
//printf("\nx: %i, z: %i", x + (16 * bx), z + (16 * bz));
|
||||||
//printf("\nGet node at: %i\n Is air: %i", nodeManager.getNodeAt(x, y + 1, z) == 0, nodeManager.isAir(x, y + 1, z));
|
//printf("\nGet node at: %i\n Is air: %i", nodeManager.getNodeAt(x, y + 1, z) == 0, nodeManager.isAir(x, y + 1, z));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user