root / trunk / pysoy / examples / liquid_example.py

Revision 1356, 8.0 kB (checked in by ArcRiley, 4 months ago)

Ticket #962 :

  • eliminated soy.bodies.Body._calcFogCoords
  • fixed some obvious problems with liquid_example.py
  • still renders just black
Line 
1#!/usr/bin/env python
2
3import soy
4import cmath
5from time import sleep, time
6
7def calculate_trig_normal(v1,v2,v3):
8    v_1 = (v2[0]-v1[0], v2[1]-v1[1], v2[2]-v1[2])
9    v_2 = (v3[0]-v1[0], v3[1]-v1[1], v3[2]-v1[2])
10    cross_a = (v_1[1] * v_2[2] - v_1[2] * v_2[1],
11               v_1[2] * v_2[0] - v_1[0] * v_2[2],
12               v_1[0] * v_2[1] - v_1[1] * v_2[0])
13    lgt = abs( cmath.sqrt( cross_a[0]**2 + cross_a[1]**2 + cross_a[2]**2))
14    return (cross_a[0] / lgt, cross_a[1]  / lgt, cross_a[2] / lgt)
15   
16
17
18def create_bottom_mesh():
19    V = soy.atoms.Vertex
20    F = soy.atoms.Face
21    #SIMPLE BOX:
22    bottom_vertices = [
23            (-5,-1.5, 5),
24            ( 5,-1.5, 5),
25            (-5, 1.5, 5),
26            ( 5, 1.5, 5),
27            (-5,-1.5,-5),
28            ( 5,-1.5,-5),
29            (-5, 1.5,-5),
30            ( 5, 1.5,-5),
31            ]
32    bottom_normals = [
33            ( 0, 0, 1), #FRONT
34            ( 0, 1, 0), #BOTTOM
35            ( 1, 0, 0), #RIGHT
36            (-1, 0, 0), #LEFT
37            ( 0, 0,-1), #BACK
38            ]
39
40    bottom_indices = [
41            ( 2, 1, 0), #FRONT
42            ( 3, 1, 2),
43            ( 0, 1, 4), #BOTTOM
44            ( 4, 1, 5),
45            ( 1, 3, 7), #RIGHT
46            ( 1, 7, 5),
47            ( 0, 6, 2), #LEFT
48            ( 0, 4, 6),
49            ( 4, 7, 6), #BACK
50            ( 4, 5, 7)
51            ]
52
53    bottom_faces = [
54            #TRIG1, TRIG2, NORMAL
55            ( 0, 1, 0), #FRONT
56            ( 2, 3, 1), #BOTTOM
57            ( 4, 5, 2), #RIGHT
58            ( 6, 7, 3), #LEFT
59            ( 8, 9, 4), #BACK
60            ]
61
62    #lava = soy.transports.File('media/lava.soy')['gimp']
63    #lavam = soy.materials.Material(lava)
64    bottom = soy.models.Mesh(material=soy.materials.stainlessSteel)
65
66    V1 = lambda x : V(bottom, position=x[0])
67    F1 = lambda x : F(bottom, verts=x, material=soy.materials.stainlessSteel)
68
69    #verts = map(V1, bottom_vertices)
70    #face_vert = [ map( lambda x: verts[x], x) for x in bottom_indices]
71    #faces = map(F1, face_vert)
72
73    for f in bottom_faces:
74        normal = bottom_normals[ f[2] ]
75
76        V2 = lambda x : V(bottom, position=x, normal=normal)
77        tri1   = [ bottom_vertices[x] for x in bottom_indices[f[0]] ]
78        tri2   = [ bottom_vertices[x] for x in bottom_indices[f[1]] ]
79
80        tri1_v = map(V2, tri1)
81        tri2_v = map(V2, tri2)
82
83        F1(tri1_v)
84        F1(tri2_v)
85
86    return bottom
87
88def create_simple_test_mesh(mat=soy.materials.stainlessSteel):
89    V = soy.atoms.Vertex
90    F = soy.atoms.Face
91
92    mesh_vertices = [
93            (0,0,0),
94            (0,10,0),
95            (1,0,0),
96            (0,0,-1)
97            ]
98
99    mesh_indices = [
100            (0,1,2),
101            (0,2,3),
102            (3,1,0),
103            (1,3,2)
104            ]
105
106    mesh = soy.models.Mesh(material=mat)
107
108    V1 = lambda x,y : V(mesh, position=x, normal=y)
109    F1 = lambda x : F(mesh, verts=x, material=mat)
110
111    #verts =
112    for f in mesh_indices:
113        v1 = mesh_vertices[ f[0] ]
114        v2 = mesh_vertices[ f[1] ]
115        v3 = mesh_vertices[ f[2] ]
116        n = calculate_trig_normal(v1,v2,v3)
117
118        vv1 = V1(v1,n)
119        vv2 = V1(v2,n)
120        vv3 = V1(v3,n)
121
122        face = F1([vv1,vv3,vv2])
123
124    return mesh
125
126class T: #empty class to hold camera data by-reference
127    def __init__(self):
128        self.move_forward = False
129        self.move_backward = False
130        self.move_left = False
131        self.move_right = False
132        self.move_up = False
133        self.move_down = False
134
135        self.forward_vel = 0.5
136        self.side_vel = 0.3
137
138def set_variable_field(var, field, val):
139    def mth():
140        setattr(var,field,val)
141    return mth
142
143def main():
144    sce = soy.scenes.Scene()
145
146    cam = soy.bodies.Camera(sce)
147    cam.position = (0.0, 3.0, 10.0)
148
149    camera_data = T()
150
151    def camera_add_rot( vec):
152        def rot_camera():
153            cam.rotation = (cam.rotation[0] + vec[0], cam.rotation[1]+vec[1], cam.rotation[2]+vec[2])
154        return rot_camera
155
156    lig = soy.bodies.Light(sce)
157    lig.position = (-10.0,1.0,-2.0)
158
159    water = soy.transports.File('media/water.soy')['gimp']
160
161    water_mat = soy.materials.Material(colorMap=water)
162    water_mat.ambient = soy.colors.blue
163    water_mat.diffuse = soy.colors.blue
164    water_mat.specular = soy.colors.black
165
166    liquid = soy.models.Liquid(material=water_mat)
167    liquid_b = soy.bodies.Body(sce, mesh=liquid)
168    liquid_b.position = (0, -1, 0)
169
170    liquid.size = (10, 3, 10)
171
172    bottom = create_bottom_mesh()
173    bottom_b = soy.bodies.Body(sce, mesh=bottom)
174    bottom_b.position = (0,-1,0)
175
176    test_mesh = create_simple_test_mesh()
177    test_mesh_b = soy.bodies.Body(sce, mesh=test_mesh)
178    test_mesh_b.position = (0, -6, 0)
179
180
181    scr = soy.Screen()
182    win = soy.Window(scr, 'Liquid example', size=(1024,768))
183
184    pro = soy.widgets.Projector(win, camera=cam)
185
186    key = soy.controllers.Keyboard(win)
187    key[ 1 ] = soy.actions.Quit() # 9 = esc key
188
189
190    key2 = soy.controllers.Keyboard2(win)
191
192    key2['w'] = ( set_variable_field(camera_data, 'move_forward', True),
193            set_variable_field(camera_data, 'move_forward', False))
194    key2['s'] = ( set_variable_field(camera_data, 'move_backward', True),
195            set_variable_field(camera_data, 'move_backward', False))
196
197    key2['q'] = ( set_variable_field(camera_data, 'move_left', True),
198            set_variable_field(camera_data, 'move_left', False))
199    key2['e'] = ( set_variable_field(camera_data, 'move_right', True),
200            set_variable_field(camera_data, 'move_right', False))
201
202    key2['r'] = ( set_variable_field(camera_data, 'move_up', True),
203            set_variable_field(camera_data, 'move_up', False))
204    key2['f'] = ( set_variable_field(camera_data, 'move_down', True),
205            set_variable_field(camera_data, 'move_down', False))
206
207    key2['a'] =  ( camera_add_rot( (0, 0.9, 0)), camera_add_rot( (0,-0.9, 0)) )
208    key2['d'] =  ( camera_add_rot( (0,-0.9, 0)), camera_add_rot( (0, 0.9, 0)) )
209
210    key2['z'] =  ( camera_add_rot( ( 0.9, 0, 0)), camera_add_rot( (-0.9, 0, 0)) )
211    key2['x'] =  ( camera_add_rot( (-0.9, 0, 0)), camera_add_rot( ( 0.9, 0, 0)) )
212
213
214    wcn = soy.controllers.Window(win)
215    wcn['close'] = soy.actions.Quit()
216
217
218    while True:
219        sleep(.03)
220        test_mesh_b.position = (0, 10*abs(cmath.sin( time()))- 6, 0)
221        if camera_data.move_forward:
222            cam.position = (
223                    cam.position[0] - camera_data.forward_vel * cam.direction[0],
224                    cam.position[1] - camera_data.forward_vel * cam.direction[1],
225                    cam.position[2] - camera_data.forward_vel * cam.direction[2],
226                    )
227        if camera_data.move_backward:
228            cam.position = (
229                    cam.position[0] + camera_data.forward_vel * cam.direction[0],
230                    cam.position[1] + camera_data.forward_vel * cam.direction[1],
231                    cam.position[2] + camera_data.forward_vel * cam.direction[2],
232                    )
233        if camera_data.move_left:
234            cam.position = (
235                    cam.position[0] - camera_data.side_vel * cam.right[0],
236                    cam.position[1] - camera_data.side_vel * cam.right[1],
237                    cam.position[2] - camera_data.side_vel * cam.right[2],
238                    )
239        if camera_data.move_right:
240            cam.position = (
241                    cam.position[0] + camera_data.side_vel * cam.right[0],
242                    cam.position[1] + camera_data.side_vel * cam.right[1],
243                    cam.position[2] + camera_data.side_vel * cam.right[2],
244                    )
245
246        if camera_data.move_down:
247            cam.position = (
248                    cam.position[0] - camera_data.side_vel * cam.up[0],
249                    cam.position[1] - camera_data.side_vel * cam.up[1],
250                    cam.position[2] - camera_data.side_vel * cam.up[2],
251                    )
252        if camera_data.move_up:
253            cam.position = (
254                    cam.position[0] + camera_data.side_vel * cam.up[0],
255                    cam.position[1] + camera_data.side_vel * cam.up[1],
256                    cam.position[2] + camera_data.side_vel * cam.up[2],
257                    )
258
259if __name__=='__main__':
260    main()
Note: See TracBrowser for help on using the browser.