source: trunk/test/unit/camera.cpp @ 2933

Last change on this file since 2933 was 2933, checked in by touky, 6 years ago

testsuite : fixed camera.cpp build

  • Property svn:eol-style set to LF
File size: 7.7 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//            (c)      2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
6//   This program is free software; you can redistribute it and/or
7//   modify it under the terms of the Do What The Fuck You Want To
8//   Public License, Version 2, as published by Sam Hocevar. See
9//   http://www.wtfpl.net/ for more details.
10//
11
12#if defined HAVE_CONFIG_H
13#   include "config.h"
14#endif
15
16#include "core.h"
17#include "lol/unit.h"
18
19namespace lol
20{
21
22LOLUNIT_FIXTURE(CameraTest)
23{
24    Camera  tc;
25    vec3    eye;
26    vec3    target;
27    vec3    up;
28    mat4    m_lookat;
29    quat    q_lookat;
30    vec3    v_lookat;
31    float   fov;
32    float   screen_size;
33    float   screen_ratio;
34    float   near;
35    float   far;
36    bool    is_shifted;
37
38    void SetUp()
39    {
40        eye = vec3(0.f, 0.f, 50.f);
41        target = vec3(0.f);
42        up = vec3(0.f, 1.f, 0.f);
43        m_lookat = mat4::lookat(eye, target, up);
44        q_lookat = quat(m_lookat);
45        v_lookat = vec3::toeuler_zyx(q_lookat);
46        fov = 90.f;
47        screen_size = 800.f;
48        screen_ratio = 1.0f;
49        near = 1.f;
50        far = 1000.f;
51        is_shifted = false;
52    }
53
54    void TearDown() {}
55
56#define TEST_VECTOR(v0, v1) \
57        LOLUNIT_ASSERT_DOUBLES_EQUAL(v0.x, v1.x, 1.e-5f); \
58        LOLUNIT_ASSERT_DOUBLES_EQUAL(v0.y, v1.y, 1.e-5f); \
59        LOLUNIT_ASSERT_DOUBLES_EQUAL(v0.z, v1.z, 1.e-5f);
60
61    LOLUNIT_TEST(SetViewTest)
62    {
63        tc.SetView(eye, target, up);
64        TEST_VECTOR(eye, tc.GetPosition());
65        TEST_VECTOR(target, tc.GetTarget());
66        TEST_VECTOR(up, tc.GetUp());
67
68        tc.SetView(eye, q_lookat);
69        TEST_VECTOR(eye, tc.GetPosition());
70        TEST_VECTOR(target, tc.GetTarget());
71        TEST_VECTOR(up, tc.GetUp());
72
73        tc.SetView(eye, v_lookat);
74        TEST_VECTOR(eye, tc.GetPosition());
75        TEST_VECTOR(target, tc.GetTarget());
76        TEST_VECTOR(up, tc.GetUp());
77
78        tc.SetView(m_lookat);
79        TEST_VECTOR(eye, tc.GetPosition());
80        TEST_VECTOR(target, tc.GetTarget());
81        TEST_VECTOR(up, tc.GetUp());
82
83        tc.UseTarget(false);
84        TEST_VECTOR(vec3(0.f, 0.f, 49.f), tc.GetTarget());
85    }
86
87#define TEST_MATRIX(m0, m1) \
88        LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[0][0], m1[0][0], 1.e-5f); \
89        LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[1][0], m1[1][0], 1.e-5f); \
90        LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[2][0], m1[2][0], 1.e-5f); \
91        LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[3][0], m1[3][0], 1.e-5f); \
92                                                                  \
93        LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[0][1], m1[0][1], 1.e-5f); \
94        LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[1][1], m1[1][1], 1.e-5f); \
95        LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[2][1], m1[2][1], 1.e-5f); \
96        LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[3][1], m1[3][1], 1.e-5f); \
97                                                                  \
98        LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[0][2], m1[0][2], 1.e-5f); \
99        LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[1][2], m1[1][2], 1.e-5f); \
100        LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[2][2], m1[2][2], 1.e-5f); \
101        LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[3][2], m1[3][2], 1.e-5f); \
102                                                                  \
103        LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[0][3], m1[0][3], 1.e-5f); \
104        LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[1][3], m1[1][3], 1.e-5f); \
105        LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[2][3], m1[2][3], 1.e-5f); \
106        LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[3][3], m1[3][3], 1.e-5f);
107
108
109    LOLUNIT_TEST(SetProjectionTest)
110    {
111        mat4 refmx = mat4::perspective(fov, screen_size, screen_size * screen_ratio, near, far);
112
113        tc.SetProjection(fov, near, far, screen_size, screen_ratio);
114        TEST_MATRIX(refmx, tc.GetProjection());
115        LOLUNIT_ASSERT_DOUBLES_EQUAL(fov, tc.GetFov(), 1.e-5f);
116        LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_size, tc.GetScreenSize(), 1.e-5f);
117        LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
118        LOLUNIT_ASSERT_DOUBLES_EQUAL(near, tc.GetNear(), 1.e-5f);
119        LOLUNIT_ASSERT_DOUBLES_EQUAL(far, tc.GetFar(), 1.e-5f);
120        LOLUNIT_ASSERT(is_shifted == tc.IsShifted());
121
122        tc.SetProjection(fov, near, far);
123        TEST_MATRIX(refmx, tc.GetProjection());
124        LOLUNIT_ASSERT_DOUBLES_EQUAL(fov, tc.GetFov(), 1.e-5f);
125        LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_size, tc.GetScreenSize(), 1.e-5f);
126        LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
127        LOLUNIT_ASSERT_DOUBLES_EQUAL(near, tc.GetNear(), 1.e-5f);
128        LOLUNIT_ASSERT_DOUBLES_EQUAL(far, tc.GetFar(), 1.e-5f);
129        LOLUNIT_ASSERT(is_shifted == tc.IsShifted());
130
131        tc.SetProjection(refmx);
132        TEST_MATRIX(refmx, tc.GetProjection());
133
134        tc.SetFov(fov);
135        TEST_MATRIX(refmx, tc.GetProjection());
136        LOLUNIT_ASSERT_DOUBLES_EQUAL(fov, tc.GetFov(), 1.e-5f);
137        LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_size, tc.GetScreenSize(), 1.e-5f);
138        LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
139        LOLUNIT_ASSERT_DOUBLES_EQUAL(near, tc.GetNear(), 1.e-5f);
140        LOLUNIT_ASSERT_DOUBLES_EQUAL(far, tc.GetFar(), 1.e-5f);
141        LOLUNIT_ASSERT(is_shifted == tc.IsShifted());
142
143        tc.SetScreenInfos(screen_size);
144        TEST_MATRIX(refmx, tc.GetProjection());
145        LOLUNIT_ASSERT_DOUBLES_EQUAL(fov, tc.GetFov(), 1.e-5f);
146        LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_size, tc.GetScreenSize(), 1.e-5f);
147        LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
148        LOLUNIT_ASSERT_DOUBLES_EQUAL(near, tc.GetNear(), 1.e-5f);
149        LOLUNIT_ASSERT_DOUBLES_EQUAL(far, tc.GetFar(), 1.e-5f);
150        LOLUNIT_ASSERT(is_shifted == tc.IsShifted());
151
152        tc.SetScreenInfos(screen_size, screen_ratio);
153        TEST_MATRIX(refmx, tc.GetProjection());
154        LOLUNIT_ASSERT_DOUBLES_EQUAL(fov, tc.GetFov(), 1.e-5f);
155        LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_size, tc.GetScreenSize(), 1.e-5f);
156        LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
157        LOLUNIT_ASSERT_DOUBLES_EQUAL(near, tc.GetNear(), 1.e-5f);
158        LOLUNIT_ASSERT_DOUBLES_EQUAL(far, tc.GetFar(), 1.e-5f);
159        LOLUNIT_ASSERT(is_shifted == tc.IsShifted());
160
161        tc.SetDrawInfos(far);
162        TEST_MATRIX(refmx, tc.GetProjection());
163        LOLUNIT_ASSERT_DOUBLES_EQUAL(fov, tc.GetFov(), 1.e-5f);
164        LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_size, tc.GetScreenSize(), 1.e-5f);
165        LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
166        LOLUNIT_ASSERT_DOUBLES_EQUAL(near, tc.GetNear(), 1.e-5f);
167        LOLUNIT_ASSERT_DOUBLES_EQUAL(far, tc.GetFar(), 1.e-5f);
168        LOLUNIT_ASSERT(is_shifted == tc.IsShifted());
169
170        tc.SetDrawInfos(near, far);
171        TEST_MATRIX(refmx, tc.GetProjection());
172        LOLUNIT_ASSERT_DOUBLES_EQUAL(fov, tc.GetFov(), 1.e-5f);
173        LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_size, tc.GetScreenSize(), 1.e-5f);
174        LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
175        LOLUNIT_ASSERT_DOUBLES_EQUAL(near, tc.GetNear(), 1.e-5f);
176        LOLUNIT_ASSERT_DOUBLES_EQUAL(far, tc.GetFar(), 1.e-5f);
177        LOLUNIT_ASSERT(is_shifted == tc.IsShifted());
178
179        is_shifted = true;
180        refmx = mat4::shifted_perspective(fov, screen_size, screen_ratio, near, far);
181
182        tc.UseShift(is_shifted);
183        TEST_MATRIX(refmx, tc.GetProjection());
184        LOLUNIT_ASSERT_DOUBLES_EQUAL(fov, tc.GetFov(), 1.e-5f);
185        LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_size, tc.GetScreenSize(), 1.e-5f);
186        LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
187        LOLUNIT_ASSERT_DOUBLES_EQUAL(near, tc.GetNear(), 1.e-5f);
188        LOLUNIT_ASSERT_DOUBLES_EQUAL(far, tc.GetFar(), 1.e-5f);
189        LOLUNIT_ASSERT(is_shifted == tc.IsShifted());
190    }
191};
192
193} /* namespace lol */
194
Note: See TracBrowser for help on using the repository browser.