gltut / Documents / Tools / Viewport.lua

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 require "vmath" module(..., package.seeall); local function AddMembers(classInst, members) for funcName, func in pairs(members) do classInst[funcName] = func; end end --2D Viewport. local View = {} function View:Size() return self.pixelSize; end --Takes points in viewport space, returns points in pixel space. --Applies the current transform function View:Transform(points) if(vmath.vtype(points) == "table") then local ret = {}; for i, realPoint in ipairs(points) do ret[i] = self:Transform(realPoint); end return ret; end local point = points; if(self.transform) then point = self.transform:Matrix():Transform(point) end; point = self.matrix:Transform(point); point = point + self.pixelOffset; return point; end --Takes points in viewport space, returns points in pixel space. --Does not apply the current transform function View:ViewportTransform(...) local transform = self.transform; self.transform = nil; local ret = self:Transform(...); self.transform = transform; return ret; end function View:SetTransform(transform) self.transform = transform; end --Returns the top-right and bottom-left corners of the viewport in viewport space. function View:Extents() local halfSize = self.vpSize / 2; local upperBound = self.vpOrigin + halfSize; local lowerBound = self.vpOrigin - halfSize; return upperBound, lowerBound; end --Computes what the given viewport length will be in pixel coordinates. function View:Length(testVal) local originVec = vmath.vec2(0, 0); local testVec = vmath.vec2(testVal, 0); local test1 = self:Transform(originVec); local test2 = self:Transform(testVec); return vmath.length(test2 - test1); end function Viewport(pixelSize, vpOrigin, vpSize, vpPixelOffset) local viewport = {}; viewport.pixelSize = vmath.vec2(pixelSize); viewport.vpOrigin = vmath.vec2(vpOrigin); viewport.pxCenter = viewport.pixelSize / 2; if(type(vpSize) == "number") then vpSize = vmath.vec2(vpSize, vpSize * (pixelSize[2] / pixelSize[1])) end; viewport.vpSize = vmath.vec2(vpSize); if(vpPixelOffset) then viewport.pixelOffset = vmath.vec2(vpPixelOffset); else viewport.pixelOffset = vmath.vec2(0.0, 0.0); end local trans = Transform2D(); trans:Translate(viewport.pxCenter); trans:Scale(viewport.pixelSize); trans:Scale(vmath.vec2(1, -1)); trans:Scale(1.0 / viewport.vpSize); trans:Translate(-viewport.vpOrigin); viewport.matrix = trans; AddMembers(viewport, View); return viewport; end -- Transform 2D. local function Identity3() return vmath.mat3(vmath.vec3{1, 0, 0}, vmath.vec3{0, 1, 0}, vmath.vec3{0, 0, 1}); end local Trans2D = {} function Trans2D:Translate(offset) local trans = Identity3(); trans:SetCol(3, vmath.vec3(offset, 1)); self.currMatrix = self.currMatrix * trans; end function Trans2D:Scale(scale) local scaleMat = Identity3(); scaleMat[1][1] = scale[1]; scaleMat[2][2] = scale[2]; self.currMatrix = self.currMatrix * scaleMat; end function Trans2D:Rotate(angleDeg) local rotation = Identity3(); angleDeg = math.rad(angleDeg); local sinAng, cosAng = math.sin(angleDeg), math.cos(angleDeg); rotation[1][1] = cosAng; rotation[2][1] = -sinAng; rotation[1][2] = sinAng; rotation[2][2] = cosAng; self.currMatrix = self.currMatrix * rotation; end function Trans2D:MultMatrix(matrix) self.currMatrix = self.currMatrix * matrix; end function Trans2D:Push() if(not self.stack) then self.stack = {}; self.stack.top = 0; end self.stack[self.stack.top + 1] = self.currMatrix; self.stack.top = self.stack.top + 1; end function Trans2D:Pop() assert(self.stack, "No Push has been called yet."); assert(self.stack.top > 0, "Matrix stack underflow."); self.currMatrix = self.stack[self.stack.top]; self.stack.top = self.stack.top - 1; end function Trans2D:Identity() self.currMatrix = Identity3(); end function Trans2D:Matrix() return self.currMatrix; end function Trans2D:Vector(point) return vmath.vec2(point); end function Trans2D:Transform(points) if(vmath.vtype(points) == "table") then local ret = {}; for i, realPoint in ipairs(points) do ret[i] = self:Transform(realPoint); end return ret; end return self.currMatrix:Transform(points); end function Transform2D() local transform = {}; transform.currMatrix = Identity3(); AddMembers(transform, Trans2D); return transform; end -- Transform 3D. local function Identity4() return vmath.mat4( vmath.vec4{1, 0, 0, 0}, vmath.vec4{0, 1, 0, 0}, vmath.vec3{0, 0, 1, 0}, vmath.vec3{0, 0, 0, 1}); end local Trans3D = {} Trans3D.Push = Trans2D.Push; Trans3D.Pop = Trans2D.Pop; function Trans3D:Translate(offset) local trans = Identity4(); trans:SetCol(4, vmath.vec4(offset, 1)); self.currMatrix = self.currMatrix * trans; end function Trans3D:Scale(scale) local scaleMat = Identity4(); scaleMat[1][1] = scale[1]; scaleMat[2][2] = scale[2]; scaleMat[3][3] = scale[3]; self.currMatrix = self.currMatrix * scaleMat; end function Trans3D:RotateX(angleDeg) local rotation = Identity4(); angleDeg = math.rad(angleDeg); local sinAng, cosAng = math.sin(angleDeg), math.cos(angleDeg); rotation[2][2] = cosAng; rotation[3][2] = -sinAng; rotation[2][3] = sinAng; rotation[3][3] = cosAng; self.currMatrix = self.currMatrix * rotation; end function Trans3D:RotateY(angleDeg) local rotation = Identity4(); angleDeg = math.rad(angleDeg); local sinAng, cosAng = math.sin(angleDeg), math.cos(angleDeg); rotation[1][1] = cosAng; rotation[3][1] = sinAng; rotation[1][3] = -sinAng; rotation[3][3] = cosAng; self.currMatrix = self.currMatrix * rotation; end function Trans3D:RotateZ(angleDeg) local rotation = Identity4(); angleDeg = math.rad(angleDeg); local sinAng, cosAng = math.sin(angleDeg), math.cos(angleDeg); rotation[1][1] = cosAng; rotation[2][1] = -sinAng; rotation[1][2] = sinAng; rotation[2][2] = cosAng; self.currMatrix = self.currMatrix * rotation; end Trans3D.MultMatrix = Trans2D.MultMatrix; Trans3D.Transform = Trans2D.Transform function Trans3D:Identity() self.currMatrix = Identity4(); end function Trans3D:Matrix() return self.currMatrix; end function Trans3D:Vector(point) return vmath.vec3(point); end function Transform3D() local transform = {}; transform.currMatrix = Identity4(); AddMembers(transform, Trans3D); return transform; end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.