Skip to content

Commit

Permalink
Init sine lookup table
Browse files Browse the repository at this point in the history
  • Loading branch information
leonmavr committed Sep 26, 2024
1 parent 0e777d7 commit b9435e0
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 18 deletions.
2 changes: 2 additions & 0 deletions demos/01_spinning_cube.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "xtrig.h"
#include "objects.h"
#include "renderer.h"
#include "arg_parser.h"
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions demos/02_3_diamonds.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "objects.h"
#include "renderer.h"
#include "arg_parser.h"
#include "xtrig.h"
#include "utils.h" // UT_MAX
#include <math.h> // sin, cos
#include <unistd.h> // for usleep
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions demos/03_3_diamonds.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "objects.h"
#include "renderer.h"
#include "arg_parser.h"
#include "xtrig.h"
#include "utils.h" // UT_MAX
#include <math.h> // sin, cos
#include <unistd.h> // for usleep
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions demos/04_overlapping_objects.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "objects.h"
#include "renderer.h"
#include "arg_parser.h" // args_parse, CFG_DIR
#include "xtrig.h"
#include <math.h> // sin, cos
#include <unistd.h> // for usleep
#include <stdbool.h> // bool
Expand All @@ -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
Expand All @@ -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();
Expand Down
8 changes: 5 additions & 3 deletions demos/05_moving_object.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "objects.h"
#include "renderer.h"
#include "arg_parser.h" // args_parse, CFG_DIR
#include "xtrig.h"
#include <math.h> // sin, cos
#include <unistd.h> // for usleep
#include <stdbool.h> // bool
Expand All @@ -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);
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion include/xtrig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
8 changes: 5 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "objects.h"
#include "renderer.h"
#include "arg_parser.h"
#include "xtrig.h"
#include "utils.h" // UT_MAX
#include <math.h> // sin, cos
#include <unistd.h> // for usleep
Expand All @@ -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
Expand All @@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions src/vector.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "xtrig.h"
#include "vector.h"
#include <stdbool.h> // true/false
#include <stdlib.h> // malloc
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down
9 changes: 5 additions & 4 deletions src/xtrig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b9435e0

Please sign in to comment.