Skip to content
Snippets Groups Projects
Commit 8928d83d authored by Marie AUDOUARD's avatar Marie AUDOUARD
Browse files

Changement de la structure de bat

parent e879a174
No related branches found
No related tags found
No related merge requests found
TODO:
Animals with animation through blender .obj
Multiple textures to have a pretty mountain with grass stone and snow
Skybox!
Day and night cycle
Balls of fire ice and thunder! -> d'abord faire des bouncing balls qu'on peut lancer
Enemies/HP
\ No newline at end of file
#include "bat.hpp" #include "bat.hpp"
#include "environment.hpp" #include "environment.hpp"
#include "key_positions_structure.hpp"
#include "interpolation.hpp"
#include "terrain.hpp"
void bat::initialize_bat() void bat::initialize_bat()
{ {
...@@ -18,9 +21,9 @@ void bat::initialize_bat() ...@@ -18,9 +21,9 @@ void bat::initialize_bat()
// Scale the model // Scale the model
body.model.scaling = 0.2f; //body.model.scaling = 0.2f;
wing1.model.scaling = 0.2f; //wing1.model.scaling = 0.2f;
wing2.model.scaling = 0.2f; //wing2.model.scaling = 0.2f;
...@@ -45,4 +48,92 @@ void bat::initialize_bat() ...@@ -45,4 +48,92 @@ void bat::initialize_bat()
bat.add(wing1, "Bat wing left1", "Bat base", { -0.05f,0,0 }); bat.add(wing1, "Bat wing left1", "Bat base", { -0.05f,0,0 });
bat.add(wing2, "Bat wing right1", "Bat base", { 0.05f,0,0 }); bat.add(wing2, "Bat wing right1", "Bat base", { 0.05f,0,0 });
}
\ No newline at end of file N = 0;
size.resize(N);
isdead.resize(N);
pos.resize(N);
pos2.resize(N);
keyframe.resize(N);
}
void bat::initialize_mvt(vec3 p, int m, float terrain_length)
{
// Definition of the initial data
//--------------------------------------//
// Key 3D positions
numarray<vec3> key_positions =
{ {-2,1,0}, {0,1,0}, {2,1,0}, {1,3,0}, {2,3,0}, {2,3,1}, {2,2.5,1.5}, {1.5,0,1}, {1.5,0,-1}, {0,0,-1}, {0,-0.5,-1}, {-2,1,-1} };
float h = 20 + evaluate_terrain_height(0, 0, terrain_length);
for (int i = 0; i < key_positions.size(); i++) {
key_positions[i] *= size[m];
key_positions[i][2] += h;
key_positions[i] += p;
}
// Key times (time at which the position must pass in the corresponding position)
numarray<float> key_times =
{ 0.0f, 1.0f, 2.0f, 2.5f, 3.0f, 3.5f, 3.75f, 4.5f, 5.0f, 6.0f, 7.0f, 8.0f };
// Initialize the helping structure to display/interact with these positions
keyframe[m].initialize(key_positions, key_times);
// Set timer bounds
// The timer must span a time interval on which the interpolation can be conducted
// By default, set the minimal time to be key_times[1], and the maximal time to be key_time[N-2] (enables cubic interpolation)
int NT = key_times.size();
timer_mvt.t_min = key_times[0];
timer_mvt.t_max = key_times[NT - 1];
timer_mvt.t = timer_mvt.t_min;
}
void bat::display_mvt()
{
// Update the current time
timer_mvt.update();
float t = timer_mvt.t;
for (int m = 0; m < N; m++) {
// clear trajectory when the timer restart
if (t < timer_mvt.t_min + 0.1f)
keyframe[m].trajectory.clear();
if (t + 0.1f > timer_mvt.t_max) pos2[m] = interpolation(0.01f, keyframe[m].key_positions, keyframe[m].key_times, 0.5f);
else pos2[m] = interpolation((t + 0.1f), keyframe[m].key_positions, keyframe[m].key_times, 0.5f);
// Display the key positions and lines b/w positions
//keyframe.display_key_positions(environment);
// Compute the interpolated position
// This is this function that you need to complete
pos[m] = interpolation(t, keyframe[m].key_positions, keyframe[m].key_times, 0.5f);
// Display the interpolated position (and its trajectory)
//keyframe.display_current_position(p, environment);
}
}
void bat::add_bat(vec3 new_pos, float terrain_length) {
N++;
pos.resize(N);
pos2.resize(N);
isdead.resize(N);
size.resize(N);
keyframe.resize(N);
pos[N - 1] = new_pos;
isdead[N - 1] = false;
size[N - 1] = rand_interval(0.05f, 1.0f);;
initialize_mvt(new_pos, N - 1, terrain_length);
}
#pragma once #pragma once
#include "cgp/cgp.hpp" #include "cgp/cgp.hpp"
#include "key_positions_structure.hpp"
using namespace cgp; using namespace cgp;
...@@ -9,5 +10,18 @@ using namespace cgp; ...@@ -9,5 +10,18 @@ using namespace cgp;
struct bat{ struct bat{
cgp::hierarchy_mesh_drawable bat; cgp::hierarchy_mesh_drawable bat;
int N;
std::vector<double> size;
std::vector<bool> isdead;
std::vector<vec3> pos;
std::vector<vec3> pos2;
// Timer used for the interpolation of the position
cgp::timer_interval timer_mvt;
// A helper structure used to store and display the key positions/time
std::vector<keyframe_structure> keyframe;
void initialize_bat(); void initialize_bat();
void add_bat(vec3 new_pos, float terrain_length);
void initialize_mvt(vec3 p, int i, float terrain_length);
void display_mvt();
}; };
\ No newline at end of file
...@@ -95,14 +95,17 @@ void scene_structure::initialize() ...@@ -95,14 +95,17 @@ void scene_structure::initialize()
GL_CLAMP_TO_BORDER); GL_CLAMP_TO_BORDER);
bird1.initialize_bird(); bird1.initialize_bird();
initialize_mvt(); //initialize_mvt();
chain1.initialize(); chain1.initialize();
bouncing.initialize(10); //10 balls bouncing.initialize(10); //10 balls
bat1.initialize_bat(); bat1.initialize_bat();
bat1.add_bat({0,0,0}, terrain_length);
projectiles.initialize(); projectiles.initialize();
sapin1.initialize_sapin(); sapin1.initialize_sapin();
sapin1.sapin["Trunk"].transform_local.rotation = rotation_transform::from_axis_angle({ 1,0,0 }, Pi / 2);
} }
/*
void scene_structure::initialize_mvt() void scene_structure::initialize_mvt()
{ {
// Definition of the initial data // Definition of the initial data
...@@ -132,6 +135,8 @@ void scene_structure::initialize_mvt() ...@@ -132,6 +135,8 @@ void scene_structure::initialize_mvt()
timer_mvt.t = timer_mvt.t_min; timer_mvt.t = timer_mvt.t_min;
} }
*/
void scene_structure::display_frame() void scene_structure::display_frame()
{ {
...@@ -164,15 +169,14 @@ void scene_structure::display_frame() ...@@ -164,15 +169,14 @@ void scene_structure::display_frame()
//tree.model.translation = tree_position[i]; //tree.model.translation = tree_position[i];
//draw(tree, environment); //draw(tree, environment);
sapin1.sapin["Trunk"].transform_local.translation = tree_position[i]; sapin1.sapin["Trunk"].transform_local.translation = tree_position[i];
sapin1.sapin["Trunk"].transform_local.rotation = rotation_transform::from_axis_angle({ 1,0,0 }, Pi / 2);
sapin1.sapin.update_local_to_global_coordinates(); sapin1.sapin.update_local_to_global_coordinates();
draw(sapin1.sapin, environment); draw(sapin1.sapin, environment);
} }
vec3 p = display_mvt(); //vec3 p = display_mvt();
//display_bird(p); //display_bird(p);
display_chain(p); //display_chain(p);
display_ball(); display_ball();
display_bat(p); display_bat();
display_projectiles(); display_projectiles();
...@@ -183,7 +187,7 @@ void scene_structure::display_frame() ...@@ -183,7 +187,7 @@ void scene_structure::display_frame()
} }
/*
void scene_structure::display_bird(vec3 p) void scene_structure::display_bird(vec3 p)
{ {
// Apply transformation to some elements of the hierarchy // Apply transformation to some elements of the hierarchy
...@@ -213,7 +217,7 @@ void scene_structure::display_bird(vec3 p) ...@@ -213,7 +217,7 @@ void scene_structure::display_bird(vec3 p)
hierarchy["Cube base"].transform_local.rotation = rotation_transform::from_axis_angle({ 1,0,0 }, 0.8 * cos(3 * timer.t)); hierarchy["Cube base"].transform_local.rotation = rotation_transform::from_axis_angle({ 1,0,0 }, 0.8 * cos(3 * timer.t));
hierarchy.update_local_to_global_coordinates(); hierarchy.update_local_to_global_coordinates();
*/
draw(bird1.bird, environment); draw(bird1.bird, environment);
if (gui.display_wireframe) { if (gui.display_wireframe) {
...@@ -223,8 +227,9 @@ void scene_structure::display_bird(vec3 p) ...@@ -223,8 +227,9 @@ void scene_structure::display_bird(vec3 p)
} }
*/
void scene_structure::display_bat(vec3 p) void scene_structure::display_bat()
{ {
// Apply transformation to some elements of the hierarchy // Apply transformation to some elements of the hierarchy
bat1.bat["Bat wing left1"].transform_local.rotation = rotation_transform::from_axis_angle({ 0,1,0 }, 0.5 * cos(5 * timer.t)); bat1.bat["Bat wing left1"].transform_local.rotation = rotation_transform::from_axis_angle({ 0,1,0 }, 0.5 * cos(5 * timer.t));
...@@ -234,23 +239,24 @@ void scene_structure::display_bat(vec3 p) ...@@ -234,23 +239,24 @@ void scene_structure::display_bat(vec3 p)
// This function must be called before the drawing in order to propagate the deformations through the hierarchy // This function must be called before the drawing in order to propagate the deformations through the hierarchy
bat1.bat.update_local_to_global_coordinates(); bat1.bat.update_local_to_global_coordinates();
bat1.display_mvt();
for (int m = 0; m < bat1.N; m++) {
//Orientation and position of the bird along the path //Rescale
float t = timer_mvt.t; bat1.bat["Bat base"].drawable.model.scaling = bat1.size[m];
vec3 p2; bat1.bat["Bat wing left1"].drawable.model.scaling = bat1.size[m];
if (t + 0.1f > timer_mvt.t_max) p2 = interpolation(0.01f, keyframe.key_positions, keyframe.key_times, gui.k); bat1.bat["Bat wing right1"].drawable.model.scaling = bat1.size[m];
else p2 = interpolation((t + 0.1f), keyframe.key_positions, keyframe.key_times, gui.k); //Orientation and position of the bat along the path
bat1.bat["Bat base"].transform_local.rotation = rotation_transform::from_vector_transform({ 0,-1,0 }, normalize(p2 - p)); bat1.bat["Bat base"].transform_local.rotation = rotation_transform::from_vector_transform({ 0,-1,0 }, normalize(bat1.pos2[m] - bat1.pos[m]));
bat1.bat["Bat base"].transform_local.translation = p; bat1.bat["Bat base"].transform_local.translation = bat1.pos[m];
if (!bat1.isdead[m]) draw(bat1.bat, environment);
draw(bat1.bat, environment); if (gui.display_wireframe) {
if (gui.display_wireframe) { draw_wireframe(bat1.bat, environment);
draw_wireframe(bat1.bat, environment); }
} }
} }
void scene_structure::display_semiTransparent() void scene_structure::display_semiTransparent()
{ {
// Enable use of alpha component as color blending for transparent elements // Enable use of alpha component as color blending for transparent elements
...@@ -285,13 +291,9 @@ void scene_structure::display_semiTransparent() ...@@ -285,13 +291,9 @@ void scene_structure::display_semiTransparent()
/*
vec3 scene_structure::display_mvt() vec3 scene_structure::display_mvt()
{ {
// Basic elements of the scene
environment.light = camera_control.camera_model.position();
if (gui.display_frame)
draw(global_frame, environment);
// Update the current time // Update the current time
timer_mvt.update(); timer_mvt.update();
...@@ -311,6 +313,7 @@ vec3 scene_structure::display_mvt() ...@@ -311,6 +313,7 @@ vec3 scene_structure::display_mvt()
// Display the interpolated position (and its trajectory) // Display the interpolated position (and its trajectory)
//keyframe.display_current_position(p, environment); //keyframe.display_current_position(p, environment);
} }
*/
void scene_structure::draw_segment(vec3 const& a, vec3 const& b) void scene_structure::draw_segment(vec3 const& a, vec3 const& b)
{ {
...@@ -345,13 +348,13 @@ void scene_structure::display_gui() ...@@ -345,13 +348,13 @@ void scene_structure::display_gui()
ImGui::Checkbox("Wireframe", &gui.display_wireframe); ImGui::Checkbox("Wireframe", &gui.display_wireframe);
ImGui::Checkbox("Fly", &gui.fly); ImGui::Checkbox("Fly", &gui.fly);
ImGui::SliderFloat("Time", &timer_mvt.t, timer_mvt.t_min, timer_mvt.t_max); //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); //ImGui::SliderFloat("Time scale", &timer_mvt.scale, 0.0f, 2.0f);
ImGui::SliderFloat("K", &gui.k, 0.0f, 10.0f); ImGui::SliderFloat("K", &gui.k, 0.0f, 10.0f);
ImGui::SliderFloat("Speed", &gui.speed, 0.0f, 10.0f); ImGui::SliderFloat("Speed", &gui.speed, 0.0f, 10.0f);
// Display the GUI associated to the key position // Display the GUI associated to the key position
keyframe.display_gui(); //keyframe.display_gui();
} }
void scene_structure::display_ball() { void scene_structure::display_ball() {
......
...@@ -60,11 +60,9 @@ struct scene_structure : cgp::scene_inputs_generic { ...@@ -60,11 +60,9 @@ struct scene_structure : cgp::scene_inputs_generic {
cgp::mesh_drawable grass; cgp::mesh_drawable grass;
// A helper structure used to store and display the key positions/time
keyframe_structure keyframe;
// Timer used for the interpolation of the position
cgp::timer_interval timer_mvt;
// Timer used for the animation // Timer used for the animation
timer_basic timer_chain; timer_basic timer_chain;
...@@ -86,7 +84,7 @@ struct scene_structure : cgp::scene_inputs_generic { ...@@ -86,7 +84,7 @@ struct scene_structure : cgp::scene_inputs_generic {
void display_semiTransparent(); void display_semiTransparent();
void display_chain(vec3 bird_pos); void display_chain(vec3 bird_pos);
void display_bird(vec3 p); void display_bird(vec3 p);
void display_bat(vec3 p); void display_bat();
void initialize_mvt(); void initialize_mvt();
vec3 display_mvt(); vec3 display_mvt();
...@@ -99,6 +97,7 @@ struct scene_structure : cgp::scene_inputs_generic { ...@@ -99,6 +97,7 @@ struct scene_structure : cgp::scene_inputs_generic {
void draw_segment(vec3 const& a, vec3 const& b); void draw_segment(vec3 const& a, vec3 const& b);
void display_ball(); void display_ball();
bool scene_structure::test_dead(vec3 p);
void display_projectiles(); void display_projectiles();
}; };
......
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