Advertisement
Guest User

simple.glsl

a guest
Jul 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  1. class MeshData( object):
  2. def __init__ ( self , ** kwargs):
  3. self . name = kwargs. get( "name" )
  4. self . vertex_format = [
  5. ( b'v_pos' , 3 , 'float' ),
  6. ( b'v_normal' , 3 , 'float' ),
  7. ( b'v_tc0' , 2 , 'float' )]
  8. self . vertices = []
  9. self . indices = []
  10. def calculate_normals ( self ):
  11. for i in range ( len( self . indices ) / ( 3 )):
  12. fi = i * 3
  13. v1i = self . indices [ fi ]
  14. v2i = self . indices [ fi + 1 ]
  15. v3i = self . indices [ fi + 2 ]
  16. vs = self . vertices
  17. p1 = [ vs [ v1i + c ] for c in range ( 3 )]
  18. p2 = [ vs [ v2i + c ] for c in range ( 3 )]
  19. p3 = [ vs [ v3i + c ] for c in range ( 3 )]
  20. u , v = [ 0 , 0 , 0 ], [ 0 , 0 , 0 ]
  21. for j in range( 3 ):
  22. v [ j ] = p2 [ j ] - p1 [ j ]
  23. u [ j ] = p3 [ j ] - p1 [ j ]
  24. n = [ 0 , 0 , 0 ]
  25. n [ 0 ] = u [ 1 ] * v [ 2 ] - u [ 2 ] * v [ 1 ]
  26. n [ 1 ] = u [ 2 ] * v [ 0 ] - u [ 0 ] * v [ 2 ]
  27. n [ 2 ] = u [ 0 ] * v [ 1 ] - u [ 1 ] * v [ 0 ]
  28. for k in range( 3 ):
  29. self . vertices [ v1i + 3 + k ] = n [ k ]
  30. self . vertices [ v2i + 3 + k ] = n [ k ]
  31. self . vertices [ v3i + 3 + k ] = n [ k ]
  32. class ObjFile :
  33. def finish_object ( self ):
  34. if self . _current_object is None:
  35. return
  36. mesh = MeshData()
  37. idx = 0
  38. for f in self . faces :
  39. verts = f [ 0 ]
  40. norms = f [ 1 ]
  41. tcs = f [ 2 ]
  42. for i in range( 3 ):
  43. # get normal components
  44. n = ( 0.0 , 0.0 , 0.0 )
  45. if norms[ i ] != - 1 :
  46. n = self . normals [ norms[ i ] - 1 ]
  47. # get texture coordinate components
  48. t = ( 0.0 , 0.0 )
  49. if tcs[ i ] != - 1 :
  50. t = self . texcoords[ tcs[ i ] - 1 ]
  51. # get vertex components
  52. v = self . vertices [ verts[ i ] - 1 ]
  53. data = [ v [ 0 ], v [ 1 ], v [ 2 ], n [ 0 ], n [ 1 ], n [ 2 ], t [ 0 ], t [ 1 ]]
  54. mesh . vertices . extend( data )
  55. tri = [ idx, idx + 1 , idx + 2 ]
  56. mesh . indices . extend( tri)
  57. idx += 3
  58. self . objects [ self . _current_object ] = mesh
  59. # mesh.calculate_normals()
  60. self . faces = []
  61. def __init__ ( self , filename , swapyz= False):
  62. """Loads a Wavefront OBJ file. """
  63. self . objects = {}
  64. self . vertices = []
  65. self . normals = []
  66. self . texcoords = []
  67. self . faces = []
  68. self . _current_object = None
  69. material = None
  70. for line in open ( filename , "r" ):
  71. if line . startswith ( '#' ):
  72. continue
  73. if line . startswith ( 's' ):
  74. continue
  75. values = line . split ()
  76. if not values:
  77. continue
  78. if values[ 0 ] == 'o' :
  79. self . finish_object ()
  80. self . _current_object = values[ 1 ]
  81. # elif values[0] == 'mtllib':
  82. #    self.mtl = MTL(values[1])
  83. # elif values[0] in ('usemtl', 'usemat'):
  84. #    material = values[1]
  85. if values[ 0 ] == 'v' :
  86. v = list ( map( float , values[ 1 : 4 ]))
  87. if swapyz:
  88. v = v [ 0 ], v [ 2 ], v [ 1 ]
  89. self . vertices . append( v )
  90. elif values[ 0 ] == 'vn' :
  91. v = list ( map( float , values[ 1 : 4 ]))
  92. if swapyz:
  93. v = v [ 0 ], v [ 2 ], v [ 1 ]
  94. self . normals . append( v )
  95. elif values[ 0 ] == 'vt' :
  96. self . texcoords. append( map( float , values[ 1 : 3 ]))
  97. elif values[ 0 ] == 'f' :
  98. face = []
  99. texcoords = []
  100. norms = []
  101. for v in values[ 1 :]:
  102. w = v . split( '/' )
  103. face . append( int( w [ 0 ]))
  104. if len( w ) >= 2 and len( w [ 1 ]) > 0 :
  105. texcoords. append( int( w [ 1 ]))
  106. else:
  107. texcoords. append( - 1 )
  108. if len( w ) >= 3 and len( w [ 2 ]) > 0 :
  109. norms . append( int( w [ 2 ]))
  110. else:
  111. norms . append( - 1 )
  112. self . faces . append((face , norms , texcoords, material))
  113. self . finish_object ()
  114. def MTL( filename ):
  115. contents = {}
  116. mtl = None
  117. return
  118. for line in open ( filename, "r" ):
  119. if line . startswith ( '#' ):
  120. continue
  121. values = line . split()
  122. if not values:
  123. continue
  124. if values[ 0 ] == 'newmtl' :
  125. mtl = contents [ values[ 1 ]] = {}
  126. elif mtl is None:
  127. raise ValueError ( "mtl file doesn't start with newmtl stmt" )
  128. mtl[ values[ 0 ]] = values[ 1 :]
  129. return contents
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement