root / PyOpenGL-Demo / NeHe / lesson26.py @ 2
Historique | Voir | Annoter | Télécharger (7,48 ko)
| 1 |
#! /usr/bin/env python
|
|---|---|
| 2 |
# -*- coding: utf8 -*-
|
| 3 |
"""Port of NeHe Lesson 26 by Ivan Izuver <izuver@users.sourceforge.net>"""
|
| 4 |
from OpenGL.GL import * |
| 5 |
from OpenGL.GLUT import * |
| 6 |
from OpenGL.GLU import * |
| 7 |
from Image import * |
| 8 |
import sys,gc |
| 9 |
|
| 10 |
|
| 11 |
ESCAPE = '\033'
|
| 12 |
|
| 13 |
# Number of the glut window.
|
| 14 |
window = 0
|
| 15 |
|
| 16 |
LightAmb=(0.7,0.7,0.7) #Окружающий свет |
| 17 |
LightDif=(1.0,1.0,0.0) #Рассеянный свет |
| 18 |
LightPos=(4.0,4.0,6.0,1.0) #Позиция источника освещения |
| 19 |
#q=GLUquadricObj()
|
| 20 |
xrot=yrot=0.0 #Вращение по Х Y |
| 21 |
|
| 22 |
xrotspeed=yrotspeed=0.0 #Скорость вращения по X Y |
| 23 |
zoom=-3.0 #Глубина сцены в экране |
| 24 |
height=0.5 #Высота мяча над полом |
| 25 |
textures = {}
|
| 26 |
|
| 27 |
def LoadTextures(fname): |
| 28 |
if textures.get( fname ) is not None: |
| 29 |
return textures.get( fname )
|
| 30 |
texture = textures[fname] = glGenTextures(1)
|
| 31 |
image = open(fname)
|
| 32 |
|
| 33 |
ix = image.size[0]
|
| 34 |
iy = image.size[1]
|
| 35 |
image = image.tostring("raw", "RGBX", 0, -1) |
| 36 |
|
| 37 |
# Create Texture
|
| 38 |
glBindTexture(GL_TEXTURE_2D, texture) # 2d texture (x and y size)
|
| 39 |
|
| 40 |
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
|
| 41 |
glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image) |
| 42 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP) |
| 43 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP) |
| 44 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) |
| 45 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) |
| 46 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) |
| 47 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) |
| 48 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL) |
| 49 |
return texture
|
| 50 |
|
| 51 |
# A general OpenGL initialization function. Sets all of the initial parameters.
|
| 52 |
def InitGL(Width, Height): # We call this right after our OpenGL window is created. |
| 53 |
glClearColor(0.2, 0.5, 1.0, 1.0) # This Will Clear The Background Color To Black |
| 54 |
glClearDepth(1.0) # Enables Clearing Of The Depth Buffer |
| 55 |
glClearStencil(0)
|
| 56 |
glDepthFunc(GL_LEQUAL) # The Type Of Depth Test To Do
|
| 57 |
glEnable(GL_DEPTH_TEST) # Enables Depth Testing
|
| 58 |
glShadeModel(GL_SMOOTH) # Enables Smooth Color Shading
|
| 59 |
|
| 60 |
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) |
| 61 |
glEnable(GL_TEXTURE_2D) |
| 62 |
|
| 63 |
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmb) |
| 64 |
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDif) |
| 65 |
glLightfv(GL_LIGHT0, GL_POSITION, LightPos) |
| 66 |
glEnable(GL_LIGHT0) |
| 67 |
glEnable(GL_LIGHTING) |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
glMatrixMode(GL_PROJECTION) |
| 72 |
glLoadIdentity() # Reset The Projection Matrix
|
| 73 |
# Calculate The Aspect Ratio Of The Window
|
| 74 |
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0) |
| 75 |
|
| 76 |
glMatrixMode(GL_MODELVIEW) |
| 77 |
|
| 78 |
# The function called when our window is resized (which shouldn't happen if you enable fullscreen, below)
|
| 79 |
def ReSizeGLScene(Width, Height): |
| 80 |
if Height == 0: # Prevent A Divide By Zero If The Window Is Too Small |
| 81 |
Height = 1
|
| 82 |
|
| 83 |
glViewport(0, 0, Width, Height) # Reset The Current Viewport And Perspective Transformation |
| 84 |
glMatrixMode(GL_PROJECTION) |
| 85 |
glLoadIdentity() |
| 86 |
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0) |
| 87 |
glMatrixMode(GL_MODELVIEW) |
| 88 |
|
| 89 |
def DrawObject(): |
| 90 |
glColor3f(1.0, 1.0, 1.0); |
| 91 |
glBindTexture( GL_TEXTURE_2D, LoadTextures('NeHe.bmp') )
|
| 92 |
|
| 93 |
Q=gluNewQuadric() |
| 94 |
gluQuadricNormals(Q, GL_SMOOTH) |
| 95 |
gluQuadricTexture(Q, GL_TRUE) |
| 96 |
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP) |
| 97 |
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP) |
| 98 |
|
| 99 |
gluSphere(Q, 0.35, 32, 16) |
| 100 |
|
| 101 |
glColor4f(1.0, 1.0, 1.0, 0.4) |
| 102 |
glEnable(GL_BLEND) |
| 103 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE) |
| 104 |
glEnable(GL_TEXTURE_GEN_S) |
| 105 |
glEnable(GL_TEXTURE_GEN_T) |
| 106 |
gluSphere(Q, 0.35, 32, 16) |
| 107 |
|
| 108 |
glDisable(GL_TEXTURE_GEN_S) |
| 109 |
glDisable(GL_TEXTURE_GEN_T) |
| 110 |
glDisable(GL_BLEND) |
| 111 |
gluDeleteQuadric( Q ) |
| 112 |
|
| 113 |
def DrawFloor(): |
| 114 |
glBindTexture( GL_TEXTURE_2D, LoadTextures('NeHe2.bmp') )
|
| 115 |
|
| 116 |
glBegin(GL_QUADS) # Begin draw
|
| 117 |
|
| 118 |
glNormal3f(0.0, 1.0, 0.0) # Upper normal |
| 119 |
glTexCoord2f(0.0, 1.0) # bottom left side of texture |
| 120 |
|
| 121 |
glVertex3f(-2.0, 0.0, 2.0) # bottom left angle of floor |
| 122 |
glTexCoord2f(0.0, 0.0) # upper left side of texture |
| 123 |
|
| 124 |
glVertex3f(-2.0, 0.0,-2.0)# upper left angle of floor |
| 125 |
glTexCoord2f(1.0, 0.0) #upper right side of texture |
| 126 |
|
| 127 |
glVertex3f( 2.0, 0.0,-2.0) # upper right angle of floor |
| 128 |
glTexCoord2f(1.0, 1.0) # bottom right side of texture |
| 129 |
|
| 130 |
glVertex3f( 2.0, 0.0, 2.0)# bottom right angle of floor |
| 131 |
|
| 132 |
glEnd() # finish draw
|
| 133 |
|
| 134 |
|
| 135 |
# The main drawing function.
|
| 136 |
def DrawGLScene(): |
| 137 |
pass
|
| 138 |
# Clear The Screen And The Depth Buffer
|
| 139 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT) |
| 140 |
eqr=(0.0,-1.0, 0.0, 0.0) |
| 141 |
|
| 142 |
glLoadIdentity() # Reset The View
|
| 143 |
|
| 144 |
glTranslatef(0.0, -0.6, zoom) |
| 145 |
|
| 146 |
glColorMask(0,0,0,0) |
| 147 |
|
| 148 |
glEnable(GL_STENCIL_TEST) |
| 149 |
|
| 150 |
glStencilFunc(GL_ALWAYS, 1, 1) |
| 151 |
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE) |
| 152 |
glDisable(GL_DEPTH_TEST) |
| 153 |
|
| 154 |
DrawFloor() |
| 155 |
|
| 156 |
glEnable(GL_DEPTH_TEST) |
| 157 |
glColorMask(1,1,1,1) |
| 158 |
glStencilFunc(GL_EQUAL, 1, 1) |
| 159 |
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP) |
| 160 |
|
| 161 |
glEnable(GL_CLIP_PLANE0) |
| 162 |
glClipPlane(GL_CLIP_PLANE0, eqr) |
| 163 |
glPushMatrix() |
| 164 |
glScalef(1.0, -1.0, 1.0) |
| 165 |
|
| 166 |
glLightfv(GL_LIGHT0, GL_POSITION, LightPos) |
| 167 |
glTranslatef(0.0, height, 0.0) |
| 168 |
glRotatef(xrot, 1.0, 0.0, 0.0) |
| 169 |
glRotatef(yrot, 0.0, 1.0, 0.0) |
| 170 |
|
| 171 |
DrawObject() |
| 172 |
|
| 173 |
glPopMatrix() |
| 174 |
glDisable(GL_CLIP_PLANE0) |
| 175 |
glDisable(GL_STENCIL_TEST) |
| 176 |
|
| 177 |
glLightfv(GL_LIGHT0, GL_POSITION, LightPos) |
| 178 |
glEnable(GL_BLEND) |
| 179 |
glDisable(GL_LIGHTING) |
| 180 |
glColor4f(1.0, 1.0, 1.0, 0.8) |
| 181 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) |
| 182 |
|
| 183 |
DrawFloor() |
| 184 |
|
| 185 |
glEnable(GL_LIGHTING) |
| 186 |
glDisable(GL_BLEND) |
| 187 |
glTranslatef(0.0, height, 0.0) |
| 188 |
glRotatef(xrot, 1.0, 0.0, 0.0) |
| 189 |
glRotatef(yrot, 0.0, 1.0, 0.0) |
| 190 |
DrawObject() |
| 191 |
|
| 192 |
glFlush() |
| 193 |
|
| 194 |
# since this is double buffered, swap the buffers to display what just got drawn.
|
| 195 |
glutSwapBuffers() |
| 196 |
|
| 197 |
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)
|
| 198 |
def keyPressed(*args): |
| 199 |
global window
|
| 200 |
# If escape is pressed, kill everything.
|
| 201 |
if args[0] == ESCAPE: |
| 202 |
sys.exit() |
| 203 |
|
| 204 |
def main(): |
| 205 |
global window
|
| 206 |
# pass arguments to init
|
| 207 |
glutInit(sys.argv) |
| 208 |
|
| 209 |
# Select type of Display mode:
|
| 210 |
# Double buffer
|
| 211 |
# RGBA color
|
| 212 |
# Alpha components supported
|
| 213 |
# Depth buffer
|
| 214 |
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) |
| 215 |
|
| 216 |
# get a 640 x 480 window
|
| 217 |
glutInitWindowSize(640, 480) |
| 218 |
|
| 219 |
# the window starts at the upper left corner of the screen
|
| 220 |
glutInitWindowPosition(0, 0) |
| 221 |
|
| 222 |
# Okay, like the C version we retain the window id to use when closing, but for those of you new
|
| 223 |
# to Python (like myself), remember this assignment would make the variable local and not global
|
| 224 |
# if it weren't for the global declaration at the start of main.
|
| 225 |
window = glutCreateWindow("Realistic Reflection by RISC")
|
| 226 |
|
| 227 |
# Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
|
| 228 |
# set the function pointer and invoke a function to actually register the callback, otherwise it
|
| 229 |
# would be very much like the C version of the code.
|
| 230 |
glutDisplayFunc(DrawGLScene) |
| 231 |
|
| 232 |
# Uncomment this line to get full screen.
|
| 233 |
#glutFullScreen()
|
| 234 |
|
| 235 |
# When we are doing nothing, redraw the scene.
|
| 236 |
glutIdleFunc(DrawGLScene) |
| 237 |
|
| 238 |
# Register the function called when our window is resized.
|
| 239 |
glutReshapeFunc(ReSizeGLScene) |
| 240 |
|
| 241 |
# Register the function called when the keyboard is pressed.
|
| 242 |
glutKeyboardFunc(keyPressed) |
| 243 |
|
| 244 |
# Initialize our window.
|
| 245 |
InitGL(640, 480) |
| 246 |
|
| 247 |
# Start Event Processing Engine
|
| 248 |
glutMainLoop() |
| 249 |
|
| 250 |
# Print message to console, and kick off the main to get it rolling.
|
| 251 |
if __name__ == "__main__": |
| 252 |
print "Hit ESC key to quit." |
| 253 |
main() |
| 254 |
|