root / PyOpenGL-Demo / NeHe / lesson11.py @ 2
Historique | Voir | Annoter | Télécharger (5,44 ko)
| 1 |
#! /usr/bin/env python
|
|---|---|
| 2 |
# -*- coding: utf8 -*-
|
| 3 |
# Conversion contributed by: Ivan Izuver <izuver@users.sourceforge.net>
|
| 4 |
from OpenGL.GL import * |
| 5 |
from OpenGL.GLU import * |
| 6 |
from OpenGL.GLUT import * |
| 7 |
|
| 8 |
from math import sin |
| 9 |
|
| 10 |
import sys |
| 11 |
import numpy # or Numeric |
| 12 |
|
| 13 |
|
| 14 |
ESCAPE = '\033'
|
| 15 |
|
| 16 |
w_count=0
|
| 17 |
hold = 0.0
|
| 18 |
|
| 19 |
# Number of the glut window.
|
| 20 |
window = 0
|
| 21 |
|
| 22 |
# 3D array
|
| 23 |
points = numpy.zeros((46, 46, 3), 'f') |
| 24 |
|
| 25 |
# rotation
|
| 26 |
xrot = yrot = zrot = 0.0
|
| 27 |
|
| 28 |
# A general OpenGL initialization function. Sets all of the initial parameters.
|
| 29 |
def InitGL(Width, Height): # We call this right after our OpenGL window is created. |
| 30 |
|
| 31 |
glClearColor(0.0, 0.0, 0.0, 0.0) # This Will Clear The Background Color To Black |
| 32 |
glClearDepth(1.0) # Enables Clearing Of The Depth Buffer |
| 33 |
glDepthFunc(GL_LESS) # The Type Of Depth Test To Do
|
| 34 |
glEnable(GL_DEPTH_TEST) # Enables Depth Testing
|
| 35 |
glShadeModel(GL_SMOOTH) # Enables Smooth Color Shading
|
| 36 |
|
| 37 |
glPolygonMode(GL_BACK,GL_FILL) |
| 38 |
glPolygonMode(GL_FRONT,GL_LINE) |
| 39 |
|
| 40 |
# walk on plane X
|
| 41 |
for x in xrange(45): |
| 42 |
# walk on plane Y
|
| 43 |
for y in xrange(45): |
| 44 |
# apply wave to grid
|
| 45 |
points[x][y][0]=float((x/5.0)-4.5) |
| 46 |
points[x][y][1]=float((y/5.0)-4.5) |
| 47 |
points[x][y][2]=float(sin((((x/5.0)*40.0)/360.0)*3.141592654*2.0)) |
| 48 |
|
| 49 |
|
| 50 |
glMatrixMode(GL_PROJECTION) |
| 51 |
glLoadIdentity() # Reset The Projection Matrix
|
| 52 |
# Calculate The Aspect Ratio Of The Window
|
| 53 |
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0) |
| 54 |
|
| 55 |
glMatrixMode(GL_MODELVIEW) |
| 56 |
|
| 57 |
# The function called when our window is resized (which shouldn't happen if you enable fullscreen, below)
|
| 58 |
def ReSizeGLScene(Width, Height): |
| 59 |
if Height == 0: # Prevent A Divide By Zero If The Window Is Too Small |
| 60 |
Height = 1
|
| 61 |
|
| 62 |
glViewport(0, 0, Width, Height) # Reset The Current Viewport And Perspective Transformation |
| 63 |
glMatrixMode(GL_PROJECTION) |
| 64 |
glLoadIdentity() |
| 65 |
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0) |
| 66 |
glMatrixMode(GL_MODELVIEW) |
| 67 |
|
| 68 |
# The main drawing function.
|
| 69 |
def DrawGLScene(): |
| 70 |
global xrot,yrot,zrot,w_count
|
| 71 |
|
| 72 |
float_x=float_y=float_xb=float_yb=0.0 # for division our flag on small quads |
| 73 |
|
| 74 |
# Clear The Screen And The Depth Buffer
|
| 75 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) |
| 76 |
glLoadIdentity() # Reset The View
|
| 77 |
|
| 78 |
glTranslatef(0.0,0.0,-12.0) |
| 79 |
|
| 80 |
glRotatef(xrot,1.0,0.0,0.0) # rotate by x |
| 81 |
glRotatef(yrot,0.0,1.0,0.0) # rotate by y |
| 82 |
glRotatef(zrot,0.0,0.0,1.0) # rotate by z |
| 83 |
|
| 84 |
# build the wave
|
| 85 |
glBegin(GL_QUADS) |
| 86 |
for x in xrange(45): |
| 87 |
for y in xrange(45): |
| 88 |
float_x = float(x)/44.0 |
| 89 |
float_y = float(y)/44.0 |
| 90 |
float_xb = float(x+1)/44.0 |
| 91 |
float_yb = float(y+1)/44.0 |
| 92 |
|
| 93 |
# bottom left
|
| 94 |
glTexCoord2f(float_x, float_y) |
| 95 |
glColor3f(0.0,0.0,0.0) |
| 96 |
glVertex3f(points[x][y][0],points[x][y][1],points[x][y][2]) |
| 97 |
|
| 98 |
# upper left
|
| 99 |
glTexCoord2f( float_x, float_yb ) |
| 100 |
glColor3f(0.0,0.5,0.0) # set grid color |
| 101 |
glVertex3f( points[x][y+1][0], points[x][y+1][1], points[x][y+1][2]) |
| 102 |
|
| 103 |
# upper right
|
| 104 |
glTexCoord2f( float_xb, float_yb ) |
| 105 |
glVertex3f( points[x+1][y+1][0], points[x+1][y+1][1], points[x+1][y+1][2]) |
| 106 |
|
| 107 |
# bottom right
|
| 108 |
glTexCoord2f( float_xb, float_y ) |
| 109 |
glVertex3f( points[x+1][y][0], points[x+1][y][1], points[x+1][y][2]) |
| 110 |
|
| 111 |
glEnd() |
| 112 |
|
| 113 |
if w_count==2: # for slowing the wave |
| 114 |
for y in xrange(45): # walk on plane Y |
| 115 |
hold=points[0][y][2] # save the current value of left side one point |
| 116 |
for x in xrange(45): # walk on plane Y |
| 117 |
points[x][y][2] = points[x+1][y][2] |
| 118 |
points[44][y][2]=hold |
| 119 |
w_count = 1
|
| 120 |
w_count+=1
|
| 121 |
|
| 122 |
# uncomment if need
|
| 123 |
#xrot+=0.6
|
| 124 |
#yrot+=0.5
|
| 125 |
#zrot+=0.8
|
| 126 |
# since this is double buffered, swap the buffers to display what just got drawn.
|
| 127 |
glutSwapBuffers() |
| 128 |
|
| 129 |
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)
|
| 130 |
def keyPressed(*args): |
| 131 |
global window
|
| 132 |
# If escape is pressed, kill everything.
|
| 133 |
if args[0] == ESCAPE: |
| 134 |
sys.exit() |
| 135 |
|
| 136 |
def main(): |
| 137 |
global window
|
| 138 |
# pass arguments to init
|
| 139 |
glutInit(sys.argv) |
| 140 |
|
| 141 |
# Select type of Display mode:
|
| 142 |
# Double buffer
|
| 143 |
# RGBA color
|
| 144 |
# Alpha components supported
|
| 145 |
# Depth buffer
|
| 146 |
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) |
| 147 |
|
| 148 |
# get a 640 x 480 window
|
| 149 |
glutInitWindowSize(800, 600) |
| 150 |
|
| 151 |
# the window starts at the upper left corner of the screen
|
| 152 |
glutInitWindowPosition(200, 200) |
| 153 |
|
| 154 |
# Okay, like the C version we retain the window id to use when closing, but for those of you new
|
| 155 |
# to Python (like myself), remember this assignment would make the variable local and not global
|
| 156 |
# if it weren't for the global declaration at the start of main.
|
| 157 |
window = glutCreateWindow("Wave by RISC")
|
| 158 |
|
| 159 |
# Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
|
| 160 |
# set the function pointer and invoke a function to actually register the callback, otherwise it
|
| 161 |
# would be very much like the C version of the code.
|
| 162 |
glutDisplayFunc(DrawGLScene) |
| 163 |
|
| 164 |
# Uncomment this line to get full screen.
|
| 165 |
#glutFullScreen()
|
| 166 |
|
| 167 |
# When we are doing nothing, redraw the scene.
|
| 168 |
glutIdleFunc(DrawGLScene) |
| 169 |
|
| 170 |
# Register the function called when our window is resized.
|
| 171 |
glutReshapeFunc(ReSizeGLScene) |
| 172 |
|
| 173 |
# Register the function called when the keyboard is pressed.
|
| 174 |
glutKeyboardFunc(keyPressed) |
| 175 |
|
| 176 |
# Initialize our window.
|
| 177 |
InitGL(640, 480) |
| 178 |
|
| 179 |
# Start Event Processing Engine
|
| 180 |
glutMainLoop() |
| 181 |
|
| 182 |
# Print message to console, and kick off the main to get it rolling.
|
| 183 |
if __name__ == "__main__": |
| 184 |
print "Hit ESC key to quit." |
| 185 |
main() |