Guest User

Untitled

a guest
Mar 3rd, 2019
75
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3. from OpenGL.GL import *
  4. from OpenGL.GLUT import *
  5. from OpenGL.GLU import *
  6. from PIL import *
  7.  
  8. ESCAPE = '\033'
  9.  
  10. window = 0
  11. ID = 0
  12.  
  13. #rotation
  14. X_AXIS = 0.0
  15. Y_AXIS = 0.0
  16. Z_AXIS = 0.0
  17.  
  18. DIRECTION = 1
  19.  
  20.  
  21. def InitGL(Width, Height):
  22.     glClearColor(0.0, 0.0, 0.0, 0.0)
  23.     glClearDepth(1.0)
  24.     glDepthFunc(GL_LESS)
  25.     glEnable(GL_DEPTH_TEST)
  26.     glShadeModel(GL_SMOOTH)  
  27.     glMatrixMode(GL_PROJECTION)
  28.     glLoadIdentity()
  29.     gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
  30.     glMatrixMode(GL_MODELVIEW)
  31.  
  32.     # initialize texture mapping
  33.     glEnable(GL_TEXTURE_2D)
  34.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
  35.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
  36.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
  37.  
  38. def keyPressed(*args):
  39.     if args[0] == ESCAPE:
  40.         sys.exit()
  41.  
  42. class Block:
  43.     def __init__(self):
  44.         self.types = ['GRASS', 'DIRT', 'STONE', 'AIR']
  45.         self.sides = ['BOTTOM', 'TOP', 'LEFT', 'RIGHT', 'FRONT', 'BACK']
  46.        
  47.         self.block_type = None
  48.         self.block_position = None
  49.         self.is_solid = None
  50.        
  51.     def create_block(self, block_type, block_position):
  52.         self.block_type = block_type
  53.         self.block_position = block_position
  54.        
  55.         if self.block_type == 'AIR':
  56.             self.is_solid = False
  57.         else:
  58.             self.is_solid = True
  59.    
  60.     def create_cube(self, cube_side):
  61.         p0 = (-0.5, -0.5, 0.5)
  62.         p1 = (0.5, -0.5,  0.5)
  63.         p2 = (0.5, -0.5, -0.5)
  64.         p3 = (-0.5, -0.5, -0.5)
  65.         p4 = (-0.5, 0.5, 0.5)
  66.         p5 = (0.5, 0.5, 0.5)
  67.         p6 = (0.5, 0.5, -0.5)
  68.         p7 = (-0.5, 0.5, -0.5)
  69.        
  70.         p0 = tuple(l + r for l, r in zip(p0, self.block_position))
  71.         p1 = tuple(l + r for l, r in zip(p1, self.block_position))
  72.         p2 = tuple(l + r for l, r in zip(p2, self.block_position))
  73.         p3 = tuple(l + r for l, r in zip(p3, self.block_position))
  74.         p4 = tuple(l + r for l, r in zip(p4, self.block_position))
  75.         p5 = tuple(l + r for l, r in zip(p5, self.block_position))
  76.         p6 = tuple(l + r for l, r in zip(p6, self.block_position))
  77.         p7 = tuple(l + r for l, r in zip(p7, self.block_position))
  78.        
  79.         uv00 = (0, 0)
  80.         uv10 = (1, 0)
  81.         uv01 = (0, 1)
  82.         uv11 = (1, 1)
  83.        
  84.         if cube_side == 'BOTTOM':
  85.             glBegin(GL_QUADS)
  86.            
  87.             glTexCoord2f(*uv11)
  88.             glVertex3f(*p0)
  89.             glTexCoord2f(*uv01)
  90.             glVertex3f(*p1)
  91.             glTexCoord2f(*uv00)
  92.             glVertex3f(*p2)
  93.             glTexCoord2f(*uv10)
  94.             glVertex3f(*p3)
  95.         elif cube_side == 'TOP':
  96.             glBegin(GL_QUADS)
  97.            
  98.             glTexCoord2f(*uv11)
  99.             glVertex3f(*p7)
  100.             glTexCoord2f(*uv01)
  101.             glVertex3f(*p6)
  102.             glTexCoord2f(*uv00)
  103.             glVertex3f(*p5)
  104.             glTexCoord2f(*uv10)
  105.             glVertex3f(*p4)
  106.         elif cube_side == 'LEFT':
  107.             glBegin(GL_QUADS)
  108.            
  109.             glTexCoord2f(*uv11)
  110.             glVertex3f(*p7)
  111.             glTexCoord2f(*uv01)
  112.             glVertex3f(*p4)
  113.             glTexCoord2f(*uv00)
  114.             glVertex3f(*p0)
  115.             glTexCoord2f(*uv10)
  116.             glVertex3f(*p3)
  117.         elif cube_side == 'RIGHT':
  118.             glBegin(GL_QUADS)
  119.            
  120.             glTexCoord2f(*uv11)
  121.             glVertex3f(*p5)
  122.             glTexCoord2f(*uv01)
  123.             glVertex3f(*p6)
  124.             glTexCoord2f(*uv00)
  125.             glVertex3f(*p2)
  126.             glTexCoord2f(*uv10)
  127.             glVertex3f(*p1)
  128.         elif cube_side == 'FRONT':
  129.             glBegin(GL_QUADS)
  130.            
  131.             glTexCoord2f(*uv11)
  132.             glVertex3f(*p4)
  133.             glTexCoord2f(*uv01)
  134.             glVertex3f(*p5)
  135.             glTexCoord2f(*uv00)
  136.             glVertex3f(*p1)
  137.             glTexCoord2f(*uv10)
  138.             glVertex3f(*p0)
  139.         elif cube_side == 'BACK':
  140.             glBegin(GL_QUADS)
  141.            
  142.             glTexCoord2f(*uv11)
  143.             glVertex3f(*p6)
  144.             glTexCoord2f(*uv01)
  145.             glVertex3f(*p7)
  146.             glTexCoord2f(*uv00)
  147.             glVertex3f(*p3)
  148.             glTexCoord2f(*uv10)
  149.             glVertex3f(*p2)
  150.         glEnd()
  151.        
  152.     def draw_cube(self, chunks, x, y, z):
  153.         if chunks[x][y][z].block_type == 'AIR':
  154.             return
  155.         try:
  156.             if not chunks[x][y][z+1].is_solid:
  157.                 self.create_cube('FRONT')
  158.         except:
  159.             pass
  160.         try:
  161.             if not chunks[x][y][z-1].is_solid:
  162.                 self.create_cube('BACK')
  163.         except:
  164.             pass
  165.         try:
  166.             if not chunks[x][y+1][z].is_solid:
  167.                 self.create_cube('TOP')
  168.         except:
  169.             pass
  170.         try:
  171.             if not chunks[x][y-1][z].is_solid:
  172.                 self.create_cube('BOTTOM')
  173.         except:
  174.             pass
  175.         try:
  176.             if not chunks[x+1][y][z].is_solid:
  177.                 self.create_cube('RIGHT')
  178.         except:
  179.             pass
  180.         try:
  181.             if not chunks[x-1][y][z].is_solid:
  182.                 self.create_cube('LEFT')
  183.         except:
  184.             pass
  185.  
  186. class Chunk:
  187.     def __init__(self, world_size, segments):
  188.         self.segments = segments
  189.         self.world_size = world_size
  190.        
  191.     def list_3D(self, dim):
  192.         return [[[None for _ in range(dim[2])] for _ in range(dim[1])] for _ in range(dim[0])]
  193.        
  194.     def create_chunk(self):
  195.         self.chunk_data = self.list_3D([self.world_size, self.world_size, self.world_size])
  196.         for i in range(self.world_size):
  197.             for j in range(self.world_size):
  198.                 for k in range(self.world_size):
  199.                     b = Block()
  200.                     if i == 0 or i == self.world_size - 1  or \
  201.                        j == 0 or j == self.world_size - 1 or \
  202.                        k == 0 or k == self.world_size - 1:
  203.                         b.create_block('AIR', (i, j, k))
  204.                     else:
  205.                         if self.segments[i+j+k] < 50:
  206.                             b.create_block('AIR', (i, j, k))
  207.                         else:
  208.                             b.create_block('DIRT', (i, j, k))
  209.                     self.chunk_data[i][j][k] = b
  210.         for i in range(self.world_size):
  211.             for j in range(self.world_size):
  212.                 for k in range(self.world_size):
  213.                     self.chunk_data[i][j][k].draw_cube(self.chunk_data, i, j, k)
  214.                    
  215.     def draw(self):
  216.         global X_AXIS,Y_AXIS,Z_AXIS
  217.         global DIRECTION
  218.  
  219.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  220.  
  221.         glLoadIdentity()
  222.         glTranslatef(0.0,0.0,-20.0)
  223.  
  224.         glRotatef(X_AXIS,1.0,0.0,0.0)
  225.         glRotatef(Y_AXIS,0.0,1.0,0.0)
  226.         glRotatef(Z_AXIS,0.0,0.0,1.0)
  227.        
  228.         self.create_chunk()
  229.        
  230.         X_AXIS = X_AXIS - 0.30
  231.         Z_AXIS = Z_AXIS - 0.30
  232.  
  233.         glutSwapBuffers()
  234.  
  235. def main():
  236.     global window
  237.     global ID
  238.  
  239.     glutInit(sys.argv)
  240.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
  241.     glutInitWindowSize(800, 600)
  242.     glutInitWindowPosition(200,200)
  243.  
  244.     window = glutCreateWindow(b'Voxel World')
  245.  
  246.     world_size = 6
  247.     segments = [random.randint(0, 100) for _ in range(world_size*3)]
  248.     c = Chunk(world_size, segments)
  249.  
  250.     glutDisplayFunc(c.draw)
  251.     glutIdleFunc(c.draw)
  252.     glutKeyboardFunc(keyPressed)
  253.     InitGL(800, 600)
  254.     #loadImage()
  255.     glutMainLoop()
  256.  
  257. if __name__ == "__main__":
  258.         main()
RAW Paste Data