Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- from OpenGL.GL import *
- from OpenGL.GLUT import *
- from OpenGL.GLU import *
- from PIL import *
- ESCAPE = '\033'
- window = 0
- ID = 0
- #rotation
- X_AXIS = 0.0
- Y_AXIS = 0.0
- Z_AXIS = 0.0
- DIRECTION = 1
- def InitGL(Width, Height):
- glClearColor(0.0, 0.0, 0.0, 0.0)
- glClearDepth(1.0)
- glDepthFunc(GL_LESS)
- glEnable(GL_DEPTH_TEST)
- glShadeModel(GL_SMOOTH)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
- glMatrixMode(GL_MODELVIEW)
- # initialize texture mapping
- glEnable(GL_TEXTURE_2D)
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
- def keyPressed(*args):
- if args[0] == ESCAPE:
- sys.exit()
- class Block:
- def __init__(self):
- self.types = ['GRASS', 'DIRT', 'STONE', 'AIR']
- self.sides = ['BOTTOM', 'TOP', 'LEFT', 'RIGHT', 'FRONT', 'BACK']
- self.block_type = None
- self.block_position = None
- self.is_solid = None
- def create_block(self, block_type, block_position):
- self.block_type = block_type
- self.block_position = block_position
- if self.block_type == 'AIR':
- self.is_solid = False
- else:
- self.is_solid = True
- def create_cube(self, cube_side):
- p0 = (-0.5, -0.5, 0.5)
- p1 = (0.5, -0.5, 0.5)
- p2 = (0.5, -0.5, -0.5)
- p3 = (-0.5, -0.5, -0.5)
- p4 = (-0.5, 0.5, 0.5)
- p5 = (0.5, 0.5, 0.5)
- p6 = (0.5, 0.5, -0.5)
- p7 = (-0.5, 0.5, -0.5)
- p0 = tuple(l + r for l, r in zip(p0, self.block_position))
- p1 = tuple(l + r for l, r in zip(p1, self.block_position))
- p2 = tuple(l + r for l, r in zip(p2, self.block_position))
- p3 = tuple(l + r for l, r in zip(p3, self.block_position))
- p4 = tuple(l + r for l, r in zip(p4, self.block_position))
- p5 = tuple(l + r for l, r in zip(p5, self.block_position))
- p6 = tuple(l + r for l, r in zip(p6, self.block_position))
- p7 = tuple(l + r for l, r in zip(p7, self.block_position))
- uv00 = (0, 0)
- uv10 = (1, 0)
- uv01 = (0, 1)
- uv11 = (1, 1)
- if cube_side == 'BOTTOM':
- glBegin(GL_QUADS)
- glTexCoord2f(*uv11)
- glVertex3f(*p0)
- glTexCoord2f(*uv01)
- glVertex3f(*p1)
- glTexCoord2f(*uv00)
- glVertex3f(*p2)
- glTexCoord2f(*uv10)
- glVertex3f(*p3)
- elif cube_side == 'TOP':
- glBegin(GL_QUADS)
- glTexCoord2f(*uv11)
- glVertex3f(*p7)
- glTexCoord2f(*uv01)
- glVertex3f(*p6)
- glTexCoord2f(*uv00)
- glVertex3f(*p5)
- glTexCoord2f(*uv10)
- glVertex3f(*p4)
- elif cube_side == 'LEFT':
- glBegin(GL_QUADS)
- glTexCoord2f(*uv11)
- glVertex3f(*p7)
- glTexCoord2f(*uv01)
- glVertex3f(*p4)
- glTexCoord2f(*uv00)
- glVertex3f(*p0)
- glTexCoord2f(*uv10)
- glVertex3f(*p3)
- elif cube_side == 'RIGHT':
- glBegin(GL_QUADS)
- glTexCoord2f(*uv11)
- glVertex3f(*p5)
- glTexCoord2f(*uv01)
- glVertex3f(*p6)
- glTexCoord2f(*uv00)
- glVertex3f(*p2)
- glTexCoord2f(*uv10)
- glVertex3f(*p1)
- elif cube_side == 'FRONT':
- glBegin(GL_QUADS)
- glTexCoord2f(*uv11)
- glVertex3f(*p4)
- glTexCoord2f(*uv01)
- glVertex3f(*p5)
- glTexCoord2f(*uv00)
- glVertex3f(*p1)
- glTexCoord2f(*uv10)
- glVertex3f(*p0)
- elif cube_side == 'BACK':
- glBegin(GL_QUADS)
- glTexCoord2f(*uv11)
- glVertex3f(*p6)
- glTexCoord2f(*uv01)
- glVertex3f(*p7)
- glTexCoord2f(*uv00)
- glVertex3f(*p3)
- glTexCoord2f(*uv10)
- glVertex3f(*p2)
- glEnd()
- def draw_cube(self, chunks, x, y, z):
- if chunks[x][y][z].block_type == 'AIR':
- return
- try:
- if not chunks[x][y][z+1].is_solid:
- self.create_cube('FRONT')
- except:
- pass
- try:
- if not chunks[x][y][z-1].is_solid:
- self.create_cube('BACK')
- except:
- pass
- try:
- if not chunks[x][y+1][z].is_solid:
- self.create_cube('TOP')
- except:
- pass
- try:
- if not chunks[x][y-1][z].is_solid:
- self.create_cube('BOTTOM')
- except:
- pass
- try:
- if not chunks[x+1][y][z].is_solid:
- self.create_cube('RIGHT')
- except:
- pass
- try:
- if not chunks[x-1][y][z].is_solid:
- self.create_cube('LEFT')
- except:
- pass
- class Chunk:
- def __init__(self, world_size, segments):
- self.segments = segments
- self.world_size = world_size
- def list_3D(self, dim):
- return [[[None for _ in range(dim[2])] for _ in range(dim[1])] for _ in range(dim[0])]
- def create_chunk(self):
- self.chunk_data = self.list_3D([self.world_size, self.world_size, self.world_size])
- for i in range(self.world_size):
- for j in range(self.world_size):
- for k in range(self.world_size):
- b = Block()
- if i == 0 or i == self.world_size - 1 or \
- j == 0 or j == self.world_size - 1 or \
- k == 0 or k == self.world_size - 1:
- b.create_block('AIR', (i, j, k))
- else:
- if self.segments[i+j+k] < 50:
- b.create_block('AIR', (i, j, k))
- else:
- b.create_block('DIRT', (i, j, k))
- self.chunk_data[i][j][k] = b
- for i in range(self.world_size):
- for j in range(self.world_size):
- for k in range(self.world_size):
- self.chunk_data[i][j][k].draw_cube(self.chunk_data, i, j, k)
- def draw(self):
- global X_AXIS,Y_AXIS,Z_AXIS
- global DIRECTION
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
- glLoadIdentity()
- glTranslatef(0.0,0.0,-20.0)
- glRotatef(X_AXIS,1.0,0.0,0.0)
- glRotatef(Y_AXIS,0.0,1.0,0.0)
- glRotatef(Z_AXIS,0.0,0.0,1.0)
- self.create_chunk()
- X_AXIS = X_AXIS - 0.30
- Z_AXIS = Z_AXIS - 0.30
- glutSwapBuffers()
- def main():
- global window
- global ID
- glutInit(sys.argv)
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
- glutInitWindowSize(800, 600)
- glutInitWindowPosition(200,200)
- window = glutCreateWindow(b'Voxel World')
- world_size = 6
- segments = [random.randint(0, 100) for _ in range(world_size*3)]
- c = Chunk(world_size, segments)
- glutDisplayFunc(c.draw)
- glutIdleFunc(c.draw)
- glutKeyboardFunc(keyPressed)
- InitGL(800, 600)
- #loadImage()
- glutMainLoop()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement