Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from OpenGL.GL import *
- from OpenGL.GLUT import *
- from OpenGL.GLU import *
- import math
- window = 0 # glut window number
- width, height = 500, 400 # window size
- def refresh2d(width, height):
- glViewport(0, 0, width, height)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- glOrtho(0.0, width, 0.0, height, 0.0, 1.0)
- glMatrixMode (GL_MODELVIEW)
- glLoadIdentity()
- def draw_circle(cx, cy, r, segments):
- theta = 2.0 * math.pi / segments
- tangetial_factor = math.tan(theta)
- radial_factor = math.cos(theta)
- x = float(r)
- y = 0.0
- glBegin(GL_LINE_LOOP)
- for index in range(segments):
- glVertex2f(x + cx, y + cy)
- tx = -y
- ty = x
- x += (tx * tangetial_factor)
- y += (ty * tangetial_factor)
- x *= radial_factor
- y *= radial_factor
- glEnd()
- def draw_arc(cx, cy, r, start_angle, arc_angle, segments):
- theta = (arc_angle / float(segments)) -1
- tangetial_factor = math.tan(math.radians(theta))
- radial_factor = math.cos(math.radians(theta))
- x = r * math.cos(math.radians(float(start_angle)))
- y = r * math.sin(math.radians(float(start_angle)))
- glBegin(GL_LINE_STRIP)
- for index in range(segments):
- glVertex2f(x + cx, y + cy)
- tx = -y
- ty = x
- x += (tx * tangetial_factor)
- y += (ty * tangetial_factor)
- x *= radial_factor
- y *= radial_factor
- glEnd()
- def draw_circle_slow(cx, cy, r, segments):
- glBegin(GL_LINE_LOOP)
- for index in range(segments):
- theta = 2.0 * math.pi * float(index) / float(segments)
- x = r * math.cos(theta)
- y = r * math.sin(theta)
- glVertex2f(x + cx, y + cy)
- glEnd()
- def draw_rect(x, y, width, height):
- glBegin(GL_QUADS) # start drawing a rectangle
- glVertex2f(x, y) # bottom left point
- glVertex2f(x + width, y) # bottom right point
- glVertex2f(x + width, y + height) # top right point
- glVertex2f(x, y + height) # top left point
- glEnd() # done drawing a rectangle
- def draw_line(x1, y1, x2, y2):
- glBegin(GL_LINES)
- glVertex2f(x1, y1)
- glVertex2f(x2, y2)
- glEnd()
- def draw(): # ondraw is called all the time
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # clear the screen
- glLoadIdentity() # reset position
- refresh2d(width, height) # set mode to 2d
- #glColor3f(0.0, 0.0, 1.0) # set color to blue
- #draw_rect(10, 10, 200, 100) # rect at (10, 10) with width 200, height 100
- glColor3f(1.0, 1.0, 1.0) # set color to white
- #draw_line(10, 250, 200, 250)
- #draw_line(200, 250, 200, 10)
- draw_circle(250.0, 200.0, 50.0, 64)
- # draw arc x center, y center, radius, start angle, included angle, number of segments
- draw_arc(250.0, 200.0, 150.0, 0.0, 180.0, 30)
- #glNormal3d(0.0, 0.0, 1.0)
- #draw_circle_slow(250.0, 250.0, 50.0, 8)
- glutSwapBuffers() # important for double buffering
- # initialization
- glutInit() # initialize glut
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
- glutInitWindowSize(width, height) # set window size
- glutInitWindowPosition(0, 0) # set window position
- window = glutCreateWindow("Nuts.com") # create window with title
- glutDisplayFunc(draw) # set draw function callback
- #glutIdleFunc(draw) # draw all the time
- glutMainLoop() # start everything
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement