-
Notifications
You must be signed in to change notification settings - Fork 2
/
camera.c
132 lines (99 loc) · 2.93 KB
/
camera.c
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdlib.h>
#include <stdio.h>
#include "camera.h"
#include "shaders_errors.h"
Camera * newCamera(int w, int h)
{
Camera * camera = (Camera *) malloc(sizeof(Camera));
camera->pos[0] = 18.0f;
camera->pos[1] = 10.0f;
camera->pos[2] = 10.0f;
camera->q[0] = 1.0f;
camera->q[1] = 0.0f;
camera->q[2] = 0.0f;
camera->q[3] = 0.0f;
rotateCamera(camera, -30.0f, -160.0f, 30.0f);
camera->viewAngleY = 45.0f;
camera->aspect = ((float) w) / ((float) h);
camera->znear = 0.001f;
camera->zfar = 100.0f;
return camera;
}
void slideCamera(Camera * camera, float dr, float du, float df)
{
vec3 xyz;
vec3 ruf;
mat4 rot;
ruf[0] = dr;
ruf[1] = du;
ruf[2] = df;
setMatrixFromQuaternion(rot, camera->q);
transposeMatrix(rot);
setMulMatrixVec3(xyz, rot, ruf);
camera->pos[0] += xyz[0];
camera->pos[1] += xyz[1];
camera->pos[2] += xyz[2];
}
void rotateCamera(Camera * camera, float head, float pitch, float roll)
{
mat4 rot;
Quaternion dq;
setRotationFromHPR(rot, head, pitch, roll);
setQuaternionFromMatrix(dq, rot);
setMulQuaternion(camera->q, dq);
}
void setupCamera(ShaderProgram * sp, const Camera * camera)
{
/* Pointers */
GLint viewProjP, viewPosP;
/* Matrices */
mat4 viewTrMatrix, viewRotMatrix, viewMatrix;
mat4 projMatrix, viewProjMatrix;
setPerspectiveMatrix(projMatrix,
camera->viewAngleY,
camera->aspect,
camera->znear,
camera->zfar);
setMatrixFromQuaternion(viewRotMatrix, camera->q);
setTranslationMatrix(viewTrMatrix,
-camera->pos[0],
-camera->pos[1],
-camera->pos[2]);
setMulMatrix(viewMatrix, viewRotMatrix, viewTrMatrix);
setMulMatrix(viewProjMatrix, projMatrix, viewMatrix);
glUseProgram(sp->p);
viewProjP = glGetUniformLocation(sp->p, "transform.viewProjection");
/* TODO: if (viewProjP == -1) {} */
glUniformMatrix4fv(viewProjP, 1, GL_TRUE, viewProjMatrix);
viewPosP = glGetUniformLocation(sp->p, "transform.viewPosition");
/* TODO: if (viewPosP == -1) {} */
glUniform3fv(viewPosP, 1, camera->pos);
#ifdef DEBUG
validateShaderProgram(sp->p);
#endif
CHECK_OPENGL_ERRORS(__FILE__, __LINE__);
}
void setupCameraAlter(ShaderProgram * sp, const Camera * camera)
{
/* Pointers */
GLint rotP, viewPosP;
/* Matrices */
mat4 rotMatrix;
setMatrixFromQuaternion(rotMatrix, camera->q);
transposeMatrix(rotMatrix);
glUseProgram(sp->p);
rotP = glGetUniformLocation(sp->p, "transform.rot");
/* TODO: if (rotP == -1) {} */
glUniformMatrix4fv(rotP, 1, GL_TRUE, rotMatrix);
viewPosP = glGetUniformLocation(sp->p, "transform.viewPosition");
/* TODO: if (viewPosP == -1) {} */
glUniform3fv(viewPosP, 1, camera->pos);
#ifdef DEBUG
validateShaderProgram(sp->p);
#endif
CHECK_OPENGL_ERRORS(__FILE__, __LINE__);
}
void freeCamera(Camera * camera)
{
free(camera);
}