orthographic projection for fov=0, rework of order and defaults for 3d camera api
This commit is contained in:
parent
20394eed6c
commit
791ab628ca
@ -79,7 +79,11 @@ TWN_API void draw_billboard(const char *texture,
|
|||||||
bool cylindrical); /* optional, default: false */
|
bool cylindrical); /* optional, default: false */
|
||||||
|
|
||||||
/* sets a perspective 3d camera to be used for all 3d commands */
|
/* sets a perspective 3d camera to be used for all 3d commands */
|
||||||
TWN_API void draw_camera(Vec3 position, float fov, Vec3 up, Vec3 direction);
|
/* fov = 0 corresponds to orthographic projection */
|
||||||
|
TWN_API void draw_camera(Vec3 position,
|
||||||
|
Vec3 direction, /* optional, default: (0, 0, -1) */
|
||||||
|
Vec3 up, /* optional, default: (0, 1, 0) */
|
||||||
|
float fov); /* optional, default: PI / 6 * 3 (90 degrees) */
|
||||||
|
|
||||||
/* same as draw_camera(), but with first person controller in mind */
|
/* same as draw_camera(), but with first person controller in mind */
|
||||||
/* direction and up vectors are inferred from roll, pitch and yaw parameters (in radians) */
|
/* direction and up vectors are inferred from roll, pitch and yaw parameters (in radians) */
|
||||||
@ -88,11 +92,12 @@ typedef struct DrawCameraFromPrincipalAxesResult {
|
|||||||
Vec3 direction;
|
Vec3 direction;
|
||||||
Vec3 up;
|
Vec3 up;
|
||||||
} DrawCameraFromPrincipalAxesResult;
|
} DrawCameraFromPrincipalAxesResult;
|
||||||
TWN_API DrawCameraFromPrincipalAxesResult draw_camera_from_principal_axes(Vec3 position,
|
TWN_API DrawCameraFromPrincipalAxesResult
|
||||||
float fov,
|
draw_camera_from_principal_axes(Vec3 position,
|
||||||
float roll,
|
float roll, /* optional, default: 0 */
|
||||||
float pitch,
|
float pitch, /* optional, default: 0 */
|
||||||
float yaw);
|
float yaw, /* optional, default: 0 */
|
||||||
|
float fov); /* optional, default: PI / 6 * 3 (90 degrees) */
|
||||||
|
|
||||||
/* expects '*' masks that will be expanded to 6 names: 'up', 'down', 'east', 'west', 'north' and 'south' */
|
/* expects '*' masks that will be expanded to 6 names: 'up', 'down', 'east', 'west', 'north' and 'south' */
|
||||||
TWN_API void draw_skybox(const char *textures);
|
TWN_API void draw_skybox(const char *textures);
|
||||||
|
@ -179,9 +179,9 @@
|
|||||||
"header": "twn_draw.h",
|
"header": "twn_draw.h",
|
||||||
"params": [
|
"params": [
|
||||||
{ "name": "position", "type": "Vec3" },
|
{ "name": "position", "type": "Vec3" },
|
||||||
{ "name": "fov", "type": "float" },
|
{ "name": "direction", "type": "Vec3", "default": { "x": 0, "y": 0, "z": -1 } },
|
||||||
{ "name": "up", "type": "Vec3" },
|
{ "name": "up", "type": "Vec3", "default": { "x": 0, "y": 1, "z": 0 } },
|
||||||
{ "name": "direction", "type": "Vec3" }
|
{ "name": "fov", "type": "float", "default": 1.57079632679 }
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -191,10 +191,10 @@
|
|||||||
"header": "twn_draw.h",
|
"header": "twn_draw.h",
|
||||||
"params": [
|
"params": [
|
||||||
{ "name": "position", "type": "Vec3" },
|
{ "name": "position", "type": "Vec3" },
|
||||||
{ "name": "fov", "type": "float" },
|
{ "name": "roll", "type": "float", "default": 0 },
|
||||||
{ "name": "roll", "type": "float" },
|
{ "name": "pitch", "type": "float", "default": 0 },
|
||||||
{ "name": "pitch", "type": "float" },
|
{ "name": "yaw", "type": "float", "default": 0 },
|
||||||
{ "name": "yaw", "type": "float" }
|
{ "name": "fov", "type": "float", "default": 1.57079632679 }
|
||||||
],
|
],
|
||||||
"return": {
|
"return": {
|
||||||
"fields": [
|
"fields": [
|
||||||
|
@ -413,8 +413,9 @@ void render(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void draw_camera(Vec3 position, float fov, Vec3 up, Vec3 direction) {
|
void draw_camera(Vec3 position, Vec3 direction, Vec3 up, float fov) {
|
||||||
if (fabsf(0.0f - fov) < 0.00001f || fov >= M_PIf)
|
bool const orthographic = fabsf(0.0f - fov) < 0.00001f;
|
||||||
|
if (!orthographic && fov >= M_PIf)
|
||||||
log_warn("Invalid fov given (%f)", (double)fov);
|
log_warn("Invalid fov given (%f)", (double)fov);
|
||||||
|
|
||||||
Camera const camera = {
|
Camera const camera = {
|
||||||
@ -424,14 +425,24 @@ void draw_camera(Vec3 position, float fov, Vec3 up, Vec3 direction) {
|
|||||||
.up = up,
|
.up = up,
|
||||||
};
|
};
|
||||||
|
|
||||||
camera_projection_matrix = camera_perspective(&camera);
|
if (!orthographic)
|
||||||
camera_look_at_matrix = camera_look_at(&camera);
|
camera_projection_matrix = camera_perspective(&camera);
|
||||||
|
else
|
||||||
|
camera_projection_matrix = camera_orthographic(&camera);
|
||||||
|
|
||||||
|
camera_look_at_matrix = camera_look_at(&camera);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* TODO: https://stackoverflow.com/questions/62493770/how-to-add-roll-in-camera-class */
|
/* TODO: https://stackoverflow.com/questions/62493770/how-to-add-roll-in-camera-class */
|
||||||
DrawCameraFromPrincipalAxesResult draw_camera_from_principal_axes(Vec3 position, float fov, float roll, float pitch, float yaw) {
|
DrawCameraFromPrincipalAxesResult draw_camera_from_principal_axes(Vec3 position,
|
||||||
if (fabsf(0.0f - fov) < 0.00001f || fov >= M_PIf)
|
float roll,
|
||||||
|
float pitch,
|
||||||
|
float yaw,
|
||||||
|
float fov)
|
||||||
|
{
|
||||||
|
bool const orthographic = fabsf(0.0f - fov) < 0.00001f;
|
||||||
|
if (!orthographic && fov >= M_PIf)
|
||||||
log_warn("Invalid fov given (%f)", (double)fov);
|
log_warn("Invalid fov given (%f)", (double)fov);
|
||||||
|
|
||||||
(void)roll;
|
(void)roll;
|
||||||
@ -451,8 +462,12 @@ DrawCameraFromPrincipalAxesResult draw_camera_from_principal_axes(Vec3 position,
|
|||||||
.up = (Vec3){0, 1, 0},
|
.up = (Vec3){0, 1, 0},
|
||||||
};
|
};
|
||||||
|
|
||||||
camera_projection_matrix = camera_perspective(&camera);
|
if (!orthographic)
|
||||||
camera_look_at_matrix = camera_look_at(&camera);
|
camera_projection_matrix = camera_perspective(&camera);
|
||||||
|
else
|
||||||
|
camera_projection_matrix = camera_orthographic(&camera);
|
||||||
|
|
||||||
|
camera_look_at_matrix = camera_look_at(&camera);
|
||||||
|
|
||||||
return (DrawCameraFromPrincipalAxesResult) {
|
return (DrawCameraFromPrincipalAxesResult) {
|
||||||
.direction = camera.target,
|
.direction = camera.target,
|
||||||
|
@ -52,3 +52,24 @@ Matrix4 camera_perspective(const Camera *const camera) {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Matrix4 camera_orthographic(const Camera *const camera) {
|
||||||
|
/* from cglm */
|
||||||
|
(void)camera;
|
||||||
|
|
||||||
|
Matrix4 result = {0};
|
||||||
|
|
||||||
|
const float rl = 1.0f / (float)1;
|
||||||
|
const float tb = 1.0f / (float)1;
|
||||||
|
const float fn = -1.0f / (CAMERA_FAR_Z - CAMERA_NEAR_Z);
|
||||||
|
|
||||||
|
result.row[0].x = 2.0f * rl;
|
||||||
|
result.row[1].y = 2.0f * tb;
|
||||||
|
result.row[2].z = 2.0f * fn;
|
||||||
|
result.row[3].x = -(float)1 * rl;
|
||||||
|
result.row[3].y = -(float)1 * tb;
|
||||||
|
result.row[3].z = (CAMERA_FAR_Z + CAMERA_NEAR_Z) * fn;
|
||||||
|
result.row[3].w = 1.0f;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
@ -16,7 +16,7 @@ typedef struct Camera {
|
|||||||
} Camera;
|
} Camera;
|
||||||
|
|
||||||
Matrix4 camera_look_at(const Camera *camera);
|
Matrix4 camera_look_at(const Camera *camera);
|
||||||
|
|
||||||
Matrix4 camera_perspective(const Camera *const camera);
|
Matrix4 camera_perspective(const Camera *const camera);
|
||||||
|
Matrix4 camera_orthographic(const Camera *const camera);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user