Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
BlitzMax 12.75 KB | None | 0 0
  1. SuperStrict
  2. 'Framework sidesign.minib3d 'https://github.com/si-design/minib3d
  3. Framework brl.Stream
  4. Import brl.linkedlist
  5. Import brl.standardio
  6.  
  7. 'Data sizes
  8. Const B_BYTE:Int = 1
  9. Const B_SHORT:Int = 2
  10. Const B_INT:Int = 4
  11. Const B_FLOAT:Int = 4
  12.  
  13. 'Graphics3D(DesktopWidth() * 0.7, DesktopHeight() * 0.7, 2)
  14.  
  15. 'Local camera:TCamera = CreateCamera()
  16. 'MoveEntity(camera, 0, 0, -10)
  17.  
  18. 'AmbientLight(255, 255, 255)
  19.  
  20. 'Local testCube:TMesh = CreateCube()
  21.  
  22. Local myBSP:TQBSP = TQBSP.LoadFile("DM1.BSP")
  23.  
  24. 'Debug make a mesh
  25. 'Local mesh:TMesh = CreateMesh()
  26. 'Local surf:TSurface = CreateSurface(mesh, Null)
  27.  
  28. Local highRequestID:Int
  29.  
  30. For Local m:TQModel = EachIn myBSP.Models
  31.     Local face:TQFace
  32.     For Local fNr:Int = m.Face_ID Until m.Face_ID + m.Face_Num
  33.         face = TQFace(myBSP.Faces.ValueAtIndex(fNr))
  34.         If face.Ledge_ID > highRequestID Then highRequestID = face.Ledge_ID
  35.     Next
  36. Next
  37. DebugLog "Highest requested Edge ID: " + highRequestID
  38.  
  39. Rem
  40. While Not AppTerminate() And Not KeyDown(KEY_ESCAPE)
  41.     Cls()
  42.    
  43.     If KeyDown(KEY_LEFT) Then TurnEntity(camera, 0, 2, 0)
  44.     If KeyDown(KEY_RIGHT) Then TurnEntity(camera, 0, -2, 0)
  45.     If KeyDown(KEY_UP) Then TurnEntity(camera, -2, 0, 0)
  46.     If KeyDown(KEY_DOWN) Then TurnEntity(camera, 2, 0, 0)
  47.    
  48.     If KeyDown(KEY_W) Then MoveEntity(camera, 0, 0, 1.5)
  49.     If KeyDown(KEY_S) Then MoveEntity(camera, 0, 0, -1.5)
  50.     If KeyDown(KEY_A) Then MoveEntity(camera, -1.5, 0, 0)
  51.     If KeyDown(KEY_D) Then MoveEntity(camera, 1.5, 0, 0)
  52.    
  53.     If KeyDown(KEY_SPACE) Then MoveEntity(camera, 0, 1, 0)
  54.     If KeyDown(KEY_LCONTROL) Then MoveEntity(camera, 0, -1, 0)
  55.    
  56.     RenderWorld()
  57.     Flip(1)
  58. Wend
  59. endrem
  60. End
  61.  
  62. Type TQBSP
  63.     Field File:String
  64.     Field Stream:TStream
  65.    
  66.     Field Header:TQHeader
  67.    
  68.     Field PLANES:TList = CreateList()
  69.     Field Vertices:TList = CreateList()
  70.     Field Nodes:TList = CreateList()
  71.     Field TexInfo:TList = CreateList()
  72.     Field Faces:TList = CreateList()
  73.     Field ClipNodes:TList = CreateList()
  74.     Field Leaves:TList = CreateList()
  75.     Field Edges:TList = CreateList()
  76.     Field Models:TList = CreateList()
  77.    
  78.     Function LoadFile:TQBSP(url:String)
  79.         Local nB:TQBSP = New TQBSP
  80.         nB.File = url
  81.        
  82.         If nB.Process() Then
  83.             Return nB
  84.         Else
  85.             nB.StreamError("Map ~q" + nB.file + "~q NOT loaded")
  86.             Return Null
  87.         EndIf
  88.     EndFunction
  89.    
  90.     Method StreamError(MSG:String)
  91.         If Stream Then CloseStream(Stream)
  92.         DebugLog "Read error: " + MSG
  93.     EndMethod
  94.    
  95.     Method Process:Int()
  96.         Stream = OpenStream(File)
  97.         If Not Stream Then
  98.             StreamError("Unable to read file ~q" + file + "~q")
  99.             Return False
  100.         EndIf
  101.        
  102.         Local num:Int 'For counting stuff
  103.         Local i:Int 'For loops
  104.        
  105.         'Read header
  106.         Self.Header = TQHeader.Read(Stream)
  107.        
  108.         'Read planes
  109.         'num = Self.Header.PLANES.Count(TQPlane.Size())
  110.         'DebugLog "Planes: " + num
  111.         'Self.Header.PLANES.JumpTo(Stream)
  112.         'For i = 0 Until num
  113.         '   ListAddLast(Self.PLANES, TQPlane.Read(Stream))
  114.         'Next
  115.        
  116.         'Read vertices
  117.         num = Self.Header.Vertices.Count(TQVertex.Size())
  118.         DebugLog "Vertices: " + num
  119.         Self.Header.Vertices.JumpTo(Stream)
  120.         For i = 0 Until num
  121.             ListAddLast(Self.Vertices, TQVertex.Read(Stream))
  122.         Next
  123.        
  124.         'Read nodes
  125.         'num = Self.Header.Nodes.Count(TQNode.Size())
  126.         'DebugLog "Nodes: " + num
  127.         'Self.Header.Nodes.JumpTo(Stream)
  128.         'For i = 0 Until num
  129.         '   ListAddLast(Self.Nodes, TQNode.Read(Stream))
  130.         'Next
  131.        
  132.         'Read texture info
  133.         num = Self.Header.TexInfo.Count(TQSurface.Size())
  134.         DebugLog "TexInfos: " + num
  135.         Self.Header.TexInfo.JumpTo(Stream)
  136.         For i = 0 Until num
  137.             ListAddLast(Self.TexInfo, TQSurface.Read(Stream))
  138.         Next
  139.        
  140.         'Read faces
  141.         num = Self.Header.Faces.Count(TQFace.Size())
  142.         DebugLog "Faces: " + num
  143.         Self.Header.Faces.JumpTo(Stream)
  144.         For i = 0 Until num
  145.             ListAddLast(Self.Faces, TQFace.Read(Stream))
  146.         Next
  147.        
  148.         'Read clipnodes
  149.         'num = Self.Header.ClipNodes.Count(TQClipNode.Size())
  150.         'DebugLog "ClipNodes: " + num
  151.         'Self.Header.ClipNodes.JumpTo(Stream)
  152.         'For i = 0 Until num
  153.         '   ListAddLast(Self.ClipNodes, TQClipNode.Read(Stream))
  154.         'Next
  155.        
  156.         'Read leaves
  157.         'num = Self.Header.Leaves.Count(TQDLeaf.Size())
  158.         'DebugLog "Leaves: " + num
  159.         'Self.Header.Leaves.JumpTo(Stream)
  160.         'For i = 0 Until num
  161.         '   ListAddLast(Self.Leaves, TQDLeaf.Read(Stream))
  162.         'Next
  163.        
  164.         'Read edges
  165.         num = Self.Header.Edges.Count(TQEdge.Size())
  166.         DebugLog "Edges: " + num
  167.         Self.Header.Edges.JumpTo(Stream)
  168.         For i = 0 Until num
  169.             ListAddLast(Self.Edges, TQEdge.Read(Stream))
  170.         Next
  171.        
  172.         'Read models
  173.         num = Self.Header.Models.Count(TQModel.Size())
  174.         DebugLog "Models: " + num
  175.         Self.Header.Models.JumpTo(Stream)
  176.         For i = 0 Until num
  177.             ListAddLast(Self.Models, TQModel.Read(Stream))
  178.         Next
  179.        
  180.         CloseStream(Stream)
  181.         Return True
  182.     EndMethod
  183. EndType
  184.  
  185. 'http://www.gamers.org/dEngine/quake/spec/quake-spec34/qkspec_4.htm
  186. 'char 8bit = negative byte
  187. 'u_char 8bit = byte
  188. 'short 16bit = negative short
  189. 'u_short 16bit = short
  190. 'long 32bit = int
  191. 'u_long 32bit = longer int
  192. 'float 32bit = float
  193. 'scalar_t 32bit = float
  194.  
  195. Type TQHeader
  196.     Field Version:Int
  197.     Field Entities:TQEntry
  198.     Field PLANES:TQEntry
  199.     Field MipTex:TQEntry
  200.     Field Vertices:TQEntry
  201.     Field VisiList:TQEntry
  202.     Field Nodes:TQEntry
  203.     Field TexInfo:TQEntry
  204.     Field Faces:TQEntry
  205.     Field LightMaps:TQEntry
  206.     Field ClipNodes:TQEntry
  207.     Field Leaves:TQEntry
  208.     Field LFace:TQEntry
  209.     Field Edges:TQEntry
  210.     Field Ledges:TQEntry
  211.     Field Models:TQEntry
  212.    
  213.     Function Read:TQHeader(Stream:TStream)
  214.         Local nH:TQHeader = New TQHeader
  215.        
  216.         nH.Version = ReadInt(Stream)
  217.         If nH.Version = "29" Then
  218.             DebugLog "Correct BSP version (" + nH.Version + ")"
  219.         Else
  220.             DebugLog "Warning: Wrong BSP version (" + nH.Version + ")"
  221.         EndIf
  222.        
  223.         nH.Entities = TQEntry.Read(Stream)
  224.         nH.PLANES = TQEntry.Read(Stream)
  225.        
  226.         nH.MipTex = TQEntry.Read(Stream)
  227.         nH.Vertices = TQEntry.Read(Stream)
  228.        
  229.         nH.VisiList = TQEntry.Read(Stream)
  230.         nH.Nodes = TQEntry.Read(Stream)
  231.        
  232.         nH.TexInfo = TQEntry.Read(Stream)
  233.        
  234.         nH.Faces = TQEntry.Read(Stream)
  235.        
  236.         nH.LightMaps = TQEntry.Read(Stream)
  237.         nH.ClipNodes = TQEntry.Read(Stream)
  238.        
  239.         nH.Leaves = TQEntry.Read(Stream)
  240.        
  241.         nH.LFace = TQEntry.Read(Stream)
  242.         nH.Edges = TQEntry.Read(Stream)
  243.        
  244.         nH.Ledges = TQEntry.Read(Stream)
  245.         nH.Models = TQEntry.Read(Stream)
  246.        
  247.         Return nH
  248.     EndFunction
  249. EndType
  250.  
  251. Type TQModel
  252.     Field Bound:TQBoundBox
  253.     Field Origin:TQVec3
  254.     Field Node_ID0:Int
  255.     Field Node_ID1:Int
  256.     Field Node_ID2:Int
  257.     Field Node_ID3:Int
  258.     Field NumLeafs:Int
  259.     Field Face_ID:Int
  260.     Field Face_Num:Int
  261.    
  262.     Function Read:TQModel(Stream:TStream)
  263.         Local nM:TQModel = New TQModel
  264.         nM.Bound = TQBoundBox.Read(Stream)
  265.         nM.Origin = TQVec3.Read(Stream)
  266.         nM.Node_ID0 = ReadInt(Stream)
  267.         nM.Node_ID1 = ReadInt(Stream)
  268.         nM.Node_ID2 = ReadInt(Stream)
  269.         nM.Node_ID3 = ReadInt(Stream)
  270.         nM.NumLeafs = ReadInt(Stream)
  271.         nM.Face_ID = ReadInt(Stream)
  272.         nM.Face_Num = ReadInt(Stream)
  273.         Return nM
  274.     EndFunction
  275.    
  276.     Function Size:Int()
  277.         Return (B_INT * 7) + TQBoundBox.Size() + TQVec3.Size()
  278.     EndFunction
  279. EndType
  280.  
  281. Type TQVertex
  282.     Field X:Float
  283.     Field Y:Float
  284.     Field Z:Float
  285.    
  286.     Function Read:TQVertex(Stream:TStream)
  287.         Local nV:TQVertex = New TQVertex
  288.         nV.Z = ReadFloat(Stream)
  289.         nV.X = ReadFloat(Stream)
  290.         nV.Y = ReadFloat(Stream)
  291.        
  292.         Return nV
  293.     EndFunction
  294.    
  295.     Function Size:Int()
  296.         Return B_FLOAT * 3
  297.     EndFunction
  298. EndType
  299.  
  300. Type TQSurface
  301.     Field VectorS:TQVec3
  302.     Field DistS:Float
  303.     Field VectorT:TQVec3
  304.     Field DistT:Float
  305.     Field Texture_ID:Int
  306.    
  307.     Field Animated:Int
  308.    
  309.     Function Read:TQSurface(Stream:TStream)
  310.         Local nS:TQSurface = New TQSurface
  311.         nS.VectorS = TQVec3.Read(Stream)
  312.         nS.DistS = ReadFloat(Stream)
  313.         nS.VectorT = TQVec3.Read(Stream)
  314.         nS.DistT = ReadFloat(Stream)
  315.         nS.Texture_ID = ReadInt(Stream)
  316.         nS.Animated = ReadInt(Stream)
  317.         Return nS
  318.     EndFunction
  319.    
  320.     Function Size:Int()
  321.         Return (B_FLOAT * 2) + (B_INT * 2) + (TQVec3.Size() * 2)
  322.     EndFunction
  323. EndType
  324.  
  325. Type TQEdge
  326.     Field Vertex0:Short
  327.     Field Vertex1:Short
  328.    
  329.     Function Read:TQEdge(Stream:TStream)
  330.         Local nE:TQEdge = New TQEdge
  331.         nE.Vertex0 = ReadShort(Stream)
  332.         nE.Vertex1 = ReadShort(Stream)
  333.         Return nE
  334.     EndFunction
  335.    
  336.     Function Size:Int()
  337.         Return B_SHORT * 2
  338.     EndFunction
  339. EndType
  340.  
  341. Type TQFace
  342.     Field Plane_ID:Short
  343.    
  344.     Field Side:Short
  345.     Field Ledge_ID:Int
  346.    
  347.     Field Ledge_Num:Short
  348.     Field TexInfo_ID:Short
  349.    
  350.     Field TypeLight:Byte
  351.     Field BaseLight:Byte
  352.     Field Light:Byte[2]
  353.     Field LightMap:Int
  354.    
  355.     Function Read:TQFace(Stream:TStream)
  356.         Local nF:TQFace = New TQFace
  357.         nF.Plane_ID = ReadShort(Stream)
  358.         nF.Side = ReadShort(Stream)
  359.         nF.Ledge_ID = ReadInt(Stream)
  360.         nF.Ledge_Num = ReadShort(Stream)
  361.         nF.TexInfo_ID = ReadShort(Stream)
  362.         nF.TypeLight = ReadByte(Stream)
  363.         nF.BaseLight = ReadByte(Stream)
  364.         nF.Light[0] = ReadByte(Stream)
  365.         nF.Light[1] = ReadByte(Stream)
  366.         nF.LightMap = ReadInt(Stream)
  367.         Return nF
  368.     EndFunction
  369.    
  370.     Function Size:Int()
  371.         Return (B_SHORT * 4) + (B_INT * 2) + (B_BYTE * 4)
  372.     EndFunction
  373. EndType
  374.  
  375. Type TQMipHeader
  376.     Field NumTex:Int
  377.     Field Offset:Int[]
  378.    
  379.     Function Read:TQMipHeader(Stream:TStream)
  380.         Local nM:TQMipHeader = New TQMipHeader
  381.         nM.NumTex = ReadInt(Stream)
  382.        
  383.         'Should I even read this?
  384.         nM.Offset = New Int[nM.NumTex]
  385.         For Local i:Int = 0 Until nM.NumTex
  386.             nM.Offset[i] = ReadInt(Stream)
  387.         Next
  388.         Return nM
  389.     EndFunction
  390. EndType
  391.  
  392. Type TQMipTex
  393.     Field Name:String
  394.     Field width:Int
  395.     Field Height:Int
  396.     Field Offset1:Int
  397.     Field Offset2:Int
  398.     Field Offset4:Int
  399.     Field Offset8:Int
  400.    
  401.     Function Read:TQMipTex(Stream:TStream)
  402.         Local nM:TQMipTex = New TQMipTex
  403.         For Local i:Int = 0 Until 16
  404.             nM.Name:+Chr(ReadByte(Stream))
  405.         Next
  406.         nM.width = ReadInt(Stream)
  407.         nM.Height = ReadInt(Stream)
  408.         nM.Offset1 = ReadInt(Stream)
  409.         nM.Offset2 = ReadInt(Stream)
  410.         nM.Offset4 = ReadInt(Stream)
  411.         nM.Offset8 = ReadInt(Stream)
  412.         Return nM
  413.     EndFunction
  414.    
  415.     Function Size:Int()
  416.         Return (B_BYTE * 16) + (B_INT * 6)
  417.     EndFunction
  418. EndType
  419.  
  420. Type TQEntry
  421.     Field Offset:Int
  422.     Field Size:Int
  423.    
  424.     Function Read:TQEntry(Stream:TStream)
  425.         Local nE:TQEntry = New TQEntry
  426.         nE.Offset = ReadInt(Stream)
  427.         nE.Size = ReadInt(Stream)
  428.         Return nE
  429.     EndFunction
  430.    
  431.     Method Count:Int(typeSize:Int)
  432.         Return Size / typeSize
  433.     EndMethod
  434.    
  435.     Method JumpTo(Stream:TStream)
  436.         Stream.Seek(Self.Offset)
  437.     EndMethod
  438. EndType
  439.  
  440. Type TQNode
  441.     Field Plane_ID:Int
  442.    
  443.     Field Front:Short
  444.    
  445.     Field Back:Short
  446.    
  447.     Field Box:TQBBoxShort
  448.     Field Face_ID:Short
  449.     Field Face_Num:Short
  450.    
  451.     Function Read:TQNode(Stream:TStream)
  452.         Local nN:TQNode = New TQNode
  453.         nN.Plane_ID = ReadInt(Stream)
  454.         nN.Front = ReadShort(Stream)
  455.         nN.Back = ReadShort(Stream)
  456.         nN.Box = TQBBoxShort.Read(Stream)
  457.         nN.Face_ID = ReadShort(Stream)
  458.         nN.Face_Num = ReadShort(Stream)
  459.         Return nN
  460.     EndFunction
  461. EndType
  462.  
  463. Type TQDLeaf
  464.     Field Typ:Int
  465.     Field VisList:Int
  466.    
  467.     Field Bound:TQBBoxShort
  468.     Field LFace_ID:Short
  469.    
  470.     Field LFace_Num:Short
  471.     Field SndWater:Byte
  472.     Field SndSky:Byte
  473.     Field SndSlime:Byte
  474.     Field SndLava:Byte
  475.    
  476.     Function Read:TQDLeaf(Stream:TStream)
  477.         Local nD:TQDLeaf = New TQDLeaf
  478.         nD.Typ = ReadInt(Stream)
  479.         nD.VisList = ReadInt(Stream)
  480.        
  481.         nD.Bound = TQBBoxShort.Read(Stream)
  482.         nD.LFace_ID = ReadShort(Stream)
  483.        
  484.         nD.LFace_Num = ReadShort(Stream)
  485.         nD.SndWater = ReadByte(Stream)
  486.         nD.SndSky = ReadByte(Stream)
  487.         nD.SndSlime = ReadByte(Stream)
  488.         nD.SndLava = ReadByte(Stream)
  489.         Return nD
  490.     EndFunction
  491. EndType
  492.  
  493. Type TQPlane
  494.     Field Normal:TQVec3
  495.    
  496.     Field Dist:Float
  497.    
  498.     Field Typ:Int
  499.    
  500.     Function Read:TQPlane(Stream:TStream)
  501.         Local nP:TQPlane = New TQPlane
  502.         nP.Normal = TQVec3.Read(Stream)
  503.         nP.Dist = ReadFloat(Stream)
  504.         nP.Typ = ReadInt(Stream)
  505.         Return nP
  506.     EndFunction
  507. EndType
  508.  
  509. Type TQClipNode
  510.     Field PlaneNum:Int
  511.     Field Front:Short
  512.    
  513.     Field Back:Short
  514.    
  515.     Function Read:TQClipNode(Stream:TStream)
  516.         Local nC:TQClipNode = New TQClipNode
  517.         nC.PlaneNum = ReadInt(Stream)
  518.         nC.Front = ReadShort(Stream)
  519.         nC.Back = ReadShort(Stream)
  520.         Return nC
  521.     EndFunction
  522. EndType
  523.  
  524. 'Basic types
  525. Type TQVec3
  526.     Field X:Float
  527.     Field Y:Float
  528.     Field Z:Float
  529.    
  530.     Function Read:TQVec3(Stream:TStream)
  531.         Local nV:TQVec3 = New TQVec3
  532.         nV.X = ReadFloat(Stream)
  533.         nV.Y = ReadFloat(Stream)
  534.         nV.Z = ReadFloat(Stream)
  535.         Return nV
  536.     EndFunction
  537.    
  538.     Function Size:Int()
  539.         Return B_FLOAT * 3
  540.     EndFunction
  541. EndType
  542.  
  543. Type TQBoundBox
  544.     Field Minimum:TQVec3
  545.     Field Maximum:TQVec3
  546.    
  547.     Function Read:TQBoundBox(Stream:TStream)
  548.         Local nB:TQBoundBox = New TQBoundBox
  549.         nB.Minimum = TQVec3.Read(Stream)
  550.         nB.Maximum = TQVec3.Read(Stream)
  551.         Return nB
  552.     EndFunction
  553.    
  554.     Function Size:Int()
  555.             Return TQVec3.Size() * 2
  556.     EndFunction
  557. EndType
  558.  
  559. Type TQBBoxShort
  560.     Field Minimum:Short
  561.     Field Maximum:Short
  562.    
  563.     Function Read:TQBBoxShort(Stream:TStream)
  564.         Local nB:TQBBoxShort = New TQBBoxShort
  565.         nB.Minimum = ReadShort(Stream)
  566.         nB.Maximum = ReadShort(Stream)
  567.         Return nB
  568.     EndFunction
  569.    
  570.     Function Size:Int()
  571.         Return B_SHORT * 2
  572.     EndFunction
  573. EndType
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement