summaryrefslogtreecommitdiff
path: root/libs/cglm/test/src/test_project.h
blob: 41bdecd7d64ea7528ebd3a7aeed4fea3115fa914 (plain)
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
/*
 * Copyright (c), Recep Aslantas.
 *
 * MIT License (MIT), http://opensource.org/licenses/MIT
 * Full license can be found in the LICENSE file
 */

#include "test_common.h"

TEST_IMPL(GLM_PREFIX, unprojecti) {
  mat4 model, view, proj, mvp;
  vec4 viewport = {0.0f, 0.0f, 800.0f, 600.0f};
  vec3 pos      = {13.0f, 45.0f, 0.74f};
  vec3 projected, unprojected;

  glm_translate_make(model, (vec3){0.0f, 0.0f, -10.0f});
  glm_lookat((vec3){0.0f, 0.0f, 0.0f}, pos, GLM_YUP, view);

  glm_perspective_default(0.5f, proj);
  glm_mat4_mulN((mat4 *[]){&proj, &view, &model}, 3, mvp);

  GLM(project)(pos, mvp, viewport, projected);

  glm_mat4_inv(mvp, mvp);
  GLM(unprojecti)(projected, mvp, viewport, unprojected);

  /* unprojected of projected vector must be same as original one */
  /* we used 0.01 because of projection floating point errors */
  ASSERT(fabsf(pos[0] - unprojected[0]) < 0.01)
  ASSERT(fabsf(pos[1] - unprojected[1]) < 0.01)
  ASSERT(fabsf(pos[2] - unprojected[2]) < 0.01)

  TEST_SUCCESS
}

TEST_IMPL(GLM_PREFIX, unproject) {
  mat4 model, view, proj, mvp;
  vec4 viewport = {0.0f, 0.0f, 800.0f, 600.0f};
  vec3 pos      = {13.0f, 45.0f, 0.74f};
  vec3 projected, unprojected;

  glm_translate_make(model, (vec3){0.0f, 0.0f, -10.0f});
  glm_lookat((vec3){0.0f, 0.0f, 0.0f}, pos, GLM_YUP, view);

  glm_perspective_default(0.5f, proj);
  glm_mat4_mulN((mat4 *[]){&proj, &view, &model}, 3, mvp);

  GLM(project)(pos, mvp, viewport, projected);
  GLM(unproject)(projected, mvp, viewport, unprojected);

  /* unprojected of projected vector must be same as original one */
  /* we used 0.01 because of projection floating point errors */
  ASSERT(fabsf(pos[0] - unprojected[0]) < 0.01)
  ASSERT(fabsf(pos[1] - unprojected[1]) < 0.01)
  ASSERT(fabsf(pos[2] - unprojected[2]) < 0.01)

  TEST_SUCCESS
}

TEST_IMPL(GLM_PREFIX, project) {
  mat4 model, view, proj, mvp;
  vec4 viewport = {0.0f, 0.0f, 800.0f, 600.0f};
  vec3 pos      = {13.0f, 45.0f, 0.74f};
  vec3 projected, unprojected;

  glm_translate_make(model, (vec3){0.0f, 0.0f, -10.0f});
  glm_lookat((vec3){0.0f, 0.0f, 0.0f}, pos, GLM_YUP, view);

  glm_perspective_default(0.5f, proj);
  glm_mat4_mulN((mat4 *[]){&proj, &view, &model}, 3, mvp);

  GLM(project)(pos, mvp, viewport, projected);
  GLM(unproject)(projected, mvp, viewport, unprojected);

  /* unprojected of projected vector must be same as original one */
  /* we used 0.01 because of projection floating point errors */
  ASSERT(fabsf(pos[0] - unprojected[0]) < 0.01)
  ASSERT(fabsf(pos[1] - unprojected[1]) < 0.01)
  ASSERT(fabsf(pos[2] - unprojected[2]) < 0.01)

  /* test with no projection */
  glm_mat4_identity(mvp);

  GLM(project)(pos, mvp, viewport, projected);
  GLM(unproject)(projected, mvp, viewport, unprojected);

  ASSERT(test_eq(pos[0], unprojected[0]))
  ASSERT(test_eq(pos[1], unprojected[1]))
  ASSERT(test_eq(pos[2], unprojected[2]))

  TEST_SUCCESS
}