Added quaternion and vector structures from the cglm library for handling 3D rotations and directions. Added a new function `euler_to_quat` to convert Euler angles to a quaternion representation. Changed the `camera_t` structure to replace separate forward, up, and right vectors with a single rotation quaternion. Changed the `camera_create` function to accept a rotation quaternion instead of separate direction vectors. Changed the `scene_init` function to initialize the camera with a default rotation quaternion. Changed the `ensure_camera_aspect_ratio` function to maintain the camera's rotation quaternion. Changed the `screen_render_pixel`, `scene_render_tile`, and `scene_render` functions to compute camera coordinates based on the quaternion rotation.
58 lines
1.3 KiB
C
58 lines
1.3 KiB
C
#ifndef COMMON_H
|
|
#define COMMON_H
|
|
|
|
#include "cglm/struct/quat.h"
|
|
#include "cglm/struct/vec4.h"
|
|
#include "cglm/struct/vec3.h"
|
|
#include <math.h>
|
|
|
|
#define RAY_EPSILON 5.192092896e-05F
|
|
|
|
#define COLOR_CLAMP(color) (unsigned char)fminf(fmaxf(color, 0.0f), 255.0f)
|
|
#define BIAS_RAY_ORIGION(positon, normal) glms_vec3_add(positon, glms_vec3_scale(normal, RAY_EPSILON))
|
|
|
|
inline float random_float()
|
|
{
|
|
return (float)rand() / (float)RAND_MAX;
|
|
}
|
|
|
|
inline uint32_t hash_uint32(uint32_t x) {
|
|
x = ((x >> 16) ^ x) * 0x45d9f3b;
|
|
x = ((x >> 16) ^ x) * 0x45d9f3b;
|
|
x = (x >> 16) ^ x;
|
|
return x;
|
|
}
|
|
|
|
inline bool has_flag(int flags, int flag)
|
|
{
|
|
return (flags & flag) != 0;
|
|
}
|
|
|
|
|
|
inline versors euler_to_quat(float x, float y, float z)
|
|
{
|
|
versors qx = glms_quatv(glm_rad(x), (vec3s){1.0f, 0.0f, 0.0f});
|
|
versors qy = glms_quatv(glm_rad(y), (vec3s){0.0f, 1.0f, 0.0f});
|
|
versors qz = glms_quatv(glm_rad(z), (vec3s){0.0f, 0.0f, 1.0f});
|
|
|
|
return glms_quat_mul(glms_quat_mul(qz, qy), qx);
|
|
}
|
|
|
|
inline vec3s quat_get_forward(versors quat)
|
|
{
|
|
vec3s forward = glms_quat_rotatev(quat, (vec3s){0.0f, 0.0f, -1.0f});
|
|
return forward;
|
|
}
|
|
|
|
inline vec3s quat_get_up(versors quat)
|
|
{
|
|
return glms_quat_rotatev(quat, (vec3s){0.0f, 1.0f, 0.0f});
|
|
}
|
|
|
|
inline vec3s quat_get_right(versors quat)
|
|
{
|
|
return glms_quat_rotatev(quat, (vec3s){1.0f, 0.0f, 0.0f});
|
|
}
|
|
|
|
#endif // COMMON_H
|