root / PyOpenGL-Demo / redbook / fog.py @ 2
Historique | Voir | Annoter | Télécharger (5,05 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 |
fog.c from the Redbook examples.
|
| 9 |
Converted to Python by Jason L. Petrone 7/00
|
| 10 |
|
| 11 |
/*
|
| 12 |
* Copyright (c) 1993-1997, Silicon Graphics, Inc.
|
| 13 |
* ALL RIGHTS RESERVED
|
| 14 |
* Permission to use, copy, modify, and distribute this software for
|
| 15 |
* any purpose and without fee is hereby granted, provided that the above
|
| 16 |
* copyright notice appear in all copies and that both the copyright notice
|
| 17 |
* and this permission notice appear in supporting documentation, and that
|
| 18 |
* the name of Silicon Graphics, Inc. not be used in advertising
|
| 19 |
* or publicity pertaining to distribution of the software without specific,
|
| 20 |
* written prior permission.
|
| 21 |
*
|
| 22 |
* THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
|
| 23 |
* AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
|
| 24 |
* INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
|
| 25 |
* FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
|
| 26 |
* GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
|
| 27 |
* SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
|
| 28 |
* KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
|
| 29 |
* LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
|
| 30 |
* THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
|
| 31 |
* ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
|
| 32 |
* ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
|
| 33 |
* POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
|
| 34 |
*
|
| 35 |
* US Government Users Restricted Rights
|
| 36 |
* Use, duplication, or disclosure by the Government is subject to
|
| 37 |
* restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
|
| 38 |
* (c)(1)(ii) of the Rights in Technical Data and Computer Software
|
| 39 |
* clause at DFARS 252.227-7013 and/or in similar or successor
|
| 40 |
* clauses in the FAR or the DOD or NASA FAR Supplement.
|
| 41 |
* Unpublished-- rights reserved under the copyright laws of the
|
| 42 |
* United States. Contractor/manufacturer is Silicon Graphics,
|
| 43 |
* Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
|
| 44 |
*
|
| 45 |
* OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
|
| 46 |
*/
|
| 47 |
'''
|
| 48 |
|
| 49 |
'''
|
| 50 |
/*
|
| 51 |
* fog.c
|
| 52 |
* This program draws 5 red spheres, each at a different
|
| 53 |
* z distance from the eye, in different types of fog.
|
| 54 |
* Pressing the f key chooses between 3 types of
|
| 55 |
* fog: exponential, exponential squared, and linear.
|
| 56 |
* In this program, there is a fixed density value, as well
|
| 57 |
* as fixed start and end values for the linear fog.
|
| 58 |
*/
|
| 59 |
'''
|
| 60 |
|
| 61 |
import sys |
| 62 |
|
| 63 |
try:
|
| 64 |
from OpenGL.GLUT import * |
| 65 |
from OpenGL.GL import * |
| 66 |
except:
|
| 67 |
print ''' |
| 68 |
ERROR: PyOpenGL not installed properly.
|
| 69 |
'''
|
| 70 |
sys.exit() |
| 71 |
|
| 72 |
|
| 73 |
# Initialize depth buffer, fog, light source,
|
| 74 |
# material property, and lighting model.
|
| 75 |
|
| 76 |
def init(): |
| 77 |
position = [ 0.5, 0.5, 3.0, 0.0 ] |
| 78 |
glEnable(GL_DEPTH_TEST) |
| 79 |
glLightfv(GL_LIGHT0, GL_POSITION, position) |
| 80 |
glEnable(GL_LIGHTING) |
| 81 |
glEnable(GL_LIGHT0) |
| 82 |
mat = [0.1745, 0.01175, 0.01175, 1.0] |
| 83 |
glMaterialfv(GL_FRONT, GL_AMBIENT, mat) |
| 84 |
mat[0] = 0.61424; mat[1] = 0.04136; mat[2] = 0.04136 |
| 85 |
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat) |
| 86 |
mat[0] = 0.727811; mat[1] = 0.626959; mat[2] = 0.626959 |
| 87 |
glMaterialfv(GL_FRONT, GL_SPECULAR, mat) |
| 88 |
glMaterialf(GL_FRONT, GL_SHININESS, 0.6*128.0) |
| 89 |
|
| 90 |
glEnable(GL_FOG) |
| 91 |
fogColor = [0.5, 0.5, 0.5, 1.0] |
| 92 |
|
| 93 |
global fogMode
|
| 94 |
fogMode = GL_EXP |
| 95 |
glFogi (GL_FOG_MODE, fogMode) |
| 96 |
glFogfv (GL_FOG_COLOR, fogColor) |
| 97 |
glFogf (GL_FOG_DENSITY, 0.35)
|
| 98 |
glHint (GL_FOG_HINT, GL_DONT_CARE) |
| 99 |
glFogf (GL_FOG_START, 1.0)
|
| 100 |
glFogf (GL_FOG_END, 5.0)
|
| 101 |
glClearColor(0.5, 0.5, 0.5, 1.0) |
| 102 |
|
| 103 |
def renderSphere(x, y, z): |
| 104 |
glPushMatrix() |
| 105 |
glTranslatef (x, y, z) |
| 106 |
glutSolidSphere(0.4, 16, 16) |
| 107 |
glPopMatrix() |
| 108 |
|
| 109 |
# display() draws 5 spheres at different z positions.
|
| 110 |
def display(): |
| 111 |
print 'redisplay' |
| 112 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) |
| 113 |
renderSphere (-2., -0.5, -1.0) |
| 114 |
renderSphere (-1., -0.5, -2.0) |
| 115 |
renderSphere (0., -0.5, -3.0) |
| 116 |
renderSphere (1., -0.5, -4.0) |
| 117 |
renderSphere (2., -0.5, -5.0) |
| 118 |
glFlush() |
| 119 |
|
| 120 |
def reshape(w, h): |
| 121 |
glViewport(0, 0, w, h) |
| 122 |
glMatrixMode(GL_PROJECTION) |
| 123 |
glLoadIdentity() |
| 124 |
if (w <= h):
|
| 125 |
glOrtho(-2.5, 2.5, -2.5*h/w, 2.5*h/w, -10.0, 10.0) |
| 126 |
else:
|
| 127 |
glOrtho(-2.5*w/h, 2.5*w/h, -2.5, 2.5, -10.0, 10.0) |
| 128 |
glMatrixMode(GL_MODELVIEW) |
| 129 |
glLoadIdentity() |
| 130 |
|
| 131 |
def keyboard(key, x, y): |
| 132 |
global fogMode
|
| 133 |
if key in ['f','F']: |
| 134 |
if (fogMode == GL_EXP):
|
| 135 |
fogMode = GL_EXP2; |
| 136 |
print "Fog mode is GL_EXP2" |
| 137 |
elif (fogMode == GL_EXP2):
|
| 138 |
fogMode = GL_LINEAR |
| 139 |
print "Fog mode is GL_LINEAR" |
| 140 |
elif (fogMode == GL_LINEAR):
|
| 141 |
fogMode = GL_EXP |
| 142 |
print "Fog mode is GL_EXP" |
| 143 |
glFogi(GL_FOG_MODE, fogMode) |
| 144 |
glutPostRedisplay() |
| 145 |
|
| 146 |
elif ord(key) == 27: |
| 147 |
sys.exit() |
| 148 |
|
| 149 |
|
| 150 |
# Main Loop
|
| 151 |
# Open window with initial window size, title bar,
|
| 152 |
# RGBA display mode, depth buffer, and handle input events.
|
| 153 |
|
| 154 |
glutInit(sys.argv) |
| 155 |
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); |
| 156 |
glutInitWindowSize(500, 500) |
| 157 |
glutCreateWindow('fog')
|
| 158 |
init() |
| 159 |
glutReshapeFunc(reshape) |
| 160 |
glutKeyboardFunc(keyboard) |
| 161 |
glutDisplayFunc(display) |
| 162 |
glutMainLoop() |