forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglutrect1.py
104 lines (84 loc) · 2.83 KB
/
glutrect1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
"""
This program draws a single red rectangle in a blue background.
It is a demonstration of the structure needed to mix
OpenGL (for graphics) and GLUT (for window control),
all within Python via PyOpenGL.
This version marks a little cross at the origin.
"""
#import pydoc #Not used
__author__= "Joseph O'Rourke"
__version__ = "1.0"
__date__= "Sep10"
import sys
#import math #Not used, but often needed
from OpenGL.GL import *
from OpenGL.GLUT import *
##############################################################################
def rect():
"""
Draw a single rectangle centered on the origin.
Assumes [-1,+1]^3 coordinate system. Note, points are in 3D,
but placed at z=0.0.
"""
print "==>rect1"
glColor3f (1.0, 0.0, 0.0) #Red
glBegin(GL_POLYGON)
glVertex3f(-0.5, -0.5, 0.0)
glVertex3f(-0.5, 0.5, 0.0)
glVertex3f(0.5, 0.5, 0.0)
glVertex3f(0.5, -0.5, 0.0)
glEnd()
# Mark origin after, and so on top of, rectangle
MarkOrigin()
def MarkOrigin( ):
"""
Marks a cross at the origin just so we can see it.
"""
glColor3f (1.0, 1.0, 1.0) #White
glBegin(GL_LINES) # disconnected line segments
glVertex2f( -0.1,0.0 )
glVertex2f( 0.1,0.0 )
glVertex2f( 0.0,-0.1 )
glVertex2f( 0.0, 0.1 )
glEnd()
##############################################################################
def displayGL():
"""
What to do when the screen is repainted: draw the rectangle.
"""
glClear( GL_COLOR_BUFFER_BIT )
rect()
glFlush() #Flush the graphics pipeline buffer
def reshapeGL(width,height):
"""
What to do when the window is resized: reset coord system etc.
Initial creation of window constitutes a resize.
"""
w = width / float(height)
h = 1.0
print "==>reshapeGL: w,h=",w,h
glViewport( 0, 0, width, height )
glMatrixMode(GL_PROJECTION) #Work on projection matrix
glLoadIdentity()
glOrtho(-1., 1., -1., 1., -1., 1.) # Origin at center of window
# Note: first four numbers are xmin, xmax, ymin, ymax.
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def initializeGL():
"""
What to do upon first invocation.
"""
print "==>initializeG:"
glClearColor(0.0, 0.0, 1.0, 0.0) #blue
##############################################################################
if __name__=='__main__':
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE)
glutInitWindowPosition(0, 0) #Upperleft corner.
glutInitWindowSize(500, 500) #500 x 500 pixels
glutCreateWindow("Rectangle")
initializeGL()
glutDisplayFunc(displayGL) #Register display callback
glutReshapeFunc(reshapeGL) #Register reshape callback
glutMainLoop() #Infinite loop