October 28, 2020, 06:10:01 AM

### Author Topic: [bb] Vector/Matrix Lib by AntonyWells [ 1+ years ago ]  (Read 1288 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Vector/Matrix Lib by AntonyWells [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Vector/Matrix Lib
Author : AntonyWells
Posted : 1+ years ago

Description : V33 and a half.

Added Gen_final_matrix..basically this totally simplifies the whole show, just use this to generate a transformation matrix,

ePitch=20
eYaw=50
rRoll=0
X#=5
y#=5
z#=5
local tMatrix#[16]

gen_final_matrix( ePitch,eYaw,eRoll,x,y,z,tMatrix)

The result will be placed in tMatrix(Or whatever you call it, name is tied.)
V1.1

Added functions to generate pitch/yaw/roll matrices and also a function to multiple two matrices by each other into a third output matrix(vital!)

Basically to transform a point by a given pitch,yaw,roll as in blitz, do this,
local pitchMatrix#[16],yawMatrix#[16],rollMatrix#[16]
local tempMatrix#[16],finalMatrix#[16],positionMatrix#[16]
gen_pitch_matrix( pitchMatrix,pitch)
gen_yaw_matrix( yawMatrix,yaw)
gen_roll_matrix( rollMatrix,roll)
gen_position_matrix( positionMatrix,x,y,z)
multi_mat( pitchMatrix,yawMatrix,tempMatrix)
multi_mat( tempMatrix,rollMatrix,yawMatrix)
multi_mat( rollMatrix,positionMatrix,finalMatrix)

Now in final matrix you have a matrix that will transform any vector by the combined pitch/yaw/roll as expected.

V1.0
-
Perform matrice operations/transforms etc.

There's also a tangent function in there to generate tangent space coords for normal/bump-mapping.

Matrices are 16 element constant arrays.

For example,

local myMatrix[16]

matrixIdentity( myMatrix)

The reason being is there's no need to pass/collect the data, as the function works *directly* on the passed object. This is important for these type of mass use funcs...

Also, vector(I.e 3 part variables) are 3 element constant arrays.

i.e, local vectorA[3]

myVectorFunc( vectorA)
(btw I believe the triangle normal functions were culled from somewhere else..I know I didn't write them anyway, but definitely freeware..or I wouldn't have used them in the first place)

