/
nehe4.py
92 lines (77 loc) · 3.05 KB
/
nehe4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#! /usr/bin/env python
'''=Animating Rotation of a Polygon (NeHe 4)=
[nehe4.py-screen-0001.png Screenshot]
This tutorial uses the (legacy) OpenGL glRotated function to
setup a model-view matrix which animates the spin of geometry
about an axis.
This tutorial is based on the [http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=04 NeHe4 tutorial] by Jeff Molofee and assumes that you are reading along
with the tutorial, so that only changes from the tutorial are noted
here.
'''
from OpenGLContext import testingcontext
BaseContext = testingcontext.getInteractive()
from OpenGL.GL import *
'''The time module is used to provide crude animation support'''
import time
class TestContext( BaseContext ):
"""This context customizes 3 points in the BaseContext"""
initialPosition = (0,0,0) # set initial camera position, tutorial does the re-positioning
'''
The OnIdle method (if present) is called whenever the GUI library
has completed all pending event processing and signals an "idle"
state. By calling:
self.triggerRedraw( force = 1 )
We force a redraw of the context to show the next "frame" of the
animation.
'''
def OnIdle( self, ):
"""Request refresh of the context whenever idle"""
self.triggerRedraw(1)
return 1
def Render( self, mode = 0):
"""Render the scene geometry"""
BaseContext.Render( self, mode )
glDisable( GL_LIGHTING) # context lights by default
glDisable( GL_CULL_FACE)
glTranslatef(-1.5,0.0,-6.0);
'''The call to time.time creates a float value which is
converted to a fraction of three seconds then multiplied
by 360 (degrees) to get the current appropriate rotation
for an object spinning at 1/3 rps.
Note that OpenGL uses *degrees*, not radians!
'''
glRotated( time.time()%(3.0)/3 * 360, 0,1,0)
glBegin(GL_TRIANGLES)
glColor3f(1,0,0)
glVertex3f( 0.0, 1.0, 0.0)
glColor3f(0,1,0)
glVertex3f(-1.0, -1.0, 0.0)
glColor3f(0,0,1)
glVertex3f( 1.0, -1.0, 0.0)
glEnd()
'''Note the need to re-load the identity matrix, as the
glRotated/glTranslatef functions modify the current matrix.
'''
glLoadIdentity()
glTranslatef(1.5,0.0,-6.0);
'''Animating as above, but at 1 rev/s'''
glRotated( time.time()%(1.0)/1 * -360, 1,0,0)
glColor3f(0.5,0.5,1.0)
glBegin(GL_QUADS)
glVertex3f(-1.0, 1.0, 0.0)
glVertex3f( 1.0, 1.0, 0.0)
glVertex3f( 1.0,-1.0, 0.0)
glVertex3f(-1.0,-1.0, 0.0)
glEnd()
if __name__ == "__main__":
TestContext.ContextMainLoop()
'''
Author: [http://nehe.gamedev.net Jeff Molofee (aka NeHe)]
COPYRIGHT AND DISCLAIMER: (c)2000 Jeff Molofee
If you plan to put this program on your web page or a cdrom of
any sort, let me know via email, I'm curious to see where
it ends up :)
If you use the code for your own projects please give me
credit, or mention my web site somewhere in your program
or it's docs.
'''