-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.c
More file actions
54 lines (47 loc) · 1.71 KB
/
camera.c
File metadata and controls
54 lines (47 loc) · 1.71 KB
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
//
// Created by Afterwind on 9/24/2017.
//
#include "camera.h"
#include "utils.h"
#include "shader.h"
#include "world.h"
#include <kazmath/vec4.h>
#include <string.h>
/* EXTERNAL FUNCTIONS */
kmVec3 cameraDefaultDirection = {0, 0, -1};
void geCameraUpdate(geCamera* camera) {
// Perspective and view matrix construction
kmMat4 projection;
kmMat4 view;
kmVec3 target;
kmMat4 rotX, rotY, rot;
memcpy(&camera->direction, &cameraDefaultDirection, sizeof(kmVec3));
kmMat4RotationY(&rotY, camera->rotation.y * PI / 180.0f);
kmMat4RotationX(&rotX, camera->rotation.x * PI / 180.0f);
kmMat4Multiply(&rot, &rotY, &rotX);
kmVec3MultiplyMat4(&camera->direction, &camera->direction, &rot);
kmVec3Add(&target, &camera->direction, &camera->pos);
kmMat4PerspectiveProjection(&projection, 70.0f, camera->aspectRatio, 0.01f, 500);
kmMat4LookAt(&view, &camera->pos, &target, &camera->up);
glUniformMatrix4fv(_U(view), 1, GL_FALSE, view.mat);
glUniformMatrix4fv(_U(projection), 1, GL_FALSE, projection.mat);
glUniform3fv(_U(viewPosition), 1, (const GLfloat *) &camera->pos);
glUniform3fv(_U(pl) + 1, 1, (const GLfloat *) &camera->pos);
}
kmVec3 geCameraRaycast(geCamera* camera, geWorld* world) {
size_t i;
kmVec3 pos = camera->pos;
kmVec3Subtract(&pos, &pos, &world->object->pos);
kmVec3 dir;
kmVec3Scale(&dir, &camera->direction, 0.01f);
kmVec3 block = { -1, -1, -1 };
for (i = 0; i < 1500; i++) {
block = geWorldFind(world, &pos);
if (block.x != -1 && block.y != -1 && block.z != -1) {
kmVec3Add(&block, &block, &world->object->pos);
return block;
}
kmVec3Add(&pos, &pos, &dir);
}
return block;
}