Fix issue in HeightField and user Perlin noise to generate terrain
This commit is contained in:
parent
7dae30718b
commit
eeb1052833
@ -57,7 +57,7 @@ HeightFieldShape::HeightFieldShape(int nbWidthGridPoints, int nbLengthGridPoints
|
|||||||
mHeightFieldData = heightFieldData;
|
mHeightFieldData = heightFieldData;
|
||||||
|
|
||||||
decimal halfHeight = (mMaxHeight - mMinHeight) * decimal(0.5);
|
decimal halfHeight = (mMaxHeight - mMinHeight) * decimal(0.5);
|
||||||
assert(halfHeight > 0);
|
assert(halfHeight >= 0);
|
||||||
|
|
||||||
// Compute the local AABB of the height field
|
// Compute the local AABB of the height field
|
||||||
if (mUpAxis == 0) {
|
if (mUpAxis == 0) {
|
||||||
|
@ -198,22 +198,23 @@ inline Vector3 HeightFieldShape::getVertexAt(int x, int y) const {
|
|||||||
// Get the height value
|
// Get the height value
|
||||||
const decimal height = getHeightAt(x, y);
|
const decimal height = getHeightAt(x, y);
|
||||||
|
|
||||||
// Get the difference between the center of AABB and zero level of the height field
|
// Height values origin
|
||||||
const decimal originToZeroHeight = (mMaxHeight - mMinHeight) * decimal(0.5);
|
const decimal heightOrigin = -(mMaxHeight - mMinHeight) * decimal(0.5) - mMinHeight;
|
||||||
|
|
||||||
Vector3 vertex;
|
Vector3 vertex;
|
||||||
switch (mUpAxis) {
|
switch (mUpAxis) {
|
||||||
case 0: vertex = Vector3(height - originToZeroHeight, -mWidth * decimal(0.5) + x, -mLength * decimal(0.5) + y) * mScaling;
|
case 0: vertex = Vector3(heightOrigin + height, -mWidth * decimal(0.5) + x, -mLength * decimal(0.5) + y);
|
||||||
break;
|
break;
|
||||||
case 1: vertex = Vector3(-mWidth * decimal(0.5) + x, height - originToZeroHeight, -mLength * decimal(0.5) + y) * mScaling;
|
case 1: vertex = Vector3(-mWidth * decimal(0.5) + x, heightOrigin + height, -mLength * decimal(0.5) + y);
|
||||||
break;
|
break;
|
||||||
case 2: vertex = Vector3(-mWidth * decimal(0.5) + x, -mLength * decimal(0.5) + y, height - originToZeroHeight) * mScaling;
|
case 2: vertex = Vector3(-mWidth * decimal(0.5) + x, -mLength * decimal(0.5) + y, heightOrigin + height);
|
||||||
break;
|
break;
|
||||||
default: assert(false);
|
default: assert(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(mAABB.contains(vertex));
|
assert(mAABB.contains(vertex));
|
||||||
return vertex;
|
|
||||||
|
return vertex * mScaling;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the height of a given (x,y) point in the height field
|
// Return the height of a given (x,y) point in the height field
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
|
|
||||||
// Libraries
|
// Libraries
|
||||||
#include "HeightField.h"
|
#include "HeightField.h"
|
||||||
|
#include "PerlinNoise.h"
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
HeightField::HeightField(const openglframework::Vector3 &position,
|
HeightField::HeightField(const openglframework::Vector3 &position,
|
||||||
@ -47,7 +48,7 @@ HeightField::HeightField(const openglframework::Vector3 &position,
|
|||||||
|
|
||||||
// Create the collision shape for the rigid body (convex mesh shape) and
|
// Create the collision shape for the rigid body (convex mesh shape) and
|
||||||
// do not forget to delete it at the end
|
// do not forget to delete it at the end
|
||||||
mHeightFieldShape = new rp3d::HeightFieldShape(HEIGHTFIELD_WIDTH, HEIGHTFIELD_LENGTH, 0, 5,
|
mHeightFieldShape = new rp3d::HeightFieldShape(NB_POINTS_WIDTH, NB_POINTS_LENGTH, mMinHeight, mMaxHeight,
|
||||||
mHeightData, rp3d::HeightFieldShape::HEIGHT_FLOAT_TYPE);
|
mHeightData, rp3d::HeightFieldShape::HEIGHT_FLOAT_TYPE);
|
||||||
|
|
||||||
// Initial position and orientation of the rigid body
|
// Initial position and orientation of the rigid body
|
||||||
@ -90,7 +91,7 @@ HeightField::HeightField(const openglframework::Vector3 &position, float mass,
|
|||||||
|
|
||||||
// Create the collision shape for the rigid body (convex mesh shape) and
|
// Create the collision shape for the rigid body (convex mesh shape) and
|
||||||
// do not forget to delete it at the end
|
// do not forget to delete it at the end
|
||||||
mHeightFieldShape = new rp3d::HeightFieldShape(HEIGHTFIELD_WIDTH, HEIGHTFIELD_LENGTH, 0, 5,
|
mHeightFieldShape = new rp3d::HeightFieldShape(NB_POINTS_WIDTH, NB_POINTS_LENGTH, mMinHeight, mMaxHeight,
|
||||||
mHeightData, rp3d::HeightFieldShape::HEIGHT_FLOAT_TYPE);
|
mHeightData, rp3d::HeightFieldShape::HEIGHT_FLOAT_TYPE);
|
||||||
|
|
||||||
// Initial position and orientation of the rigid body
|
// Initial position and orientation of the rigid body
|
||||||
@ -188,12 +189,33 @@ void HeightField::render(openglframework::Shader& shader,
|
|||||||
// Compute the heights of the height field
|
// Compute the heights of the height field
|
||||||
void HeightField::generateHeightField() {
|
void HeightField::generateHeightField() {
|
||||||
|
|
||||||
mMinHeight = 0.0;
|
double persistence = 6;
|
||||||
mMaxHeight = 5.0;
|
double frequency = 0.13;
|
||||||
|
double amplitude = 18;
|
||||||
|
int octaves = 1;
|
||||||
|
int randomseed = 779;
|
||||||
|
PerlinNoise perlinNoise(persistence, frequency, amplitude, octaves, randomseed);
|
||||||
|
|
||||||
for (int i=0; i<HEIGHTFIELD_WIDTH; i++) {
|
mMinHeight = 0;
|
||||||
for (int j=0; j<HEIGHTFIELD_LENGTH; j++) {
|
mMaxHeight = 5;
|
||||||
mHeightData[i][j] = 5.0f;
|
|
||||||
|
float width = (NB_POINTS_WIDTH - 1);
|
||||||
|
float length = (NB_POINTS_LENGTH - 1);
|
||||||
|
|
||||||
|
for (int i=0; i<NB_POINTS_WIDTH; i++) {
|
||||||
|
for (int j=0; j<NB_POINTS_LENGTH; j++) {
|
||||||
|
|
||||||
|
int arrayIndex = j * NB_POINTS_WIDTH + i;
|
||||||
|
|
||||||
|
mHeightData[arrayIndex] = (float)(perlinNoise.GetHeight(-width * 0.5 + i, -length * 0.5 + j));
|
||||||
|
|
||||||
|
if (i==0 && j==0) {
|
||||||
|
mMinHeight = mHeightData[arrayIndex] ;
|
||||||
|
mMaxHeight = mHeightData[arrayIndex] ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mHeightData[arrayIndex] > mMaxHeight) mMaxHeight = mHeightData[arrayIndex] ;
|
||||||
|
if (mHeightData[arrayIndex] < mMinHeight) mMinHeight = mHeightData[arrayIndex] ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -204,21 +226,27 @@ void HeightField::generateGraphicsMesh() {
|
|||||||
std::vector<uint> indices;
|
std::vector<uint> indices;
|
||||||
int vertexId = 0;
|
int vertexId = 0;
|
||||||
|
|
||||||
for (int i=0; i<HEIGHTFIELD_WIDTH; i++) {
|
float horizontalScale = 1.0f;
|
||||||
for (int j=0; j<HEIGHTFIELD_LENGTH; j++) {
|
|
||||||
|
|
||||||
float height = mHeightData[i][j] - (mMaxHeight - mMinHeight) * 0.5f;
|
for (int i=0; i<NB_POINTS_WIDTH; i++) {
|
||||||
openglframework::Vector3 vertex(-(HEIGHTFIELD_WIDTH - 1) * 0.5f + i, height, -(HEIGHTFIELD_LENGTH - 1) * 0.5 + j);
|
for (int j=0; j<NB_POINTS_LENGTH; j++) {
|
||||||
|
|
||||||
|
float originHeight = -(mMaxHeight - mMinHeight) * 0.5f - mMinHeight;
|
||||||
|
float height = originHeight + mHeightData[j * NB_POINTS_WIDTH + i];
|
||||||
|
openglframework::Vector3 vertex(-(NB_POINTS_WIDTH - 1) * 0.5f + i, height, -(NB_POINTS_LENGTH - 1) * 0.5f + j);
|
||||||
|
|
||||||
|
vertex.x *= horizontalScale;
|
||||||
|
vertex.z *= horizontalScale;
|
||||||
|
|
||||||
mVertices.push_back(vertex);
|
mVertices.push_back(vertex);
|
||||||
|
|
||||||
// Triangle indices
|
// Triangle indices
|
||||||
if ((i < HEIGHTFIELD_WIDTH - 1) && (j < HEIGHTFIELD_LENGTH - 1)) {
|
if ((i < NB_POINTS_WIDTH - 1) && (j < NB_POINTS_LENGTH - 1)) {
|
||||||
|
|
||||||
int v1 = vertexId;
|
int v1 = vertexId;
|
||||||
int v2 = vertexId + 1;
|
int v2 = vertexId + 1;
|
||||||
int v3 = vertexId + HEIGHTFIELD_LENGTH;
|
int v3 = vertexId + NB_POINTS_LENGTH;
|
||||||
int v4 = vertexId + HEIGHTFIELD_LENGTH + 1;
|
int v4 = vertexId + NB_POINTS_LENGTH + 1;
|
||||||
|
|
||||||
// First triangle
|
// First triangle
|
||||||
indices.push_back(v1);
|
indices.push_back(v1);
|
||||||
@ -326,4 +354,3 @@ void HeightField::setScaling(const openglframework::Vector3& scaling) {
|
|||||||
0, 0, scaling.z, 0,
|
0, 0, scaling.z, 0,
|
||||||
0, 0, 0, 1.0f);
|
0, 0, 0, 1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,18 +31,19 @@
|
|||||||
#include "reactphysics3d.h"
|
#include "reactphysics3d.h"
|
||||||
#include "PhysicsObject.h"
|
#include "PhysicsObject.h"
|
||||||
|
|
||||||
|
|
||||||
// Class HeightField
|
// Class HeightField
|
||||||
class HeightField : public openglframework::Mesh, public PhysicsObject {
|
class HeightField : public openglframework::Mesh, public PhysicsObject {
|
||||||
|
|
||||||
private :
|
private :
|
||||||
|
|
||||||
static const int HEIGHTFIELD_WIDTH = 10;
|
static const int NB_POINTS_WIDTH = 100;
|
||||||
static const int HEIGHTFIELD_LENGTH = 5;
|
static const int NB_POINTS_LENGTH = 100;
|
||||||
|
|
||||||
// -------------------- Attributes -------------------- //
|
// -------------------- Attributes -------------------- //
|
||||||
|
|
||||||
/// Height field data
|
/// Height field data
|
||||||
float mHeightData[HEIGHTFIELD_WIDTH][HEIGHTFIELD_LENGTH];
|
float mHeightData[NB_POINTS_WIDTH * NB_POINTS_LENGTH];
|
||||||
|
|
||||||
/// Previous transform (for interpolation)
|
/// Previous transform (for interpolation)
|
||||||
rp3d::Transform mPreviousTransform;
|
rp3d::Transform mPreviousTransform;
|
||||||
@ -74,13 +75,13 @@ class HeightField : public openglframework::Mesh, public PhysicsObject {
|
|||||||
|
|
||||||
// -------------------- Methods -------------------- //
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
// Create the Vertex Buffer Objects used to render with OpenGL.
|
/// Create the Vertex Buffer Objects used to render with OpenGL.
|
||||||
void createVBOAndVAO();
|
void createVBOAndVAO();
|
||||||
|
|
||||||
// Compute the heights of the height field
|
/// Compute the heights of the height field
|
||||||
void generateHeightField();
|
void generateHeightField();
|
||||||
|
|
||||||
// Generate the graphics mesh to render the height field
|
/// Generate the graphics mesh to render the height field
|
||||||
void generateGraphicsMesh();
|
void generateGraphicsMesh();
|
||||||
|
|
||||||
public :
|
public :
|
||||||
|
111
testbed/common/PerlinNoise.cpp
Normal file
111
testbed/common/PerlinNoise.cpp
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
#include "PerlinNoise.h"
|
||||||
|
|
||||||
|
PerlinNoise::PerlinNoise()
|
||||||
|
{
|
||||||
|
persistence = 0;
|
||||||
|
frequency = 0;
|
||||||
|
amplitude = 0;
|
||||||
|
octaves = 0;
|
||||||
|
randomseed = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
PerlinNoise::PerlinNoise(double _persistence, double _frequency, double _amplitude, int _octaves, int _randomseed)
|
||||||
|
{
|
||||||
|
persistence = _persistence;
|
||||||
|
frequency = _frequency;
|
||||||
|
amplitude = _amplitude;
|
||||||
|
octaves = _octaves;
|
||||||
|
randomseed = 2 + _randomseed * _randomseed;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PerlinNoise::Set(double _persistence, double _frequency, double _amplitude, int _octaves, int _randomseed)
|
||||||
|
{
|
||||||
|
persistence = _persistence;
|
||||||
|
frequency = _frequency;
|
||||||
|
amplitude = _amplitude;
|
||||||
|
octaves = _octaves;
|
||||||
|
randomseed = 2 + _randomseed * _randomseed;
|
||||||
|
}
|
||||||
|
|
||||||
|
double PerlinNoise::GetHeight(double x, double y) const
|
||||||
|
{
|
||||||
|
return amplitude * Total(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
double PerlinNoise::Total(double i, double j) const
|
||||||
|
{
|
||||||
|
//properties of one octave (changing each loop)
|
||||||
|
double t = 0.0f;
|
||||||
|
double _amplitude = 1;
|
||||||
|
double freq = frequency;
|
||||||
|
|
||||||
|
for(int k = 0; k < octaves; k++)
|
||||||
|
{
|
||||||
|
t += GetValue(j * freq + randomseed, i * freq + randomseed) * _amplitude;
|
||||||
|
_amplitude *= persistence;
|
||||||
|
freq *= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
double PerlinNoise::GetValue(double x, double y) const
|
||||||
|
{
|
||||||
|
int Xint = (int)x;
|
||||||
|
int Yint = (int)y;
|
||||||
|
double Xfrac = x - Xint;
|
||||||
|
double Yfrac = y - Yint;
|
||||||
|
|
||||||
|
//noise values
|
||||||
|
double n01 = Noise(Xint-1, Yint-1);
|
||||||
|
double n02 = Noise(Xint+1, Yint-1);
|
||||||
|
double n03 = Noise(Xint-1, Yint+1);
|
||||||
|
double n04 = Noise(Xint+1, Yint+1);
|
||||||
|
double n05 = Noise(Xint-1, Yint);
|
||||||
|
double n06 = Noise(Xint+1, Yint);
|
||||||
|
double n07 = Noise(Xint, Yint-1);
|
||||||
|
double n08 = Noise(Xint, Yint+1);
|
||||||
|
double n09 = Noise(Xint, Yint);
|
||||||
|
|
||||||
|
double n12 = Noise(Xint+2, Yint-1);
|
||||||
|
double n14 = Noise(Xint+2, Yint+1);
|
||||||
|
double n16 = Noise(Xint+2, Yint);
|
||||||
|
|
||||||
|
double n23 = Noise(Xint-1, Yint+2);
|
||||||
|
double n24 = Noise(Xint+1, Yint+2);
|
||||||
|
double n28 = Noise(Xint, Yint+2);
|
||||||
|
|
||||||
|
double n34 = Noise(Xint+2, Yint+2);
|
||||||
|
|
||||||
|
//find the noise values of the four corners
|
||||||
|
double x0y0 = 0.0625*(n01+n02+n03+n04) + 0.125*(n05+n06+n07+n08) + 0.25*(n09);
|
||||||
|
double x1y0 = 0.0625*(n07+n12+n08+n14) + 0.125*(n09+n16+n02+n04) + 0.25*(n06);
|
||||||
|
double x0y1 = 0.0625*(n05+n06+n23+n24) + 0.125*(n03+n04+n09+n28) + 0.25*(n08);
|
||||||
|
double x1y1 = 0.0625*(n09+n16+n28+n34) + 0.125*(n08+n14+n06+n24) + 0.25*(n04);
|
||||||
|
|
||||||
|
//interpolate between those values according to the x and y fractions
|
||||||
|
double v1 = Interpolate(x0y0, x1y0, Xfrac); //interpolate in x direction (y)
|
||||||
|
double v2 = Interpolate(x0y1, x1y1, Xfrac); //interpolate in x direction (y+1)
|
||||||
|
double fin = Interpolate(v1, v2, Yfrac); //interpolate in y direction
|
||||||
|
|
||||||
|
return fin;
|
||||||
|
}
|
||||||
|
|
||||||
|
double PerlinNoise::Interpolate(double x, double y, double a) const
|
||||||
|
{
|
||||||
|
double negA = 1.0 - a;
|
||||||
|
double negASqr = negA * negA;
|
||||||
|
double fac1 = 3.0 * (negASqr) - 2.0 * (negASqr * negA);
|
||||||
|
double aSqr = a * a;
|
||||||
|
double fac2 = 3.0 * aSqr - 2.0 * (aSqr * a);
|
||||||
|
|
||||||
|
return x * fac1 + y * fac2; //add the weighted factors
|
||||||
|
}
|
||||||
|
|
||||||
|
double PerlinNoise::Noise(int x, int y) const
|
||||||
|
{
|
||||||
|
int n = x + y * 57;
|
||||||
|
n = (n << 13) ^ n;
|
||||||
|
int t = (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff;
|
||||||
|
return 1.0 - double(t) * 0.931322574615478515625e-9;/// 1073741824.0);
|
||||||
|
}
|
44
testbed/common/PerlinNoise.h
Normal file
44
testbed/common/PerlinNoise.h
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#ifndef PERLIN_NOISE_H
|
||||||
|
#define PERLIN_NOISE_H
|
||||||
|
|
||||||
|
/// Class PerlinNoise
|
||||||
|
/// Code from http://stackoverflow.com/questions/4753055/perlin-noise-generation-for-terrain
|
||||||
|
class PerlinNoise {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
PerlinNoise();
|
||||||
|
PerlinNoise(double _persistence, double _frequency, double _amplitude, int _octaves, int _randomseed);
|
||||||
|
|
||||||
|
// Get Height
|
||||||
|
double GetHeight(double x, double y) const;
|
||||||
|
|
||||||
|
// Get
|
||||||
|
double Persistence() const { return persistence; }
|
||||||
|
double Frequency() const { return frequency; }
|
||||||
|
double Amplitude() const { return amplitude; }
|
||||||
|
int Octaves() const { return octaves; }
|
||||||
|
int RandomSeed() const { return randomseed; }
|
||||||
|
|
||||||
|
// Set
|
||||||
|
void Set(double _persistence, double _frequency, double _amplitude, int _octaves, int _randomseed);
|
||||||
|
|
||||||
|
void SetPersistence(double _persistence) { persistence = _persistence; }
|
||||||
|
void SetFrequency( double _frequency) { frequency = _frequency; }
|
||||||
|
void SetAmplitude( double _amplitude) { amplitude = _amplitude; }
|
||||||
|
void SetOctaves( int _octaves) { octaves = _octaves; }
|
||||||
|
void SetRandomSeed( int _randomseed) { randomseed = _randomseed; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
double Total(double i, double j) const;
|
||||||
|
double GetValue(double x, double y) const;
|
||||||
|
double Interpolate(double x, double y, double a) const;
|
||||||
|
double Noise(int x, int y) const;
|
||||||
|
|
||||||
|
double persistence, frequency, amplitude;
|
||||||
|
int octaves, randomseed;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -164,7 +164,7 @@ void HeightFieldScene::reset() {
|
|||||||
rp3d::Transform transform(rp3d::Vector3(0, 0, 0), initOrientation);
|
rp3d::Transform transform(rp3d::Vector3(0, 0, 0), initOrientation);
|
||||||
mHeightField->resetTransform(transform);
|
mHeightField->resetTransform(transform);
|
||||||
|
|
||||||
rp3d::Vector3 spherePos(-3.6, 15, 1.7);
|
rp3d::Vector3 spherePos(0, 13, 0);
|
||||||
rp3d::Transform sphereTransform(spherePos, initOrientation);
|
rp3d::Transform sphereTransform(spherePos, initOrientation);
|
||||||
mBox->resetTransform(sphereTransform);
|
mBox->resetTransform(sphereTransform);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user