Advertisement
Dartess

openglol

Jun 11th, 2015
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. IncludeFile "RotateImage.pb"
  2.  
  3. UsePNGImageEncoder()
  4.  
  5. #WhiteColor = -1
  6. #RedColor = -16776961
  7. #Width = 800
  8. #Height = 800
  9.  
  10. Interface objectMethods
  11.   readObject()
  12.   drawObjIntoFile(fName.s)
  13. EndInterface
  14.  
  15. Structure vertice
  16.   x.f : y.f : z.f
  17. EndStructure
  18.  
  19. Structure face
  20.   v1.l : v2.l : v3.l
  21. EndStructure
  22.  
  23. Structure objectModel
  24.   *vTable
  25.   pathToObjFile.s
  26.   objFile.l
  27.   imgFile.l
  28.   Array vertices.vertice(0)
  29.   Array faces.face(0)
  30. EndStructure
  31.  
  32. Procedure InizializeObject(name.s)
  33.   Protected *this.objectModel = AllocateMemory(SizeOf(objectModel))
  34.   InitializeStructure(*this,objectModel)
  35.   *this\pathToObjFile = name
  36.   *this\imgFile = CreateImage(#PB_Any, #Width, #Height)
  37.   *this\vTable = ?objectMethods_vTable
  38.   ProcedureReturn *this
  39. EndProcedure
  40.  
  41. Procedure VeloLine(x0.l, y0.l, x1.l, y1.l, color.l)
  42.   steep.a = 0
  43.   y10.a = 0
  44.   If (Abs(x0-x1) < Abs(y0-y1))
  45.     Swap x0, y0
  46.     Swap x1, y1
  47.     steep = 1
  48.   EndIf
  49.   If (x0 > x1)
  50.     Swap x0, x1
  51.     Swap y0, y1
  52.   EndIf
  53.   dx.l = x1 - x0
  54.   dy.l = y1 - y0
  55.   derror2.l = Abs(dy)*2
  56.   error2.l = 0
  57.   y.l = y0  
  58.   If (y1 > y0)
  59.     y10 = 1
  60.   EndIf  
  61.   For x = x0 To x1
  62.     If (steep = 1)
  63.       If (y < #Width And x < #Height)
  64.         Plot(y, x, color)
  65.       EndIf      
  66.     Else
  67.       If (x < #Width And y < #Height)
  68.         Plot(x, y, color)
  69.       EndIf
  70.     EndIf
  71.     error2 = error2 + derror2
  72.     If (error2 > dx)
  73.       If y10 = 1
  74.         y = y + 1
  75.       Else
  76.         y = y - 1
  77.       EndIf
  78.       error2 = error2 - dx*2;
  79.     EndIf
  80.   Next
  81. EndProcedure
  82.  
  83. Procedure objectMethods_ReadObject(*this.objectModel)
  84.   *this\objFile = ReadFile(#PB_Any, *this\pathToObjFile)
  85.   If *this\objFile
  86.     temp.s = ""
  87.     v_index.l = 0
  88.     v_faces.l = 0
  89.     While Not Eof(*this\objFile)
  90.       temp = ReadString(*this\objFile)
  91.       Select StringField(temp, 1, " ")
  92.         Case "v"
  93.           *this\vertices(v_index)\x = ValF(StringField(temp, 2, " "))
  94.           *this\vertices(v_index)\y = ValF(StringField(temp, 3, " "))
  95.           *this\vertices(v_index)\z = ValF(StringField(temp, 4, " "))
  96.           v_index = v_index + 1
  97.           ReDim *this\vertices(v_index)
  98.         Case "f"
  99.           *this\faces(v_faces)\v1 = Val(StringField(StringField(temp, 2, " "), 1, "/"))
  100.           *this\faces(v_faces)\v2 = Val(StringField(StringField(temp, 3, " "), 1, "/"))
  101.           *this\faces(v_faces)\v3 = Val(StringField(StringField(temp, 4, " "), 1, "/"))
  102.           v_faces = v_faces + 1
  103.           ReDim *this\faces(v_faces)
  104.       EndSelect
  105.     Wend
  106.     CloseFile(*this\objFile)
  107.   EndIf  
  108. EndProcedure
  109.  
  110. Procedure objectMethods_DrawObjIntoFile(*this.objectModel, name.s)  
  111.   count_vertices = ArraySize(*this\vertices()) - 1
  112.   count_faces = ArraySize(*this\faces()) - 1  
  113.   StartDrawing(ImageOutput(*this\imgFile))  
  114.   For i = 0 To count_faces
  115.     current_face.face = *this\faces(i)
  116.     For j = 0 To 2
  117.       Select j
  118.         Case 0
  119.           v0.vertice = *this\vertices(current_face\v1 - 1)
  120.           v1.vertice = *this\vertices(current_face\v2 - 1)
  121.         Case 1
  122.           v0.vertice = *this\vertices(current_face\v2 - 1)
  123.           v1.vertice = *this\vertices(current_face\v3 - 1)
  124.         Case 2
  125.           v0.vertice = *this\vertices(current_face\v3 - 1)
  126.           v1.vertice = *this\vertices(current_face\v1 - 1)
  127.       EndSelect    
  128.       temp.f = 0
  129.       temp = (v0\x+1)*#Width/2
  130.       x0.l = temp
  131.       temp = (v0\y+1)*#Height/2
  132.       y0.l = temp
  133.       temp = (v1\x+1)*#Width/2
  134.       x1.l = temp
  135.       temp = (v1\y+1)*#Height/2
  136.       y1.l = temp
  137.       VeloLine(x0, y0, x1, y1, #WhiteColor)
  138.     Next
  139.   Next    
  140.   StopDrawing()    
  141.   flip = FlipImage(*this\imgFile)
  142.   SaveImage(flip, name, #PB_ImagePlugin_PNG)
  143. EndProcedure  
  144.  
  145. Procedure Main()
  146.  
  147.   testObj.objectMethods = InizializeObject("model.obj")  
  148.  
  149.   testObj\readObject()
  150.  
  151.   testObj\drawObjIntoFile("output80026.png")
  152.  
  153. EndProcedure
  154.  
  155. Main()
  156. End
  157.  
  158. DataSection
  159.   objectMethods_vTable:
  160.   Data.i @objectMethods_ReadObject()
  161.   Data.i @objectMethods_DrawObjIntoFile()
  162. EndDataSection
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement