Line data Source code
1 : /**
2 : * @file api_vector.cpp
3 : * @brief Krig Game Engine Lua Scripting API - Rotation Library.
4 : *
5 : * Quaternion-based (4-D vector) rotation API functionality.
6 : */
7 : #include "api.h"
8 : #include "api_rotation.h"
9 :
10 : #if DOXYGEN_ONLY
11 : /**
12 : * Add two rotations represented as quaternions.
13 : * @param Vector4
14 : * @param Vector4
15 : * @return Vector4
16 : */
17 : Vector4 add(Vector4, Vector4);
18 : #endif
19 0 : static int add(lua_State *L) {
20 0 : Quaternion t = loadQuaternion(L, 1);
21 0 : Quaternion u = loadQuaternion(L, 2);
22 :
23 0 : t = t * u;
24 :
25 0 : returnQuaternion(L, t);
26 : return 1;
27 0 : }
28 :
29 : #if DOXYGEN_ONLY
30 : /**
31 : * Copy a quaternion.
32 : * @param Vector4
33 : * @return Vector4
34 : */
35 : Vector4 copy(Vector4);
36 : #endif
37 0 : static int copy(lua_State *L) {
38 0 : Quaternion t = loadQuaternion(L, 1);
39 0 : returnQuaternion(L, t);
40 : return 1;
41 0 : }
42 :
43 : #if DOXYGEN_ONLY
44 : /**
45 : * Convert rotation represented as an axis and an angle into a quaternion.
46 : * @param Vector3
47 : * @param float
48 : * @return Vector4
49 : */
50 : Vector4 from_axis(Vector3, float);
51 : #endif
52 0 : static int from_axis(lua_State *L) {
53 0 : int index = 1;
54 0 : Vector t = loadVector(L, index);
55 0 : float f = lua_tonumber(L, index);
56 :
57 0 : Quaternion qt;
58 0 : qt.buildFromAxis(t, f);
59 :
60 0 : returnQuaternion(L, qt);
61 : return 1;
62 0 : }
63 :
64 : #if DOXYGEN_ONLY
65 : /**
66 : * Convert rotation represented as euler angles into a quaternion.
67 : * @param Vector3
68 : * @return Vector4
69 : */
70 : Vector4 from_euler(Vector3);
71 : #endif
72 0 : static int from_euler(lua_State *L) {
73 0 : int index = 1;
74 0 : Vector t = loadVector(L, index);
75 :
76 0 : Quaternion qt;
77 0 : qt.buildFromEuler(t);
78 :
79 0 : returnQuaternion(L, qt);
80 : return 1;
81 0 : }
82 :
83 : #if DOXYGEN_ONLY
84 : /**
85 : * Convert quaternion to euler angles.
86 : * @param Vector4
87 : * @return Vector3
88 : */
89 : Vector3 to_euler(Vector4);
90 : #endif
91 0 : static int to_euler(lua_State *L) {
92 0 : Quaternion t = loadQuaternion(L, 1);
93 :
94 0 : Vector v;
95 0 : t.getEulerAngles(v);
96 :
97 0 : returnVector(L, v);
98 : return 1;
99 0 : }
100 :
101 : static const luaL_Reg krigRotationLib[] = {
102 : {"add", add},
103 : {"copy", copy},
104 : {"from_axis", from_axis},
105 : {"from_euler", from_euler},
106 : {"to_euler", to_euler},
107 : {NULL, NULL}
108 : };
109 :
110 9 : int luaopen_krigRotation (lua_State *L) {
111 9 : luaL_openlib(L, "krig.rotation", krigRotationLib, 0);
112 9 : return 1;
113 : }
|