~zjm/Moon3D

ref: 0fe31db4998f636b8f65ca790b75c0585ba47d86 Moon3D/src/3d/image/camera.c -rw-r--r-- 1.1 KiB
0fe31db4Zack Michener refactor camera/projection to avoid globals 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "camera.h"
#include <stdlib.h>
#include <assert.h>

Camera *NewCamera(Vector position)
{
	Camera *camera = malloc(sizeof(Camera));
	camera->position = position;
	camera->direction = V(0, 0, -1);
	camera->up = V(0, 1, 0);
	camera->transform = Identity();
	return camera;
}

void LookAt(Vector p, Camera *camera)
{
	camera->direction = NormalizeVector(VecSub(p, camera->position));
}

void MoveCamera(Vector dv, Camera *camera)
{
	camera->position = VecAdd(camera->position, dv);
}

void RotateCamera(double rx, double ry, double rz, Camera *camera)
{
	Transform t = RotationX(rx);
	AddTransform(RotationY(ry), &t);
	AddTransform(RotationZ(rz), &t);
	camera->direction = NormalizeVector(ApplyTransform(t, camera->direction));
}

Transform AlignCameraToAxis(Camera *camera)
{
	Vector Rx, Ry, Rz;
	Transform op = Identity();
	Rz = NegVec(camera->direction);
	Rx = CrossProd(camera->up, NegVec(camera->direction));
	Ry = CrossProd(Rz, Rx);
	op.m[0][0] = Rx.x;
	op.m[0][1] = Rx.y;
	op.m[0][2] = Rx.z;

	op.m[1][0] = Ry.x;
	op.m[1][1] = Ry.y;
	op.m[1][2] = Ry.z;

	op.m[2][0] = Rz.x;
	op.m[2][1] = Rz.y;
	op.m[2][2] = Rz.z;
	return op;
}