source: src/models/Liquid.pym @ 1211:67d37d97d815

Revision 1211:67d37d97d815, 5.8 KB checked in by Arc Riley <arcriley@…>, 6 years ago (diff)

Ticket #901 :

  • upgrade to Pyrex 0.9.8
  • implement cyclic cimport fix in bodies
  • update API respectively, removing redundant typecasts
  • nogil: in use for bodies
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 1257 2008-05-15 22:19:30Z ArcRiley $
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> _scene._bodies._list[_i])._calcFogCoords()
121        (<soy.bodies.Body> _scene._bodies._list[_i])._render()
122    gl.glPopMatrix()
123    gl.glDisableClientState(gl.GL_FOG_COORDINATE_ARRAY_EXT)
124    gl.glDisable(gl.GL_FOG)
125    #
126    #RENDER SURFACE
127    self._material._coreBind()
128    self._renderSurf()
129    self._material._coreUnBind()
130    gl.glDisable(gl.GL_STENCIL_TEST)
131
132
133  cdef void _renderSurf(self) :
134    cdef float _halfSize[3]
135    _halfSize[0] = self._size[0]/2.0
136    _halfSize[1] = self._size[1]/2.0
137    _halfSize[2] = self._size[2]/2.0
138    gl.glBegin(gl.GL_TRIANGLES)
139    gl.glNormal3f(0, 1, 0) #LIQUID FACE
140    gl.glTexCoord2f(0,1)
141    gl.glVertex3f(-_halfSize[0], _halfSize[1], _halfSize[2] )
142    gl.glTexCoord2f(1,1)
143    gl.glVertex3f( _halfSize[0], _halfSize[1], _halfSize[2] )
144    gl.glTexCoord2f(1,0)
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(0,0)
149    gl.glVertex3f(-_halfSize[0], _halfSize[1],-_halfSize[2] )
150    gl.glTexCoord2f(0,1)
151    gl.glVertex3f(-_halfSize[0], _halfSize[1], _halfSize[2] )
152    gl.glEnd()
153
154  cdef void _renderBottom(self) :
155    cdef float       _halfSize[3]
156    _halfSize[0] = self._size[0]/2.0
157    _halfSize[1] = self._size[1]/2.0
158    _halfSize[2] = self._size[2]/2.0
159    gl.glDisable(gl.GL_LIGHTING)
160    gl.glColor3f((<soy.colors.Color> self._color)._rgba[0],
161                 (<soy.colors.Color> self._color)._rgba[1],
162                 (<soy.colors.Color> self._color)._rgba[2])
163    gl.glBegin(gl.GL_TRIANGLES)
164    gl.glNormal3f(0, 1, 0) #LIQUID FACE
165    gl.glVertex3f(-1000, -_halfSize[1], 1000 )
166    gl.glVertex3f( 1000, -_halfSize[1], 1000 )
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.glEnd()
172    gl.glEnable(gl.GL_LIGHTING)
Note: See TracBrowser for help on using the repository browser.