Advertisement
ezak

Terrain

Mar 25th, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 20.07 KB | None | 0 0
  1.  
  2. --# Frustum
  3. --                  Frustum Culling                 --
  4. ------------------------------------------------------
  5.  
  6. -- point distance from plane: distance = a*x + b*y + c*z + d
  7. function sphereInFrustum(f, s)
  8.     local d
  9.     local fp
  10.     local sx = s.x
  11.     local sy = s.y
  12.     local sz = s.z
  13.     local sr = s.r
  14.    
  15.     for p=1, 6 do
  16.         fp = f[p]
  17.         d = fp.a * sx + fp.b * sy + fp.c * sz + fp.d
  18.         if d<= -sr then
  19.             return 0
  20.         end
  21.     end
  22.     d = d + sr
  23. -- distance based...not great but damn cheap
  24.     if (d < 250) then
  25.         return 1
  26.     else
  27.         if (d < 500) then
  28.             return 2
  29.         else
  30.             if (d < 750) then
  31.                 return 3
  32.             else
  33.                 return 4
  34.             end
  35.         end
  36.     end
  37.    
  38.     return d
  39. end
  40.  
  41. -- Extract view frustum planes  
  42. function extractFrustum()
  43.    
  44.     local mat =  viewMatrix() * projectionMatrix()
  45.     local a, b, c, d
  46.     local frustum = {}
  47.     local mat1  = mat[ 1]
  48.     local mat2  = mat[ 2]
  49.     local mat3  = mat[ 3]
  50.     local mat4  = mat[ 4]
  51.     local mat5  = mat[ 5]
  52.     local mat6  = mat[ 6]
  53.     local mat7  = mat[ 7]
  54.     local mat8  = mat[ 8]
  55.     local mat9  = mat[ 9]
  56.     local mat10 = mat[10]
  57.     local mat11 = mat[11]
  58.     local mat12 = mat[12]
  59.     local mat13 = mat[13]
  60.     local mat14 = mat[14]
  61.     local mat15 = mat[15]
  62.     local mat16 = mat[16]
  63.    
  64. -- Right plane
  65.     a = mat4  - mat1
  66.     b = mat8  - mat5
  67.     c = mat12 - mat9
  68.     d = mat16 - mat13
  69.     frustum[1] = Plane(a, b, c, d)
  70.     frustum[1]:normalize()
  71.    
  72. -- Left plane
  73.     a = mat4  + mat1
  74.     b = mat8  + mat5
  75.     c = mat12 + mat9
  76.     d = mat16 + mat13
  77.     frustum[2] = Plane(a, b, c, d)
  78.     frustum[2]:normalize()
  79.    
  80. -- Bottom plane
  81.     a = mat4  + mat2
  82.     b = mat8  + mat6
  83.     c = mat12 + mat10
  84.     d = mat16 + mat14
  85.     frustum[3] = Plane(a, b, c, d)
  86.     frustum[3]:normalize()
  87.    
  88. -- Top plane
  89.     a = mat4  - mat2
  90.     b = mat8  - mat6
  91.     c = mat12 - mat10
  92.     d = mat16 - mat14
  93.     frustum[4] = Plane(a, b, c, d)
  94.     frustum[4]:normalize()
  95.    
  96. -- Far plane
  97.     a = mat4  - mat3
  98.     b = mat8  - mat7
  99.     c = mat12 - mat11
  100.     d = mat16 - mat15
  101.     frustum[5] = Plane(a, b, c, d)
  102.     frustum[5]:normalize()
  103.    
  104. -- Near plane
  105.     a = mat4  + mat3
  106.     b = mat8  + mat7
  107.     c = mat12 + mat11
  108.     d = mat16 + mat15
  109.     frustum[6] = Plane(a, b, c, d)
  110.     frustum[6]:normalize()
  111.    
  112.     return frustum
  113. end
  114. --# Heightmap
  115. --            Heightmap building class              --
  116. ------------------------------------------------------
  117.  
  118. Terrain = class()
  119.    
  120. function Terrain:init(quality, str)
  121.     local mapSize = mapSize
  122.     local colorTexture = colorTexture
  123.    
  124.     nbChunks = 64
  125.    
  126.     local nP = 1/nPower
  127.     local mS = 1/mapSize
  128.     local sqrt = math.sqrt
  129.     self.mdl = {}
  130.     for i=1, nbChunks do
  131.         self.mdl[i] = {}
  132.        
  133.     end
  134.    
  135.     local ratio = mapSize/sqrt(nbChunks)
  136.     local ins = table.insert
  137.    
  138.     local v, A, B, C, D
  139.     local va, vb, vc, vd
  140.     local ta, tb, tc, td
  141.     local lowx, highx, lowz, highz
  142.     for lod=1, 4 do
  143.         local step = math.pow(2, lod+1)
  144.         local stp = 1/step
  145.         local vertex = {}
  146.         local tex = {}
  147.         local n = 1
  148.        
  149.     for valx = 0, mapSize-ratio, ratio do  
  150.         lowx = 0 + valx    
  151.         highx = ratio + valx  
  152.        
  153.     for valz = 0, mapSize-ratio, ratio do
  154.         lowz = 0 + valz    
  155.         highz = ratio + valz
  156.    
  157.         local polygons = {}
  158.         local texCoords = {}
  159.        
  160.        
  161. -- create vertices, the height value of the heightmap is a simple noise
  162.    
  163.         for z=lowz, highz-step, step do
  164.             for x=lowx, highx-step, step do
  165.                
  166.                 A = (x + (z) * (mapSize+step)*stp)*stp+1
  167.                 B = (x + (z+step) * (mapSize+step)*stp)*stp+1
  168.                 C = (x+step + (z+step) * (mapSize+step)*stp)*stp+1
  169.                 D = (x+step + (z) * (mapSize+step)*stp)*stp+1
  170.                
  171.                 if not vertex[A] then
  172.                     if (loop == 1) then
  173.                         v = Noise(mapSize, x*nP, z*nP, 0, 0)*str
  174.                     else
  175.                         v = noise(x*nP, z*nP)*str
  176.                     end
  177.                     vertex[A] = vec3(x, v, z)
  178.                 end
  179.                 if not vertex[B] then
  180.                     if (loop == 1) then
  181.                         v = Noise(mapSize, x*nP, (z+step)*nP, 0, 0)*str
  182.                     else
  183.                         v = noise(x*nP, (z+step)*nP)*str
  184.                     end
  185.                     vertex[B] = vec3(x, v, z+step)
  186.                 end
  187.                 if not vertex[C] then
  188.                     if (loop == 1) then
  189.                         v = Noise(mapSize, (x+step)*nP, (z+step)*nP, 0, 0)*str
  190.                     else
  191.                         v = noise((x+step)*nP, (z+step)*nP)*str
  192.                     end
  193.                     vertex[C] = vec3(x+step, v, z+step)
  194.                 end
  195.                 if not vertex[D] then
  196.                     if (loop == 1) then
  197.                         v = Noise(mapSize, (x+step)*nP, (z+step)*nP, 0, 0)*str
  198.                     else
  199.                         v = noise((x+step)*nP, (z+step)*nP)*str
  200.                     end
  201.                     vertex[D] = vec3(x+step, v, z)
  202.                 end
  203.                
  204.                 ins(polygons, vertex[A])
  205.                 ins(polygons, vertex[B])
  206.                 ins(polygons, vertex[C])
  207.                 ins(polygons, vertex[C])
  208.                 ins(polygons, vertex[D])
  209.                 ins(polygons, vertex[A])
  210.                    
  211.                 if texture == 1 then
  212.                     if not tex[A] then tex[A] = vec2( x*stp, z*stp) end
  213.                     if not tex[B] then tex[B] = vec2( x*stp, (z+step)*stp) end
  214.                     if not tex[C] then tex[C] = vec2( (x+step)*stp, (z+step)*stp) end
  215.                     if not tex[D] then tex[D] = vec2( (x+step)*stp, z*stp) end
  216.                 else
  217.                     if not tex[A] then tex[A] = vec2( x*mS, z*mS) end
  218.                     if not tex[B] then tex[B] = vec2( x*mS, (z+step)*mS) end
  219.                     if not tex[C] then tex[C] = vec2( (x+step)*mS, (z+step)*mS) end
  220.                     if not tex[D] then tex[D] = vec2( (x+step)*mS, z*mS) end
  221.                 end
  222.                  
  223.                 ins(texCoords, tex[A])
  224.                 ins(texCoords, tex[B])
  225.                 ins(texCoords, tex[C])
  226.                 ins(texCoords, tex[C])
  227.                 ins(texCoords, tex[D])
  228.                 ins(texCoords, tex[A])
  229.             end
  230.         end
  231.    
  232.         self.mdl[n][lod] = mesh()
  233.         self.mdl[n][lod].vertices = polygons
  234.        
  235.         if texture == 1 then
  236.             self.mdl[n][lod].texture = detailTexture
  237.         else
  238.             if texture == 2 then
  239.                 self.mdl[n][lod].texture = colorTexture
  240.             end
  241.         end
  242.        
  243.         self.mdl[n][lod].texCoords = texCoords
  244.         self.mdl[n][lod]:setColors(255, 255, 255, 255)
  245.        
  246.         mx = ((lowx + highx)/2)
  247.         mz = ((lowz + highz)/2)
  248.         mr = sqrt( (lowx-mx)*(lowx-mx) + (lowz-mz)*(lowz-mz) )  
  249.         self.mdl[n].boundingSphere = Sphere(mx, 0, mz, mr)
  250.        
  251.         n = n + 1
  252.     end
  253.     end
  254.     end
  255.    
  256.     print("Terrain loaded OK")
  257.     return self
  258. end
  259.  
  260. function Terrain:draw()
  261.     if not frustum then frustum = extractFrustum() end
  262.    
  263.     local d
  264.     nbPolygon=0
  265.     local mdl = self.mdl
  266.  
  267.     for i=1, #mdl do
  268.         local m = mdl[i]
  269.         d = sphereInFrustum(frustum, m.boundingSphere)
  270.        
  271.         if d~=0 then
  272.             -- ignoring distance based LOD for now
  273.             d = quality
  274.             local s = math.pow(2, d+1)
  275.             p = ((mapSize/s)*(mapSize/s)*2)/nbChunks
  276.             nbPolygon = nbPolygon + p
  277.             m[d]:draw()
  278.         end
  279.        
  280.     end
  281.    
  282.    
  283.    
  284. end
  285.  
  286. --# Main
  287. --                   Main program                   --
  288. ------------------------------------------------------
  289.        
  290. function setup()
  291.   --  displayMode(FULLSCREEN)
  292.     supportedOrientations(LANDSCAPE_ANY)
  293.     saveProjectInfo("Description", "3D renderer")
  294.     saveProjectInfo("Author", "Xavier de Boysson")
  295.     setupParameters()
  296.     texWidth = 256
  297.     tex1 = image(texWidth, texWidth)
  298.     tex2 = image(texWidth, texWidth)
  299.     tex3 = image(texWidth, texWidth)
  300.     genTextures(texWidth)
  301.     watch("camX")
  302.     watch("camY")
  303.     watch("camZ")
  304.    
  305.     textureQ = defaultTextureQuality*64
  306.     baseL = loop
  307.     baseS = str
  308.     baseQ = quality
  309.     baseP = nPower  
  310.     baseX = X
  311.     baseZ = Z
  312.     baseT = textureQ
  313.     baseTex = texture
  314.     baseDTQ = defaultTextureQuality
  315.     baseBl = blending
  316.     cnt = 0
  317.     txt = ""
  318.     nbPolygon = 0
  319.     lx = 0.1
  320.     lz = 0.1
  321.     baseX = lx
  322.     baseZ = lz
  323.    
  324.     nbTouches = 0
  325.     touches = {}
  326.     delta = 0
  327.     frustum = nil
  328.    
  329.     camX = 280
  330.     camY = 200
  331.     camZ = 0
  332.    
  333.     lookX = 280
  334.     lookY = 60
  335.     lookZ = 256
  336.    
  337.     mapSize = 512
  338.    
  339.     detailTexture = genWireframe(32)
  340.     colorTexture = nil
  341.     genTexture(textureQ)
  342.     scene = nil
  343.    
  344.     skyTex = image(4, 4)
  345.    
  346.    
  347.     sky = mesh()
  348.     sky.texture = skyTex
  349.  
  350.     sky:addRect(1,1,WIDTH*2,HEIGHT*2)
  351.    
  352. --[[
  353.   z = -10
  354.     sky.vertices = {vec3(1, 1, z),
  355.                     vec3(1, HEIGHT, z),
  356.                     vec3(WIDTH, HEIGHT, z),
  357.                     vec3(1, 1, z),
  358.                     vec3(WIDTH, 1, z),
  359.                     vec3(WIDTH, HEIGHT, z)}
  360.                
  361.     sky.texCoords = {vec2(0, 0),
  362.                      vec2(0, 1),
  363.                      vec2(1, 1),
  364.                      vec2(0, 0),
  365.                      vec2(1, 0),
  366.                      vec2(1, 1)}
  367. --]]
  368.  
  369.     sky:setColors(255,255,255,255)
  370.    
  371.    
  372.     loadMdl()
  373.        
  374.     textMode(CORNER)
  375.     spriteMode(CORNER)
  376. end
  377.  
  378.  
  379. function setupParameters()
  380.     parameter("blending", 1, 8, 2.2)
  381.     iparameter("loop", 0, 1, 0)
  382.    
  383.     iparameter("moveLight", 0, 1, 0)
  384.  
  385. -- quality is the heightmap level of detail (step size)
  386.     iparameter("quality", 1, 4, 1)
  387. -- quality of the texture, warning :P
  388.     iparameter("defaultTextureQuality", 1, 4, 4)
  389.    
  390. -- str is the scaling of height values
  391.     iparameter("str", 1, 256, 100)
  392.    
  393. -- npower is the perlin noise scaling
  394.     iparameter("nPower", 40, 110, 50)
  395.    
  396.     iparameter("texture", 0, 2, 2)
  397.  
  398.  
  399. end
  400.  
  401.  
  402. -- Ugly mess to handle parametres
  403. function readParameters()
  404.    
  405.     if (baseS ~= str) then
  406.         clearOutput()
  407.         loadMdl()
  408.        
  409.         baseS = str
  410.     end
  411.    
  412.     if (baseP ~= nPower or baseL ~= loop) then
  413.         clearOutput()
  414.         if texture == 2 then
  415.             genTexture(textureQ)
  416.         end
  417.         loadMdl()
  418.         baseL = loop
  419.         baseP = nPower
  420.     end
  421.    
  422.     if (blending ~= baseBl or baseT ~= textureQ or baseX ~= lx or baseZ ~= lz) then
  423.         clearOutput()
  424.         genTexture(textureQ)
  425.         applyTex()
  426.         baseX = lx
  427.         baseZ = lz
  428.         baseBl = blending
  429.         baseT = textureQ
  430.     end
  431.    
  432.     if baseDTQ ~= defaultTextureQuality then
  433.         clearOutput()
  434.         baseDTQ = defaultTextureQuality
  435.         textureQ = defaultTextureQuality*64
  436.         genTexture(textureQ)
  437.         applyTex()
  438.         baseT = textureQ
  439.     end
  440.    
  441.     if baseTex ~= texture then
  442.         clearOutput()
  443.         loadMdl()
  444.         baseTex = texture
  445.     end
  446. end
  447.  
  448.  
  449. -- Load the heightmap
  450. function loadMdl()
  451.     scene = Terrain:init(quality, str)
  452.     cnt = 0
  453. end
  454.  
  455. function applyTex()
  456.     local detailTexture = detailTexture
  457.     local colorTexture = colorTexture
  458.     for n=1, #scene.mdl do
  459.         for lod=1, 4 do
  460.    
  461.             if texture == 1 then
  462.                 scene.mdl[n][lod].texture = detailTexture
  463.             else
  464.                 if texture == 2 then
  465.                     scene.mdl[n][lod].texture = colorTexture
  466.                 end
  467.             end
  468.            
  469.         end
  470.     end
  471. end
  472.  
  473.  
  474.  
  475. --# Render
  476. --                   Render Loop                    --
  477. ------------------------------------------------------
  478. function draw()
  479.     local skyTex = skyTex
  480.     local sky = sky
  481.     local scene = scene
  482.     readParameters()
  483.  
  484.     background(0, 0, 0, 255)
  485.    
  486.    
  487.     for y=1, 4 do
  488.         for x=1, 4 do
  489.             local col = 128+128*noise((x+cnt/256), y+cnt/256)
  490.             skyTex:set(x, y, 255-col, 255-col, col, 255)
  491.         end
  492.     end
  493.    
  494.     -- refresh fps every 20 frames so its actually readable
  495.     if cnt%20 == 0 then
  496.         txt = (nbPolygon).." polygons at "..(math.floor(1/DeltaTime*10)/10).." FPS"
  497.        
  498.        
  499.          
  500.     end
  501.  
  502. -- transparent pixels only show bg color or stuff in its transformation matrix
  503.     sprite("Planet Cute:Character Boy", 0, HEIGHT - 171)
  504.    
  505.     translate(0, 0, -10)
  506.     sky:draw()
  507.     sprite("Planet Cute:Character Boy", 101, HEIGHT - 171)
  508.    
  509.     text(txt, WIDTH - 250, HEIGHT - 50)
  510.    
  511.     perspective(60)  
  512.     camera(camX, camY, camZ, lookX, lookY, lookZ, 0,1,0)
  513.      
  514.     scene:draw()    
  515.  
  516.     cnt = cnt + 1
  517. end
  518. --# Texturing
  519. --                 Texture Generation               --
  520. ------------------------------------------------------
  521.  
  522. function Noise(w,x,y, a, b)
  523.     rs = (mapSize/w)/nPower
  524.     x = x/rs
  525.     y = y/rs
  526.    
  527.    
  528.        
  529.         if x>w/2 then
  530.             x = (w/2)-(x-(w/2))+1
  531.         end
  532.         if y>w/2 then
  533.             y = (w/2)-(y-(w/2))+1
  534.         end
  535.    
  536.    
  537.     x = x*rs
  538.    
  539.     y = y*rs
  540.    
  541.    
  542.     x = x+a
  543.     y = y+b
  544.     return noise(x,y)
  545. end
  546.  
  547. -- Main texture    
  548. function genTexture(w)
  549.     local r = mapSize/w
  550.     local ratio = texWidth/w
  551.    
  552.     colorTexture = image(w, w)
  553.     local sx, sy, L, h, R, G, B
  554.     local colorTexture = colorTexture
  555. -- define texture colors      
  556.     local t1x, t1y, t1z, t1w
  557.     local t2x, t2y, t2z, t2w
  558.     local t3x, t3y, t3z, t3w
  559.     local tW
  560.  
  561.     local tex1 = tex1
  562.     local tex2 = tex2
  563.     local tex3 = tex3
  564.        
  565.     local nx, ny, nz
  566.     local col = 0
  567.        
  568.     local min = math.min
  569.     local max = math.max
  570.     local abs = math.abs
  571.     local sqrt = math.sqrt
  572.     local floor = math.floor
  573.     local a, b
  574.     local rs = r/nPower
  575.     local xrs, zrs
  576.     local blending = blending
  577.     for z=1, w do
  578.         for x=1, w do
  579.             xrs = x*rs
  580.             zrs = z*rs
  581.             if (loop == 1) then
  582.                 h = min(1,max(0,(1+Noise(w, xrs, zrs, 0, 0))/2))
  583.             else
  584.                 h = min(1,max(0,(1+noise(xrs, zrs))/2))
  585.             end
  586.         -- Get the weight of each texture color type at current height
  587.       --      a = min(texWidth,max(1,x))
  588.      --       b = min(texWidth,max(1,z))
  589.                
  590.             t1x, t1y, t1z = tex1:get(x, z)
  591.             t2x, t2y, t2z = tex2:get(x, z)
  592.             t3x, t3y, t3z = tex3:get(x, z)
  593.                
  594.             t1w = min(1,max(0, 1 - abs(h-0.3)*blending))
  595.             t2w = min(1,max(0, 1 - abs(h-0.55)*blending))
  596.             t3w = min(1,max(0, 1 - abs(h-0.8)*blending))
  597.             tW = 1/(t1w + t2w + t3w)
  598.            
  599.             t1w = t1w*tW
  600.             t2w = t2w*tW
  601.             t3w = t3w*tW
  602.                
  603.         --  Blend the texture colors together
  604.             R = (t1x*t1w + t2x*t2w + t3x*t3w)
  605.             G = (t1y*t1w + t2y*t2w + t3y*t3w)
  606.             B = (t1z*t1w + t2z*t2w + t3z*t3w)
  607.              
  608.         --  Calculate the normal for current pixel
  609.            
  610.             if (loop == 1) then
  611.                 sx = Noise(w, xrs, zrs, rs , 0) - Noise(w, xrs, zrs, -rs, 0)
  612.                 sy = Noise(w, xrs, zrs, 0, rs) - Noise(w, xrs, zrs, 0, -rs)
  613.             else
  614.                 sx = noise(xrs+rs, zrs) - noise(xrs-rs, zrs)
  615.                 sy = noise(xrs, zrs+rs) - noise(xrs, zrs-rs)
  616.             end
  617.             nx = -sx*w
  618.             nz = sy*w
  619.                
  620.             L = 1/sqrt(nx*nx + 4 + nz*nz)
  621.             nx = nx*L
  622.             ny = 2*L
  623.             nz = nz*L
  624.                                
  625.         --  Calculate lightning
  626.             col = min(1,max(0.1, nx*lx + ny + nz*lz)*h*4)
  627.                
  628.         --  Blend lightning with texture color
  629.             colorTexture:set(x, z,col*R, col*G, col*B, 255)
  630.    --       t:set(x, z,col, col, col, 255)                
  631.            
  632.         end
  633.     end
  634.     print("Color texture loaded OK")
  635. end
  636.  
  637. -- Wireframe texture
  638. function genWireframe(w)
  639.     local t = image(w, w)
  640.    
  641.     for y=1, w do
  642.         for x=1, 3 do
  643.             t:set(x, y, 255, 255, 255, 255)
  644.         end
  645.     end
  646.        
  647.     for x=1, w do
  648.         for y=1, 3 do    
  649.             t:set(x, y, 255, 255, 255, 255)
  650.         end
  651.     end
  652.        
  653.     for x=2, w-1 do
  654.         t:set(x+1, x, 255, 255, 255, 255)
  655.         t:set(x, x, 255, 255, 255, 255)
  656.         t:set(x-1, x, 255, 255, 255, 255)
  657.     end
  658.    
  659.     t:set(w, w, 255, 255, 255, 255)
  660.     t:set(w-1, w, 255, 255, 255, 255)
  661.    
  662.     return t
  663. end
  664.  
  665.  
  666. function genTextures()
  667.     local tex1 = tex1
  668.     local tex2 = tex2
  669.     local tex3 = tex3
  670.     local min = math.min
  671.     local max = math.max
  672.     local n1p = 1/10
  673.     local n2p = 1/4
  674.     local n3p = 1/20
  675.     for y=1, texWidth do
  676.         for x=1, texWidth do
  677.             local n1 = min(1, max(0.3, (1+noise(x*n1p, y*n1p))*.5)*2)
  678.             local n2 = min(1, max(0.6, (1+noise(x*n2p, y*n2p))*.5))
  679.             local n3 = min(1, max(0.3, (1+noise(x*n3p, y*n3p))*.5)*2)
  680.             tex1:set(x, y, 0, 0, n1*255)
  681.             tex2:set(x, y, 0, n2*255, 0)
  682.             tex3:set(x, y, n3*255, n3*255, n3*255)
  683.         end
  684.     end
  685.     print("Detail textures loaded OK")
  686. end
  687. --# Touches
  688. --                   Touch Handling                 --
  689. ------------------------------------------------------
  690.    
  691. function touched(touch)
  692.    
  693.     frustum = extractFrustum()
  694.     local state = touch.state
  695.     local touches = touches
  696.    
  697. -- Get number of touches
  698.     if state == BEGAN then
  699.         nbTouches = nbTouches + 1        
  700.     end
  701.    
  702.     if state == ENDED then
  703.         nbTouches = nbTouches - 1
  704.         touches[touch.id] = nil
  705.     else
  706.         touches[touch.id] = touch
  707.     end
  708.    
  709.     local delta = delta
  710.    
  711. --  need to save delta before looping through vertices, else laggy on high quality model
  712.     local deltaX = touch.deltaX
  713.     local deltaY = touch.deltaY
  714.  
  715.    
  716.    
  717.    
  718.    
  719. -- Handle pinch to zoom
  720.     if nbTouches == 2 then
  721.         local newPt = {}
  722.         local lastPt = {}
  723.         local ins = table.insert  
  724.         for k, p in pairs(touches) do
  725.             local px = p.x
  726.             local py = p.y
  727.             ins(newPt, vec2(px, py))
  728.             if state == BEGAN then
  729.                 ins(lastPt, vec2(px, py))
  730.             else
  731.                 ins(lastPt, vec2(px - p.deltaX, py - p.deltaY))
  732.             end
  733.         end
  734.  
  735.         delta = lastPt[1]:dist(lastPt[2]) - newPt[1]:dist(newPt[2])
  736.         camY = camY + delta
  737.         if camY<lookY then
  738.             camY = lookY
  739.             delta = 0
  740.         end
  741.     end
  742.    
  743. -- change light vector and reduce texture quality for smoother animation
  744.     if moveLight == 1 then
  745.         if state == BEGAN then
  746.             textureQ = 64
  747.         end
  748.         lx = -((WIDTH*.5 - touch.x)/WIDTH)*.5
  749.         lz = -((HEIGHT*.5 - touch.y)/HEIGHT)*.5
  750.         if state == ENDED then
  751.             textureQ = defaultTextureQuality*64
  752.             moveLight = 0
  753.         end
  754.     else
  755.         camX = camX + deltaX
  756.         camZ = camZ - deltaY
  757.        
  758.         lookX = lookX + deltaX
  759.         lookZ = lookZ - deltaY
  760.     end
  761.    
  762.    
  763. end
  764. --# Types
  765. --                    Custom Types                  --
  766. ------------------------------------------------------
  767.  
  768. Plane = class()
  769.  
  770. function Plane:init(a, b, c, d)
  771.     self.a = a
  772.     self.b = b
  773.     self.c = c
  774.     self.d = d
  775. end
  776.  
  777. function Plane:normalize()
  778.     local d = 1/math.sqrt(self.a*self.a + self.b*self.b + self.c*self.c)
  779.     self.a = self.a * d
  780.     self.b = self.b * d
  781.     self.c = self.c * d
  782.     self.d = self.d * d
  783. end
  784.  
  785. ------------------------------------------------------
  786.  
  787. Sphere = class()
  788.  
  789. function Sphere:init(x, y, z, r)
  790.     self.x = x
  791.     self.y = y
  792.     self.z = z
  793.     self.r = r
  794. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement