Advertisement
tthtlc

OpenGL torus

Jan 10th, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. #### sudo pip install pyglet
  4.  
  5. from math import pi, sin, cos
  6.  
  7. from OpenGL.GLUT import *
  8. from OpenGL.GLU import *
  9. from OpenGL.GL import *
  10.  
  11. import os
  12. import pyglet
  13. from pyglet.gl import *
  14. from PIL import Image
  15.  
  16. try:
  17. # Try and create a window with multisampling (antialiasing)
  18. config = Config(sample_buffers=1, samples=4,
  19. depth_size=16, double_buffer=True,)
  20. window = pyglet.window.Window(resizable=True, config=config)
  21. except pyglet.window.NoSuchConfigException:
  22. # Fall back to no multisampling for old hardware
  23. window = pyglet.window.Window(resizable=True)
  24.  
  25. @window.event
  26. #def on_mouse_press1(x, y, button, modifiers):
  27. # # Capture image from the OpenGL buffer
  28. # buffer = ( GLubyte * (3*window.width*window.height) )(0)
  29. # glReadPixels(0, 0, window.width, window.height, GL_RGB, GL_UNSIGNED_BYTE, buffer)
  30. #
  31. # # Use PIL to convert raw RGB buffer and flip the right way up
  32. # image = Image.fromstring(mode="RGB", size=(window.width, window.height), data=buffer)
  33. # image = image.transpose(Image.FLIP_TOP_BOTTOM)
  34. #
  35. # # Save image to disk
  36. # image.save('jpap.png')
  37. #def on_mouse_press(x, y, button, modifiers):
  38. # a = (GLuint * 1)(0)
  39. # glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_INT, a)
  40. # print a[0]
  41. def on_resize(width, height):
  42. # Override the default on_resize handler to create a 3D projection
  43. glViewport(0, 0, width, height)
  44. glMatrixMode(GL_PROJECTION)
  45. glLoadIdentity()
  46. gluPerspective(60., width / float(height), .1, 1000.)
  47. glMatrixMode(GL_MODELVIEW)
  48. return pyglet.event.EVENT_HANDLED
  49.  
  50. def update(dt):
  51. global rx, ry, rz
  52. rx += dt * 1
  53. ry += dt * 80
  54. rz += dt * 30
  55. rx %= 360
  56. ry %= 360
  57. rz %= 360
  58. pyglet.clock.schedule(update)
  59.  
  60. @window.event
  61. def on_draw():
  62. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  63. glLoadIdentity()
  64. glTranslatef(0, 0, -4)
  65. glRotatef(rz, 0, 0, 1)
  66. glRotatef(ry, 0, 1, 0)
  67. glRotatef(rx, 1, 0, 0)
  68. batch.draw()
  69.  
  70. def setup():
  71. # One-time GL setup
  72. glClearColor(1, 1, 1, 1)
  73. glColor3f(1, 0, 0)
  74. glEnable(GL_DEPTH_TEST)
  75. glEnable(GL_CULL_FACE)
  76.  
  77. # Uncomment this line for a wireframe view
  78. #glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
  79.  
  80. # Simple light setup. On Windows GL_LIGHT0 is enabled by default,
  81. # but this is not the case on Linux or Mac, so remember to always
  82. # include it.
  83. glEnable(GL_LIGHTING)
  84. glEnable(GL_LIGHT0)
  85. glEnable(GL_LIGHT1)
  86.  
  87. # Define a simple function to create ctypes arrays of floats:
  88. def vec(*args):
  89. return (GLfloat * len(args))(*args)
  90.  
  91. glLightfv(GL_LIGHT0, GL_POSITION, vec(.5, .5, 1, 0))
  92. glLightfv(GL_LIGHT0, GL_SPECULAR, vec(.5, .5, 1, 1))
  93. glLightfv(GL_LIGHT0, GL_DIFFUSE, vec(1, 1, 1, 1))
  94. glLightfv(GL_LIGHT1, GL_POSITION, vec(1, 0, .5, 0))
  95. glLightfv(GL_LIGHT1, GL_DIFFUSE, vec(.5, .5, .5, 1))
  96. glLightfv(GL_LIGHT1, GL_SPECULAR, vec(1, 1, 1, 1))
  97.  
  98. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0.5, 0, 0.3, 1))
  99. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, vec(1, 1, 1, 1))
  100. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50)
  101.  
  102. class Torus(object):
  103. list = None
  104. def __init__(self, radius, inner_radius, slices, inner_slices,
  105. batch, group=None):
  106. # Create the vertex and normal arrays.
  107. vertices = []
  108. normals = []
  109.  
  110. u_step = 2 * pi / (slices - 1)
  111. v_step = 2 * pi / (inner_slices - 1)
  112. u = 0.
  113. for i in range(slices):
  114. cos_u = cos(2*u)
  115. sin_u = sin(2*u)
  116. v = 0.
  117. for j in range(inner_slices):
  118. cos_v = cos(2*v)
  119. sin_v = sin(2*v)
  120.  
  121. d = (radius + inner_radius * cos_v)
  122. x = d * (cos_u +2)* cos_u
  123. y = d * (cos_u +2)* sin_u
  124. z = inner_radius * sin_v
  125.  
  126. nx = cos_u * cos_v
  127. ny = sin_u * cos_v
  128. nz = sin_v
  129.  
  130. vertices.extend([x, y, z])
  131. normals.extend([nx, ny, nz])
  132. v += v_step
  133. u += u_step
  134.  
  135. # Create a list of triangle indices.
  136. indices = []
  137. for i in range(slices - 1):
  138. for j in range(inner_slices - 1):
  139. p = i * inner_slices + j
  140. indices.extend([p, p + inner_slices, p + inner_slices + 1])
  141. indices.extend([p, p + inner_slices + 1, p + 1])
  142.  
  143. self.vertex_list = batch.add_indexed(len(vertices)//3,
  144. GL_TRIANGLE_STRIP,
  145. group,
  146. indices,
  147. ('v3f/static', vertices),
  148. ('n3f/static', normals))
  149.  
  150. def delete(self):
  151. self.vertex_list.delete()
  152.  
  153. setup()
  154. batch = pyglet.graphics.Batch()
  155. torus = Torus(1, 1, 80, 30, batch=batch)
  156. rx = ry = rz = 0
  157.  
  158. pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement