73 lines
1.8 KiB
C
73 lines
1.8 KiB
C
#include <string.h>
|
|
#include <cglm/cam.h>
|
|
#include <GL/glew.h>
|
|
#include "include/camera.h"
|
|
|
|
struct camera *create_camera(int type) {
|
|
if (type != CAMERA_PERSPECTIVE &&
|
|
type != CAMERA_ORTHOGONAL) {
|
|
return NULL;
|
|
}
|
|
|
|
struct camera *c = malloc(sizeof(struct camera));
|
|
if (c == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
memset(c, 0, sizeof(struct camera));
|
|
|
|
c->type = type;
|
|
c->fov = 80.0f;
|
|
c->near_z = 0.01f;
|
|
c->far_z = 1000.0f;
|
|
c->yaw = -90.0f;
|
|
c->pitch = 0.0f;
|
|
c->sensitivity = 0.02f;
|
|
|
|
if (c->type == CAMERA_ORTHOGONAL) {
|
|
c->viewport_modifier = 0.5f;
|
|
}
|
|
|
|
c->pos[0] = 0.0f;
|
|
c->pos[1] = 0.0f;
|
|
c->pos[2] = 10.0f;
|
|
|
|
c->front[0] = 0.0f;
|
|
c->front[1] = 0.0f;
|
|
c->front[2] = -1.0f;
|
|
|
|
c->up[0] = 0.0f;
|
|
c->up[1] = 1.0f;
|
|
c->up[2] = 0.0f;
|
|
|
|
return c;
|
|
}
|
|
|
|
int bake_camera(struct camera *c) {
|
|
glm_mat4_identity(c->view);
|
|
glm_vec3_add(c->pos, c->front, c->center);
|
|
glm_lookat(c->pos, c->center, c->up, c->view);
|
|
|
|
glm_mat4_identity(c->projection);
|
|
|
|
int screen_viewport[4];
|
|
glGetIntegerv(GL_VIEWPORT, screen_viewport);
|
|
if (c->type == CAMERA_PERSPECTIVE) {
|
|
glm_perspective(glm_rad(c->fov),
|
|
(float) ((float)screen_viewport[2]/(float)screen_viewport[3]),
|
|
c->near_z, c->far_z,
|
|
c->projection);
|
|
} else if (c->type == CAMERA_ORTHOGONAL) {
|
|
float left = -screen_viewport[2]/2 * c->viewport_modifier;
|
|
float right = screen_viewport[2]/2 * c->viewport_modifier;
|
|
float top = screen_viewport[3]/2 * c->viewport_modifier;
|
|
float bottom = -screen_viewport[3]/2 * c->viewport_modifier;
|
|
glm_ortho(left, right, bottom, top, c->near_z, c->far_z, c->projection);
|
|
} else {
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|