Original post.
https://www.freebasic.net/forum/viewto ... s#p216409
You will need:
matn.bi
Code: Select all
'Original glslstyle.bi by D.J. Peters.
'Edited by ShawnLG.
'Cleaned up source.
'Fix bugs.
'Added features for better compatibility with freebasic.
'Seperated datatypes into their own .bi files.
#include once "vecn.bi"
' ########
' # mat2 #
' ########
'M subscript for matrices (column),(row)
type mat2
declare constructor(d as single=1)
declare constructor(a as single,b as single, c as single,d as single)
declare operator cast as string
as single m00=any,m10=any
as single m01=any,m11=any
end type
constructor mat2(d as single)
m00=d : m10=0
m01=0 : m11=d
end constructor
constructor mat2(a as single, b as single, c as single, d as single)
m00=a : m10=b
m01=c : m11=d
end constructor
operator mat2.cast () as string
return "(" + str(m00) + ", " + str(m01) + "), (" + str(m10) + ", " + str(m11) + ")"
end operator
operator *(byref l as mat2, byref r as vec2) as vec2' assume column vector
return vec2(l.m00 * r.x + l.m01 * r.y, _
l.m10 * r.x + l.m11 * r.y)
end operator
operator *( byref r as vec2, byref l as mat2) as vec2' assume row vector
return vec2(l.m00 * r.x + l.m10 * r.y, _
l.m01 * r.x + l.m11 * r.y)
end operator
operator *(byref l as mat2, byref r as mat2) as mat2
return mat2(l.m00 * r.m00 + l.m01 * r.m10, _'m00
l.m10 * r.m00 + l.m11 * r.m10, _'m10
l.m00 * r.m10 + l.m01 * r.m11, _'m01
l.m10 * r.m01 + l.m11 * r.m11) 'm11
end operator
' ########
' # mat3 #
' ########
type mat3
declare constructor(d as single=1)
declare constructor( a as single, b as single, c as single, d as single, e as single, f as single, g as single, h as single, j as single)
declare constructor(byref a as vec3, byref b as vec3, byref c as vec3)
declare operator cast as string
declare sub setZRotationD(deg as single)
declare sub setZRotationR(rad as single)
declare sub setRotationD(byref axis as vec3, deg as single)
declare sub setRotationR(byref axis as vec3, rad as single)
declare sub setScaling(byref scale as vec2)
declare sub setScaling(scale as single)
declare sub setScaling(scaleX as single, scaleY as single)
as single m00=any,m10=any,m20=any
as single m01=any,m11=any,m21=any
as single m02=any,m12=any,m22=any
end type
constructor mat3(d as single)
m00=d : m10=0 : m20=0'currect
m01=0 : m11=d : m21=0
m02=0 : m12=0 : m22=d
end constructor
constructor mat3(a as single, b as single, c as single, d as single, e as single, f as single, g as single, h as single, j as single)
m00=a : m10=b : m20=c'currect
m01=d : m11=e : m21=f
m02=g : m12=h : m22=j
end constructor
constructor mat3(byref a as vec3, byref b as vec3, byref c as vec3)
m00=a.x : m10=a.y : m20=a.z 'currect
m01=b.x : m11=b.y : m21=b.z
m02=c.x : m12=c.y : m22=c.z
end constructor
operator mat3.cast () as string
return "(" + str(m00) + ", " + str(m01) + ", " + str(m02) + "), (" + str(m10) + ", " + str(m11) + ", " + str(m12) + "), (" + str(m20) + ", " + str(m21) + ", " + str(m22) + ")"
end operator
sub mat3.setZRotationD(deg as single)
setZRotationR(deg*Deg2Rad)
end sub
sub mat3.setZRotationR(rad as single)
dim as single co = cosf(rad)
dim as single si = sinf(rad)
m00= co : m10 = si : m20 = 0
m01=-si : m11 = co : m21 = 0
m02= 0 : m12 = 0 : m22 = 1
end sub
sub mat3.setRotationD(byref axis as vec3, deg as single)
setRotationR(axis,deg*Deg2Rad)
end sub
sub mat3.setRotationR(byref axis as vec3, rad as single)
dim as single co = cosf(rad)
dim as single si = sinf(rad)
dim as single oc = 1.0 - co
m00 = oc * axis.x * axis.x + co
m10 = oc * axis.x * axis.y - axis.z * si
m20 = oc * axis.z * axis.x + axis.y * si
m01 = oc * axis.x * axis.y + axis.z * si
m11 = oc * axis.y * axis.y + co
m21 = oc * axis.y * axis.z - axis.x * si
m02 = oc * axis.z * axis.x - axis.y * si
m12 = oc * axis.y * axis.z + axis.x * si
m22 = oc * axis.z * axis.z + co
end sub
sub mat3.setScaling(byref scale as vec2)
setScaling(scale.x,scale.y)
end sub
sub mat3.setScaling(scale as single)
setScaling(scale,scale)
end sub
sub mat3.setScaling(scaleX as single, scaleY as single)
M00 = scaleX : M10 = 0 : M20 = 0
M01 = 0 : M11 = scaleY : M21 = 0
M02 = 0 : M12 = 0 : M22 = 1
end sub
operator *(byref l as mat3, byref r as vec3) as vec3' assume column vector
return vec3(l.m00 * r.x + l.m01 * r.y + l.m02 * r.z, _
l.m10 * r.x + l.m11 * r.y + l.m12 * r.z, _
l.m20 * r.x + l.m21 * r.y + l.m22 * r.z)
end operator
operator *(byref r as vec3, byref l as mat3) as vec3' assume row vector
return vec3(l.m00 * r.x + l.m10 * r.y + l.m20 * r.z, _
l.m01 * r.x + l.m11 * r.y + l.m21 * r.z, _
l.m02 * r.x + l.m12 * r.y + l.m22 * r.z)
end operator
operator *(byref l as mat3, byref r as mat3) as mat3
return mat3(l.m00 * r.m00 + l.m01 * r.m10 + l.m02 * r.m20, _'m00
l.m10 * r.m00 + l.m11 * r.m10 + l.m12 * r.m20, _'m10
l.m20 * r.m00 + l.m21 * r.m10 + l.m22 * r.m20, _'m20
_
l.m00 * r.m01 + l.m01 * r.m11 + l.m02 * r.m21, _'m01
l.m10 * r.m01 + l.m11 * r.m11 + l.m12 * r.m21, _'m11
l.m20 * r.m01 + l.m21 * r.m11 + l.m22 * r.m21, _'m21
_
l.m00 * r.m02 + l.m01 * r.m12 + l.m02 * r.m22, _'m02
l.m10 * r.m02 + l.m11 * r.m12 + l.m12 * r.m22, _'m12
l.m20 * r.m02 + l.m21 * r.m12 + l.m22 * r.m22) 'm22
end operator
' ########
' # mat4 #
' ########
type mat4
declare constructor(d as single=1)
declare constructor(a as single,b as single,c as single,d as single,e as single,f as single,g as single,h as single,i as single,j as single,k as single,l as single,m as single,n as single,o as single,p as single)
declare operator cast as string
declare sub setProjection(fov as single=45, aspectRatio as single=4/3, near as single=0.1, far as single=100.0)
declare sub setOrtho2d(x as single, y as single, w as single, h as single)
declare sub setOrtho2d(x as single, y as single, w as single, h as single, near as single, far as single)
declare sub setOrtho(left_ as single, right_ as single, bottom as single, top as single, near as single, far as single)
as single m00=any,m10=any,m20=any,m30=any
as single m01=any,m11=any,m21=any,m31=any
as single m02=any,m12=any,m22=any,m32=any
as single m03=any,m13=any,m23=any,m33=any
end type
constructor mat4(a as single,b as single,c as single,d as single, _
e as single,f as single,g as single,h as single, _
i as single,j as single,k as single,l as single, _
m as single,n as single,o as single,p as single)
m00=a : m10=b : m20=c : m30=d
m01=e : m11=f : m21=g : m31=h
m02=i : m12=j : m22=k : m32=l
m03=m : m13=n : m23=o : m33=p
end constructor
constructor mat4(d as single)
m00=d : m10=0 : m20=0 : m30=0
m01=0 : m11=d : m21=0 : m31=0
m02=0 : m12=0 : m22=d : m32=0
m03=0 : m13=0 : m23=0 : m33=d
end constructor
operator mat4.cast () as string
return "(" + str(m00) + ", " + str(m01) + ", " + str(m02) + ", " + str(m03) + "), ("_
+ str(m10) + ", " + str(m11) + ", " + str(m12) + ", " + str(m13) + "), ("_
+ str(m20) + ", " + str(m21) + ", " + str(m22) + ", " + str(m23) + "), ("_
+ str(m30) + ", " + str(m31) + ", " + str(m32) + ", " + str(m33) + ")"
end operator
sub mat4.setProjection(fov as single, aspectRatio as single, near as single, far as single)
dim as single a = 1.0 / tanf((fov * Deg2Rad) * 0.5)
dim as single b = a / aspectRatio
dim as single c = (far + near) / (near - far)
dim as single d = (2 * far * near) / (near - far)
m00 = b : m10 = 0 : m20 = 0 : m30 = 0
m01 = 0 : m11 = b : m21 = 0 : m31 = 0
m02 = 0 : m12 = 0 : m22 = c : m32 =-1
m03 = 0 : m13 = 0 : m23 = d : m33 = 0
end sub
sub mat4.setOrtho2d(x as single, y as single, w as single, h as single)
setOrtho( x, x + w, y + h, y, 1, -1)
end sub
sub mat4.setOrtho2d(x as single, y as single, w as single, h as single, near as single, far as single)
setOrtho( x, x + w, y, y + h, near, far)
end sub
sub mat4.setOrtho(left_ as single, right_ as single, bottom as single, top as single, near as single, far as single)
dim as single x_orth = 2 / (right_ - left_)
dim as single y_orth = 2 / (top - bottom)
dim as single z_orth =-2 / (far - near)
dim as single tx = -(right_ + left_ ) / (right_ - left_)
dim as single ty = -(top + bottom) / (top - bottom)
dim as single tz = -(far + near ) / (far - near)
m00 = x_orth : m10 = 0 : m20 = 0 : m30 = 0
m01 = 0 : m11 = y_orth : m21 = 0 : m31 = 0
m02 = 0 : m12 = 0 : m22 = z_orth : m32 = 0
m03 = tx : m13 = ty : m23 = tz : m33 = 1
end sub
operator *(byref l as mat4, byref r as vec4) as vec4'assume column vector
return vec4(l.m00 * r.x + l.m01 * r.y + l.m02 * r.z + l.m03 * r.w, _
l.m10 * r.x + l.m11 * r.y + l.m12 * r.z + l.m13 * r.w, _
l.m20 * r.x + l.m21 * r.y + l.m22 * r.z + l.m23 * r.w, _
l.m30 * r.x + l.m31 * r.y + l.m32 * r.z + l.m33 * r.w)
end operator
operator *(byref r as vec4, byref l as mat4) as vec4' assume row vector
return vec4(l.m00 * r.x + l.m10 * r.y + l.m20 * r.z + l.m30 * r.w, _
l.m01 * r.x + l.m11 * r.y + l.m21 * r.z + l.m31 * r.w, _
l.m02 * r.x + l.m12 * r.y + l.m22 * r.z + l.m32 * r.w, _
l.m03 * r.x + l.m13 * r.y + l.m23 * r.z + l.m33 * r.w)
end operator
operator *(byref l as mat4, byref r as mat4) as mat4
return mat4( _
l.m00 * r.m00 + l.m01 * r.m10 + l.m02 * r.m20 + l.m03 * r.m30, _'m00
l.m10 * r.m00 + l.m11 * r.m10 + l.m12 * r.m20 + l.m13 * r.m30, _'m10
l.m20 * r.m00 + l.m21 * r.m10 + l.m22 * r.m20 + l.m23 * r.m30, _'m20
l.m30 * r.m00 + l.m31 * r.m10 + l.m32 * r.m20 + l.m33 * r.m30, _'m30
_
l.m00 * r.m01 + l.m01 * r.m11 + l.m02 * r.m21 + l.m03 * r.m31, _'m01
l.m10 * r.m01 + l.m11 * r.m11 + l.m12 * r.m21 + l.m13 * r.m31, _'m11
l.m20 * r.m01 + l.m21 * r.m11 + l.m22 * r.m21 + l.m23 * r.m31, _'m21
l.m30 * r.m01 + l.m31 * r.m11 + l.m32 * r.m21 + l.m33 * r.m31, _'m31
_
l.m00 * r.m02 + l.m01 * r.m12 + l.m02 * r.m22 + l.m03 * r.m32, _'m02
l.m10 * r.m02 + l.m11 * r.m12 + l.m12 * r.m22 + l.m13 * r.m32, _'m12
l.m20 * r.m02 + l.m21 * r.m12 + l.m22 * r.m22 + l.m23 * r.m32, _'m22
l.m30 * r.m02 + l.m31 * r.m12 + l.m32 * r.m22 + l.m33 * r.m32, _'m32
_
l.m00 * r.m03 + l.m01 * r.m13 + l.m02 * r.m23 + l.m03 * r.m33, _'m03
l.m10 * r.m03 + l.m11 * r.m13 + l.m12 * r.m23 + l.m13 * r.m33, _'m13
l.m20 * r.m03 + l.m21 * r.m13 + l.m22 * r.m23 + l.m23 * r.m33, _'m23
l.m30 * r.m03 + l.m31 * r.m13 + l.m32 * r.m23 + l.m33 * r.m33) 'm33
end operator
' The matrixCompMult function returns a matrix resulting from a component-wise multiplication.
' The function has two input parameters of the type singleing point matrix and returns a matrix of the same type.
' The indices of the returned matrix are calculated as follows: z[i][j] = x[i][j] * y[i][j]
' Side note: This is NOT the matrix multiplication known from linear algebra.
' To obtain the "normal" matrix multiplication the * operator is used: z = x * y
function matrixCompMult overload (byref x as mat2,byref y as mat2) as mat2
dim as mat2 z
dim as single ptr pz=@z.m00,px=@x.m00,py=@y.m00
dim as integer i2
for i as integer = 0 to 1
dim as integer ij=i2
for j as integer = 0 to 1
pz[ij] = px[ij] * py[ij] :ij+=1
next
i2+=2
next
return z
end function
function matrixCompMult (byref x as mat3, byref y as mat3) as mat3
dim as mat3 z
dim as single ptr pz=@z.m00,px=@x.m00,py=@y.m00
dim as integer i3
for i as integer = 0 to 1
dim as integer ij=i3
for j as integer = 0 to 1
pz[ij] = px[ij] * py[ij] :ij+=1
next
i3+=3
next
return z
end function
function matrixCompMult (byref x as mat4, byref y as mat4) as mat4
dim as mat4 z
dim as single ptr pz=@z.m00,px=@x.m00,py=@y.m00
dim as integer i4
for i as integer = 0 to 1
dim as integer ij=i4
for j as integer = 0 to 1
pz[ij] = px[ij] * py[ij] :ij+=1
next
i4+=4
next
return z
end function
Code: Select all
'Original glslstyle.bi by D.J. Peters.
'Edited by ShawnLG.
'Cleaned up source.
'Fix bugs.
'Added features for better compatibility with freebasic.
'Seperated datatypes into their own .bi files.
#include once "crt.bi" ' math.bi ...
#define DEG2RAD M_PI/180.0
#define RAD2DEG 180.0/M_PI
#undef atn
#undef exp
#undef log
#undef sqrt
#undef exp2
#undef log2
#undef pow
#undef ceil
#undef floor
' ########
' # vec2 #
' ########
type vec2
declare constructor
declare constructor(pxy as single)
declare constructor(px as single, py as single)
declare constructor(byref v2 as vec2)
'assignment operators
declare operator let(pxy as single)
declare operator let(byref p as vec2)
'cast as string for print function
declare operator cast() as String
'evaluate and assign
declare operator +=(v as single)
declare operator -=(v as single)
declare operator *=(v as single)
declare operator /=(v as single)
declare operator +=(byref v2 as vec2)
declare operator -=(byref v2 as vec2)
declare operator *=(byref v2 as vec2)
declare operator /=(byref v2 as vec2)
'Swizzle functions
declare function xx as vec2
declare function xy as vec2
declare function yx as vec2
declare function yy as vec2
'Common letters used by vectors
union : as single x,u,s : end union
union : as single y,v,t : end union
end type
constructor vec2
end constructor
constructor vec2(pxy as single)
x=pxy : y=pxy
end constructor
constructor vec2(px as single, py as single)
x=px : y=py
end constructor
constructor vec2(byref v2 as vec2)
x=v2.x : y=v2.y
end constructor
operator vec2.let(pxy as single)
x=pxy:y=pxy
end operator
operator vec2.let(byref p as vec2)
x=p.x:y=p.y
end operator
operator vec2.cast () as string
return "(" + str(x) + ", " + str(y) + ")"
end operator
operator vec2.+=(v as single)
x+=v : y+=v
end operator
operator vec2.-=(v as single)
x-=v : y-=v
end operator
operator vec2.*=(v as single)
x*=v : y*=v
end operator
operator vec2./=(v as single)
x/=v : y/=v
end operator
operator vec2.+=(byref v2 as vec2)
x+=v2.x : y+=v2.y
end operator
operator vec2.-=(byref v2 as vec2)
x-=v2.x : y-=v2.y
end operator
operator vec2.*=(byref v2 as vec2)
x*=v2.x : y*=v2.y
end operator
operator vec2./=(byref v2 as vec2)
x/=v2.x : y/=v2.y
end operator
function vec2.xx as vec2
return vec2(x,x)
end function
function vec2.xy as vec2
return vec2(x,y)
end function
function vec2.yx as vec2
return vec2(y,x)
end function
function vec2.yy as vec2
return vec2(y,y)
end function
operator -(byref l as vec2) as vec2
return vec2(-l.x, -l.y)
end operator
operator +(byref l as vec2, byref r as vec2) as vec2
return vec2(l.x+r.x, l.y+r.y)
end operator
operator +(byref l as vec2, byref r as single) as vec2
return vec2(l.x+r, l.y+r)
end operator
operator +(byref l as single, byref r as vec2) as vec2
return vec2(r.x+l, r.y+l)
end operator
operator -(byref l as vec2, byref r as vec2) as vec2
return vec2(l.x-r.x, l.y-r.y)
end operator
operator -(byref l as vec2, byref r as single) as vec2
return vec2(l.x-r, l.y-r)
end operator
operator -(byref l as single, byref r as vec2) as vec2
return vec2(r.x-l, r.y-l)
end operator
operator *(byref l as vec2, byref r as vec2) as vec2
return vec2(l.x*r.x, l.y*r.y)
end operator
operator *(byref l as vec2, byref r as single) as vec2
return vec2(l.x*r, l.y*r)
end operator
operator *(byref l as single, byref r as vec2) as vec2
return vec2(l*r.x, l*r.y)
end operator
operator /(byref l as vec2, byref r as vec2) as vec2
return vec2(l.x/r.x, l.y/r.y)
end operator
operator =(byref l as vec2,byref r as vec2) as integer
if (l.x xor r.x) or (l.y xor r.y) then return 0
return -1
end operator
operator <>(byref l as vec2,byref r as vec2) as integer
if (l.x xor r.x) or (l.y xor r.y) then return -1
return 0
end operator
' ########
' # vec3 #
' ########
type vec3
declare constructor
declare constructor(pxyz as single)
declare constructor(px as single, py as single, pz as single)
declare constructor(byref v2 as vec2, pz as single)
declare constructor(px as single, byref v2 as vec2)
declare constructor(byref v3 as vec3)
declare operator let(pxyz as single)
declare operator let(byref p as vec2)
declare operator let(byref p as vec3)
declare operator cast as string
declare operator cast as ulong
declare operator +=(v as single)
declare operator -=(v as single)
declare operator *=(v as single)
declare operator /=(v as single)
declare operator +=(byref v3 as vec3)
declare operator -=(byref v3 as vec3)
declare operator *=(byref v3 as vec3)
declare operator /=(byref v3 as vec3)
declare function xx as vec2
declare function xy as vec2
declare function xz as vec2
declare function yx as vec2
declare function yy as vec2
declare function yz as vec2
declare function zx as vec2
declare function zy as vec2
declare function zz as vec2
declare function xxx as vec3
declare function xxy as vec3
declare function xxz as vec3
declare function xyx as vec3
declare function xyy as vec3
declare function xyz as vec3
declare function xzx as vec3
declare function xzy as vec3
declare function xzz as vec3
declare function yxx as vec3
declare function yxy as vec3
declare function yxz as vec3
declare function yyx as vec3
declare function yyy as vec3
declare function yyz as vec3
declare function yzx as vec3
declare function yzy as vec3
declare function yzz as vec3
declare function zxx as vec3
declare function zxy as vec3
declare function zxz as vec3
declare function zyx as vec3
declare function zyy as vec3
declare function zyz as vec3
declare function zzx as vec3
declare function zzy as vec3
declare function zzz as vec3
declare function rgb as vec3
union : as single x,r,s : end union
union : as single y,g,t : end union
union : as single z,b,p : end union
end type
constructor vec3
end constructor
constructor vec3(pxyz as single)
x=pxyz : y=pxyz : z=pxyz
end constructor
constructor vec3(px as single, py as single, pz as single)
x=px : y=py : z=pz
end constructor
constructor vec3(byref v2 as vec2,pz as single)
x=v2.x : y=v2.y : z=pz
end constructor
constructor vec3(px as single,byref v2 as vec2)
x=px : y=v2.x : z=v2.y
end constructor
constructor vec3(byref v3 as vec3)
x=v3.x : y=v3.y :z = v3.z
end constructor
operator vec3.let(pxyz as single)
x=pxyz:y=pxyz:z=pxyz
end operator
operator vec3.let(byref p as vec2)
x=p.x:y=p.y:z=1
end operator
operator vec3.let(byref p as vec3)
x=p.x:y=p.y:z=p.z
end operator
operator vec3.cast () as string
return "(" + str(x) + ", " + str(y) + ", " + str(z) + ")"
end operator
operator vec3.cast as ulong
dim as ulong t = any
dim as ulong a = 255
#macro clip(v)
a shl = 8
if v < 0 then
elseif v > 1 then
a or = 255
else
t = v * 255
a or = t
end if
#endmacro
clip(x)
clip(y)
clip(z)
#undef clip
return a
end operator
operator vec3.+=(v as single)
x+=v : y+=v : z+=v
end operator
operator vec3.-=(v as single)
x-=v : y-=v : z-=v
end operator
operator vec3.*=(v as single)
x*=v : y*=v : z*=v
end operator
operator vec3./=(v as single)
x/=v : y/=v : z/=v
end operator
operator vec3.+=(byref v3 as vec3)
x+=v3.x : y+=v3.y : z+=v3.z
end operator
operator vec3.-=(byref v3 as vec3)
x-=v3.x : y-=v3.y : z-=v3.z
end operator
operator vec3.*=(byref v3 as vec3)
x*=v3.x : y*=v3.y : z*=v3.z
end operator
operator vec3./=(byref v3 as vec3)
x/=v3.x : y/=v3.y : z/=v3.z
end operator
function vec3.xx as vec2
return vec2(x,x)
end function
function vec3.xy as vec2
return vec2(x,y)
end function
function vec3.xz as vec2
return vec2(x,z)
end function
function vec3.yx as vec2
return vec2(y,x)
end function
function vec3.yy as vec2
return vec2(y,y)
end function
function vec3.yz as vec2
return vec2(y,z)
end function
function vec3.zx as vec2
return vec2(z,x)
end function
function vec3.zy as vec2
return vec2(z,y)
end function
function vec3.zz as vec2
return vec2(z,z)
end function
function vec3.xxx as vec3
return vec3(x,x,x)
end function
function vec3.xxy as vec3
return vec3(x,x,y)
end function
function vec3.xxz as vec3
return vec3(x,x,z)
end function
function vec3.xyx as vec3
return vec3(x,y,x)
end function
function vec3.xyy as vec3
return vec3(x,y,y)
end function
function vec3.xyz as vec3
return vec3(x,y,z)
end function
function vec3.xzx as vec3
return vec3(x,z,x)
end function
function vec3.xzy as vec3
return vec3(x,z,y)
end function
function vec3.xzz as vec3
return vec3(x,z,z)
end function
function vec3.yxx as vec3
return vec3(y,x,x)
end function
function vec3.yxy as vec3
return vec3(y,x,y)
end function
function vec3.yxz as vec3
return vec3(y,x,z)
end function
function vec3.yyx as vec3
return vec3(y,y,x)
end function
function vec3.yyy as vec3
return vec3(y,y,y)
end function
function vec3.yyz as vec3
return vec3(y,y,z)
end function
function vec3.yzx as vec3
return vec3(y,z,x)
end function
function vec3.yzy as vec3
return vec3(y,z,y)
end function
function vec3.yzz as vec3
return vec3(y,z,z)
end function
function vec3.zxx as vec3
return vec3(z,x,x)
end function
function vec3.zxy as vec3
return vec3(z,x,y)
end function
function vec3.zxz as vec3
return vec3(z,x,z)
end function
function vec3.zyx as vec3
return vec3(z,y,x)
end function
function vec3.zyy as vec3
return vec3(z,y,y)
end function
function vec3.zyz as vec3
return vec3(z,y,z)
end function
function vec3.zzx as vec3
return vec3(z,z,x)
end function
function vec3.zzy as vec3
return vec3(z,z,y)
end function
function vec3.zzz as vec3
return vec3(z,z,z)
end function
function vec3.rgb as vec3
return vec3(x,y,z)
end function
operator -(byref l as vec3) as vec3
return vec3(-l.x, -l.y, -l.z)
end operator
operator +(byref l as vec3, r as single) as vec3
return vec3(l.x+r, l.y+r, l.z+r)
end operator
operator +(l as single, byref r as vec3) as vec3
return vec3(r.x+l, r.y+l, r.z+l)
end operator
operator -(byref l as vec3, r as single) as vec3
return vec3(l.x-r, l.y-r, l.z-r)
end operator
operator -(l as single, byref r as vec3) as vec3
return vec3(r.x-l, r.y-l, r.z-l)
end operator
operator *(byref l as vec3, r as single) as vec3
return vec3(l.x*r, l.y*r, l.z*r)
end operator
operator *(l as single, byref r as vec3) as vec3
return vec3(r.x*l, r.y*l, r.z*l)
end operator
operator /(byref l as vec3, r as single) as vec3
return vec3(l.x/r, l.y/r, l.z/r)
end operator
operator /(l as single, byref r as vec3) as vec3
return vec3(r.x/l, r.y/l, r.z/l)
end operator
operator +(byref l as vec3, byref r as vec3) as vec3
return vec3(l.x+r.x, l.y+r.y, l.z+r.z)
end operator
operator -(byref l as vec3,byref r as vec3) as vec3
return vec3(l.x-r.x, l.y-r.y, l.z-r.z)
end operator
operator *(byref l as vec3, byref r as vec3) as vec3
return vec3(l.x*r.x, l.y*r.y, l.z*r.z)
end operator
operator /(byref l as vec3, byref r as vec3) as vec3
return vec3(l.x/r.x, l.y/r.y, l.z/r.z)
end operator
operator =(byref l as vec3, byref r as vec3) as integer
if (l.x xor r.x) or (l.y xor r.y) or (l.z xor r.z) then return 0
return -1
end operator
operator <>(byref l as vec3, byref r as vec3) as integer
if (l.x xor r.x) or (l.y xor r.y) or (l.z xor r.z) then return -1
return 0
end operator
' ########
' # vec4 #
' ########
type vec4
declare constructor
declare constructor(pxyz as single, pw as single=1)
declare constructor(px as single, py as single, pz as single, pw as single=1)
declare constructor(byref v1 as vec2, pz as single, pw as single)
declare constructor(byref v1 as vec2, byref v2 as vec2)
declare constructor(byref v3 as vec3, pw as single=1)
declare constructor(px as single, byref v3 as vec3)
declare constructor(byref v4 as vec4)
declare operator let(pxyzw as single)
declare operator let(byref p as vec2)
declare operator let(byref p as vec3)
declare operator let(byref p as vec4)
declare operator cast as string
declare operator cast as ulong
declare operator +=(v as single)
declare operator -=(v as single)
declare operator *=(v as single)
declare operator /=(v as single)
declare operator +=(byref v3 as vec3)
declare operator -=(byref v3 as vec3)
declare operator *=(byref v3 as vec3)
declare operator /=(byref v3 as vec3)
declare operator +=(byref v4 as vec4)
declare operator -=(byref v4 as vec4)
declare operator *=(byref v4 as vec4)
declare operator /=(byref v4 as vec4)
declare function xx as vec2
declare function xy as vec2
declare function xz as vec2
declare function yx as vec2
declare function yy as vec2
declare function yz as vec2
declare function zx as vec2
declare function zy as vec2
declare function zz as vec2
declare function xxx as vec3
declare function xxy as vec3
declare function xxz as vec3
declare function xyx as vec3
declare function xyy as vec3
declare function xyz as vec3
declare function xzx as vec3
declare function xzy as vec3
declare function xzz as vec3
declare function yxx as vec3
declare function yxy as vec3
declare function yxz as vec3
declare function yyx as vec3
declare function yyy as vec3
declare function yyz as vec3
declare function yzx as vec3
declare function yzy as vec3
declare function yzz as vec3
declare function zxx as vec3
declare function zxy as vec3
declare function zxz as vec3
declare function zyx as vec3
declare function zyy as vec3
declare function zyz as vec3
declare function zzx as vec3
declare function zzy as vec3
declare function zzz as vec3
declare function rgb as vec3
declare function rgba as vec4
union : as single x,r,s : end union
union : as single y,g,t : end union
union : as single z,b,p : end union
union : as single w,a,q : end union
end type
constructor vec4
w=1
end constructor
constructor vec4(pxyz as single, pw as single)
x=pxyz: y=pxyz : z=pxyz : w=pw
end constructor
constructor vec4(px as single, py as single, pz as single, pw as single)
x=px : y=py : z=pz : w=pw
end constructor
constructor vec4(byref v1 as vec2, pz as single, pw as single)
x=v1.x : y=v1.y : z=pz : w=pw
end constructor
constructor vec4(byref v1 as vec2, byref v2 as vec2)
x=v1.x : y= v1.y : z=v2.x : w=v2.y
end constructor
constructor vec4(byref v3 as vec3, pw as single)
x=v3.x : y=v3.y : z=v3.z : w=pw
end constructor
constructor vec4(px as single, byref v3 as vec3)
x=px : y= v3.x : z=v3.y : w=v3.z
end constructor
constructor vec4(byref v4 as vec4)
x=v4.x : y=v4.y : z=v4.z : w=v4.w
end constructor
operator vec4.let(pxyzw as single)
x=pxyzw: y=pxyzw : z=pxyzw : w=pxyzw
end operator
operator vec4.let(byref p as vec2)
x=p.x:y=p.y:z=1:w=1
end operator
operator vec4.let(byref p as vec3)
x=p.x:y=p.y:z=p.z:w=1
end operator
operator vec4.let(p as vec4)
x=p.x:y=p.y:z=p.z:w=p.w
end operator
operator vec4.cast () as string
return "(" + str(x) + ", " + str(y) + ", " + str(z) + ", " + str(w) + ")"
end operator
operator vec4.cast as ulong
dim as ulong c
#macro clip255(v)
c shl = 8
if v < 1/255 then
elseif v > 1 then
c or = 255
else
c or = v * 255
end if
#endmacro
clip255(w)
clip255(x)
clip255(y)
clip255(z)
#undef clip255
return c
end operator
operator vec4.+=(v as single)
x+=v : y+=v : z+=v : w+=v
end operator
operator vec4.-=(v as single)
x-=v : y-=v : z-=v : w-=v
end operator
operator vec4.*=(v as single)
x*=v : y*=v : z*=v : w*=v
end operator
operator vec4./=(v as single)
x/=v : y/=v : z/=v : w/=v
end operator
operator vec4.+=(byref v3 as vec3)
x+=v3.x : y+=v3.y : z+=v3.z
end operator
operator vec4.-=(byref v3 as vec3)
x-=v3.x : y-=v3.y : z-=v3.z
end operator
operator vec4.*=(byref v3 as vec3)
x*=v3.x : y*=v3.y : z*=v3.z
end operator
operator vec4./=(byref v3 as vec3)
x/=v3.x : y/=v3.y : z/=v3.z
end operator
operator vec4.+=(byref v4 as vec4)
x+=v4.x : y+=v4.y : z+=v4.z : w+=v4.w
end operator
operator vec4.-=(byref v4 as vec4)
x-=v4.x : y-=v4.y : z-=v4.z : w-=v4.w
end operator
operator vec4.*=(byref v4 as vec4)
x*=v4.x : y*=v4.y : z*=v4.z : w*=v4.w
end operator
operator vec4./=(byref v4 as vec4)
x/=v4.x : y/=v4.y : z/=v4.z : w/=v4.w
end operator
function vec4.xx as vec2
return vec2(x,x)
end function
function vec4.xy as vec2
return vec2(x,y)
end function
function vec4.xz as vec2
return vec2(y,z)
end function
function vec4.yx as vec2
return vec2(y,x)
end function
function vec4.yy as vec2
return vec2(y,y)
end function
function vec4.yz as vec2
return vec2(y,z)
end function
function vec4.zx as vec2
return vec2(z,x)
end function
function vec4.zy as vec2
return vec2(z,y)
end function
function vec4.zz as vec2
return vec2(z,z)
end function
function vec4.xxx as vec3
return vec3(x,x,x)
end function
function vec4.xxy as vec3
return vec3(x,x,y)
end function
function vec4.xxz as vec3
return vec3(x,x,z)
end function
function vec4.xyx as vec3
return vec3(x,y,x)
end function
function vec4.xyy as vec3
return vec3(x,y,y)
end function
function vec4.xyz as vec3
return vec3(x,y,z)
end function
function vec4.xzx as vec3
return vec3(x,z,x)
end function
function vec4.xzy as vec3
return vec3(x,z,y)
end function
function vec4.xzz as vec3
return vec3(x,z,z)
end function
function vec4.yxx as vec3
return vec3(y,x,x)
end function
function vec4.yxy as vec3
return vec3(y,x,y)
end function
function vec4.yxz as vec3
return vec3(y,x,z)
end function
function vec4.yyx as vec3
return vec3(y,y,x)
end function
function vec4.yyy as vec3
return vec3(y,y,y)
end function
function vec4.yyz as vec3
return vec3(y,y,z)
end function
function vec4.yzx as vec3
return vec3(y,z,x)
end function
function vec4.yzy as vec3
return vec3(y,z,y)
end function
function vec4.yzz as vec3
return vec3(y,z,z)
end function
function vec4.zxx as vec3
return vec3(z,x,x)
end function
function vec4.zxy as vec3
return vec3(z,x,y)
end function
function vec4.zxz as vec3
return vec3(z,x,z)
end function
function vec4.zyx as vec3
return vec3(z,y,x)
end function
function vec4.zyy as vec3
return vec3(z,y,y)
end function
function vec4.zyz as vec3
return vec3(z,y,z)
end function
function vec4.zzx as vec3
return vec3(z,z,x)
end function
function vec4.zzy as vec3
return vec3(z,z,y)
end function
function vec4.zzz as vec3
return vec3(z,z,z)
end function
function vec4.rgb as vec3
return vec3(x,y,z)
end function
function vec4.rgba as vec4
return vec4(x,y,z,w)
end function
operator -(byref l as vec4) as vec4
return vec4(-l.x, -l.y, -l.z, -l.w)
end operator
operator +(byref l as vec4, byref r as vec4) as vec4
return vec4(l.x+r.x, l.y+r.y, l.z+r.z, l.w+r.w)
end operator
operator -(byref l as vec4, byref r as vec4) as vec4
return vec4(l.x-r.x, l.y-r.y, l.z-r.z, l.w-r.w)
end operator
operator *(byref l as vec4, byref r as vec4) as vec4
return vec4(l.x*r.x, l.y*r.y, l.z*r.z, l.w*r.w)
end operator
operator /(byref l as vec4, byref r as vec4) as vec4
return vec4(l.x/r.x, l.y/r.y, l.z/r.z, l.w/r.w)
end operator
' vec4 single
operator +(byref l as vec4, r as single) as vec4
return vec4(l.x+r, l.y+r, l.z+r, l.w+r)
end operator
operator +(l as single, byref r as vec4) as vec4
return vec4(r.x+l, r.y+l, r.z+l, r.w+l)
end operator
operator -(byref l as vec4, r as single) as vec4
return vec4(l.x-r, l.y-r, l.z-r, l.w-r)
end operator
operator -(l as single, byref r as vec4) as vec4
return vec4(r.x-l, r.y-l, r.z-l, r.w-l)
end operator
operator *(byref l as vec4, r as single) as vec4
return vec4(l.x*r, l.y*r, l.z*r, l.w*r)
end operator
operator *(l as single, byref r as vec4) as vec4
return vec4(l*r.x, l*r.y, l*r.z, l*r.w)
end operator
operator /(byref l as vec4, r as single) as vec4
return vec4(l.x/r, l.y/r, l.z/r, l.w/r)
end operator
operator /(l as single, byref r as vec4) as vec4
return vec4(r.x/l, r.y/l, r.z/l, r.w/l)
end operator
operator +(byref l as vec4, byref r as vec3) as vec4
return vec4(l.x+r.x, l.y+r.y, l.z+r.z, l.w)
end operator
operator +(byref l as vec3, byref r as vec4) as vec4
return vec4(l.x+r.x, l.y+r.y, l.z+r.z)
end operator
operator -(byref l as vec4, byref r as vec3) as vec4
return vec4(l.x-r.x, l.y-r.y, l.z-r.z, l.w)
end operator
operator -(byref l as vec3, byref r as vec4) as vec4
return vec4(l.x-r.x, l.y-r.y, l.z-r.z)
end operator
operator *(byref l as vec4, byref r as vec3) as vec4
return vec4(l.x*r.x, l.y*r.y, l.z*r.z, l.w)
end operator
operator *(byref l as vec3, byref r as vec4) as vec4
return vec4(l.x*r.x, l.y*r.y, l.z*r.z)
end operator
operator /(byref l as vec4, byref r as vec3) as vec4
return vec4(l.x/r.x, l.y/r.y, l.z/r.z, l.w)
end operator
operator /(byref l as vec3, byref r as vec4) as vec4
return vec4(l.x/r.x, l.y/r.y, l.z/r.z)
end operator
operator =(byref l as vec4,byref r as vec4) as integer
if (l.x xor r.x) or (l.y xor r.y) or (l.z xor r.z) or (l.w xor r.w) then return 0
return -1
end operator
operator <>(byref l as vec4,byref r as vec4) as integer
if (l.x xor r.x) or (l.y xor r.y) or (l.z xor r.z) or (l.w xor r.w) then return -1
return 0
end operator
#include once "vecn_functions.bi"