123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- #include "camera.h"
- void cameraRecompute(struct Camera *cam) {
- cam->dir = aVec3fNormalize(cam->dir);
- const struct AVec3f
- z = aVec3fNeg(cam->dir),
- x = aVec3fNormalize(aVec3fCross(cam->up, z)),
- y = aVec3fCross(z, x);
- cam->axes = aMat3fv(x, y, z);
- cam->orientation = aMat3fTranspose(cam->axes);
- cam->view_projection = aMat4fMul(cam->projection,
- aMat4f3(cam->orientation, aVec3fMulMat(cam->orientation, aVec3fNeg(cam->pos))));
- }
- void cameraProjection(struct Camera *cam, float znear, float zfar, float horizfov, float aspect) {
- const float w = 2.f * znear * tanf(horizfov / 2.f), h = w / aspect;
- cam->z_far = zfar;
- cam->z_near = znear;
- //aAppDebugPrintf("%f %f %f %f -> %f %f", near, far, horizfov, aspect, w, h);
- cam->projection = aMat4fPerspective(znear, zfar, w, h);
- }
- void cameraLookAt(struct Camera *cam, struct AVec3f pos, struct AVec3f at, struct AVec3f up) {
- cam->pos = pos;
- cam->dir = aVec3fNormalize(aVec3fSub(at, pos));
- cam->up = up;
- cameraRecompute(cam);
- }
- void cameraMove(struct Camera *cam, struct AVec3f v) {
- cam->pos = aVec3fAdd(cam->pos, aVec3fMulf(cam->axes.X, v.x));
- cam->pos = aVec3fAdd(cam->pos, aVec3fMulf(cam->axes.Y, v.y));
- cam->pos = aVec3fAdd(cam->pos, aVec3fMulf(cam->axes.Z, v.z));
- }
- void cameraRotateYaw(struct Camera *cam, float yaw) {
- const struct AMat3f rot = aMat3fRotateAxis(cam->up, yaw);
- cam->dir = aVec3fMulMat(rot, cam->dir);
- }
- void cameraRotatePitch(struct Camera *cam, float pitch) {
- /* TODO limit pitch */
- const struct AMat3f rot = aMat3fRotateAxis(cam->axes.X, pitch);
- cam->dir = aVec3fMulMat(rot, cam->dir);
- }
|