root / PyOpenGL-Demo / redbook / lines.py @ 2
Historique | Voir | Annoter | Télécharger (4,32 ko)
| 1 |
#!/usr/bin/python
|
|---|---|
| 2 |
|
| 3 |
# This is statement is required by the build system to query build info
|
| 4 |
if __name__ == '__build__': |
| 5 |
raise Exception |
| 6 |
|
| 7 |
'''
|
| 8 |
lines.c from the Redbook examples.
|
| 9 |
Converted to Python by Jason L. Petrone 6/00
|
| 10 |
|
| 11 |
/*
|
| 12 |
* lines.c
|
| 13 |
* This program demonstrates geometric primitives and
|
| 14 |
* their attributes.
|
| 15 |
*/
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
Copyright (c) 1993-1997, Silicon Graphics, Inc.
|
| 20 |
ALL RIGHTS RESERVED
|
| 21 |
Permission to use, copy, modify, and distribute this software for
|
| 22 |
any purpose and without fee is hereby granted, provided that the above
|
| 23 |
copyright notice appear in all copies and that both the copyright notice
|
| 24 |
and this permission notice appear in supporting documentation, and that
|
| 25 |
the name of Silicon Graphics, Inc. not be used in advertising
|
| 26 |
or publicity pertaining to distribution of the software without specific,
|
| 27 |
written prior permission.
|
| 28 |
|
| 29 |
THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
|
| 30 |
AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
|
| 31 |
INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
|
| 32 |
FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
|
| 33 |
GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
|
| 34 |
SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
|
| 35 |
KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
|
| 36 |
LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
|
| 37 |
THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
|
| 38 |
ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
|
| 39 |
ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
|
| 40 |
POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
|
| 41 |
|
| 42 |
US Government Users Restricted Rights
|
| 43 |
Use, duplication, or disclosure by the Government is subject to
|
| 44 |
restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
|
| 45 |
(c)(1)(ii) of the Rights in Technical Data and Computer Software
|
| 46 |
clause at DFARS 252.227-7013 and/or in similar or successor
|
| 47 |
clauses in the FAR or the DOD or NASA FAR Supplement.
|
| 48 |
Unpublished-- rights reserved under the copyright laws of the
|
| 49 |
United States. Contractor/manufacturer is Silicon Graphics,
|
| 50 |
Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
|
| 51 |
|
| 52 |
OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
|
| 53 |
|
| 54 |
'''
|
| 55 |
|
| 56 |
import sys |
| 57 |
|
| 58 |
try:
|
| 59 |
from OpenGL.GLUT import * |
| 60 |
from OpenGL.GL import * |
| 61 |
from OpenGL.GLU import * |
| 62 |
except:
|
| 63 |
print ''' |
| 64 |
ERROR: PyOpenGL not installed properly.
|
| 65 |
'''
|
| 66 |
sys.exit() |
| 67 |
|
| 68 |
def drawOneLine(x1, y1, x2, y2): |
| 69 |
glBegin(GL_LINES) |
| 70 |
glVertex2f(x1, y1) |
| 71 |
glVertex2f(x2, y2) |
| 72 |
glEnd() |
| 73 |
|
| 74 |
|
| 75 |
def init(): |
| 76 |
glClearColor(0.0, 0.0, 0.0, 0.0) |
| 77 |
glShadeModel(GL_FLAT) |
| 78 |
|
| 79 |
def display(): |
| 80 |
glClear(GL_COLOR_BUFFER_BIT) |
| 81 |
|
| 82 |
# select white for all lines
|
| 83 |
glColor3f(1.0, 1.0, 1.0) |
| 84 |
|
| 85 |
# in 1st row, 3 lines, each with a different stipple
|
| 86 |
glEnable(GL_LINE_STIPPLE) |
| 87 |
|
| 88 |
glLineStipple (1, 0x0101) # dotted |
| 89 |
drawOneLine (50.0, 125.0, 150.0, 125.0) |
| 90 |
glLineStipple (1, 0x00FF) # dashed |
| 91 |
drawOneLine (150.0, 125.0, 250.0, 125.0); |
| 92 |
glLineStipple (1, 0x1C47) # dash/dot/dash |
| 93 |
drawOneLine (250.0, 125.0, 350.0, 125.0) |
| 94 |
|
| 95 |
# in 2nd row, 3 wide lines, each with different stipple
|
| 96 |
glLineWidth(5.0)
|
| 97 |
glLineStipple(1, 0x0101) # dotted |
| 98 |
drawOneLine(50.0, 100.0, 150.0, 100.0) |
| 99 |
glLineStipple(1, 0x00FF) # dashed |
| 100 |
drawOneLine(150.0, 100.0, 250.0, 100.0) |
| 101 |
glLineStipple(1, 0x1C47) # dash/dot/dash |
| 102 |
drawOneLine(250.0, 100.0, 350.0, 100.0) |
| 103 |
glLineWidth(1.0)
|
| 104 |
|
| 105 |
# in 3rd row, 6 lines, with dash/dot/dash stipple
|
| 106 |
# as part of a single connected line strip
|
| 107 |
glLineStipple (1, 0x1C47) # dash/dot/dash |
| 108 |
glBegin (GL_LINE_STRIP) |
| 109 |
for i in range(0, 7): |
| 110 |
glVertex2f(50.0 + (i * 50.0), 75.0) |
| 111 |
glEnd() |
| 112 |
|
| 113 |
# in 4th row, 6 independent lines with same stipple */
|
| 114 |
for i in range(0, 6): |
| 115 |
drawOneLine (50.0 + (i * 50.0), 50.0, 50.0 + ((i+1) * 50.0), 50.0) |
| 116 |
|
| 117 |
# in 5th row, 1 line, with dash/dot/dash stipple
|
| 118 |
# and a stipple repeat factor of 5
|
| 119 |
glLineStipple (5, 0x1C47) # dash/dot/dash |
| 120 |
drawOneLine (50.0, 25.0, 350.0, 25.0) |
| 121 |
|
| 122 |
glDisable (GL_LINE_STIPPLE) |
| 123 |
glFlush () |
| 124 |
|
| 125 |
def reshape(w, h): |
| 126 |
glViewport(0, 0, w, h) |
| 127 |
glMatrixMode(GL_PROJECTION) |
| 128 |
glLoadIdentity() |
| 129 |
gluOrtho2D(0.0, w, 0.0, h) |
| 130 |
|
| 131 |
def keyboard(key, x, y): |
| 132 |
if key == chr(27): |
| 133 |
sys.exit(0)
|
| 134 |
|
| 135 |
glutInit(sys.argv) |
| 136 |
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) |
| 137 |
glutInitWindowSize(400, 150) |
| 138 |
glutInitWindowPosition(100, 100) |
| 139 |
glutCreateWindow('Lines')
|
| 140 |
init() |
| 141 |
glutDisplayFunc(display) |
| 142 |
glutReshapeFunc(reshape) |
| 143 |
glutKeyboardFunc(keyboard) |
| 144 |
glutMainLoop() |