Advertisement
Techmo

GL_CUBES_2.0

May 28th, 2014
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.42 KB | None | 0 0
  1. __author__ = 'bradly.tillmann'
  2. from OpenGL.GL import *
  3. from OpenGL.GLUT import *
  4. from OpenGL.GLU import *
  5. import sys, random
  6. #import serial
  7. import os
  8. import threading
  9.  
  10. ESCAPE = '\033'
  11.  
  12. window = 0
  13.  
  14. #rotation
  15. X_AXIS = 0.0
  16. Y_AXIS = 0.0
  17. Z_AXIS = 0.0
  18. global loc_x
  19. global loc_y
  20. global loc_z
  21. global mouse_x
  22. global mouse_y
  23. loc_x = 0
  24. loc_y = 0
  25. loc_z = 0
  26. mouse_x = 0
  27. mouse_y = 0
  28. player_y = 0
  29. world = []
  30. DIRECTION = 1
  31. file = open("key.txt", "a")
  32.  
  33. def InitGL(Width, Height):
  34.  
  35.         glClearColor(0.0, 0.0, 0.0, 0.0)
  36.         glClearDepth(1.0)
  37.         glDepthFunc(GL_LESS)
  38.         glEnable(GL_DEPTH_TEST)
  39.         glShadeModel(GL_SMOOTH)
  40.         glMatrixMode(GL_PROJECTION)
  41.         glLoadIdentity()
  42.         gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
  43.         glMatrixMode(GL_MODELVIEW)
  44. def world_generator(noise, max_x, max_y, max_z):
  45.         for a in range(max_x):
  46.                 for b in range(max_y):
  47.                         for c in range(max_z):
  48.                                 world.append([a, b, c, random.randrange(0, 2), random.randrange(0, 2), random.randrange(0, 2)])
  49.                                 chance = random.randrange(0, noise)
  50.                                 if chance < noise/2 and b == max_y:
  51.                                         world.append([a, b+1, c, random.randrange(0, 2), random.randrange(0, 2), random.randrange(0, 2)])
  52.                                 elif b == max_y:
  53.                                         world.append([a, b+1, c, random.randrange(0, 2), random.randrange(0, 2), random.randrange(0, 2)])
  54.                                         world.append([a, b+2, c, random.randrange(0, 2), random.randrange(0, 2), random.randrange(0, 2)])
  55.                                                
  56. def render_world():
  57.         for a in world:
  58.                 cube(a[0], a[1], a[2], a[3], a[4], a[5])
  59. def keyPressed(key, x, y):
  60.     if key == 'w':
  61.         global loc_x
  62.         loc_x += 1
  63.     if key == 's':
  64.         global loc_x
  65.         loc_x -= 1
  66.     if key == 'a':
  67.         global loc_y
  68.         loc_y += 1
  69.     if key == 'd':
  70.         global loc_y
  71.         loc_y -= 1
  72.     if key == 'e':
  73.         global loc_z
  74.         loc_z += 0.1
  75.     if key == 'q':
  76.         global loc_z
  77.         loc_z -= 0.1
  78.     if key == ' ':
  79.         global player_y
  80.         player_y -= 0.1
  81. def cube(x, y, z, red, green, blue):
  82.         glBegin(GL_QUADS)
  83.  
  84.         glColor3f(red, green, blue)
  85.         glVertex3f( 1.0 + (x * 2), 1.0 + (y * 2),-1.0 + (z * 2))
  86.         glVertex3f(-1.0 + (x * 2), 1.0 + (y * 2),-1.0 + (z * 2))
  87.         glVertex3f(-1.0 + (x * 2), 1.0 + (y * 2), 1.0 + (z * 2))
  88.         glVertex3f( 1.0 + (x * 2), 1.0 + (y * 2), 1.0 + (z * 2))
  89.  
  90.         glColor3f(red, green, blue)
  91.         glVertex3f( 1.0 + (x * 2),-1.0 + (y * 2), 1.0 + (z * 2))
  92.         glVertex3f(-1.0 + (x * 2),-1.0 + (y * 2), 1.0 + (z * 2))
  93.         glVertex3f(-1.0 + (x * 2),-1.0 + (y * 2),-1.0 + (z * 2))
  94.         glVertex3f( 1.0 + (x * 2),-1.0 + (y * 2),-1.0 + (z * 2))
  95.  
  96.         glColor3f(red, green, blue)
  97.         glVertex3f( 1.0 + (x * 2), 1.0 + (y * 2), 1.0 + (z * 2))
  98.         glVertex3f(-1.0 + (x * 2), 1.0 + (y * 2), 1.0 + (z * 2))
  99.         glVertex3f(-1.0 + (x * 2),-1.0 + (y * 2), 1.0 + (z * 2))
  100.         glVertex3f( 1.0 + (x * 2),-1.0 + (y * 2), 1.0 + (z * 2))
  101.  
  102.         glColor3f(red, green, blue)
  103.         glVertex3f( 1.0 + (x * 2),-1.0 + (y * 2),-1.0 + (z * 2))
  104.         glVertex3f(-1.0 + (x * 2),-1.0 + (y * 2),-1.0 + (z * 2))
  105.         glVertex3f(-1.0 + (x * 2), 1.0 + (y * 2),-1.0 + (z * 2))
  106.         glVertex3f( 1.0 + (x * 2), 1.0 + (y * 2),-1.0 + (z * 2))
  107.  
  108.         glColor3f(red, green, blue)
  109.         glVertex3f(-1.0 + (x * 2), 1.0 + (y * 2), 1.0 + (z * 2))
  110.         glVertex3f(-1.0 + (x * 2), 1.0 + (y * 2),-1.0 + (z * 2))
  111.         glVertex3f(-1.0 + (x * 2),-1.0 + (y * 2),-1.0 + (z * 2))
  112.         glVertex3f(-1.0 + (x * 2),-1.0 + (y * 2), 1.0 + (z * 2))
  113.  
  114.         glColor3f(red, green, blue)
  115.         glVertex3f( 1.0 + (x * 2), 1.0 + (y * 2),-1.0 + (z * 2))
  116.         glVertex3f( 1.0 + (x * 2), 1.0 + (y * 2), 1.0 + (z * 2))
  117.         glVertex3f( 1.0 + (x * 2),-1.0 + (y * 2), 1.0 + (z * 2))
  118.         glVertex3f( 1.0 + (x * 2),-1.0 + (y * 2),-1.0 + (z * 2))
  119.  
  120.         glEnd()
  121. def DrawGLScene():
  122.         global X_AXIS,Y_AXIS,Z_AXIS
  123.         global DIRECTION
  124.  
  125.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  126.  
  127.         glLoadIdentity()
  128.         glTranslatef(0.0,0.0,-6.0)
  129.         glTranslatef(0.0,float(player_y),0.0)
  130.         glRotatef(X_AXIS,1.0,0.0,0.0)
  131.         glRotatef(Y_AXIS,0.0,1.0,0.0)
  132.         glTranslatef(0.0, 0.0, float(Z_AXIS))
  133.  
  134.         # Draw Cube (multiple quads)
  135.         render_world()
  136.         X_AXIS = loc_x
  137.         Z_AXIS = loc_z
  138.         Y_AXIS = loc_y
  139.         glutSwapBuffers()
  140.  
  141.  
  142.  
  143. def main():
  144.  
  145.         global window
  146.  
  147.         glutInit(sys.argv)
  148.         glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
  149.         world_generator(3, 10, 10, 10)
  150.         glutInitWindowSize(1024,1024)
  151.         glutInitWindowPosition(200,200)
  152.         window = glutCreateWindow('OpenGL Python Cube')
  153.         glutDisplayFunc(DrawGLScene)
  154.         glutIdleFunc(DrawGLScene)
  155.         glutKeyboardFunc(keyPressed)
  156.         glutKeyboardFunc(keyPressed)
  157.         InitGL(640, 480)
  158.         glutMainLoop()
  159.  
  160. def display():
  161.  
  162.     glClear(GL_COLOR_BUFFER_BIT)
  163.  
  164.     glBegin(GL_LINES)
  165.     glVertex2f(1.0, 1.0)
  166.     glVertex2f(-1.0, -1.0)
  167.     glEnd()
  168.  
  169.     glutSwapBuffers()
  170.  
  171. if __name__ == "__main__":
  172.         main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement