root / PyOpenGL-Demo / NeHe / lesson48 / Lesson48.py @ 2
Historique | Voir | Annoter | Télécharger (5,44 ko)
| 1 |
|
|---|---|
| 2 |
from OpenGL.GL import * |
| 3 |
from OpenGL.GLUT import * |
| 4 |
from OpenGL.GLU import * |
| 5 |
import sys |
| 6 |
import copy |
| 7 |
from math import cos, sin |
| 8 |
|
| 9 |
from ArcBall import * # ArcBallT and this tutorials set of points/vectors/matrix types |
| 10 |
|
| 11 |
PI2 = 2.0*3.1415926535 # 2 * PI (not squared!) // PI Squared |
| 12 |
|
| 13 |
# *********************** Globals ***********************
|
| 14 |
# Python 2.2 defines these directly
|
| 15 |
try:
|
| 16 |
True
|
| 17 |
except NameError: |
| 18 |
True = 1==1 |
| 19 |
False = 1==0 |
| 20 |
|
| 21 |
g_Transform = Matrix4fT () |
| 22 |
g_LastRot = Matrix3fT () |
| 23 |
g_ThisRot = Matrix3fT () |
| 24 |
|
| 25 |
g_ArcBall = ArcBallT (640, 480) |
| 26 |
g_isDragging = False
|
| 27 |
g_quadratic = None
|
| 28 |
|
| 29 |
|
| 30 |
# A general OpenGL initialization function. Sets all of the initial parameters.
|
| 31 |
def Initialize (Width, Height): # We call this right after our OpenGL window is created. |
| 32 |
global g_quadratic
|
| 33 |
|
| 34 |
glClearColor(0.0, 0.0, 0.0, 1.0) # This Will Clear The Background Color To Black |
| 35 |
glClearDepth(1.0) # Enables Clearing Of The Depth Buffer |
| 36 |
glDepthFunc(GL_LEQUAL) # The Type Of Depth Test To Do
|
| 37 |
glEnable(GL_DEPTH_TEST) # Enables Depth Testing
|
| 38 |
glShadeModel (GL_FLAT); # Select Flat Shading (Nice Definition Of Objects)
|
| 39 |
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # Really Nice Perspective Calculations
|
| 40 |
|
| 41 |
g_quadratic = gluNewQuadric(); |
| 42 |
gluQuadricNormals(g_quadratic, GLU_SMOOTH); |
| 43 |
gluQuadricDrawStyle(g_quadratic, GLU_FILL); |
| 44 |
# Why? this tutorial never maps any textures?! ?
|
| 45 |
# gluQuadricTexture(g_quadratic, GL_TRUE); # // Create Texture Coords
|
| 46 |
|
| 47 |
glEnable (GL_LIGHT0) |
| 48 |
glEnable (GL_LIGHTING) |
| 49 |
|
| 50 |
glEnable (GL_COLOR_MATERIAL) |
| 51 |
|
| 52 |
return True |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
def Upon_Drag (cursor_x, cursor_y): |
| 58 |
""" Mouse cursor is moving
|
| 59 |
Glut calls this function (when mouse button is down)
|
| 60 |
and pases the mouse cursor postion in window coords as the mouse moves.
|
| 61 |
"""
|
| 62 |
global g_isDragging, g_LastRot, g_Transform, g_ThisRot
|
| 63 |
|
| 64 |
if (g_isDragging):
|
| 65 |
mouse_pt = Point2fT (cursor_x, cursor_y) |
| 66 |
ThisQuat = g_ArcBall.drag (mouse_pt) # // Update End Vector And Get Rotation As Quaternion
|
| 67 |
g_ThisRot = Matrix3fSetRotationFromQuat4f (ThisQuat) # // Convert Quaternion Into Matrix3fT
|
| 68 |
# Use correct Linear Algebra matrix multiplication C = A * B
|
| 69 |
g_ThisRot = Matrix3fMulMatrix3f (g_LastRot, g_ThisRot) # // Accumulate Last Rotation Into This One
|
| 70 |
g_Transform = Matrix4fSetRotationFromMatrix3f (g_Transform, g_ThisRot) # // Set Our Final Transform's Rotation From This One
|
| 71 |
return
|
| 72 |
|
| 73 |
def Upon_Click (button, button_state, cursor_x, cursor_y): |
| 74 |
""" Mouse button clicked.
|
| 75 |
Glut calls this function when a mouse button is
|
| 76 |
clicked or released.
|
| 77 |
"""
|
| 78 |
global g_isDragging, g_LastRot, g_Transform, g_ThisRot
|
| 79 |
|
| 80 |
g_isDragging = False
|
| 81 |
if (button == GLUT_RIGHT_BUTTON and button_state == GLUT_UP): |
| 82 |
# Right button click
|
| 83 |
g_LastRot = Matrix3fSetIdentity (); # // Reset Rotation
|
| 84 |
g_ThisRot = Matrix3fSetIdentity (); # // Reset Rotation
|
| 85 |
g_Transform = Matrix4fSetRotationFromMatrix3f (g_Transform, g_ThisRot); # // Reset Rotation
|
| 86 |
elif (button == GLUT_LEFT_BUTTON and button_state == GLUT_UP): |
| 87 |
# Left button released
|
| 88 |
g_LastRot = copy.copy (g_ThisRot); # // Set Last Static Rotation To Last Dynamic One
|
| 89 |
elif (button == GLUT_LEFT_BUTTON and button_state == GLUT_DOWN): |
| 90 |
# Left button clicked down
|
| 91 |
g_LastRot = copy.copy (g_ThisRot); # // Set Last Static Rotation To Last Dynamic One
|
| 92 |
g_isDragging = True # // Prepare For Dragging |
| 93 |
mouse_pt = Point2fT (cursor_x, cursor_y) |
| 94 |
g_ArcBall.click (mouse_pt); # // Update Start Vector And Prepare For Dragging
|
| 95 |
|
| 96 |
return
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
def Torus(MinorRadius, MajorRadius): |
| 101 |
# // Draw A Torus With Normals
|
| 102 |
glBegin( GL_TRIANGLE_STRIP ); # // Start A Triangle Strip
|
| 103 |
for i in xrange (20): # // Stacks |
| 104 |
for j in xrange (-1, 20): # // Slices |
| 105 |
# NOTE, python's definition of modulus for negative numbers returns
|
| 106 |
# results different than C's
|
| 107 |
# (a / d)*d + a % d = a
|
| 108 |
if (j < 0): |
| 109 |
wrapFrac = (-j%20)/20.0 |
| 110 |
wrapFrac *= -1.0
|
| 111 |
else:
|
| 112 |
wrapFrac = (j%20)/20.0; |
| 113 |
phi = PI2*wrapFrac; |
| 114 |
sinphi = sin(phi); |
| 115 |
cosphi = cos(phi); |
| 116 |
|
| 117 |
r = MajorRadius + MinorRadius*cosphi; |
| 118 |
|
| 119 |
glNormal3f (sin(PI2*(i%20+wrapFrac)/20.0)*cosphi, sinphi, cos(PI2*(i%20+wrapFrac)/20.0)*cosphi); |
| 120 |
glVertex3f (sin(PI2*(i%20+wrapFrac)/20.0)*r, MinorRadius*sinphi, cos(PI2*(i%20+wrapFrac)/20.0)*r); |
| 121 |
|
| 122 |
glNormal3f (sin(PI2*(i+1%20+wrapFrac)/20.0)*cosphi, sinphi, cos(PI2*(i+1%20+wrapFrac)/20.0)*cosphi); |
| 123 |
glVertex3f (sin(PI2*(i+1%20+wrapFrac)/20.0)*r, MinorRadius*sinphi, cos(PI2*(i+1%20+wrapFrac)/20.0)*r); |
| 124 |
glEnd(); # // Done Torus
|
| 125 |
return
|
| 126 |
|
| 127 |
def Draw (): |
| 128 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); # // Clear Screen And Depth Buffer
|
| 129 |
glLoadIdentity(); # // Reset The Current Modelview Matrix
|
| 130 |
glTranslatef(-1.5,0.0,-6.0); # // Move Left 1.5 Units And Into The Screen 6.0 |
| 131 |
|
| 132 |
glPushMatrix(); # // NEW: Prepare Dynamic Transform
|
| 133 |
glMultMatrixf(g_Transform); # // NEW: Apply Dynamic Transform
|
| 134 |
glColor3f(0.75,0.75,1.0); |
| 135 |
Torus(0.30,1.00); |
| 136 |
glPopMatrix(); # // NEW: Unapply Dynamic Transform
|
| 137 |
|
| 138 |
glLoadIdentity(); # // Reset The Current Modelview Matrix
|
| 139 |
glTranslatef(1.5,0.0,-6.0); # // Move Right 1.5 Units And Into The Screen 7.0 |
| 140 |
|
| 141 |
glPushMatrix(); # // NEW: Prepare Dynamic Transform
|
| 142 |
glMultMatrixf(g_Transform); # // NEW: Apply Dynamic Transform
|
| 143 |
glColor3f(1.0,0.75,0.75); |
| 144 |
gluSphere(g_quadratic,1.3,20,20); |
| 145 |
glPopMatrix(); # // NEW: Unapply Dynamic Transform
|
| 146 |
|
| 147 |
glFlush (); # // Flush The GL Rendering Pipeline
|
| 148 |
glutSwapBuffers() |
| 149 |
return
|
| 150 |
|