Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2016
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Lightmap UV
  2. If face.LightMap >= 0 Then
  3.     'face.LightMapPos int: Pixel position on big lightmap (not counting padding)
  4.     'face.LightMinUV float: DotProduct(vert.x, vert.y, vert.z, texInfo.VectorS.x, texInfo.VectorS.y, texInfo.VectorS.z) + texInfo.DistS
  5.     'face.LightDist int: Ceil(LightMaxUV.x / 16) - Floor(LightMinUV.x / 16)
  6.     'face.LightMapSize int: LightDist + 1
  7.     'lightU/V float
  8.     'LightMapTexture.width/height int
  9.    
  10.     lightU = (s - Floor(face.LightMinUV.x / 16) * 16) / (face.LightDist.x + 1)
  11.     lightV = (t - Ceil(face.LightMinUV.y / 16) * 16) / (face.LightDist.y + 1)
  12.    
  13.     lightU = (face.LightMapPos.x + lightU * face.LightMapSize.x) / Float(Self.LightMapTexture.width)
  14.     lightV = (face.LightMapPos.y + lightV * face.LightMapSize.y) / Float(Self.LightMapTexture.Height)
  15.    
  16.     'surf, vertice count, light U, light V, Light W, Coord set
  17.     VertexTexCoords(surf, vertCount, lightU, lightV, 0, 1)
  18. EndIf
  19.  
  20. Method CalcLightmapSize:TQIntVec2(bsp:TQBSP)
  21.         If Self.LightMap < 0 Then Return New TQIntVec2
  22.        
  23.         If LightMapSize Then
  24.             'Return same if already calculated
  25.             Return LightMapSize
  26.         Else
  27.             'Being a new calculation!
  28.             LightMapSize = New TQIntVec2
  29.         EndIf
  30.        
  31.         'Get the vertex data from all edges
  32.         LightDist = New TQIntVec2
  33.         LightMinUV = New TQVec2
  34.         LightMaxUV = New TQVec2
  35.        
  36.         Local ledge:TQLedge
  37.         Local edge:TQEdge
  38.         Local vert:TQVertex
  39.         Local texInfo:TQSurface = bsp.texInfo[Self.TexInfo_ID]
  40.        
  41.         For Local eNr:Int = Self.Ledge_ID Until Self.Ledge_ID + Self.Ledge_Num 'Go through edges
  42.             ledge = bsp.Ledges[eNr] 'Get ledge
  43.             edge = bsp.Edges[Abs(ledge.edge)] 'Get edge via ledge
  44.            
  45.             If ledge.edge < 0 Then
  46.                 vert = bsp.Vertices[edge.Vertex0]
  47.             Else
  48.                 vert = bsp.Vertices[edge.Vertex1]
  49.             EndIf
  50.            
  51.             LightS = DotProduct(vert.x, vert.y, vert.z, texInfo.VectorS.x, texInfo.VectorS.y, texInfo.VectorS.z) + texInfo.DistS
  52.             LightT = DotProduct(vert.x, vert.y, vert.z, texInfo.VectorT.x, texInfo.VectorT.y, texInfo.VectorT.z) + texInfo.DistT
  53.            
  54.             If (eNr = Self.Ledge_ID) Then
  55.                 'Starting point
  56.                 LightMinUV.x = LightS
  57.                 LightMinUV.y = LightT
  58.                 LightMaxUV.x = LightS
  59.                 LightMaxUV.y = LightT
  60.             Else
  61.                 'Is this a new minimum?
  62.                 If LightS < LightMinUV.x Then LightMinUV.x = LightS
  63.                 If LightT < LightMinUV.y Then LightMinUV.y = LightT
  64.                
  65.                 'Is this a new maximum
  66.                 If LightS > LightMaxUV.x Then LightMaxUV.x = LightS
  67.                 If LightT > LightMaxUV.y Then LightMaxUV.y = LightT
  68.             End If
  69.         Next
  70.        
  71.         'Get distances
  72.         LightDist.x = Ceil(LightMaxUV.x / 16) - Floor(LightMinUV.x / 16)
  73.         LightDist.y = Ceil(LightMaxUV.y / 16) - Floor(LightMinUV.y / 16)
  74.        
  75.         LightMapSize.x = LightDist.x + 1
  76.         LightMapSize.y = LightDist.y + 1
  77.        
  78.         LightDist.x:*16
  79.         LightDist.y:*16
  80.        
  81.         Return LightMapSize
  82.     EndMethod
  83.    
  84.     Method MakeLightmap(bsp:TQBSP)
  85.         If Self.LightMap < 0 Then Return
  86.        
  87.         If LightMapSize.x <= 0 Or LightMapSize.y <= 0 Then Return
  88.        
  89.         If bsp.LightMapPreW + LightMapSize.x >= bsp.LightMapPixmap.width Then
  90.             bsp.LightMapPreW = LIGHT_PAD * 2
  91.             bsp.LightMapPreH:+bsp.LightMapMaxH
  92.             bsp.LightMapMaxH = 0
  93.         EndIf
  94.        
  95.         Self.LightMapPos = New TQVec2
  96.         Self.LightMapPos.x = bsp.LightMapPreW
  97.         Self.LightMapPos.y = bsp.LightMapPreH
  98.        
  99.         Local x:Int
  100.         Local y:Int
  101.         Local cI:Int
  102.         Local color:Byte
  103.        
  104.         For y = 0 Until LightMapSize.y
  105.         For x = 0 Until LightMapSize.x
  106.             color = bsp.LightMapLump[Self.LightMap + cI]
  107.             color = Min(color + bsp.LightMapBrightness, 255)
  108.            
  109.             If color < bsp.LightMapMinBright Then color = bsp.LightMapMinBright
  110.             If color > bsp.LightMapMaxBright Then color = bsp.LightMapMaxBright
  111.            
  112.             'Debug outline
  113.             'If x = 0 Or y = 0 Or x = LightMapSize.x - 1 Or y = LightMapSize.y - 1 Then
  114.             '   bsp.LightMapPixmap.WritePixel(bsp.LightMapPreW + x, bsp.LightMapPreH + y, GenRGB(255, 0, 0))
  115.             'Else
  116.                 bsp.LightMapPixmap.WritePixel(bsp.LightMapPreW + x, bsp.LightMapPreH + y, GenRGB(color, color, color))
  117.             'EndIf
  118.            
  119.             'Padding
  120.             If y = 0 And x = 0 Then bsp.LightMapPixmap.WritePixel(bsp.LightMapPreW + x - 1, bsp.LightMapPreH + y - 1, GenRGB(color, color, color))
  121.             If y = 0 And x = LightMapSize.x - 1 Then bsp.LightMapPixmap.WritePixel(bsp.LightMapPreW + x + 1, bsp.LightMapPreH + y - 1, GenRGB(color, color, color))
  122.            
  123.             If y = LightMapSize.y - 1 And x = 0 Then bsp.LightMapPixmap.WritePixel(bsp.LightMapPreW + x - 1, bsp.LightMapPreH + y + 1, GenRGB(color, color, color))
  124.             If y = LightMapSize.y - 1 And x = LightMapSize.x - 1 Then bsp.LightMapPixmap.WritePixel(bsp.LightMapPreW + x + 1, bsp.LightMapPreH + y + 1, GenRGB(color, color, color))
  125.            
  126.             If y = 0 Then bsp.LightMapPixmap.WritePixel(bsp.LightMapPreW + x, bsp.LightMapPreH + y - 1, GenRGB(color, color, color))
  127.             If x = 0 Then bsp.LightMapPixmap.WritePixel(bsp.LightMapPreW + x - 1, bsp.LightMapPreH + y, GenRGB(color, color, color))
  128.            
  129.             If y = LightMapSize.y - 1 Then bsp.LightMapPixmap.WritePixel(bsp.LightMapPreW + x, bsp.LightMapPreH + y + 1, GenRGB(color, color, color))
  130.             If x = LightMapSize.x - 1 Then bsp.LightMapPixmap.WritePixel(bsp.LightMapPreW + x + 1, bsp.LightMapPreH + y, GenRGB(color, color, color))
  131.            
  132.             cI:+1
  133.         Next
  134.         Next
  135.        
  136.         bsp.LightMapPreW:+LightMapSize.x + LIGHT_PAD * 2
  137.         If bsp.LightMapMaxH < Self.LightMapSize.y + LIGHT_PAD Then bsp.LightMapMaxH = Self.LightMapSize.y + LIGHT_PAD
  138.     EndMethod
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement