source: src/models/Liquid.pym @ 1182:9b46968aa773

Revision 1182:9b46968aa773, 5.9 KB checked in by Jon Neal <reportingsjr@…>, 7 years ago (diff)

Ticket #954 :

  • Migrated soy.bodies to PyMill
  • Migrated soy.models to PyMill
Line 
1# PySoy's models.Liquid class
2#
3# Copyright (C) 2006,2007,2008 PySoy Group
4#
5#  This program is free software; you can redistribute it and/or modify
6#  it under the terms of the GNU Affero General Public License as published
7#  by the Free Software Foundation, either version 3 of the License, or
8#  (at your option) any later version.
9#
10#  This program is distributed in the hope that it will be useful,
11#  but WITHOUT ANY WARRANTY; without even the implied warranty of
12#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#  GNU Affero General Public License for more details.
14#
15#  You should have received a copy of the GNU Affero General Public License
16#  along with this program; if not, see http://www.gnu.org/licenses
17#
18# $Id: Liquid.pym 1228 2008-04-04 06:12:51Z JonNeal $
19
20cdef class Liquid (Model) :
21  '''soy.models.Liquid
22
23    This models class renders a liquid with volumetric fog
24  '''
25
26  ############################################################################
27  #
28  # Python functions
29  #
30 
31  def __cinit__(self, size=(1,1,1), color=None, material=None) :
32    self._size[0] = size[0]
33    self._size[1] = size[1]
34    self._size[2] = size[2]
35    #
36    # Set Color
37    if color :
38      if not isinstance(color, soy.colors.Color) :
39        raise TypeError('color must be an instance of soy.colors.Color')
40      self._color = color
41    else :
42      import soy.colors
43      self._color = soy.colors.blue
44    #
45    # Set Material
46    if material :
47      if not isinstance(material, soy.materials.Material) :
48        raise TypeError('material is not an instance of soy.materials.Material')
49      self._material = material
50    else :
51      import soy.materials
52      self._material = soy.materials.StainlessSteel()
53
54
55  ############################################################################
56  #
57  # Properties
58  #
59
60  property size:
61    def __get__(self):
62      return (self._size[0], self._size[1], self._size[2])
63
64    def __set__(self, value):
65      self._size[0] = value[0]
66      self._size[1] = value[1]
67      self._size[2] = value[2]
68
69
70  property material :
71    def __get__(self):
72      return self._material
73
74    def __set__(self, value):
75      self._material = value
76
77
78  ############################################################################
79  #
80  # WindowLoop Functions
81  #
82
83  cdef void _render(self, void* _body) :
84    cdef int                _i
85    cdef soy.scenes.Scene   _scene
86    cdef ode.dReal*         _pos
87    cdef float              _halfSize[3]
88    _pos = (<soy.bodies.Body> _body)._position
89    _halfSize[0] = self._size[0]/2.0
90    _halfSize[1] = self._size[1]/2.0
91    _halfSize[2] = self._size[2]/2.0
92    gl.glClearStencil(0)
93    gl.glEnable(gl.GL_STENCIL_TEST)
94    gl.glClear(gl.GL_STENCIL_BUFFER_BIT)
95    gl.glStencilFunc(gl.GL_NEVER, 0, 0)
96    gl.glStencilOp(gl.GL_INCR, gl.GL_INCR, gl.GL_INCR)
97    self._renderSurf()
98    gl.glStencilFunc(gl.GL_EQUAL, 1, 1)
99    gl.glStencilOp(gl.GL_KEEP, gl.GL_KEEP, gl.GL_KEEP)
100    gl.glDisable(gl.GL_DEPTH_TEST)
101    self._renderBottom()
102    gl.glEnable(gl.GL_DEPTH_TEST)
103    #
104    #RENDER BODIES
105    gl.glEnable(gl.GL_FOG)
106    gl.glEnableClientState(gl.GL_FOG_COORDINATE_ARRAY_EXT)
107    gl.glFogi(gl.GL_FOG_MODE, gl.GL_LINEAR)
108    gl.glFogfv(gl.GL_FOG_COLOR, (<soy.colors.Color> self._color)._rgba)
109    gl.glFogf(gl.GL_FOG_START, pos[1] + _halfSize[1])
110    gl.glFogf(gl.GL_FOG_END, pos[1] - _halfSize[1])
111    gl.glHint(gl.GL_FOG_HINT, gl.GL_NICEST)
112    gl.glFogi(gl.GL_FOG_COORDINATE_SOURCE_EXT, gl.GL_FOG_COORDINATE_EXT)
113    gl.glPushMatrix()
114    gl.glTranslatef(-pos[0],-pos[1], -pos[2])
115    #
116    # We need not _iterStart here since _bodies is already iterating via Scene
117    _scene = <soy.scenes.Scene> (<soy.bodies.Body> _body)._scene
118    for _i from 0 <= _i < _scene._bodies._current :
119      if _body != (<void*> _scene._bodies._list[_i]) :
120        (<soy.bodies.Body>
121         _scene._bodies._list[_i])._coreCreateVertFogCoords()
122        (<soy.bodies.Body>
123         _scene._bodies._list[_i])._render()
124    gl.glPopMatrix()
125    gl.glDisableClientState(gl.GL_FOG_COORDINATE_ARRAY_EXT)
126    gl.glDisable(gl.GL_FOG)
127    #
128    #RENDER SURFACE
129    self._material._coreBind()
130    self._renderSurf()
131    self._material._coreUnBind()
132    gl.glDisable(gl.GL_STENCIL_TEST)
133
134
135  cdef void _renderSurf(self) :
136    cdef float _halfSize[3]
137    _halfSize[0] = self._size[0]/2.0
138    _halfSize[1] = self._size[1]/2.0
139    _halfSize[2] = self._size[2]/2.0
140    gl.glBegin(gl.GL_TRIANGLES)
141    gl.glNormal3f(0, 1, 0) #LIQUID FACE
142    gl.glTexCoord2f(0,1)
143    gl.glVertex3f(-_halfSize[0], _halfSize[1], _halfSize[2] )
144    gl.glTexCoord2f(1,1)
145    gl.glVertex3f( _halfSize[0], _halfSize[1], _halfSize[2] )
146    gl.glTexCoord2f(1,0)
147    gl.glVertex3f( _halfSize[0], _halfSize[1],-_halfSize[2] )
148    gl.glTexCoord2f(1,0)
149    gl.glVertex3f( _halfSize[0], _halfSize[1],-_halfSize[2] )
150    gl.glTexCoord2f(0,0)
151    gl.glVertex3f(-_halfSize[0], _halfSize[1],-_halfSize[2] )
152    gl.glTexCoord2f(0,1)
153    gl.glVertex3f(-_halfSize[0], _halfSize[1], _halfSize[2] )
154    gl.glEnd()
155
156  cdef void _renderBottom(self) :
157    cdef float       _halfSize[3]
158    _halfSize[0] = self._size[0]/2.0
159    _halfSize[1] = self._size[1]/2.0
160    _halfSize[2] = self._size[2]/2.0
161    gl.glDisable(gl.GL_LIGHTING)
162    gl.glColor3f((<soy.colors.Color> self._color)._rgba[0],
163                 (<soy.colors.Color> self._color)._rgba[1],
164                 (<soy.colors.Color> self._color)._rgba[2])
165    gl.glBegin(gl.GL_TRIANGLES)
166    gl.glNormal3f(0, 1, 0) #LIQUID FACE
167    gl.glVertex3f(-1000, -_halfSize[1], 1000 )
168    gl.glVertex3f( 1000, -_halfSize[1], 1000 )
169    gl.glVertex3f( 1000, -_halfSize[1],-1000 )
170    gl.glVertex3f( 1000, -_halfSize[1],-1000 )
171    gl.glVertex3f(-1000, -_halfSize[1],-1000 )
172    gl.glVertex3f(-1000, -_halfSize[1], 1000 )
173    gl.glEnd()
174    gl.glEnable(gl.GL_LIGHTING)
Note: See TracBrowser for help on using the repository browser.