Jump to content

What is vec3() source code?


Aily
 Share

Recommended Posts

..here is TVec3 function..

 

Function Vec3:TVec3( x#=0.0,y#=0.0/0.0,z#=0.0/0.0 )
Local t:TVec3=New TVec3
If IsNan(y) y=x
If IsNan(z) z=y
t.x=x
t.y=y
t.z=z
Return t
EndFunction

 

..here is corresponding TVec3 Class...

Type TVec3 {value}
Field x#,y#,z# {attribute}

Method Copy:TVec3()
 Return Vec3( x,y,z )
End Method

Method ToString$()
 Return "Vec3("+x+","+y+","+z+")"
End Method

Method ToVec4:TVec4()
 Return Vec4( x,y,z,1 )
End Method

Method ToField$()
 Return x+","+y+","+z
End Method

Method FromField:TVec3( t$ )
 Local bits$[]=t.Split( "," )
 If bits.length<>3 Throw "Format error"
 x=bits[0].ToFloat()
 y=bits[1].ToFloat()
 z=bits[2].ToFloat()
 Return Self
End Method
Method Pointer:Float Ptr()
 Return Varptr x
EndMethod
Method Length#()
 Return Sqr( x*x+y*y+z*z )
End Method

Method Dot#( v:TVec3 )
 Return x*v.x+y*v.y+z*v.z
End Method

Method Inverse:TVec3()
 Return Vec3( -x,-y,-z )
End Method

Method Reciprocal:TVec3()
 Return Vec3( 1/x,1/y,1/z )
End Method

Method Normalize:TVec3()
 Local t#=Sqr( x*x+y*y+z*z )
 If t=0.0 Return vec3(0.0,0.0,0.0)
 Return Vec3( x/t,y/t,z/t )
End Method

Method Scale:TVec3( scale# )
 Return Vec3( x*scale,y*scale,z*scale )
End Method

Method DistanceTo#( v:TVec3 )
 Local dx#=x-v.x,dy#=y-v.y,dz#=z-v.z
 Return Sqr( dx*dx+dy*dy+dz*dz )
End Method

Method Plus:TVec3( v:TVec3 )
 Return Vec3( x+v.x,y+v.y,z+v.z )
End Method

Method Minus:TVec3( v:TVec3 )
 Return Vec3( x-v.x,y-v.y,z-v.z )
End Method

Method Times:TVec3( v:TVec3 )
 Return Vec3( x*v.x,y*v.y,z*v.z )
End Method

Method DividedBy:TVec3( v:TVec3 )
 Return Vec3( x/v.x,y/v.y,z/v.z )
End Method

Method Cross:TVec3( v:TVec3 )
 Return Vec3( y*v.z-z*v.y,z*v.x-x*v.z,x*v.y-y*v.x )
End Method

Method Blend:TVec3( v:TVec3,Alpha# )
 Local beta#=1-Alpha
 Return Vec3( x*beta+v.x*Alpha,y*beta+v.y*Alpha,z*beta+v.z*Alpha )
End Method

Method ToQuat:TQuat()
 Local c1#=Cos(-z/2)
 Local s1#=Sin(-z/2)
 Local c2#=Cos(-x/2)
 Local s2#=Sin(-x/2)
 Local c3#=Cos( y/2)
 Local s3#=Sin( y/2)
 Local c1_c2#=c1*c2
 Local s1_s2#=s1*s2
 Local t:TQuat=New TQuat
 t.x=c1*s2*c3 - s1*c2*s3;
 t.y=c1_c2*s3 + s1_s2*c3;
 t.z=s1*c2*c3 + c1*s2*s3;
 t.w=c1_c2*c3 - s1_s2*s3;
 Return t
EndMethod

Function FromQuat:TVec3(quat:TQuat)
 Return quat.toeuler()
EndFunction

Method Compare:Int(o:Object)
 Local t:TVec3=TVec3(o)
 If x<t.x Return -1
 If y<t.y Return -1
 If z<t.z Return -1
 If x>t.x Return 1
 If y>t.y Return 1
 If z>t.z Return 1
 Return 0
EndMethod

EndType

 

..I hope it helps :) ..

  • Upvote 1

 

Link to comment
Share on other sites

Thanks Naughty Alien.

 

Important thing here is new operator.

 

Couple weeks ago i was looking in to minib3d engine, found that it's turnentity() method does not works correct.

Looking in to blitzbasic.com forum found this bugfix, there was functions for quaternion rotations, here's one of those functions:

 

Function MultiplyQuats:TQuaternion(q1:TQuaternion,q2:TQuaternion)
 Local q:TQuaternion=New TQuaternion

 q.w = q1.w*q2.w - q1.x*q2.x - q1.y*q2.y - q1.z*q2.z
 q.x = q1.w*q2.x + q1.x*q2.w + q1.y*q2.z - q1.z*q2.y
 q.y = q1.w*q2.y + q1.y*q2.w + q1.z*q2.x - q1.x*q2.z
 q.z = q1.w*q2.z + q1.z*q2.w + q1.x*q2.y - q1.y*q2.x
 Return q
End Function

 

Local q:TQuaternion=New TQuaternion

This line is some confuse me, because this means - each time i turn somethig - bMax allocates memory for TQuaternion

type, makes it initializations.

 

My test scene have 2000 same time rotating cubes - using this function, at FPS ~ 100

 

Just for test i reworked this:

Global single_q_3:TQuaternion=New TQuaternion
Function MultiplyQuats:TQuaternion(q1:TQuaternion,q2:TQuaternion)
 Local q:TQuaternion=single_q_3

 q.w = q1.w*q2.w - q1.x*q2.x - q1.y*q2.y - q1.z*q2.z
 q.x = q1.w*q2.x + q1.x*q2.w + q1.y*q2.z - q1.z*q2.y
 q.y = q1.w*q2.y + q1.y*q2.w + q1.z*q2.x - q1.x*q2.z
 q.z = q1.w*q2.z + q1.z*q2.w + q1.x*q2.y - q1.y*q2.x
 Return q
End Function

 

 

As you can see - i creating temporary Global single_q_3:TQuaternion=New TQuaternion and use just like in source function.

 

Yes, i'm lost some cool OOP thing - returning new TQuaternion, but - nothing breakes there in test, and same 2000 cubes start to run at 130(!!!!)FPS...

 

This means - ~30% faster.

 

So i know whell - better do not use constructions like

 

PositionEntity(entity,vec3(1,2,3))

 

in Leadwerks too.

"Better" is big enemy of "good"

Link to comment
Share on other sites

yup..or even better , just create desired position Vec3 as a part of class you are using it at and it will be even faster (Globals will at some point start slowing ya down, if too many used)..so, to my experience, everything im using in code is contained withing class itself and there is no temporary variables..so its all working much much faster..

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...