Skip to content
Snippets Groups Projects
Commit 7f47be17 authored by Noé's avatar Noé
Browse files

Larger terrain and perlin tests

parent db85ae58
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -21,7 +21,7 @@ void ball::initialize()
void ball::simulate(float dt, float terrain_length) {
for (int i = 0; i < N; i++) {
if (pos[i][2] < -1 || cgp::abs(pos[i][0]) > terrain_length / 2 || cgp::abs(pos[i][1]) > terrain_length / 2) {
pos[i] = { 0,0, evaluate_terrain_height(0,0) };
pos[i] = { 0,0, evaluate_terrain_height(0,0, terrain_length) };
v[i] = { 3 * rand_interval(), 3 * rand_interval(), 4 + i };
}
else {
......@@ -29,8 +29,8 @@ void ball::simulate(float dt, float terrain_length) {
v[i][2] -= 10 * dt;
}
if (pos[i][2] < evaluate_terrain_height(pos[i][0], pos[i][1])) {
vec3 normal = normalize(terrain_orientation(pos[i][0], pos[i][1]));
if (pos[i][2] < evaluate_terrain_height(pos[i][0], pos[i][1], terrain_length)) {
vec3 normal = normalize(terrain_orientation(pos[i][0], pos[i][1], terrain_length));
v[i][2] += 10 * dt;
vec3 temp = v[i] - dot(v[i], normal) * normal;
......@@ -42,11 +42,11 @@ void ball::simulate(float dt, float terrain_length) {
}
}
vec3 terrain_orientation(float x, float y) {
float z = evaluate_terrain_height(x, y);
vec3 terrain_orientation(float x, float y, float terrain_length) {
float z = evaluate_terrain_height(x, y, terrain_length);
float step = 0.1f;
vec3 u = { step, 0, evaluate_terrain_height(x + step,y) - z };
vec3 v = { 0, step, evaluate_terrain_height(x,y + step) - z };
vec3 u = { step, 0, evaluate_terrain_height(x + step,y, terrain_length) - z };
vec3 v = { 0, step, evaluate_terrain_height(x,y + step, terrain_length) - z };
return cross(v, u);
}
......@@ -17,5 +17,5 @@ struct ball {
void simulate(float dt, float terrain_length);
};
vec3 terrain_orientation(float x, float y);
vec3 terrain_orientation(float x, float y, float terrain_length);
......@@ -11,7 +11,7 @@ int num_trees = 50;
int num_grass = 100;
std::vector<vec3> tree_position;
std::vector<vec3> grass_position;
float terrain_length = 20;
float terrain_length = 50;
void scene_structure::initialize()
{
......@@ -55,7 +55,7 @@ void scene_structure::initialize()
float x = 1.0;
float y = 2.0;
tree.model.translation = { x, y, evaluate_terrain_height(x,y) };
tree.model.translation = { x, y, evaluate_terrain_height(x,y, terrain_length) };
tree_position = generate_positions_on_terrain(num_trees, terrain_length-1);
......@@ -119,9 +119,12 @@ void scene_structure::display_frame()
//custom function we added to ..first_person_eulor, changes whether the cursor is trapped or not
camera_control.trap_cursor(gui.trap_cursor);
vec3 eye_level = camera_control.camera_model.position_camera;
eye_level[2] = evaluate_terrain_height(eye_level[0], eye_level[1]) + 0.75;
camera_control.camera_model.position_camera = eye_level;
//Walking on ground
if (!gui.fly) {
vec3 eye_level = camera_control.camera_model.position_camera;
eye_level[2] = evaluate_terrain_height(eye_level[0], eye_level[1], terrain_length) + 0.75;
camera_control.camera_model.position_camera = eye_level;
}
// Set the light to the current position of the camera
environment.light = camera_control.camera_model.position();
......@@ -284,6 +287,7 @@ void scene_structure::display_gui()
ImGui::Checkbox("Frame", &gui.display_frame);
ImGui::Checkbox("Wireframe", &gui.display_wireframe);
ImGui::Checkbox("Trapped Cursor", &gui.trap_cursor);
ImGui::Checkbox("Fly", &gui.fly);
ImGui::SliderFloat("Time", &timer_mvt.t, timer_mvt.t_min, timer_mvt.t_max);
ImGui::SliderFloat("Time scale", &timer_mvt.scale, 0.0f, 2.0f);
......
......@@ -23,6 +23,7 @@ struct gui_parameters {
bool display_wireframe = false;
float k = 0.5f;
bool trap_cursor = true;
bool fly = false;
};
// The structure of the custom scene
......
......@@ -6,20 +6,29 @@ using namespace cgp;
// Evaluate 3D position of the terrain for any (x,y)
float evaluate_terrain_height(float x, float y)
float evaluate_terrain_height(float x, float y, float terrain_length)
{
//p, h, sigma -> positions, heights and wideness of bell curves
vec2 p[] = { {-10, -10}, {5, 5}, {-3, 4}, {6, 4} };
float h[] = { 3.0f, -1.5f, 1.0f, 2.0f };
float sigma[] = { 10.0f, 3.0f, 4.0f, 4.0f };
float noise = 0.4*noise_perlin(vec2(x/50,y/50), 4.5f, 0.5, 5.0f);
float z = 0;
for (int i = 0; i < 4; i++) {
float d = norm(vec2(x, y) - p[i]) / sigma[i];
z += h[i] * std::exp(-d * d);
}
z += noise;
return z;
perlin_noise_parameters parameters;
parameters.terrain_height = 6.03;
parameters.octave = 5;
parameters.frequency_gain = 2.89;
parameters.persistency = 0.39;
float perlin_noise = parameters.terrain_height * noise_perlin(vec2(x / terrain_length, y / terrain_length), parameters.octave, parameters.persistency, parameters.frequency_gain);
return perlin_noise;
}
......@@ -44,7 +53,7 @@ mesh create_terrain_mesh(int N, float terrain_length)
float y = (v - 0.5f) * terrain_length;
// Compute the surface height function at the given sampled coordinate
float z = evaluate_terrain_height(x,y);
float z = evaluate_terrain_height(x,y, terrain_length);
// Store vertex coordinates
terrain.position[kv+N*ku] = {x,y,z};
......@@ -81,7 +90,7 @@ std::vector<cgp::vec3> generate_positions_on_terrain(int N, float terrain_length
for (int i = 0; i < N; i++) {
x = rand_interval(-d, d);
y = rand_interval(-d, d);
rand_pos.push_back({ x,y, evaluate_terrain_height(x, y) });
rand_pos.push_back({ x,y, evaluate_terrain_height(x, y, terrain_length) });
}
return rand_pos;
......
......@@ -2,8 +2,15 @@
#include "cgp/cgp.hpp"
struct perlin_noise_parameters
{
float persistency = 0.35f;
float frequency_gain = 2.0f;
int octave = 6;
float terrain_height = 0.5f;
};
float evaluate_terrain_height(float x, float y);
float evaluate_terrain_height(float x, float y, float terrain_length);
/** Compute a terrain mesh
The (x,y) coordinates of the terrain are set in [-length/2, length/2].
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment