diff --git a/demos/01_spinning_cube.c b/demos/01_spinning_cube.c index 6e6222d..72073a0 100644 --- a/demos/01_spinning_cube.c +++ b/demos/01_spinning_cube.c @@ -1,3 +1,4 @@ +#include "xtrig.h" #include "objects.h" #include "renderer.h" #include "arg_parser.h" @@ -23,6 +24,7 @@ int main(int argc, char** argv) { signal(SIGINT, interrupt_handler); // initialise objects to render mesh_t* shape = obj_mesh_from_file(g_mesh_file, g_cx, g_cy, g_cz, g_width, g_height, g_depth); + init_lookup_tables(); // do the actual rendering render_init(); for (size_t t = 0; t < g_max_iterations; ++t) { diff --git a/demos/02_3_diamonds.c b/demos/02_3_diamonds.c index 3b55ed7..e428193 100644 --- a/demos/02_3_diamonds.c +++ b/demos/02_3_diamonds.c @@ -1,6 +1,7 @@ #include "objects.h" #include "renderer.h" #include "arg_parser.h" +#include "xtrig.h" #include "utils.h" // UT_MAX #include // sin, cos #include // for usleep @@ -33,6 +34,7 @@ int main(int argc, char** argv) { sprintf(mesh_filepath, "%s/%s", mesh_dir, mesh_filename); assert(access(mesh_filepath, F_OK) == 0); + init_lookup_tables(); mesh_t* obj1 = obj_mesh_from_file(mesh_filepath, -40, 0, 150, 60, 80, 60); mesh_t* obj2 = obj_mesh_from_file(mesh_filepath, 0, 0, 250, 60, 80, 60); mesh_t* obj3 = obj_mesh_from_file(mesh_filepath, 40, 0, 350, 60, 80, 60); diff --git a/demos/03_3_diamonds.c b/demos/03_3_diamonds.c index 002ae5c..77a0106 100644 --- a/demos/03_3_diamonds.c +++ b/demos/03_3_diamonds.c @@ -2,6 +2,7 @@ #include "objects.h" #include "renderer.h" #include "arg_parser.h" +#include "xtrig.h" #include "utils.h" // UT_MAX #include // sin, cos #include // for usleep @@ -39,6 +40,7 @@ int main(int argc, char** argv) { const char* mesh_filename = "rhombus.scl"; sprintf(mesh_filepath, "%s/%s", mesh_dir, mesh_filename); assert(access(mesh_filepath, F_OK) == 0); + init_lookup_tables(); // draw the same object in 3 different depths to showcase perspective mesh_t* obj1 = obj_mesh_from_file(mesh_filepath, -80, 0, 600, w, h, d); diff --git a/demos/04_overlapping_objects.c b/demos/04_overlapping_objects.c index 82bc35d..1105ec1 100644 --- a/demos/04_overlapping_objects.c +++ b/demos/04_overlapping_objects.c @@ -1,6 +1,7 @@ #include "objects.h" #include "renderer.h" #include "arg_parser.h" // args_parse, CFG_DIR +#include "xtrig.h" #include // sin, cos #include // for usleep #include // bool @@ -27,6 +28,7 @@ int main(int argc, char** argv) { // make sure we end gracefully if the user hits Ctr+C signal(SIGINT, interrupt_handler); + init_lookup_tables(); render_init(); // path to directory where meshes are stored - dir stored in CFG_DIR prep. constant @@ -53,9 +55,9 @@ int main(int argc, char** argv) { #endif for (size_t t = 0; t < UINT_MAX; ++t) { obj_mesh_rotate_to(obj1, 1.0/80*t, 1.0/40*t, 1.0/60*t); - obj_mesh_rotate_to(obj2, amplitude_x*sin(random_rot_speed_x*sin(random_rot_speed_x*t) + 2*random_bias_x), - amplitude_y*sin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y), - amplitude_z*sin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z)); + obj_mesh_rotate_to(obj2, amplitude_x*xsin(random_rot_speed_x*xsin(random_rot_speed_x*t) + 2*random_bias_x), + amplitude_y*xsin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y), + amplitude_z*xsin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z)); render_write_shape(obj1); render_write_shape(obj2); render_flush(); diff --git a/demos/05_moving_object.c b/demos/05_moving_object.c index 6854669..18c6179 100644 --- a/demos/05_moving_object.c +++ b/demos/05_moving_object.c @@ -1,6 +1,7 @@ #include "objects.h" #include "renderer.h" #include "arg_parser.h" // args_parse, CFG_DIR +#include "xtrig.h" #include // sin, cos #include // for usleep #include // bool @@ -24,6 +25,7 @@ int main(int argc, char** argv) { // make sure we end gracefully if the user hits Ctr+C signal(SIGINT, interrupt_handler); + init_lookup_tables(); render_init(); mesh_t* shape = obj_mesh_from_file(g_mesh_file, g_cx, g_cy, g_cz, g_width, g_height, g_depth); @@ -38,9 +40,9 @@ int main(int argc, char** argv) { #endif for (size_t t = 0; t < g_max_iterations; ++t) { if (g_use_random_rotation) - obj_mesh_rotate_to(shape, amplitude_x*sin(random_rot_speed_x*sin(random_rot_speed_x*t) + 2*random_bias_x), - amplitude_y*sin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y), - amplitude_z*sin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z)); + obj_mesh_rotate_to(shape, amplitude_x*xsin(random_rot_speed_x*xsin(random_rot_speed_x*t) + 2*random_bias_x), + amplitude_y*xsin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y), + amplitude_z*xsin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z)); else obj_mesh_rotate_to(shape, g_rot_speed_x/20*t, g_rot_speed_y/20*t, g_rot_speed_z/20*t); if ((t % 100) >= 50) diff --git a/include/xtrig.h b/include/xtrig.h index 6284938..1113943 100644 --- a/include/xtrig.h +++ b/include/xtrig.h @@ -6,7 +6,7 @@ #define LUT_BIN_SIZE 0.00174533 // in radians #define HALF_PI (M_PI / 2.0) -#define LUT_SIZE (int)(HALF_PI/ LUT_BIN_SIZE + 1) +#define LUT_SIZE 901 // (int)(HALF_PI/ LUT_BIN_SIZE + 1) /** sine lookup table (LUT) with sampled values from 0 to pi/2 */ extern double sine_lookup[LUT_SIZE]; diff --git a/main.c b/main.c index 6619ed1..483e0f8 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,7 @@ #include "objects.h" #include "renderer.h" #include "arg_parser.h" +#include "xtrig.h" #include "utils.h" // UT_MAX #include // sin, cos #include // for usleep @@ -23,6 +24,7 @@ int main(int argc, char** argv) { signal(SIGINT, interrupt_handler); render_init(); + init_lookup_tables(); mesh_t* shape = obj_mesh_from_file(g_mesh_file, g_cx, g_cy, g_cz, g_width, g_height, g_depth); // spinning parameters in case random rotation was selected @@ -36,9 +38,9 @@ int main(int argc, char** argv) { #endif for (size_t t = 0; t < g_max_iterations; ++t) { if (g_use_random_rotation) - obj_mesh_rotate_to(shape, amplitude_x*sin(random_rot_speed_x*sin(random_rot_speed_x*t) + 2*random_bias_x), - amplitude_y*sin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y), - amplitude_z*sin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z)); + obj_mesh_rotate_to(shape, amplitude_x*xsin(random_rot_speed_x*xsin(random_rot_speed_x*t) + 2*random_bias_x), + amplitude_y*xsin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y), + amplitude_z*xsin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z)); else obj_mesh_rotate_to(shape, g_rot_speed_x/20*t, g_rot_speed_y/20*t, g_rot_speed_z/20*t); if (g_bounce_every != 0) { diff --git a/src/vector.c b/src/vector.c index 8feb1e4..df88348 100644 --- a/src/vector.c +++ b/src/vector.c @@ -1,3 +1,4 @@ +#include "xtrig.h" #include "vector.h" #include // true/false #include // malloc @@ -61,8 +62,8 @@ void vec_vec3_rotate(vec3_t* src, float angle_x_rad, float angle_y_rad, float an src->z -= z0; float a = angle_x_rad, b = angle_y_rad, c = angle_z_rad; - float ca = cos(a), cb = cos(b), cc = cos(c); - float sa = sin(a), sb = sin(b), sc = sin(c); + float ca = xcos(a), cb = xcos(b), cc = xcos(c); + float sa = xsin(a), sb = xsin(b), sc = xsin(c); float matrix_rotx[3][3] = { {1, 0, 0 }, {0, ca, -sa}, @@ -164,8 +165,8 @@ void vec_vec3i_rotate(vec3i_t* src, float angle_x_rad, float angle_y_rad, float rotated.z -= z0; const float a = angle_x_rad, b = angle_y_rad, c = angle_z_rad; - const float ca = cos(a), cb = cos(b), cc = cos(c); - const float sa = sin(a), sb = sin(b), sc = sin(c); + const float ca = xcos(a), cb = xcos(b), cc = xcos(c); + const float sa = xsin(a), sb = xsin(b), sc = xsin(c); const float matrix_rotx[3][3] = { {1, 0, 0 }, {0, ca, -sa}, diff --git a/src/xtrig.c b/src/xtrig.c index bac1d1f..58b70ec 100644 --- a/src/xtrig.c +++ b/src/xtrig.c @@ -16,14 +16,15 @@ double xsin(double rad) { // use the period and symmetry to compute based off [0, pi/2] rad = fmod(rad, 2*M_PI); if (rad < 0) rad += 2*M_PI; - if (rad < HALF_PI) + if (rad <= HALF_PI) { return sine_lookup[get_lookup_index(rad)]; - else if (rad < M_PI) + } else if (rad <= M_PI) { return sine_lookup[get_lookup_index(M_PI - rad)]; - else if (rad < 3*HALF_PI) + } else if (rad <= 3*HALF_PI) { return -sine_lookup[get_lookup_index(rad - M_PI)]; - else + } else { return -sine_lookup[get_lookup_index(2*M_PI - rad)]; + } } double xcos(double rad) {