Statistics
| Revision:

root / PyOpenGL-Demo / GLUT / molehill.py @ 1

History | View | Annotate | Download (4.8 kB)

1
#! /usr/bin/env python
2
"""
3
/* Copyright (c) Mark J. Kilgard, 1995. */
4

5
/* This program is freely distributable without licensing fees 
6
   and is provided without guarantee or warrantee expressed or 
7
   implied. This program is -not- in the public domain. */
8

9
/* molehill uses the GLU NURBS routines to draw some nice surfaces. */
10
"""
11

    
12
import string
13
__version__ = string.split('$Revision: 1.4 $')[1]
14
__date__ = string.join(string.split('$Date: 2008/09/05 20:23:33 $')[1:3], ' ')
15
__author__ = 'Tarn Weisner Burton <twburton@users.sourceforge.net>'
16

    
17
import OpenGL 
18
OpenGL.ERROR_ON_COPY = True 
19

    
20
#
21
# Ported to PyOpenGL 2.0 by Tarn Weisner Burton 10May2001
22

    
23
from OpenGL.GL import *
24
from OpenGL.GLU import *
25
from OpenGL.GLUT import *
26
vec3 = GLfloat_3
27
vec4 = GLfloat_4
28
try:
29
        import numpy 
30
except ImportError, err:
31
        import Numeric as numpy
32
import sys
33

    
34
array = numpy.array 
35

    
36
THE_LIST = None
37

    
38

    
39
def display():
40
        glutSetWindow( context )
41
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
42
        glEnable(GL_LIGHT0)
43
        glEnable(GL_LIGHTING)
44
        glDisable( GL_CULL_FACE )
45
        glEnable(GL_DEPTH_TEST)
46
        glCallList(THE_LIST)
47
        glutSwapBuffers()
48

    
49

    
50
def main():
51
        mat_red_diffuse = numpy.array( ( 0.7, 0.0, 0.1, 1.0 ), 'f' )
52
        mat_green_diffuse = numpy.array(( 0.0, 0.7, 0.1, 1.0 ),'f')
53
        mat_blue_diffuse = numpy.array(( 0.0, 0.1, 0.7, 1.0 ),'f')
54
        mat_yellow_diffuse = numpy.array(( 0.7, 0.8, 0.1, 1.0 ),'f')
55
        mat_specular = numpy.array(( 1.0, 1.0, 1.0, 1.0 ),'f')
56
        mat_shininess = GLfloat( 100.0 )
57
        knots = numpy.array( (0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0),'f' )
58

    
59
        glutInit(sys.argv)
60
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
61
        global context
62
        context = glutCreateWindow('molehill')
63
        glutSetWindow( context )
64

    
65

    
66
        nurb = gluNewNurbsRenderer()
67
        # get a really good sampling
68
        gluNurbsProperty(nurb, GLU_SAMPLING_TOLERANCE, 5.0)
69
        gluNurbsProperty(nurb, GLU_DISPLAY_MODE, GLU_FILL)
70
#        gluNurbsProperty(nurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON)
71
        
72

    
73
        # Build control points for NURBS mole hills. 
74
        pts1 = []
75
        pts2 = []
76
        pts3 = []
77
        pts4 = []
78

    
79
        for u in range(4):
80
                pts1.append([])
81
                pts2.append([])
82
                pts3.append([])
83
                pts4.append([])
84
                for v in range(4):
85
                        # Red. 
86
                        pts1[u].append([2.0*u, 2.0*v, 0.0])
87
                        if (u == 1 or u == 2) and (v == 1 or v == 2):
88
                                pts1[u][v][2] = 6.0
89
        
90
                        # Green. 
91
                        pts2[u].append([2.0*u - 6.0, 2.0*v - 6.0, 0.0])
92
                        if (u == 1 or u == 2) and (v == 1 or v == 2):
93
                                if u == 1 and v == 1: 
94
                                        # Pull hard on single middle square. 
95
                                        pts2[u][v][2] = 15.0
96
                                else:
97
                                        # Push down on other middle squares. 
