~zjm/Moon3D

ref: 0e81b821a0782674dbed435a58ad682e7aef0282 Moon3D/src/3d/image/camera.c -rw-r--r-- 1.4 KiB
0e81b821Zack Michener wrap rendering objects in a RenderingContext struct 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "camera.h"
#include <stdlib.h>
#include <assert.h>

static Camera *camera = NULL;

void SetCamera(Vector position)
{
	if (camera != NULL) {
		free(camera);
	}
	camera = malloc(sizeof(Camera));
	camera->position = position;
	camera->direction = V(0, 0, -1);
	camera->up = V(0, 1, 0);
	camera->transform = Identity();
}

Camera *GetCamera(void)
{
	return camera;
}

Vector CameraPosition(void)
{
	assert(camera != NULL);
	return camera->position;
}

Vector CameraDirection(void)
{
	assert(camera != NULL);
	return camera->direction;
}

void LookAt(Vector p)
{
	assert(camera != NULL);
	camera->direction = NormalizeVector(VecSub(p, camera->position));
}

void MoveCamera(Vector dv)
{
	assert(camera != NULL);
	camera->position = VecAdd(camera->position, dv);
}

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

void PlaceCamera(Vector p)
{
	assert(camera != NULL);
	camera->position = p;
}

Transform AlignCameraToAxis(void)
{
	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;
}