Advertisement
Guest User

Untitled

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