twn_draw.h: new camera api

This commit is contained in:
veclavtalica
2024-10-28 12:34:48 +03:00
parent 1d35a3859b
commit d11143ac86
7 changed files with 73 additions and 41 deletions

View File

@ -1,8 +1,9 @@
#include "twn_draw_c.h"
#include "twn_draw.h"
#include "twn_engine_context_c.h"
#include "twn_camera.h"
#include "twn_camera_c.h"
#include "twn_types.h"
#include "twn_vec.h"
#include <SDL2/SDL.h>
#include <stb_ds.h>
@ -382,8 +383,39 @@ void render(void) {
}
void draw_camera(const Camera *const camera) {
/* TODO: skip recaulculating if it's the same? */
camera_projection_matrix = camera_perspective(camera);
camera_look_at_matrix = camera_look_at(camera);
void draw_camera(Vec3 position, float fov, Vec3 up, Vec3 direction) {
Camera const camera = {
.fov = fov,
.pos = position,
.target = direction,
.up = up,
};
camera_projection_matrix = camera_perspective(&camera);
camera_look_at_matrix = camera_look_at(&camera);
}
/* 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) {
(void)roll;
float yawc, yaws, pitchc, pitchs;
sincosf(yaw, &yaws, &yawc);
sincosf(pitch, &pitchs, &pitchc);
Camera const camera = {
.fov = fov,
.pos = position,
.target = m_vec_norm(((Vec3){
yawc * pitchc,
pitchs,
yaws * pitchc,
})),
.up = (Vec3){0, 1, 0},
};
camera_projection_matrix = camera_perspective(&camera);
camera_look_at_matrix = camera_look_at(&camera);
return (DrawCameraFromPrincipalAxesResult) {
.direction = camera.target,
.up = camera.up,
};
}

View File

@ -1,4 +1,4 @@
#include "twn_camera.h"
#include "twn_camera_c.h"
#include "twn_vec.h"
#include "twn_engine_context_c.h"

21
src/twn_camera_c.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef TWN_CAMERA_H
#define TWN_CAMERA_H
#include "twn_types.h"
/* TODO: make it cached? */
/* for example, perspective matrix only needs recaluclation on FOV change */
/* first person camera class */
typedef struct Camera {
Vec3 pos; /* eye position */
Vec3 target; /* normalized target vector */
Vec3 up; /* normalized up vector */
float fov; /* field of view, in radians */
} Camera;
Matrix4 camera_look_at(const Camera *camera);
Matrix4 camera_perspective(const Camera *const camera);
#endif

View File

@ -17,8 +17,8 @@
typedef struct Texture {
Rect srcrect; /* position in atlas */
SDL_Surface *data; /* original image data */
Rect srcrect; /* position in atlas */
SDL_Surface *data; /* original image data */
int atlas_index;
GPUTexture loner_texture; /* stored directly for loners, == 0 means atlas_index should be used */
GPUTexture repeating_texture; /* separately allocated Texture, for loners == loner_texture */