98
                                        pts2[u][v][2] = -2.0
99
        
100
                        # Blue.
101
                        pts3[u].append([2.0*u - 6.0, 2.0*v, 0.0])
102
                        if (u == 1 or u == 2) and (v == 1 or v == 2):
103
                                if u == 1 and v == 2: 
104
                                        # Pull up on single middple square. 
105
                                        pts3[u][v][2] = 11.0
106
                                else:
107
                                        # Pull up slightly on other middle squares.
108
                                        pts3[u][v][2] = 2.0
109
        
110
                        # Yellow. 
111
                        pts4[u].append([2.0*u, 2.0*v - 6.0, 0.0])
112
                        if u != 0 and (v == 1 or v == 2):
113
                                if v == 1: 
114
                                        # Push down front middle and right squares. 
115
                                        pts4[u][v][2] = -2.0
116
                                else:
117
                                        # Pull up back middle and right squares. 
118
                                        pts4[u][v][2] = 5.0
119
        
120

    
121
        # Stretch up red's far right corner. 
122
        pts1[3][3][2] = 6.0
123
        # Pull down green's near left corner a little. 
124
        pts2[0][0][2] = -2.0
125
        # Turn up meeting of four corners. 
126
        pts1[0][0][2] = 1.0
127
        pts2[3][3][2] = 1.0
128
        pts3[3][0][2] = 1.0
129
        pts4[0][3][2] = 1.0
130
        
131
        pts1,pts2,pts3,pts4 = array(pts1,'f'),array(pts2,'f'),array(pts3,'f'),array(pts4,'f')
132
        glMatrixMode(GL_PROJECTION)
133
        gluPerspective(55.0, 1.0, 2.0, 24.0)
134
        glMatrixMode(GL_MODELVIEW)
135
        glTranslatef(0.0, 0.0, -15.0)
136
        glRotatef(330.0, 1.0, 0.0, 0.0)
137
        
138
        global THE_LIST
139
        THE_LIST = glGenLists( 1 )
140
        glNewList(THE_LIST, GL_COMPILE)
141
        
142
        glEnable(GL_NORMALIZE)
143
        glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular)
144
        glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess)
145

    
146
        # Render red hill. 
147
        glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_red_diffuse)
148
        gluBeginSurface(nurb)
149
        gluNurbsSurface(nurb, knots, knots, pts1, GL_MAP2_NORMAL)
150
        gluNurbsSurface(nurb, knots, knots, pts1, GL_MAP2_VERTEX_3)
151
        gluEndSurface(nurb)
152

    
153
        # Render green hill. 
154
        glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_green_diffuse)
155
        gluBeginSurface(nurb)
156
        gluNurbsSurface(nurb, knots, knots, pts2, GL_MAP2_NORMAL)
157
        gluNurbsSurface(nurb, knots, knots, pts2, GL_MAP2_VERTEX_3)
158
        gluEndSurface(nurb)
159

    
160
        # Render blue hill. 
161
        glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_blue_diffuse)
162
        gluBeginSurface(nurb)
163
        gluNurbsSurface(nurb, knots, knots, pts3, GL_MAP2_NORMAL)
164
        gluNurbsSurface(nurb, knots, knots, pts3, GL_MAP2_VERTEX_3)
165
        gluEndSurface(nurb)
166

    
167
        # Render yellow hill. 
168
        glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_yellow_diffuse)
169
        gluBeginSurface(nurb)
170
        gluNurbsSurface(nurb, knots, knots, pts4, GL_MAP2_NORMAL)
171
        gluNurbsSurface(nurb, knots, knots, pts4, GL_MAP2_VERTEX_3)
172
        gluEndSurface(nurb)
173
        glEndList()
174

    
175
        glutDisplayFunc(display)
176

    
177

    
178
if __name__ == '__main__':
179
        try:
180
                GLU_VERSION_1_2
181
        except:
182
                print "Need GLU 1.2 to run this demo"
183
                sys.exit(1)
184
        main()
185
        glutMainLoop()
186