Warcraft 3 documentation
vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

Camera No New Posts Codes & Snippets

Started by
moyack

0 Members and 1 Guest are viewing this topic.

Camera
on: January 19, 2012, 02:57:58 PM
Category: Variables
Language: vJASS

Related Topics or Resources



by

Camera library to ease camera setup and control.

Code: jass
  1. library Camera initializer Init requires Math
  2.  
  3. globals
  4.     private constant integer TypeUnit = 'dgui'
  5.     private location GL4Cam = Location(0,0)
  6.     private unit AtUnit = null
  7.     private real DeltaZ = 0
  8. endglobals
  9.  
  10. globals
  11.     constant real WidthScreen = 0.544
  12.     constant real HeightScreen = 0.302
  13.     constant real AspectRatio = WidthScreen/HeightScreen
  14. endglobals
  15.  
  16. private function Matrix4Perspective1 takes MATRIX4 Output, real fovy, real Aspect, real zn, real zf returns MATRIX4
  17.     return Output.SetValues(2*zn/fovy,0,0,0,0,2*zn/Aspect,0,0,0,0,zf/(zf-zn),1,0,0,zn*zf/(zn-zf),0)
  18. endfunction
  19.  
  20. private function Matrix4Perspective2 takes MATRIX4 Output, real n, real f, real r, real l, real t, real b returns MATRIX4
  21.     return Output.SetValues(2*n/(r-l), 0, (r+l)/(r-l), 0, 0, 2*n/(t-b), (t+b)/(t-b), 0, 0, 0, -(f+n)/(f-n), -2*f*n/(f-n), 0, 0, -1, 0)
  22. endfunction
  23.  
  24. private function Matrix4Look takes MATRIX4 Output, VECTOR3 PosCamera, VECTOR3 AxisX, VECTOR3 AxisY, VECTOR3 AxisZ returns MATRIX4
  25.     return Output.SetValues(AxisX.x,AxisY.x,AxisZ.x,0,AxisX.y,AxisY.y,AxisZ.y,0,AxisX.z,AxisY.z,AxisZ.z,0,-Vec3Dot(AxisX, PosCamera),-Vec3Dot(AxisY, PosCamera),-Vec3Dot(AxisZ, PosCamera),1)
  26. endfunction
  27.  
  28. struct CAMERA
  29.     VECTOR3 Eye
  30.     VECTOR3 At
  31.     real Distance
  32.     real Yaw
  33.     real Pitch
  34.     real Roll
  35.     VECTOR3 AxisX
  36.     VECTOR3 AxisY
  37.     VECTOR3 AxisZ
  38.     private MATRIX4 View
  39.     private MATRIX4 Projection
  40.     private boolean change
  41.     integer CostumValue
  42.    
  43.     method Win2World takes real X, real Y, real Range returns VECTOR3
  44.         local VECTOR3 Output = VECTOR3.create()
  45.         set Output.x = .Eye.x+.AxisZ.x*Range+X*.AxisX.x*WidthScreen*Range+Y*.AxisY.x*HeightScreen*Range
  46.         set Output.y = .Eye.y+.AxisZ.y*Range+X*.AxisX.y*WidthScreen*Range+Y*.AxisY.y*HeightScreen*Range
  47.         set Output.z = .Eye.z+.AxisZ.z*Range+X*.AxisX.z*WidthScreen*Range+Y*.AxisY.z*HeightScreen*Range
  48.         return Output
  49.     endmethod
  50.  
  51.     method World2Win takes real X, real Y, real Z returns VECTOR3
  52.         local VECTOR3 Pos = VECTOR3.New_1(X, Y, Z)
  53.         local boolean b
  54.         call Vec3Transform_2(Pos, Pos, .View)
  55.         set b = Pos.z < 0
  56.         call Vec3Transform_2(Pos, Pos, .Projection)
  57.         if b then
  58.             set Pos.z = -Pos.z
  59.         endif
  60.         return Pos
  61.     endmethod
  62.    
  63.     private method UpdateDistanceYawPitch takes nothing returns nothing
  64.         local real dx = .At.x-.Eye.x
  65.         local real dy = .At.y-.Eye.y
  66.         local real dz = .At.z-.Eye.z
  67.         local real len2d
  68.         set .Distance = SquareRoot(dx*dx+dy*dy+dz*dz)
  69.         set .Yaw = Atan2(dy, dx)
  70.         set len2d = SquareRoot(dx*dx+dy*dy)
  71.         set .Pitch = Atan2(dz, len2d)
  72.     endmethod
  73.    
  74.     private method UpdateAxisMatrix takes nothing returns nothing
  75.         local MATRIX3 mat
  76.         call Vec3Normalize(.AxisZ, Vec3Subtract(.AxisZ, .At, .Eye))
  77.         set mat = Matrix3RotationAxis(MATRIX3.create(), .AxisZ, -.Roll)
  78.         call Vec3Normalize(.AxisX, Vec3Cross(.AxisX, .AxisZ, VECTOR3.oneZ))
  79.         call Vec3Transform_1(.AxisY, Vec3Cross(.AxisY, .AxisX, .AxisZ), mat)
  80.         call Vec3Transform_1(.AxisX, .AxisX, mat)
  81.         call Matrix4Look(.View, .Eye, .AxisX, .AxisY, .AxisZ)
  82.         call mat.destroy()
  83.     endmethod
  84.  
  85.     method ApplyCameraForPlayer takes player p, boolean IgnorChange returns boolean
  86.         if GetLocalPlayer() == p then
  87.             call SetCameraField(CAMERA_FIELD_ROTATION, .Yaw*bj_RADTODEG, 0)
  88.             call SetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK, .Pitch*bj_RADTODEG, 0)
  89.             call SetCameraField(CAMERA_FIELD_ROLL, .Roll*bj_RADTODEG, 0)
  90.             call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, .Distance, 0)
  91.             call SetCameraTargetController(AtUnit, .At.x, .At.y, false)
  92.             call SetCameraField(CAMERA_FIELD_ZOFFSET, .At.z-DeltaZ, 0)
  93.         endif
  94.         if .change or IgnorChange then
  95.             set .change = false
  96.             return true
  97.         endif
  98.         return false
  99.     endmethod
  100.  
  101.     method SetPosition takes real x, real y, real z returns nothing
  102.         local real dx = x-.At.x
  103.         local real dy = y-.At.y
  104.         local real dz = z-.At.z
  105.         set .Eye.x = .Eye.x+dx
  106.         set .Eye.y = .Eye.y+dy
  107.         set .Eye.z = .Eye.z+dz
  108.         set .At.x = x
  109.         set .At.y = y
  110.         set .At.z = z
  111.         set .change = true
  112.     endmethod
  113.    
  114.     method SetEyeAndAt takes real ex, real ey, real ez, real tx, real ty, real tz returns nothing
  115.         set .Eye.x = ex
  116.         set .Eye.y = ey
  117.         set .Eye.z = ez
  118.         set .At.x = tx
  119.         set .At.y = ty
  120.         set .At.z = tz
  121.         call .UpdateDistanceYawPitch()
  122.         call .UpdateAxisMatrix()
  123.         set .change = true
  124.     endmethod
  125.    
  126.     method SetYawPitchRoll takes real yaw, real pitch, real roll, boolean EyeLock returns nothing
  127.         local real Z = .Distance*Sin(pitch)
  128.         local real XY = .Distance*Cos(pitch)
  129.         local real X = XY*Cos(yaw)
  130.         local real Y = XY*Sin(yaw)
  131.         set .Yaw = yaw
  132.         set .Pitch = pitch
  133.         set .Roll = roll
  134.         if EyeLock then
  135.             set .At.x = .Eye.x+X
  136.             set .At.y = .Eye.y+Y
  137.             set .At.z = .Eye.z+Z
  138.         else
  139.             set .Eye.x = .At.x-X
  140.             set .Eye.y = .At.y-Y
  141.             set .Eye.z = .At.z-Z
  142.         endif
  143.         call .UpdateAxisMatrix()
  144.         set .change = true
  145.     endmethod
  146.    
  147.     static method New takes nothing returns CAMERA
  148.         local CAMERA this = CAMERA.create()
  149.         set .CostumValue = 0
  150.         set .change = true
  151.         set .Eye = VECTOR3.New_1(0.0,-922.668,DeltaZ+1367.912)
  152.         set .At = VECTOR3.New_1(0, 0, DeltaZ)
  153.         set .Distance = 0
  154.         set .Yaw = 0
  155.         set .Pitch = 0
  156.         set .Roll = 0
  157.         set .AxisX = VECTOR3.create()
  158.         set .AxisY = VECTOR3.create()
  159.         set .AxisZ = VECTOR3.create()
  160.         set .View  = MATRIX4.create()
  161.         set .Projection = Matrix4Perspective2(MATRIX4.create(), 0.5, 10000, -WidthScreen/2, WidthScreen/2, -HeightScreen/2, HeightScreen/2)
  162.         call .UpdateDistanceYawPitch()
  163.         call .UpdateAxisMatrix()
  164.         return this
  165.     endmethod
  166.    
  167.     method Delete takes nothing returns nothing
  168.         call .Eye.destroy()
  169.         call .At.destroy()
  170.         call .AxisX.destroy()
  171.         call .AxisY.destroy()
  172.         call .AxisZ.destroy()
  173.         call .View.destroy()
  174.         call .Projection.destroy()
  175.         call this.destroy()
  176.     endmethod
  177.    
  178. endstruct
  179.  
  180. globals
  181.     private real TempX = 0
  182.     private real TempY = 0
  183. endglobals
  184. private function InitDeltaZ_Timer takes nothing returns nothing
  185.     set DeltaZ = GetCameraTargetPositionZ()
  186.     call SetCameraPosition(TempX, TempY)
  187. endfunction
  188. function InitDeltaZ takes nothing returns nothing
  189.     set TempX = GetCameraTargetPositionX()
  190.     set TempY = GetCameraTargetPositionY()
  191.     call SetCameraPosition(0, 0)
  192.     call TimerStart(CreateTimer(), 0.04, false, function InitDeltaZ_Timer)
  193. endfunction
  194.  
  195. private function Init takes nothing returns nothing
  196.     set AtUnit = CreateUnit(Player(15), TypeUnit, 0, 0, 0)
  197.     call ShowUnit(AtUnit, false)
  198.     call InitDeltaZ()
  199. endfunction
  200.  
  201. endlibrary
« Last Edit: December 18, 2017, 06:18:39 AM by moyack »



 

Started by Purgeandfire

Replies: 0
Views: 4200
Jass Tutorials

Started by moyack

Replies: 40
Views: 58650
Jass Theory & Questions

Started by City17

Replies: 0
Views: 353
Jassdoc

Started by City17

Replies: 0
Views: 388
Jassdoc
Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   Power of Corruption, an altered melee featuring Naga and Demon. Play it now!!!   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)   Warcraft RESOURCES Reforged: Modelos, mapas, proyectos y mas...