Guest User

Untitled

a guest
Feb 1st, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # Demonstrates OpenGL evaluators to draw bezier curve
  3. # Ben Smith
  4. #
  5. # Based heavily on Bezier.cpp
  6. # OpenGL SuperBible
  7. # Program by Richard S. Wright Jr.
  8.  
  9. import pyglet
  10. from pyglet.gl import *
  11. from pyglet import window
  12.  
  13.  
  14. # The number of control points for this curve
  15. nNumPoints = 3
  16.  
  17. ctrlPoints = (GLfloat * 3 * 3)(   (-4.0, 0.0, 0.0), # End Point
  18.     (-6.0, 4.0, 0.0),   # Control Point
  19.     (6.0, -4.0, 0.0))    # End Point
  20.  
  21. # This function is used to superimpose the control points over the curve
  22. def DrawPoints():
  23.     # Set point size larger to make more visible
  24.     glPointSize(5.0)
  25.  
  26.     # Loop through all control points for this example
  27.     glBegin(GL_POINTS)
  28.     for i in range(0, nNumPoints):
  29.         glVertex2fv(ctrlPoints[i])
  30.     glEnd()
  31.  
  32.  
  33. class MainWindow(window.Window):
  34.     def __init__(self, *args, **kwargs):
  35.         window.Window.__init__(self, *args, **kwargs)
  36.  
  37.         # Clear Window to white
  38.         glClearColor(1.0, 1.0, 1.0, 1.0 )
  39.  
  40.         # Draw in Blue
  41.         glColor3f(0.0, 0.0, 1.0)
  42.  
  43.  
  44.         # Called to draw scene
  45.     def on_draw(self):
  46.         # Clear the window with current clearing color
  47.         glClear(GL_COLOR_BUFFER_BIT)
  48.  
  49.         # Sets up the bezier
  50.         # This actually only needs to be called once and could go in
  51.         # the setup function
  52.         glMap1f(GL_MAP1_VERTEX_3,  # Type of data generated
  53.             0.0,                                    # Lower u range
  54.             100.0,                                 # Upper u range
  55.             3,                                       # Distance between points in the data
  56.             nNumPoints,                         # number of control points
  57.             ctrlPoints[0])                  # array of control points
  58.  
  59.         # Enable the evaluator
  60.         glEnable(GL_MAP1_VERTEX_3)
  61.  
  62.         # Use a line strip to "connect-the-dots"
  63.         #glBegin(GL_LINE_STRIP)
  64.  
  65.         #for i in range (0, 101):
  66.         #    # Evaluate the curve at this point
  67.         #    glEvalCoord1f(i)
  68.         #glEnd()
  69.  
  70.         # Map a grid of 100 points from 0 to 100
  71.         glMapGrid1d(100,0.0,100.0)
  72.         #glMapGrid2d(100, 0.0, 100.0, 100, 0.0, 100.0)
  73.  
  74.         #Evaluate the grid, using lines
  75.         glEvalMesh1(GL_LINE,0,100)
  76.         DrawPoints()
  77.  
  78.     ###################/
  79.     # Set 2D Projection negative 10 to positive 10 in X and Y
  80.     # Called when the window has changed size (including when the window is created)
  81.     def on_resize(self, w, h):
  82.  
  83.         # Prevent a divide by zero
  84.         if h == 0:
  85.             h = 1
  86.  
  87.         # Set Viewport to window dimensions
  88.         glViewport(0, 0, w, h)
  89.         glMatrixMode(GL_PROJECTION)
  90.         glLoadIdentity()
  91.  
  92.         gluOrtho2D(-10.0, 10.0, -10.0, 10.0)
  93.  
  94.         # Modelview matrix reset
  95.         glMatrixMode(GL_MODELVIEW)
  96.         glLoadIdentity()
  97.  
  98. # Main program entry point
  99. if __name__ == '__main__':
  100.     w = MainWindow(800, 600, caption='2D Bezier Curve', resizable=True)
  101.     pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment