Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
BlitzMax 18.92 KB | None | 0 0
  1. SuperStrict
  2. Framework sidesign.minib3d 'https://github.com/si-design/minib3d
  3. Import brl.Stream
  4. Import brl.linkedlist
  5. Import brl.standardio
  6. Import brl.bmploader
  7.  
  8. 'Data sizes
  9. Const B_BYTE:Int = 1
  10. Const B_SHORT:Int = 2
  11. Const B_INT:Int = 4
  12. Const B_FLOAT:Int = 4
  13.  
  14. SetGraphicsDriver GLGraphicsDriver(),GRAPHICS_BACKBUFFER|GRAPHICS_DEPTHBUFFER
  15. Graphics3D(DesktopWidth() * 0.7, DesktopHeight() * 0.7, 0, 2)
  16. ClearTextureFilters()
  17.  
  18. Local camera:TCamera = CreateCamera()
  19. MoveEntity(camera, 0, 0, -10)
  20.  
  21. 'AmbientLight(255, 255, 255)
  22. Local light:TLight = CreateLight(1)
  23. RotateEntity(light,45,45,0)
  24.  
  25. TQPalette.LoadPalette("palette.lmp")
  26. Local myBSP:TQBSP = TQBSP.LoadFile("DM1.BSP")
  27.  
  28. 'Debug make a mesh
  29. Local mesh:TMesh = CreateMesh()
  30. Local surf:TSurface[256]
  31.  
  32. Local face:TQFace
  33. Local ledge:TQLedge
  34. Local edge:TQEdge
  35. Local plane:TQPlane
  36. Local texInfo:TQSurface
  37. Local vert:TQVertex
  38. Local fNr:Int
  39. Local vNr:Int
  40. Local surfNr:Int
  41. Local vertCount:Int
  42. Local newVertCount:Int
  43.  
  44. Local s:Float
  45. Local t:Float
  46.  
  47. 'Local testBrush:TBrush = LoadBrush("test.bmp", 0)
  48.  
  49. For Local model:TQModel = EachIn myBSP.Models 'Go through models
  50.     For fNr = model.Face_ID Until model.Face_ID + model.Face_Num 'Go through faces
  51.         'Debug progress
  52.         'If fNr Mod 100 <= 0 Then Print "Generating: " + fNr + "/" + model.Face_Num
  53.         face = myBSP.Faces[fNr] 'Get current face
  54.         texInfo = myBSP.texInfo[face.TexInfo_ID]
  55.         plane = myBSP.PLANES[face.Plane_ID]
  56.        
  57.         'Move to next surface if we're using too many vertices
  58.         If surf[surfNr] And surf[surfNr].CountVertices() > (10000 * 3) - 1 Then surfNr:+1
  59.        
  60.         'If we don't have a surface to work with, create one
  61.         If Not surf[surfNr] Then surf[surfNr] = CreateSurface(mesh, Null)
  62.        
  63.         'Get verts
  64.         newVertCount = 0
  65.         For vNr = face.Ledge_ID Until face.Ledge_ID + face.Ledge_Num 'Go through edges
  66.             ledge = myBSP.Ledges[vNr] 'Get ledge
  67.             edge = myBSP.Edges[Abs(ledge.edge)] 'Get edge via ledge
  68.            
  69.             If ledge.edge < 0 Then
  70.                 vert = myBSP.Vertices[edge.Vertex0]
  71.             Else
  72.                 vert = myBSP.Vertices[edge.Vertex1]
  73.             EndIf
  74.            
  75.             AddVertex(surf[surfNr], vert.Z * 0.2, vert.Y * 0.2, vert.X * 0.2)
  76.            
  77.             'Calculate UV
  78.             s = DotProduct(vert.X, vert.Y, vert.Z, texInfo.VectorS.X, texInfo.VectorS.Y, texInfo.VectorS.Z) + texInfo.DistS
  79.             t = DotProduct(vert.X, vert.Y, vert.Z, texInfo.VectorT.X, texInfo.VectorT.Y, texInfo.VectorT.Z) + texInfo.DistT
  80.            
  81.             'Random attempt to scale S and T to texture size
  82.             s:/myBSP.MipTexs[texInfo.Texture_ID].Texture.width
  83.             t:/myBSP.MipTexs[texInfo.Texture_ID].Texture.Height
  84.            
  85.             VertexTexCoords(surf[surfNr], vertCount, s, t)
  86.             VertexNormal(surf[surfNr], vertCount, plane.Normal.Z, plane.Normal.Y, plane.Normal.X)
  87.            
  88.             vertCount:+1
  89.             newVertCount:+1
  90.         Next
  91.        
  92.         'Make Tris
  93.         For Local i:Int = vertCount - newVertCount Until vertCount - 2
  94.             AddTriangle(surf[surfNr], vertCount - newVertCount, i + 1, i + 2)
  95.         Next
  96.        
  97.        
  98.     Next
  99.     'surf[surfNr].
  100.    
  101.     PaintSurface(surf[surfNr], myBSP.MipTexs[texInfo.Texture_ID].Brush)
  102.    
  103.     surfNr:+1
  104.     vertCount = 0
  105. Next
  106.  
  107. 'EntityTexture(mesh, testTex)
  108.  
  109. 'Main loop
  110. While Not AppTerminate() And Not KeyDown(KEY_ESCAPE)
  111.     'Cls()
  112.     If KeyHit(KEY_O) Then Wireframe(False)
  113.     If KeyHit(KEY_P) Then Wireframe(True)
  114.    
  115.     If KeyDown(KEY_LEFT) Then TurnEntity(camera, 0, 2, 0)
  116.     If KeyDown(KEY_RIGHT) Then TurnEntity(camera, 0, -2, 0)
  117.     If KeyDown(KEY_UP) Then TurnEntity(camera, -2, 0, 0)
  118.     If KeyDown(KEY_DOWN) Then TurnEntity(camera, 2, 0, 0)
  119.    
  120.     If KeyDown(KEY_W) Then MoveEntity(camera, 0, 0, 1.5)
  121.     If KeyDown(KEY_S) Then MoveEntity(camera, 0, 0, -1.5)
  122.     If KeyDown(KEY_A) Then MoveEntity(camera, -1.5, 0, 0)
  123.     If KeyDown(KEY_D) Then MoveEntity(camera, 1.5, 0, 0)
  124.    
  125.     If KeyDown(KEY_SPACE) Then MoveEntity(camera, 0, 1, 0)
  126.     If KeyDown(KEY_LCONTROL) Then MoveEntity(camera, 0, -1, 0)
  127.    
  128.     RenderWorld()
  129.     Flip(1)
  130. Wend
  131. End
  132.  
  133. Type TQBSP
  134.     Field File:String
  135.     Field Stream:TStream
  136.    
  137.     Field Header:TQHeader
  138.    
  139.     Field PLANES:TQPlane[]
  140.     Field Vertices:TQVertex[]
  141.     Field Nodes:TList = CreateList()
  142.     Field TexInfo:TQSurface[]
  143.     Field MipTexHeader:TQMipHeader
  144.     Field MipTexs:TQMipTex[]
  145.     Field Faces:TQFace[]
  146.     Field ClipNodes:TList = CreateList()
  147.     Field Leaves:TList = CreateList()
  148.     Field Edges:TQEdge[]
  149.     Field Ledges:TQLedge[]
  150.     Field Models:TQModel[]
  151.    
  152.     Function LoadFile:TQBSP(url:String)
  153.         Local nB:TQBSP = New TQBSP
  154.         nB.File = url
  155.        
  156.         If nB.Process() Then
  157.             Return nB
  158.         Else
  159.             nB.StreamError("Map ~q" + nB.file + "~q NOT loaded")
  160.             Return Null
  161.         EndIf
  162.     EndFunction
  163.    
  164.     Method StreamError(MSG:String)
  165.         If Stream Then CloseStream(Stream)
  166.         DebugLog "Read error: " + MSG
  167.     EndMethod
  168.    
  169.     Method Process:Int()
  170.         Stream = OpenStream(File)
  171.         If Not Stream Then
  172.             StreamError("Unable to read file ~q" + file + "~q")
  173.             Return False
  174.         EndIf
  175.        
  176.         Local num:Int 'For counting stuff
  177.         Local i:Int 'For loops
  178.        
  179.         'Read header
  180.         Self.Header = TQHeader.Read(Stream)
  181.        
  182.         'Read planes
  183.         num = Self.Header.PLANES.Count(TQPlane.Size())
  184.         DebugLog "Planes: " + num
  185.         Self.PLANES = New TQPlane[num]
  186.         Self.Header.PLANES.JumpTo(Stream)
  187.         For i = 0 Until num
  188.             'ListAddLast(Self.PLANES, TQPlane.Read(Stream))
  189.             Self.PLANES[i] = TQPlane.Read(Stream)
  190.         Next
  191.        
  192.         'Read vertices
  193.         num = Self.Header.Vertices.Count(TQVertex.Size())
  194.         DebugLog "Vertices: " + num
  195.         Self.Vertices = New TQVertex[num]
  196.         Self.Header.Vertices.JumpTo(Stream)
  197.         For i = 0 Until num
  198.             'ListAddLast(Self.Vertices, TQVertex.Read(Stream))
  199.             Self.Vertices[i] = TQVertex.Read(Stream)
  200.         Next
  201.        
  202.         'Read nodes
  203.         'num = Self.Header.Nodes.Count(TQNode.Size())
  204.         'DebugLog "Nodes: " + num
  205.         'Self.Header.Nodes.JumpTo(Stream)
  206.         'For i = 0 Until num
  207.         '   ListAddLast(Self.Nodes, TQNode.Read(Stream))
  208.         'Next
  209.        
  210.         'Read texture info
  211.         num = Self.Header.TexInfo.Count(TQSurface.Size())
  212.         DebugLog "TexInfo: " + num
  213.         Self.TexInfo = New TQSurface[num]
  214.         Self.Header.TexInfo.JumpTo(Stream)
  215.         For i = 0 Until num
  216.             Self.TexInfo[i] = TQSurface.Read(Stream)
  217.         Next
  218.        
  219.         'Read mipmap header
  220.         Self.Header.MipTex.JumpTo(Stream)
  221.         Self.MipTexHeader = TQMipHeader.Read(Stream)
  222.         num = Self.MipTexHeader._count
  223.         DebugLog "MipMap Texs: " + num
  224.        
  225.         'Read miptexs from mipmap header
  226.         CreateDir(AppDir + "/tex/", True) 'Debug for exporting
  227.        
  228.         MipTexs = New TQMipTex[num]
  229.         For i = 0 Until num
  230.             Stream.Seek(Self.Header.MipTex.Offset + Self.MipTexHeader.Offset[i])
  231.             Self.MipTexs[i] = TQMipTex.Read(Stream)
  232.            
  233.             Local pix:TPixmap = CreatePixmap(Self.MipTexs[i].width, Self.MipTexs[i].Height, PF_RGB888)
  234.             Local x:Int
  235.             Local y:Int
  236.             Local colorID:Byte
  237.             Local palColor:TQColor32
  238.            
  239.             Stream.Seek(Self.Header.MipTex.Offset + Self.MipTexHeader.Offset[i] + Self.MipTexs[i].Offset1)
  240.             For Local bI:Int = 0 Until pix.width * pix.Height
  241.                 colorID = ReadByte(Stream)
  242.                 palColor = TQPalette.colors[colorID]
  243.                
  244.                 pix.WritePixel(x, y, GenRGB(palColor.r, palColor.g, palColor.b))
  245.                
  246.                 x:+1
  247.                 If x >= pix.width Then
  248.                     x = 0
  249.                     y:+1
  250.                 EndIf
  251.             Next
  252.            
  253.             Self.MipTexs[i].Texture = CreateTexture(pix.width, pix.Height)
  254.             Self.MipTexs[i].Brush = CreateBrush()
  255.             DrawPixmap(pix, 0, 0)
  256.             BackBufferToTex(Self.MipTexs[i].Texture)
  257.             BrushTexture(Self.MipTexs[i].Brush, Self.MipTexs[i].Texture)
  258.         Next
  259.        
  260.         'Read faces
  261.         num = Self.Header.Faces.Count(TQFace.Size())
  262.         DebugLog "Faces: " + num
  263.         Self.Faces = New TQFace[num]
  264.         Self.Header.Faces.JumpTo(Stream)
  265.         For i = 0 Until num
  266.             Self.Faces[i] = TQFace.Read(Stream)
  267.         Next
  268.        
  269.         'Read clipnodes
  270.         'num = Self.Header.ClipNodes.Count(TQClipNode.Size())
  271.         'DebugLog "ClipNodes: " + num
  272.         'Self.Header.ClipNodes.JumpTo(Stream)
  273.         'For i = 0 Until num
  274.         '   ListAddLast(Self.ClipNodes, TQClipNode.Read(Stream))
  275.         'Next
  276.        
  277.         'Read leaves
  278.         'num = Self.Header.Leaves.Count(TQDLeaf.Size())
  279.         'DebugLog "Leaves: " + num
  280.         'Self.Header.Leaves.JumpTo(Stream)
  281.         'For i = 0 Until num
  282.         '   ListAddLast(Self.Leaves, TQDLeaf.Read(Stream))
  283.         'Next
  284.        
  285.         'Read edges
  286.         num = Self.Header.Edges.Count(TQEdge.Size())
  287.         DebugLog "Edges: " + num
  288.         Self.Edges = New TQEdge[num]
  289.         Self.Header.Edges.JumpTo(Stream)
  290.         For i = 0 Until num
  291.             'ListAddLast(Self.Edges, TQEdge.Read(Stream))
  292.             Self.Edges[i] = TQEdge.Read(Stream)
  293.         Next
  294.        
  295.         'Read ledges
  296.         num = Self.Header.Ledges.Count(TQLedge.Size())
  297.         DebugLog "Ledges: " + num
  298.         Self.Ledges = New TQLedge[num]
  299.         Self.Header.Ledges.JumpTo(Stream)
  300.         For i = 0 Until num
  301.             'ListAddLast(Self.Ledges, TQLedge.Read(Stream))
  302.             Self.Ledges[i] = TQLedge.Read(Stream)
  303.         Next
  304.        
  305.         'Read models
  306.         num = Self.Header.Models.Count(TQModel.Size())
  307.         DebugLog "Models: " + num
  308.         Self.Models = New TQModel[num]
  309.         Self.Header.Models.JumpTo(Stream)
  310.         For i = 0 Until num
  311.             'ListAddLast(Self.Models, TQModel.Read(Stream))
  312.             Self.Models[i] = TQModel.Read(Stream)
  313.         Next
  314.        
  315.         CloseStream(Stream)
  316.         Return True
  317.     EndMethod
  318. EndType
  319.  
  320. 'http://www.gamers.org/dEngine/quake/spec/quake-spec34/qkspec_4.htm
  321. 'char 8bit = negative byte
  322. 'u_char 8bit = byte
  323. 'short 16bit = negative short
  324. 'u_short 16bit = short
  325. 'long 32bit = int
  326. 'u_long 32bit = longer int
  327. 'float 32bit = float
  328. 'scalar_t 32bit = float
  329.  
  330. Type TQHeader
  331.     Field Version:Int
  332.     Field Entities:TQEntry
  333.     Field PLANES:TQEntry
  334.     Field MipTex:TQEntry
  335.     Field Vertices:TQEntry
  336.     Field VisiList:TQEntry
  337.     Field Nodes:TQEntry
  338.     Field TexInfo:TQEntry
  339.     Field Faces:TQEntry
  340.     Field LightMaps:TQEntry
  341.     Field ClipNodes:TQEntry
  342.     Field Leaves:TQEntry
  343.     Field LFace:TQEntry
  344.     Field Edges:TQEntry
  345.     Field Ledges:TQEntry
  346.     Field Models:TQEntry
  347.    
  348.     Function Read:TQHeader(Stream:TStream)
  349.         Local nH:TQHeader = New TQHeader
  350.        
  351.         nH.Version = ReadInt(Stream)
  352.         If nH.Version = "29" Then
  353.             DebugLog "Correct BSP version (" + nH.Version + ")"
  354.         Else
  355.             DebugLog "Warning: Wrong BSP version (" + nH.Version + ")"
  356.         EndIf
  357.        
  358.         nH.Entities = TQEntry.Read(Stream)
  359.         nH.PLANES = TQEntry.Read(Stream)
  360.        
  361.         nH.MipTex = TQEntry.Read(Stream)
  362.         nH.Vertices = TQEntry.Read(Stream)
  363.        
  364.         nH.VisiList = TQEntry.Read(Stream)
  365.         nH.Nodes = TQEntry.Read(Stream)
  366.        
  367.         nH.TexInfo = TQEntry.Read(Stream)
  368.        
  369.         nH.Faces = TQEntry.Read(Stream)
  370.        
  371.         nH.LightMaps = TQEntry.Read(Stream)
  372.         nH.ClipNodes = TQEntry.Read(Stream)
  373.        
  374.         nH.Leaves = TQEntry.Read(Stream)
  375.        
  376.         nH.LFace = TQEntry.Read(Stream)
  377.         nH.Edges = TQEntry.Read(Stream)
  378.        
  379.         nH.Ledges = TQEntry.Read(Stream)
  380.         nH.Models = TQEntry.Read(Stream)
  381.        
  382.         Return nH
  383.     EndFunction
  384. EndType
  385.  
  386. Type TQModel
  387.     Field Bound:TQBoundBox
  388.     Field Origin:TQVec3
  389.     Field Node_ID0:Int
  390.     Field Node_ID1:Int
  391.     Field Node_ID2:Int
  392.     Field Node_ID3:Int
  393.     Field NumLeafs:Int
  394.     Field Face_ID:Int
  395.     Field Face_Num:Int
  396.    
  397.     Function Read:TQModel(Stream:TStream)
  398.         Local nM:TQModel = New TQModel
  399.         nM.Bound = TQBoundBox.Read(Stream)
  400.         nM.Origin = TQVec3.Read(Stream)
  401.         nM.Node_ID0 = ReadInt(Stream)
  402.         nM.Node_ID1 = ReadInt(Stream)
  403.         nM.Node_ID2 = ReadInt(Stream)
  404.         nM.Node_ID3 = ReadInt(Stream)
  405.         nM.NumLeafs = ReadInt(Stream)
  406.         nM.Face_ID = ReadInt(Stream)
  407.         nM.Face_Num = ReadInt(Stream)
  408.         Return nM
  409.     EndFunction
  410.    
  411.     Function Size:Int()
  412.         Return (B_INT * 7) + TQBoundBox.Size() + TQVec3.Size()
  413.     EndFunction
  414. EndType
  415.  
  416. Type TQVertex
  417.     Field X:Float
  418.     Field Y:Float
  419.     Field Z:Float
  420.    
  421.     Function Read:TQVertex(Stream:TStream)
  422.         Local nV:TQVertex = New TQVertex
  423.         nV.Z = ReadFloat(Stream)
  424.         nV.X = ReadFloat(Stream)
  425.         nV.Y = ReadFloat(Stream)
  426.        
  427.         Return nV
  428.     EndFunction
  429.    
  430.     Function Size:Int()
  431.         Return B_FLOAT * 3
  432.     EndFunction
  433. EndType
  434.  
  435. Type TQSurface
  436.     Field VectorS:TQVec3
  437.     Field DistS:Float
  438.     Field VectorT:TQVec3
  439.     Field DistT:Float
  440.     Field Texture_ID:Int
  441.    
  442.     Field Animated:Int
  443.    
  444.     Function Read:TQSurface(Stream:TStream)
  445.         Local nS:TQSurface = New TQSurface
  446.         nS.VectorS = TQVec3.Read(Stream)
  447.         nS.DistS = ReadFloat(Stream)
  448.         nS.VectorT = TQVec3.Read(Stream)
  449.         nS.DistT = ReadFloat(Stream)
  450.         nS.Texture_ID = ReadInt(Stream)
  451.         nS.Animated = ReadInt(Stream)
  452.         Return nS
  453.     EndFunction
  454.    
  455.     Function Size:Int()
  456.         Return (B_FLOAT * 2) + (B_INT * 2) + (TQVec3.Size() * 2)
  457.     EndFunction
  458. EndType
  459.  
  460. Type TQEdge
  461.     Field Vertex0:Short
  462.     Field Vertex1:Short
  463.    
  464.     Function Read:TQEdge(Stream:TStream)
  465.         Local nE:TQEdge = New TQEdge
  466.         nE.Vertex0 = ReadShort(Stream)
  467.         nE.Vertex1 = ReadShort(Stream)
  468.         Return nE
  469.     EndFunction
  470.    
  471.     Function Size:Int()
  472.         Return B_SHORT * 2
  473.     EndFunction
  474. EndType
  475.  
  476. Type TQLedge
  477.     Field Edge:Int
  478.    
  479.     Function Read:TQLedge(Stream:TStream)
  480.         Local nL:TQLedge = New TQLedge
  481.         nL.Edge = ReadInt(Stream)
  482.         Return nL
  483.     EndFunction
  484.    
  485.     Function Size:Int()
  486.         Return B_INT
  487.     EndFunction
  488. EndType
  489.  
  490. Type TQFace
  491.     Field Plane_ID:Short
  492.    
  493.     Field Side:Short
  494.     Field Ledge_ID:Int
  495.    
  496.     Field Ledge_Num:Short
  497.     Field TexInfo_ID:Short
  498.    
  499.     Field TypeLight:Byte
  500.     Field BaseLight:Byte
  501.     Field Light:Byte[2]
  502.     Field LightMap:Int
  503.    
  504.     Function Read:TQFace(Stream:TStream)
  505.         Local nF:TQFace = New TQFace
  506.         nF.Plane_ID = ReadShort(Stream)
  507.         nF.Side = ReadShort(Stream)
  508.         nF.Ledge_ID = ReadInt(Stream)
  509.         nF.Ledge_Num = ReadShort(Stream)
  510.         nF.TexInfo_ID = ReadShort(Stream)
  511.         nF.TypeLight = ReadByte(Stream)
  512.         nF.BaseLight = ReadByte(Stream)
  513.         nF.Light[0] = ReadByte(Stream)
  514.         nF.Light[1] = ReadByte(Stream)
  515.         nF.LightMap = ReadInt(Stream)
  516.         Return nF
  517.     EndFunction
  518.    
  519.     Function Size:Int()
  520.         Return (B_SHORT * 4) + (B_INT * 2) + (B_BYTE * 4)
  521.     EndFunction
  522. EndType
  523.  
  524. Type TQMipHeader
  525.     Field NumTex:Int
  526.     Field Offset:Int[]
  527.    
  528.     Field _count:Int
  529.    
  530.     Function Read:TQMipHeader(Stream:TStream)
  531.         Local nM:TQMipHeader = New TQMipHeader
  532.         nM.NumTex = ReadInt(Stream)
  533.        
  534.         'Should I even read this?
  535.         nM.Offset = New Int[nM.NumTex]
  536.         For Local i:Int = 0 Until nM.NumTex
  537.             nM.Offset[i] = ReadInt(Stream)
  538.             nM._count:+1
  539.         Next
  540.         Return nM
  541.     EndFunction
  542. EndType
  543.  
  544. Type TQMipTex
  545.     Field Name:String
  546.     Field width:Int
  547.     Field Height:Int
  548.     Field Offset1:Int
  549.     Field Offset2:Int
  550.     Field Offset4:Int
  551.     Field Offset8:Int
  552.    
  553.     Field Texture:TTexture
  554.     Field Brush:TBrush
  555.    
  556.     Function Read:TQMipTex(Stream:TStream)
  557.         Local nM:TQMipTex = New TQMipTex
  558.         For Local i:Int = 0 Until 16
  559.             nM.Name:+Chr(ReadByte(Stream))
  560.         Next
  561.         nM.width = ReadInt(Stream)
  562.         nM.Height = ReadInt(Stream)
  563.         nM.Offset1 = ReadInt(Stream)
  564.         nM.Offset2 = ReadInt(Stream)
  565.         nM.Offset4 = ReadInt(Stream)
  566.         nM.Offset8 = ReadInt(Stream)
  567.         Return nM
  568.     EndFunction
  569.    
  570.     Function Size:Int()
  571.         Return (B_BYTE * 16) + (B_INT * 6)
  572.     EndFunction
  573. EndType
  574.  
  575. Type TQEntry
  576.     Field Offset:Int
  577.     Field Size:Int
  578.    
  579.     Function Read:TQEntry(Stream:TStream)
  580.         Local nE:TQEntry = New TQEntry
  581.         nE.Offset = ReadInt(Stream)
  582.         nE.Size = ReadInt(Stream)
  583.         Return nE
  584.     EndFunction
  585.    
  586.     Method Count:Int(typeSize:Int)
  587.         Return Size / typeSize
  588.     EndMethod
  589.    
  590.     Method JumpTo(Stream:TStream)
  591.         Stream.Seek(Self.Offset)
  592.     EndMethod
  593. EndType
  594.  
  595. Type TQNode
  596.     Field Plane_ID:Int
  597.    
  598.     Field Front:Short
  599.    
  600.     Field Back:Short
  601.    
  602.     Field Box:TQBBoxShort
  603.     Field Face_ID:Short
  604.     Field Face_Num:Short
  605.    
  606.     Function Read:TQNode(Stream:TStream)
  607.         Local nN:TQNode = New TQNode
  608.         nN.Plane_ID = ReadInt(Stream)
  609.         nN.Front = ReadShort(Stream)
  610.         nN.Back = ReadShort(Stream)
  611.         nN.Box = TQBBoxShort.Read(Stream)
  612.         nN.Face_ID = ReadShort(Stream)
  613.         nN.Face_Num = ReadShort(Stream)
  614.         Return nN
  615.     EndFunction
  616. EndType
  617.  
  618. Type TQDLeaf
  619.     Field Typ:Int
  620.     Field VisList:Int
  621.    
  622.     Field Bound:TQBBoxShort
  623.     Field LFace_ID:Short
  624.    
  625.     Field LFace_Num:Short
  626.     Field SndWater:Byte
  627.     Field SndSky:Byte
  628.     Field SndSlime:Byte
  629.     Field SndLava:Byte
  630.    
  631.     Function Read:TQDLeaf(Stream:TStream)
  632.         Local nD:TQDLeaf = New TQDLeaf
  633.         nD.Typ = ReadInt(Stream)
  634.         nD.VisList = ReadInt(Stream)
  635.        
  636.         nD.Bound = TQBBoxShort.Read(Stream)
  637.         nD.LFace_ID = ReadShort(Stream)
  638.        
  639.         nD.LFace_Num = ReadShort(Stream)
  640.         nD.SndWater = ReadByte(Stream)
  641.         nD.SndSky = ReadByte(Stream)
  642.         nD.SndSlime = ReadByte(Stream)
  643.         nD.SndLava = ReadByte(Stream)
  644.         Return nD
  645.     EndFunction
  646. EndType
  647.  
  648. Type TQPlane
  649.     Field Normal:TQVec3
  650.    
  651.     Field Dist:Float
  652.    
  653.     Field Typ:Int
  654.    
  655.     Function Read:TQPlane(Stream:TStream)
  656.         Local nP:TQPlane = New TQPlane
  657.         nP.Normal = TQVec3.Read(Stream)
  658.         nP.Dist = ReadFloat(Stream)
  659.         nP.Typ = ReadInt(Stream)
  660.         Return nP
  661.     EndFunction
  662.    
  663.     Function Size:Int()
  664.         Return B_FLOAT + B_INT + TQVec3.Size()
  665.     EndFunction
  666. EndType
  667.  
  668. Type TQClipNode
  669.     Field PlaneNum:Int
  670.     Field Front:Short
  671.    
  672.     Field Back:Short
  673.    
  674.     Function Read:TQClipNode(Stream:TStream)
  675.         Local nC:TQClipNode = New TQClipNode
  676.         nC.PlaneNum = ReadInt(Stream)
  677.         nC.Front = ReadShort(Stream)
  678.         nC.Back = ReadShort(Stream)
  679.         Return nC
  680.     EndFunction
  681. EndType
  682.  
  683. Type TQPalette
  684.     Global colors:TQColor32[]
  685.    
  686.     Function LoadPalette(url:String)
  687.         Local Stream:TStream = OpenStream(url, True, False)
  688.         If Not Stream Then Print("Unable to load palette ~q" + url + "~q")
  689.         colors = New TQColor32[Stream.Size() / 3]
  690.        
  691.         For Local i:Int = 0 Until colors.Length
  692.             colors[i] = New TQColor32
  693.             colors[i].r = Stream.ReadByte()
  694.             colors[i].g = Stream.ReadByte()
  695.             colors[i].b = Stream.ReadByte()
  696.         Next
  697.        
  698.         'debug save palette
  699.         Rem
  700.         Local pix:TPixmap = CreatePixmap(colors.Length, 8, PF_RGB888)
  701.         For Local i:Int = 0 Until colors.Length
  702.             For Local y:Int = 0 Until pix.Height
  703.                 pix.WritePixel(i, y, GenRGB(colors[i].r, colors[i].g, colors[i].b))
  704.             Next
  705.         Next
  706.         SavePixmapPNG(pix, AppDir + "/palette.png", 9)
  707.         endrem
  708.        
  709.         CloseStream(Stream)
  710.         DebugLog "Palette: " + url + " @ " + colors.Length + " colors"
  711.     EndFunction
  712.    
  713. EndType
  714.  
  715. Type TQColor32
  716.     Field r:Byte
  717.     Field g:Byte
  718.     Field b:Byte
  719. EndType
  720.  
  721. 'Basic types
  722. Type TQVec3
  723.     Field X:Float
  724.     Field Y:Float
  725.     Field Z:Float
  726.    
  727.     Function Read:TQVec3(Stream:TStream)
  728.         Local nV:TQVec3 = New TQVec3
  729.         nV.X = ReadFloat(Stream)
  730.         nV.Y = ReadFloat(Stream)
  731.         nV.Z = ReadFloat(Stream)
  732.         Return nV
  733.     EndFunction
  734.    
  735.     Function Size:Int()
  736.         Return B_FLOAT * 3
  737.     EndFunction
  738. EndType
  739.  
  740. Type TQBoundBox
  741.     Field Minimum:TQVec3
  742.     Field Maximum:TQVec3
  743.    
  744.     Function Read:TQBoundBox(Stream:TStream)
  745.         Local nB:TQBoundBox = New TQBoundBox
  746.         nB.Minimum = TQVec3.Read(Stream)
  747.         nB.Maximum = TQVec3.Read(Stream)
  748.         Return nB
  749.     EndFunction
  750.    
  751.     Function Size:Int()
  752.             Return TQVec3.Size() * 2
  753.     EndFunction
  754. EndType
  755.  
  756. Type TQBBoxShort
  757.     Field Minimum:Short
  758.     Field Maximum:Short
  759.    
  760.     Function Read:TQBBoxShort(Stream:TStream)
  761.         Local nB:TQBBoxShort = New TQBBoxShort
  762.         nB.Minimum = ReadShort(Stream)
  763.         nB.Maximum = ReadShort(Stream)
  764.         Return nB
  765.     EndFunction
  766.    
  767.     Function Size:Int()
  768.         Return B_SHORT * 2
  769.     EndFunction
  770. EndType
  771.  
  772. Function CrossProduct:TQVec3(x1:Float, y1:Float, z1:Float, x2:Float, y2:Float, z2:Float)
  773.     Local CProd:TQVec3 = New TQVec3
  774.     CProd.x = (y1 * z2) - (z1 * y2)
  775.     CProd.y = (z1 * x2) - (x1 * z2)
  776.     CProd.z = (x1 * y2) - (y1 * x2)
  777.     Return CProd
  778. End Function
  779. Function DotProduct:Float(x1:Float, y1:Float, z1:Float, x2:Float, y2:Float, z2:Float)
  780.     Return ((x1 * x2) + (y1 * y2) + (z1 * z2))
  781. EndFunction
  782.  
  783. Function GenRGB:Int(r:Byte, g:Byte, b:Byte, a:Byte = 255)
  784.     Return (a Shl 24) | (r Shl 16) | (g Shl 8) | b
  785. EndFunction
  786.    
  787. Function GetRGB:Int(argb:Int, a:byte Var, r:byte Var, g:byte Var, b:byte Var)
  788.     a=(argb Shr 24) & $ff
  789.     r=(argb Shr 16) & $ff
  790.     g=(argb Shr 8) & $ff
  791.     b=argb & $ff
  792. EndFunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement