Line data Source code
1 : /**
2 : * @file api_vector.cpp
3 : * @brief Krig Game Engine Lua Scripting API - Vector Library
4 : *
5 : * 3-D Vector math API functionality.
6 : */
7 : #include "api.h"
8 : #include "api_vector.h"
9 :
10 : #if DOXYGEN_ONLY
11 : /**
12 : * Calculate scalar value between two vectors.
13 : * @param Vector3
14 : * @param Vector3
15 : * @return float
16 : */
17 : float scalar(Vector3, Vector3);
18 : #endif
19 1 : static int scalar(lua_State *L) {
20 1 : int index = 1;
21 1 : Vector t = loadVector(L, index);
22 1 : Vector u = loadVector(L, index);
23 1 : lua_pushnumber(L, t.getScaler(u));
24 1 : return 1;
25 : }
26 :
27 : #if DOXYGEN_ONLY
28 : /**
29 : * Normalize the vector.
30 : * @param Vector3
31 : * @return Vector3
32 : */
33 : Vector3 normalize(Vector3);
34 : #endif
35 1 : static int normalize(lua_State *L) {
36 1 : int index = 1;
37 1 : Vector t = loadVector(L, index);
38 1 : t.normalize();
39 1 : returnVector(L, t);
40 1 : return 1;
41 : }
42 :
43 : #if DOXYGEN_ONLY
44 : /**
45 : * Calculate dot product between two vectors.
46 : * @param Vector3
47 : * @param Vector3
48 : * @return float
49 : */
50 : float dot_product(Vector3, Vector3);
51 : #endif
52 1 : static int dot_product(lua_State *L) {
53 1 : int index = 1;
54 1 : Vector t = loadVector(L, index);
55 1 : Vector u = loadVector(L, index);
56 1 : float angle = t.dotProduct(u);
57 1 : lua_pushnumber(L, angle);
58 1 : return 1;
59 : }
60 :
61 : #if DOXYGEN_ONLY
62 : /**
63 : * Calculate cross product between two vectors.
64 : * @param Vector3
65 : * @param Vector3
66 : * @return Vector3
67 : */
68 : Vector3 cross_product(Vector3, Vector3);
69 : #endif
70 1 : static int cross_product(lua_State *L) {
71 1 : int index = 1;
72 1 : Vector t = loadVector(L, index);
73 1 : Vector u = loadVector(L, index);
74 1 : Vector result;
75 1 : result.crossProduct(t, u);
76 1 : returnVector(L, result);
77 1 : return 1;
78 : }
79 :
80 : #if DOXYGEN_ONLY
81 : /**
82 : * Calculate average of two vectors.
83 : * @param Vector3
84 : * @param Vector3
85 : * @return Vector3
86 : */
87 : Vector3 average(Vector3, Vector3);
88 : #endif
89 0 : static int average(lua_State *L) {
90 0 : int index = 1;
91 0 : Vector t = loadVector(L, index);
92 0 : Vector u = loadVector(L, index);
93 0 : Vector result;
94 0 : result.average(t, u);
95 0 : returnVector(L, result);
96 0 : return 1;
97 : }
98 :
99 : #if DOXYGEN_ONLY
100 : /**
101 : * Add two vectors.
102 : * @param Vector3
103 : * @param Vector3
104 : * @return Vector3
105 : */
106 : Vector3 add(Vector3, Vector3);
107 : #endif
108 0 : static int add(lua_State *L) {
109 0 : int index = 1;
110 0 : Vector t = loadVector(L, index);
111 0 : Vector u = loadVector(L, index);
112 0 : t += u;
113 0 : returnVector(L, t);
114 0 : return 1;
115 : }
116 :
117 : #if DOXYGEN_ONLY
118 : /**
119 : * Subtract two vectors.
120 : * @param Vector3
121 : * @param Vector3
122 : * @return Vector3
123 : */
124 : Vector3 subtract(Vector3, Vector3);
125 : #endif
126 0 : static int subtract(lua_State *L) {
127 0 : int index = 1;
128 0 : Vector t = loadVector(L, index);
129 0 : Vector u = loadVector(L, index);
130 0 : t -= u;
131 0 : returnVector(L, t);
132 0 : return 1;
133 : }
134 :
135 : #if DOXYGEN_ONLY
136 : /**
137 : * Scale a vector.
138 : * @param Vector3
139 : * @param float
140 : * @return Vector3
141 : */
142 : Vector3 scale(Vector3, float);
143 : #endif
144 0 : static int scale(lua_State *L) {
145 0 : int index = 1;
146 0 : Vector t = loadVector(L, index);
147 0 : int f = (int)lua_tonumber(L,index);
148 0 : t = t * f;
149 0 : returnVector(L, t);
150 0 : return 1;
151 : }
152 :
153 : #if DOXYGEN_ONLY
154 : /**
155 : * Copy a vector.
156 : * @param Vector3
157 : * @return Vector3
158 : */
159 : Vector3 copy(Vector3);
160 : #endif
161 0 : static int copy(lua_State *L) {
162 0 : int index = 1;
163 0 : Vector t = loadVector(L, index);
164 0 : returnVector(L, t);
165 0 : return 1;
166 : }
167 :
168 : #if DOXYGEN_ONLY
169 : /**
170 : * Calculate distance between two vectors.
171 : * @param Vector3
172 : * @param Vector3
173 : * @return float
174 : */
175 : float distance(Vector3, Vector3);
176 : #endif
177 0 : static int distance(lua_State *L) {
178 0 : int index = 1;
179 0 : Vector t = loadVector(L, index);
180 0 : Vector u = loadVector(L, index);
181 0 : float distance = t.getDistance(u);
182 0 : lua_pushnumber(L, distance);
183 0 : return 1;
184 : }
185 :
186 : #if DOXYGEN_ONLY
187 : /**
188 : * Add vector components.
189 : * @param Vector3
190 : * @return float
191 : */
192 : float sum(Vector3);
193 : #endif
194 0 : static int sum(lua_State *L) {
195 0 : int index = 1;
196 0 : Vector t = loadVector(L, index);
197 0 : lua_pushnumber(L, t.getSum());
198 0 : return 1;
199 : }
200 :
201 : static const luaL_Reg krigVectorLib[] = {
202 : {"add", add},
203 : {"average", average},
204 : {"copy", copy},
205 : {"cross_product", cross_product},
206 : {"distance", distance},
207 : {"dot_product", dot_product},
208 : {"normalize", normalize},
209 : {"scalar", scalar},
210 : {"scale", scale},
211 : {"subtract", subtract},
212 : {"sum", sum},
213 : {NULL, NULL}
214 : };
215 :
216 13 : int luaopen_krigVector (lua_State *L) {
217 13 : luaL_openlib(L, "krig.vector", krigVectorLib, 0);
218 13 : return 1;
219 : }
|