Code :
Code: BlitzBasic
1. Function matrixIdentity(matrix#[16])
2.   matrix[ 0] = 1.0;  matrix[ 1] = 0.0;  matrix[ 2] = 0.0;  matrix[ 3] = 0.0;
3.   matrix[ 4] = 0.0;  matrix[ 5] = 1.0;  matrix[ 6] = 0.0;  matrix[ 7] = 0.0;
4.   matrix[ 8] = 0.0;  matrix[ 9] = 0.0;  matrix[10] = 1.0;  matrix[11] = 0.0;
5.   matrix[12] = 0.0;  matrix[13] = 0.0;  matrix[14] = 0.0;  matrix[15] = 1.0;
6. End Function
7.
8.
9. Function gen_pitch_matrix(matrix#[16])
10.         matrix[0]=1:matrix[1]=0:matrix[2]=0:matrix[3]=0
11.         matrix[4]=0:matrix[5]=Cos(a):matrix[6]=Sin(a):matrix[7]=0
12.         matrix[8]=0:matrix[9]=-Sin(a):matrix[10]=Cos(a):matrix[11]=0
13.         matrix[12]=0:matrix[13]=0:matrix[14]=0:matrix[15]=1
14. End Function
15.
16. Function gen_yaw_mat(matrix#[16])
17.         matrix[0]=Cos(a):matrix[1]=0:matrix[2]=-Sin(a):matrix[4]=0
18.         matrix[4]=0:matrix[5]=1:matrix[6]=0:matrix[7]=0
19.         matrix[8]=Sin(a):matrix[9]=0:matrix[10]=Cos(a):matrix[11]=0
20.         matrix[12]=0:matrix[13]=0:matrix[14]=0:matrix[15]=0
21. End Function
22.
23. Function gen_roll_matrix(matrix#[16])
24.         matrix[0]=Cos(a):matrix[1]=Sin(a):matrix[2]=0:matrix[3]=0
25.         matrix[4]=-Sin(a):matrix[5]=Cos(a):matrix[6]=0:matrix[7]=0
26.         matrix[8]=0:matrix[9]=0:matrix[10]=0:matrix[11]=0
27.         matrix[12]=0:matrix[13]=0:matrix[14]=0:matrix[15]=0
28. End Function
29.
30. Function gen_position_matrix(matrix#[16],x#,y#,z#)
31.         matrix[0]=1:matrix[1]=0:matrix[2]=0:matrix[3]=x
32.         matrix[4]=0:matrix[5]=1:matrix[6]=0:matrix[7]=y
33.         matrix[8]=0:matrix[9]=0:matrix[10]=0:matrix[11]=z
34.         matrix[12]=0:matrix[13]=0:matrix[14]=0:matrix[15]=1
35. End Function
36.
37. Function multi_mat(matrix1#[16],matrix2#[16],matrix3#[16]) ;Takes matrices i1 and i2 and combines them, resulting in i3
38.         For m=0 To 3
39.                 For m1=0 To 3
40.                         matrix3[ m1*4+m]=0
41.                         For m2=0 To 3
42.                                 matrix3[ m1*4+m]=matrix3[ m1*4+m1]+matrix2[m2*4+m]*matrix1[m1*4+m2]
43.                         Next
44.                 Next
45.         Next
46. End Function
47.
48. Function gen_final_matrix( pitch#,yaw#,roll#,x#,y#,z#,outMatrix#[16])
49.         Local pitchMatrix#[16],yawMatrix#[16],rollMatrix#[16]
50.         Local positionMatrix#[16],tempMatrix#[16]
51.         gen_pitch_matrix( pitchMatrix,pitch)
52.         gen_yaw_matrix( yawMatrix,yaw)
53.         gen_roll_matrix( rollMatrix,roll)
54.         gen_position_matrix( positionMatrix,x,y,z)
55.         multi_mat(pitchMatrix,yawMatrix,tempMatrix)
56.         multi_mat(tempMatrix,rollMatrix,pitchMatrix)
57.         multi_mat(pitchMatrix,positionMatrix,outMatrix)
58. End Function
59.
60. ;//////////////////////////
61. ;// Invert a matrix. (Matrix MUST be orhtonormal!)
62. ;//   in - Input matrix
63. ;//   out - Output matrix
64. ;//////////////////////////
65. Function matrixInvert(in#[16], out#[16])
66.  ; // Transpose rotation
67.   out[ 0] = in[ 0];  out[ 1] = in[ 4];  out[ 2] = in[ 8];
68.   out[ 4] = in[ 1];  out[ 5] = in[ 5];  out[ 6] = in[ 9];
69.   out[ 8] = in[ 2];  out[ 9] = in[ 6];  out[10] = in[10];
70.
71.  ; // Clear shearing terms
72.   out[3] = 0.0;f; out[7] = 0.0f; out[11] = 0.0f; out[15] = 1.0f;
73.
74.  ; // Translation is minus the dot of tranlation And rotations
75.   out[12] = -(in[12]*in[ 0]) - (in[13]*in[ 1]) - (in[14]*in[ 2]);
76.   out[13] = -(in[12]*in[ 4]) - (in[13]*in[ 5]) - (in[14]*in[ 6]);
77.   out[14] = -(in[12]*in[ 8]) - (in[13]*in[ 9]) - (in[14]*in[10]);
78. End Function
79.
80.
81. ;//////////////////////////
82. ;// Multiply a vector by a matrix.
83. ;//   vecIn - Input vector
84. ;//   m - Input matrix
85. ;///////////////////////////
86.
87. Function vecMatMult(vecIn#[3],m#[16], vecOut#[3])
88.   vecOut[0] = (vecIn[0]*m[ 0]) + (vecIn[1]*m[ 4]) + (vecIn[2]*m[ 8]) + m[12];
89.   vecOut[1] = (vecIn[0]*m[ 1]) + (vecIn[1]*m[ 5]) + (vecIn[2]*m[ 9]) + m[13];
90.   vecOut[2] = (vecIn[0]*m[ 2]) + (vecIn[1]*m[ 6]) + (vecIn[2]*m[10]) + m[14];
91. End Function
92.
93.
94. ;//////////////////////////
95. ;// Multiply a vector by just the 3x3 portion of a matrix.
96. ;//   vecIn - Input vector
97. ;//   m - Input matrix
98. ;//   vecOut - Output vector
99. ;//////////////////////////
100. ;void
101. Function vecMat3x3Mult(vecIn#[3], m#[16], vecOut#[3])
102.   vecOut[0] = (vecIn[0]*m[ 0]) + (vecIn[1]*m[ 4]) + (vecIn[2]*m[ 8]);
103.   vecOut[1] = (vecIn[0]*m[ 1]) + (vecIn[1]*m[ 5]) + (vecIn[2]*m[ 9]);
104.   vecOut[2] = (vecIn[0]*m[ 2]) + (vecIn[1]*m[ 6]) + (vecIn[2]*m[10]);
105. End Function
106.
107.
108. Function vecCrossProd (vecA#[3], vecB#[3], vecOut#[3])
109.    vecOut[0] =  vecA[1]*vecB[2] - vecA[2]*vecB[1];
110.    vecOut[1] =  vecA[2]*vecB[0] - vecA[0]*vecB[2];
111.    vecOut[2] =  vecA[0]*vecB[1] - vecA[1]*vecB[0];
112. End Function
113.
114. Function vecNormalize#(vec#[3])
115.    mag# = Sqr(vec[0]*vec[0] +vec[1]*vec[1] +vec[2]*vec[2]);
116.
117.    ;// don't divide by zero
118.    If (mag=0)
119.       vec[0] = 0.0;f;
120.       vec[1] = 0.0;f;
121.       vec[2] = 0.0;f;
122.       Return(0.0);
123.    EndIf
124.
125.    vec[0] =vec[0]/mag;
126.    vec[1] =vec[1]/mag;
127.    vec[2] =vec[2]/mag;
128.
129.    Return(mag);
130. End Function
131.
132. Function vecDotProd#(vecA#[3], vecB#[3])
133.    Return(vecA[0]*vecB[0] +vecA[1]*vecB[1] +vecA[2]*vecB[2]);
134. End Function
135.
136.
137. Function vecCopy (vecIn#[3], vecOut#[3])
138.    vecOut[0] = vecIn[0];
139.    vecOut[1] = vecIn[1];
140.    vecOut[2] = vecIn[2];
141.
142. End Function
143.
144.
145. Function vector( v1#,v2#,v3#,vect#[3])
146.         vect[0]=v1
147.         vect[1]=v2
148.         vect[2]=v3
149. End Function
150.
151.
152. Function tang( vertex#[3], vertex2#[3], vertex3#[3],texcoords#[2], texcoords2#[2], texcoords3#[2],polynormal#[3], tangent#[3], binormal#[3], normal#[3] )
153.
154. Local txb#[3];
155. Local v1#[3],v2#[3]
156.
157. VECTOR( vertex2[0] - vertex[0], texcoords2[0] - texcoords[0], texcoords2[1] - texcoords[1],v1 );
158. VECTOR( vertex3[0] - vertex[0], texcoords3[0] - texcoords[0], texcoords3[1] - texcoords[1],v2 );
159.
160. crossProduct( v1, v2,txb );
161.
162.
163.
164. If( Abs( txb[0] ) > EPSILON )
165. tangent[0]  = -txb[1] / txb[0];
166. binormal[0] = -txb[2] / txb[0];
167. EndIf
168.
169.
170.
171.
172. v1[0] = vertex2[1] - vertex[1];
173.
174. v2[0] = vertex3[1] - vertex[1];
175.
176.
177. CrossProduct( v1, v2,txb );
178.
179. If( Abs( txb[0] ) > EPSILON )
180.         tangent[1]  = -txb[1] / txb[0];
181.         binormal[1] = -txb[2] / txb[0];
182. EndIf
183.
184. v1[0] = vertex2[2] - vertex[2];
185. v2[0] = vertex3[2] - vertex[2];
186.
187.
188. CrossProduct( v1, v2,txb);
189.
190.
191. If( Abs( txb[0] ) > EPSILON )
192.
193.         tangent[2]  = -txb[1] / txb[0];
194.         binormal[2] = -txb[2] / txb[0];
195. EndIf
196.
197.
198. Normalize( tangent );
199.
200. Normalize( binormal );
201.
202.
203. ;// Make a normal based on the tangent And binormal b/c it may be different than the poly's
204. ;// normal, this normal being computed here is better
205.
206. CrossProduct( tangent, binormal,normal);
207. Normalize( normal );
208.
209. ;// Make tangent space vectors orthogonal by recomputing the binormal with the corrected
210.
211. ;// tangent space normal.
212.
213. CrossProduct( tangent, normal,biNormal);
214. Normalize( binormal );
215.
216. If( vecDotProd( normal, polynormal ) < 0.0 )
217.         normal[0] = -normal[0];
218.         normal[1] = -normal[1];
219.         normal[2] = -normal[2];
220. EndIf
221.
222. End Function
223.
224.
225.
226.
227.
228.
229. Function TriangleNormal#(v1#[3],v2#[3],v3#[3],o#[3])
230. ;SubVector v1,v2,
231.
232. ux#=VectorX()
233. uy#=VectorY()
234. uz#=VectorZ()
235. ;SubVector Cx#,Cy#,Cz#,Bx#,By#,Bz#
236. vx#=VectorX()
237. vy#=VectorY()
238. vz#=VectorZ()
239. ;CrossProduct vx#,vy#,vz#,ux#,uy#,uz#
240. ;Normalize vectorx,vectory,vectorz
241. ;Return Ax#*vectorx+Ay#*vectory+Az#*vectorz
242. End Function
243.
244. Function VectorX#()
245. Return vectorx
246. End Function
247.
248. Function VectorY#()
249. Return vectory
250. End Function
251.
252. Function VectorZ#()
253. Return vectorz
254. End Function
255.
256. Function VectorW#()
257. Return vectorw
258. End Function
259.
260. Function SubVector(v1#[3],v2#[3],o#[3])
261.         o[0]=v1[0]-v2[0]
262.         o[1]=v1[1]-v2[1]
263.         o[2]=v1[2]=v2[2]
264. End Function
265.
266. Function Normalize(v#[3])
267. If v[0]=0 And v[1]=0 And v[2]=0 Return
268.         m#=Magnitude(v)
269.         v[0]=v[0]/m#
270.         v[1]=v[1]/m#
271.         v[2]=v[2]/m#
272. End Function
273.
274. Function CrossProduct(v1#[3],v2#[3],o#[3])
275. o[0]=v1[1]*v2[2]-v2[2]*v2[1]
276. o[1]=v1[2]*v2[0]-v1[0]*v2[2]
277. o[2]=v1[0]*v2[1]-v1[1]*v2[0]
278. End Function
279.
280.
281. Function Magnitude(v#[3])
282.         Return Sqr( (v[0]*v[0]) + (v[1]*v[1]) + (v[2]*v[2]) )
283. End Function
284.
285.
286. Function TriangleNX#(surf,tri_no)
287.
288. v0=TriangleVertex(surf,tri_no,0)
289. v1=TriangleVertex(surf,tri_no,1)
290. v2=TriangleVertex(surf,tri_no,2)
291.
292. ax#=VertexX#(surf,v1)-VertexX#(surf,v0)
293. ay#=VertexY#(surf,v1)-VertexY#(surf,v0)
294. az#=VertexZ#(surf,v1)-VertexZ#(surf,v0)
295.
296. bx#=VertexX#(surf,v2)-VertexX#(surf,v1)
297. by#=VertexY#(surf,v2)-VertexY#(surf,v1)
298. bz#=VertexZ#(surf,v2)-VertexZ#(surf,v1)
299.
300. nx#=(ay#*bz#)-(az#*by#)
301.
302. Return nx#
303.
304. End Function
305.
306.
307. Function TriangleNY#(surf,tri_no)
308.
309. v0=TriangleVertex(surf,tri_no,0)
310. v1=TriangleVertex(surf,tri_no,1)
311. v2=TriangleVertex(surf,tri_no,2)
312.
313. ax#=VertexX#(surf,v1)-VertexX#(surf,v0)
314. ay#=VertexY#(surf,v1)-VertexY#(surf,v0)
315. az#=VertexZ#(surf,v1)-VertexZ#(surf,v0)
316.
317. bx#=VertexX#(surf,v2)-VertexX#(surf,v1)
318. by#=VertexY#(surf,v2)-VertexY#(surf,v1)
319. bz#=VertexZ#(surf,v2)-VertexZ#(surf,v1)
320.
321. ny#=(az#*bx#)-(ax#*bz#)
322.
323. Return ny#
324.
325. End Function
326.
327.
328. Function TriangleNZ#(surf,tri_no)
329.
330. v0=TriangleVertex(surf,tri_no,0)
331. v1=TriangleVertex(surf,tri_no,1)
332. v2=TriangleVertex(surf,tri_no,2)
333.
334. ax#=VertexX#(surf,v1)-VertexX#(surf,v0)
335. ay#=VertexY#(surf,v1)-VertexY#(surf,v0)
336. az#=VertexZ#(surf,v1)-VertexZ#(surf,v0)
337.
338. bx#=VertexX#(surf,v2)-VertexX#(surf,v1)
339. by#=VertexY#(surf,v2)-VertexY#(surf,v1)
340. bz#=VertexZ#(surf,v2)-VertexZ#(surf,v1)
341.
342. nz#=(ax#*by#)-(ay#*bx#)
343.
344. Return nz#
345.
346. End Function

Comments :

JoshK(Posted 1+ years ago)

Well does gen_pitch_matrix() have one or two parameters?  And where is gen_yaw_matrix?

bytecode77(Posted 1+ years ago)

did you even test/use this matrix lib before you posted it here, or did you just write it down and put it here?the matrixIdentity function is crap as well.

N(Posted 1+ years ago)

Riiiiight.  Three year old math library and you're complaining to a banned user?

Naughty Alien(Posted 1+ years ago)

HAHAHAHAHHHAH

markcw(Posted 1+ years ago)

gen_yaw_matrix() is gen_yaw_mat().Agreed, it's not very tidy code but what do you expect, it's free. I think it's pretty good code overall.

SimplePortal 2.3.6 © 2008-2014, SimplePortal