Browse Source

New noise with opensimplex

testing
= 4 years ago
parent
commit
8d4d550ab4

+ 26
- 0
include/terrain/noise.h View File

@@ -0,0 +1,26 @@
/*
* Simple noise module utilizing smcameron's c port of OpenSimplex
*/

#pragma once

#include "terrain/open_simplex_noise.h"
#include "terrain/noisemodule.h"

namespace vtk {

class Noise : public NoiseModule {
public:
Noise(const int64_t& seed);
~Noise();
double get2D(const double& x, const double& y);
double get3D(const double& x, const double& y, const double& z);
void setScale(const double& scale);

protected:
double mScale;
struct osn_context* mCtx;
};

}

+ 15
- 0
include/terrain/noisemodule.h View File

@@ -0,0 +1,15 @@
/*
* Module on which noise generation objects sit
*/

#pragma once

namespace vtk {

class NoiseModule {
public:
virtual double get2D(const double& x, const double& y){ return 0.0; }
virtual double get3D(const double& x, const double& y, const double& z) { return 0.0; }
};

}

+ 52
- 0
include/terrain/open_simplex_noise.h View File

@@ -0,0 +1,52 @@
#ifndef OPEN_SIMPLEX_NOISE_H__
#define OPEN_SIMPLEX_NOISE_H__

/*
* OpenSimplex (Simplectic) Noise in C.
* Ported to C from Kurt Spencer's java implementation by Stephen M. Cameron
*
* v1.1 (October 6, 2014)
* - Ported to C
*
* v1.1 (October 5, 2014)
* - Added 2D and 4D implementations.
* - Proper gradient sets for all dimensions, from a
* dimensionally-generalizable scheme with an actual
* rhyme and reason behind it.
* - Removed default permutation array in favor of
* default seed.
* - Changed seed-based constructor to be independent
* of any particular randomization library, so results
* will be the same when ported to other languages.
*/

#if ((__GNUC_STDC_INLINE__) || (__STDC_VERSION__ >= 199901L))
#include <stdint.h>
#define INLINE inline
#elif (defined (_MSC_VER) || defined (__GNUC_GNU_INLINE__))
#include <stdint.h>
#define INLINE __inline
#else
/* ANSI C doesn't have inline or stdint.h. */
#define INLINE
#endif

#ifdef __cplusplus
extern "C" {
#endif

struct osn_context;

int open_simplex_noise(int64_t seed, struct osn_context **ctx);
void open_simplex_noise_free(struct osn_context *ctx);
int open_simplex_noise_init_perm(struct osn_context *ctx, int16_t p[], int nelements);
double open_simplex_noise2(struct osn_context *ctx, double x, double y);
double open_simplex_noise3(struct osn_context *ctx, double x, double y, double z);
double open_simplex_noise4(struct osn_context *ctx, double x, double y, double z, double w);

#ifdef __cplusplus
}
#endif

#endif


+ 26
- 0
include/terrain/ygradient.h View File

@@ -0,0 +1,26 @@
/*
* Prouces a Y axis gradient with -1.0 at start and 1.0 at finish
*/

#pragma once

#include "terrain/noisemodule.h"

namespace vtk {

class YGradient : public NoiseModule {
public:
YGradient();
YGradient(const double& start, const double& end);

void setStart(const double& start);
void setEnd(const double& end);

double get3D(const double& x, const double& y, const double& z);

protected:
double mStart;
double mEnd;
};

}

+ 4
- 8
include/terraingen.h View File

@@ -18,10 +18,10 @@

#pragma once

#include <noise/noise.h>
#include <map>
#include <utility>

#include "terrain/noisemodule.h"

namespace vtk {

@@ -33,15 +33,11 @@ public:

void generateChunk(Chunk* chunk);

//libnoise modules
noise::module::Select selectorMod;
noise::module::Const const1Mod;
noise::module::Const const0Mod;
noise::module::Const constNeg1Mod;
noise::module::Perlin terrainShape;
protected:
NoiseModule* mNoise;

//variables (replace by config)
double terrainScale;
double mTerrainScale;
};

}

+ 1
- 1
src/scenes/testscene.cpp View File

@@ -126,7 +126,7 @@ void TestScene::init() {
int chunkCount = 1;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
for (int k = 0; k < 30; k++) {
for (int k = 0; k < 8; k++) {
std::cout << "\rGenerating chunks (" << chunkCount << "/" << 8*8*8 << ")" << std::flush;
world.generateChunk(i,j,k);
chunkCount++;

+ 33
- 0
src/terrain/noise.cpp View File

@@ -0,0 +1,33 @@
#include "terrain/noise.h"

namespace vtk {

Noise::Noise(const int64_t& seed) {
mScale = 32.0;
open_simplex_noise(seed, &mCtx);
}

Noise::~Noise() {
open_simplex_noise_free(mCtx);
}

double Noise::get2D(const double& x, const double& y) {
return open_simplex_noise2(mCtx,
x / mScale,
y / mScale);
}

double Noise::get3D(const double& x, const double& y, const double& z) {
return open_simplex_noise3(mCtx,
x / mScale,
y / mScale,
z / mScale);

}

void Noise::setScale(const double& scale) {
mScale = scale;
}

}

+ 1
- 0
src/terrain/noisemodule.cpp View File

@@ -0,0 +1 @@
#include "terrain/noisemodule.h"

+ 2249
- 0
src/terrain/open_simplex_noise.cpp
File diff suppressed because it is too large
View File


+ 29
- 0
src/terrain/ygradient.cpp View File

@@ -0,0 +1,29 @@
#include "terrain/ygradient.h"

namespace vtk {

YGradient::YGradient() {
mStart = 0.0;
mEnd = 0.0;
}

YGradient::YGradient(const double& start, const double& end) {
mStart = start;
mEnd = end;
}

void YGradient::setStart(const double& start) {
mStart = start;
}

void YGradient::setEnd(const double& end) {
mEnd = end;
}

double YGradient::get3D(const double& x, const double& y, const double& z) {
double factor = (y - mStart) / (mEnd - mStart);
if (factor >= 1.0) return 1.0;
else if (factor <= 0.0) return -1.0;
return (factor * 2.0) - 1.0;
}
}

+ 14
- 0
src/terraingen.cpp View File

@@ -19,6 +19,8 @@
#include "terraingen.h"
#include "chunk.h"
#include "mathplus.h"
#include "terrain/noise.h"
#include "terrain/ygradient.h"

#include <iostream>
#include <time.h>
@@ -27,6 +29,9 @@
namespace vtk {

TerrainGen::TerrainGen() {
mNoise = new YGradient(0.0, 128.0);

mTerrainScale = 32.0;
}

void TerrainGen::generateChunk(Chunk* chunk) {
@@ -34,6 +39,15 @@ void TerrainGen::generateChunk(Chunk* chunk) {
for (int i = 0; i < 16; i++) { //x
for (int j = 0; j < 16; j++) { //y
for (int k = 0; k < 16; k++) { //z
double nVal = mNoise->get3D(
(double)(chunk->chunkPos.x * 16 + i),
(double)(chunk->chunkPos.y * 16 + j),
(double)(chunk->chunkPos.z * 16 + k));
if (nVal <= 0.0) {
chunk->setVoxelType(i,j,k,1);
} else {
chunk->setVoxelType(i,j,k,0);
}
/*
glm::dvec3 pos((double)(chunk->chunkPos.x * 16 + i) * terrainScale,
(double)(chunk->chunkPos.y * 16 + j) * terrainScale,

Loading…
Cancel
Save