Advertisement
Guest User

Untitled

a guest
Nov 13th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from OpenGL.GL import *
  4. from OpenGL.GLUT import *
  5. from OpenGL.GLU import *
  6. import math
  7.  
  8. window = 0 # glut window number
  9. width, height = 500, 400 # window size
  10.  
  11. def refresh2d(width, height):
  12. glViewport(0, 0, width, height)
  13. glMatrixMode(GL_PROJECTION)
  14. glLoadIdentity()
  15. glOrtho(0.0, width, 0.0, height, 0.0, 1.0)
  16. glMatrixMode (GL_MODELVIEW)
  17. glLoadIdentity()
  18.  
  19. def draw_circle(cx, cy, r, segments):
  20. theta = 2.0 * math.pi / segments
  21. tangetial_factor = math.tan(theta)
  22. radial_factor = math.cos(theta)
  23. x = float(r)
  24. y = 0.0
  25. glBegin(GL_LINE_LOOP)
  26. for index in range(segments):
  27. glVertex2f(x + cx, y + cy)
  28. tx = -y
  29. ty = x
  30. x += (tx * tangetial_factor)
  31. y += (ty * tangetial_factor)
  32. x *= radial_factor
  33. y *= radial_factor
  34. glEnd()
  35.  
  36. def draw_arc(cx, cy, r, start_angle, arc_angle, segments):
  37. theta = (arc_angle / float(segments)) -1
  38. tangetial_factor = math.tan(math.radians(theta))
  39. radial_factor = math.cos(math.radians(theta))
  40. x = r * math.cos(math.radians(float(start_angle)))
  41. y = r * math.sin(math.radians(float(start_angle)))
  42. glBegin(GL_LINE_STRIP)
  43. for index in range(segments):
  44. glVertex2f(x + cx, y + cy)
  45. tx = -y
  46. ty = x
  47. x += (tx * tangetial_factor)
  48. y += (ty * tangetial_factor)
  49. x *= radial_factor
  50. y *= radial_factor
  51. glEnd()
  52.  
  53. def draw_circle_slow(cx, cy, r, segments):
  54. glBegin(GL_LINE_LOOP)
  55. for index in range(segments):
  56. theta = 2.0 * math.pi * float(index) / float(segments)
  57. x = r * math.cos(theta)
  58. y = r * math.sin(theta)
  59. glVertex2f(x + cx, y + cy)
  60. glEnd()
  61.  
  62. def draw_rect(x, y, width, height):
  63. glBegin(GL_QUADS) # start drawing a rectangle
  64. glVertex2f(x, y) # bottom left point
  65. glVertex2f(x + width, y) # bottom right point
  66. glVertex2f(x + width, y + height) # top right point
  67. glVertex2f(x, y + height) # top left point
  68. glEnd() # done drawing a rectangle
  69.  
  70. def draw_line(x1, y1, x2, y2):
  71. glBegin(GL_LINES)
  72. glVertex2f(x1, y1)
  73. glVertex2f(x2, y2)
  74. glEnd()
  75.  
  76. def draw(): # ondraw is called all the time
  77. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # clear the screen
  78. glLoadIdentity() # reset position
  79. refresh2d(width, height) # set mode to 2d
  80. #glColor3f(0.0, 0.0, 1.0) # set color to blue
  81. #draw_rect(10, 10, 200, 100) # rect at (10, 10) with width 200, height 100
  82. glColor3f(1.0, 1.0, 1.0) # set color to white
  83. #draw_line(10, 250, 200, 250)
  84. #draw_line(200, 250, 200, 10)
  85. draw_circle(250.0, 200.0, 50.0, 64)
  86. # draw arc x center, y center, radius, start angle, included angle, number of segments
  87. draw_arc(250.0, 200.0, 150.0, 0.0, 180.0, 30)
  88. #glNormal3d(0.0, 0.0, 1.0)
  89. #draw_circle_slow(250.0, 250.0, 50.0, 8)
  90. glutSwapBuffers() # important for double buffering
  91.  
  92. # initialization
  93. glutInit() # initialize glut
  94. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
  95. glutInitWindowSize(width, height) # set window size
  96. glutInitWindowPosition(0, 0) # set window position
  97. window = glutCreateWindow("Nuts.com") # create window with title
  98. glutDisplayFunc(draw) # set draw function callback
  99. #glutIdleFunc(draw) # draw all the time
  100. glutMainLoop() # start everything
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement