source: src/models/Liquid.pym @ 1310:c4c8a2791fc4

Revision 1310:c4c8a2791fc4, 6.0 KB checked in by Arc Riley <arcriley@…>, 6 years ago (diff)

Ticket #962 :

  • eliminated soy.bodies.Body._calcFogCoords
  • fixed some obvious problems with liquid_example.py
  • still renders just black
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 1356 2008-09-08 19:24:04Z 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, soy.bodies.Body _body) :
84    cdef int                _i
85    cdef ode.dReal*         _pos
86    cdef float              _halfSize[3]
87    _pos = _body._position
88    _halfSize[0] = self._size[0]/2.0
89    _halfSize[1] = self._size[1]/2.0
90    _halfSize[2] = self._size[2]/2.0
91    gl.glClearStencil(0)
92    gl.glEnable(gl.GL_STENCIL_TEST)
93    gl.glClear(gl.GL_STENCIL_BUFFER_BIT)
94    gl.glStencilFunc(gl.GL_NEVER, 0, 0)
95    gl.glStencilOp(gl.GL_INCR, gl.GL_INCR, gl.GL_INCR)
96    self._renderSurf()
97    gl.glStencilFunc(gl.GL_EQUAL, 1, 1)
98    gl.glStencilOp(gl.GL_KEEP, gl.GL_KEEP, gl.GL_KEEP)
99    gl.glDisable(gl.GL_DEPTH_TEST)
100    self._renderBottom()
101    gl.glEnable(gl.GL_DEPTH_TEST)
102    #
103    #RENDER BODIES
104    gl.glEnable(gl.GL_FOG)
105    gl.glEnableClientState(gl.GL_FOG_COORDINATE_ARRAY_EXT)
106    gl.glFogi(gl.GL_FOG_MODE, gl.GL_LINEAR)
107    gl.glFogfv(gl.GL_FOG_COLOR, (<soy.colors.Color> self._color)._rgba)
108    gl.glFogf(gl.GL_FOG_START, pos[1] + _halfSize[1])
109    gl.glFogf(gl.GL_FOG_END, pos[1] - _halfSize[1])
110    gl.glHint(gl.GL_FOG_HINT, gl.GL_NICEST)
111    gl.glFogi(gl.GL_FOG_COORDINATE_SOURCE_EXT, gl.GL_FOG_COORDINATE_EXT)
112    gl.glPushMatrix()
113    gl.glTranslatef(-pos[0],-pos[1], -pos[2])
114    #
115    # We need not _iterStart here since _bodies is already iterating via Scene
116    for _i from 0 <= _i < _body._scene._bodies._current :
117      if (<void*> _body != (_body._scene._bodies._list[_i]) and
118          (<soy.bodies.Body>
119           _body._scene._bodies._list[_i])._model is not None) :
120        (<soy.bodies.Body>
121         _body._scene._bodies._list[_i])._model._calcFogCoords(
122          (<soy.bodies.Body> _body._scene._bodies._list[_i]))
123        (<soy.bodies.Body>
124         _body._scene._bodies._list[_i])._model._render(
125          (<soy.bodies.Body> _body._scene._bodies._list[_i]))
126    gl.glPopMatrix()
127    gl.glDisableClientState(gl.GL_FOG_COORDINATE_ARRAY_EXT)
128    gl.glDisable(gl.GL_FOG)
129    #
130    #RENDER SURFACE
131    self._material._coreBind()
132    self._renderSurf()
133    self._material._coreUnBind()
134    gl.glDisable(gl.GL_STENCIL_TEST)
135
136
137  cdef void _renderSurf(self) :
138    cdef float _halfSize[3]
139    _halfSize[0] = self._size[0]/2.0
140    _halfSize[1] = self._size[1]/2.0
141    _halfSize[2] = self._size[2]/2.0
142    gl.glBegin(gl.GL_TRIANGLES)
143    gl.glNormal3f(0, 1, 0) #LIQUID FACE
144    gl.glTexCoord2f(0,1)
145    gl.glVertex3f(-_halfSize[0], _halfSize[1], _halfSize[2] )
146    gl.glTexCoord2f(1,1)
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(1,0)
151    gl.glVertex3f( _halfSize[0], _halfSize[1],-_halfSize[2] )
152    gl.glTexCoord2f(0,0)
153    gl.glVertex3f(-_halfSize[0], _halfSize[1],-_halfSize[2] )
154    gl.glTexCoord2f(0,1)
155    gl.glVertex3f(-_halfSize[0], _halfSize[1], _halfSize[2] )
156    gl.glEnd()
157
158  cdef void _renderBottom(self) :
159    cdef float       _halfSize[3]
160    _halfSize[0] = self._size[0]/2.0
161    _halfSize[1] = self._size[1]/2.0
162    _halfSize[2] = self._size[2]/2.0
163    gl.glDisable(gl.GL_LIGHTING)
164    gl.glColor3f((<soy.colors.Color> self._color)._rgba[0],
165                 (<soy.colors.Color> self._color)._rgba[1],
166                 (<soy.colors.Color> self._color)._rgba[2])
167    gl.glBegin(gl.GL_TRIANGLES)
168    gl.glNormal3f(0, 1, 0) #LIQUID FACE
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.glVertex3f(-1000, -_halfSize[1],-1000 )
174    gl.glVertex3f(-1000, -_halfSize[1], 1000 )
175    gl.glEnd()
176    gl.glEnable(gl.GL_LIGHTING)
Note: See TracBrowser for help on using the repository browser.