| 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 1375 2008-11-11 05:45:21Z DavidCzech $ |
|---|
| 19 | |
|---|
| 20 | cdef class Liquid (Model) : |
|---|
| 21 | '''soy.models.Liquid |
|---|
| 22 | |
|---|
| 23 | This :class:`~soy.model.Model` 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 | cdef gl.GLfloat _mtx[16] |
|---|
| 88 | # |
|---|
| 89 | ###################################### |
|---|
| 90 | # |
|---|
| 91 | # set model's matrix |
|---|
| 92 | # |
|---|
| 93 | _mtx[0] = _body._rotation[0] |
|---|
| 94 | _mtx[1] = _body._rotation[4] |
|---|
| 95 | _mtx[2] = _body._rotation[8] |
|---|
| 96 | _mtx[3] = 0.0 |
|---|
| 97 | _mtx[4] = _body._rotation[1] |
|---|
| 98 | _mtx[5] = _body._rotation[5] |
|---|
| 99 | _mtx[6] = _body._rotation[9] |
|---|
| 100 | _mtx[7] = 0.0 |
|---|
| 101 | _mtx[8] = _body._rotation[2] |
|---|
| 102 | _mtx[9] = _body._rotation[6] |
|---|
| 103 | _mtx[10] = _body._rotation[10] |
|---|
| 104 | _mtx[11] = 0.0 |
|---|
| 105 | _mtx[12] = _body._position[0] |
|---|
| 106 | _mtx[13] = _body._position[1] |
|---|
| 107 | _mtx[14] = _body._position[2] |
|---|
| 108 | _mtx[15] = 1.0 |
|---|
| 109 | # |
|---|
| 110 | ###################################### |
|---|
| 111 | # |
|---|
| 112 | # save current matix before setting it |
|---|
| 113 | # |
|---|
| 114 | gl.glPushMatrix() |
|---|
| 115 | gl.glMultMatrixf(_mtx) |
|---|
| 116 | # |
|---|
| 117 | ###################################### |
|---|
| 118 | # |
|---|
| 119 | # render liquid surface and bottom |
|---|
| 120 | # |
|---|
| 121 | _pos = _body._position |
|---|
| 122 | _halfSize[0] = self._size[0]/2.0 |
|---|
| 123 | _halfSize[1] = self._size[1]/2.0 |
|---|
| 124 | _halfSize[2] = self._size[2]/2.0 |
|---|
| 125 | gl.glClearStencil(0) |
|---|
| 126 | gl.glEnable(gl.GL_STENCIL_TEST) |
|---|
| 127 | gl.glClear(gl.GL_STENCIL_BUFFER_BIT) |
|---|
| 128 | gl.glStencilFunc(gl.GL_NEVER, 0, 0) |
|---|
| 129 | gl.glStencilOp(gl.GL_INCR, gl.GL_INCR, gl.GL_INCR) |
|---|
| 130 | self._renderSurf() |
|---|
| 131 | gl.glStencilFunc(gl.GL_EQUAL, 1, 1) |
|---|
| 132 | gl.glStencilOp(gl.GL_KEEP, gl.GL_KEEP, gl.GL_KEEP) |
|---|
| 133 | gl.glDisable(gl.GL_DEPTH_TEST) |
|---|
| 134 | self._renderBottom() |
|---|
| 135 | gl.glEnable(gl.GL_DEPTH_TEST) |
|---|
| 136 | # |
|---|
| 137 | ###################################### |
|---|
| 138 | # |
|---|
| 139 | # return to camera matrix |
|---|
| 140 | # |
|---|
| 141 | gl.glPopMatrix() |
|---|
| 142 | # |
|---|
| 143 | ###################################### |
|---|
| 144 | # |
|---|
| 145 | # render each body inside the liquid |
|---|
| 146 | # |
|---|
| 147 | gl.glEnable(gl.GL_FOG) |
|---|
| 148 | gl.glEnableClientState(gl.GL_FOG_COORDINATE_ARRAY_EXT) |
|---|
| 149 | gl.glFogi(gl.GL_FOG_MODE, gl.GL_LINEAR) |
|---|
| 150 | gl.glFogfv(gl.GL_FOG_COLOR, (<soy.colors.Color> self._color)._rgba) |
|---|
| 151 | gl.glFogf(gl.GL_FOG_START, pos[1] + _halfSize[1]) |
|---|
| 152 | gl.glFogf(gl.GL_FOG_END, pos[1] - _halfSize[1]) |
|---|
| 153 | gl.glHint(gl.GL_FOG_HINT, gl.GL_NICEST) |
|---|
| 154 | gl.glFogi(gl.GL_FOG_COORDINATE_SOURCE_EXT, gl.GL_FOG_COORDINATE_EXT) |
|---|
| 155 | # |
|---|
| 156 | # We need not _iterStart here since _bodies is already iterating via Scene |
|---|
| 157 | for _i from 0 <= _i < _body._scene._bodies._current : |
|---|
| 158 | if (<void*> _body != (_body._scene._bodies._list[_i]) and |
|---|
| 159 | (<soy.bodies.Body> |
|---|
| 160 | _body._scene._bodies._list[_i])._model is not None) : |
|---|
| 161 | (<soy.bodies.Body> |
|---|
| 162 | _body._scene._bodies._list[_i])._model._calcFogCoords( |
|---|
| 163 | (<soy.bodies.Body> _body._scene._bodies._list[_i])) |
|---|
| 164 | (<soy.bodies.Body> |
|---|
| 165 | _body._scene._bodies._list[_i])._model._render( |
|---|
| 166 | (<soy.bodies.Body> _body._scene._bodies._list[_i])) |
|---|
| 167 | gl.glDisableClientState(gl.GL_FOG_COORDINATE_ARRAY_EXT) |
|---|
| 168 | gl.glDisable(gl.GL_FOG) |
|---|
| 169 | # |
|---|
| 170 | ###################################### |
|---|
| 171 | # |
|---|
| 172 | # save current matix before setting it |
|---|
| 173 | # |
|---|
| 174 | gl.glPushMatrix() |
|---|
| 175 | gl.glMultMatrixf(_mtx) |
|---|
| 176 | self._material._coreBind() |
|---|
| 177 | self._renderSurf() |
|---|
| 178 | self._material._coreUnBind() |
|---|
| 179 | gl.glDisable(gl.GL_STENCIL_TEST) |
|---|
| 180 | # |
|---|
| 181 | ###################################### |
|---|
| 182 | # |
|---|
| 183 | # return to camera matrix |
|---|
| 184 | # |
|---|
| 185 | gl.glPopMatrix() |
|---|
| 186 | # |
|---|
| 187 | ###################################### |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | cdef void _renderSurf(self) : |
|---|
| 191 | cdef float _halfSize[3] |
|---|
| 192 | _halfSize[0] = self._size[0]/2.0 |
|---|
| 193 | _halfSize[1] = self._size[1]/2.0 |
|---|
| 194 | _halfSize[2] = self._size[2]/2.0 |
|---|
| 195 | gl.glBegin(gl.GL_TRIANGLES) |
|---|
| 196 | gl.glNormal3f(0, 1, 0) #LIQUID FACE |
|---|
| 197 | gl.glTexCoord2f(0,1) |
|---|
| 198 | gl.glVertex3f(-_halfSize[0], _halfSize[1], _halfSize[2] ) |
|---|
| 199 | gl.glTexCoord2f(1,1) |
|---|
| 200 | gl.glVertex3f( _halfSize[0], _halfSize[1], _halfSize[2] ) |
|---|
| 201 | gl.glTexCoord2f(1,0) |
|---|
| 202 | gl.glVertex3f( _halfSize[0], _halfSize[1],-_halfSize[2] ) |
|---|
| 203 | gl.glTexCoord2f(1,0) |
|---|
| 204 | gl.glVertex3f( _halfSize[0], _halfSize[1],-_halfSize[2] ) |
|---|
| 205 | gl.glTexCoord2f(0,0) |
|---|
| 206 | gl.glVertex3f(-_halfSize[0], _halfSize[1],-_halfSize[2] ) |
|---|
| 207 | gl.glTexCoord2f(0,1) |
|---|
| 208 | gl.glVertex3f(-_halfSize[0], _halfSize[1], _halfSize[2] ) |
|---|
| 209 | gl.glEnd() |
|---|
| 210 | |
|---|
| 211 | cdef void _renderBottom(self) : |
|---|
| 212 | cdef float _halfSize[3] |
|---|
| 213 | _halfSize[0] = self._size[0]/2.0 |
|---|
| 214 | _halfSize[1] = self._size[1]/2.0 |
|---|
| 215 | _halfSize[2] = self._size[2]/2.0 |
|---|
| 216 | gl.glDisable(gl.GL_LIGHTING) |
|---|
| 217 | gl.glColor3f((<soy.colors.Color> self._color)._rgba[0], |
|---|
| 218 | (<soy.colors.Color> self._color)._rgba[1], |
|---|
| 219 | (<soy.colors.Color> self._color)._rgba[2]) |
|---|
| 220 | gl.glBegin(gl.GL_TRIANGLES) |
|---|
| 221 | gl.glNormal3f(0, 1, 0) #LIQUID FACE |
|---|
| 222 | gl.glVertex3f(-1000, -_halfSize[1], 1000 ) |
|---|
| 223 | gl.glVertex3f( 1000, -_halfSize[1], 1000 ) |
|---|
| 224 | gl.glVertex3f( 1000, -_halfSize[1],-1000 ) |
|---|
| 225 | gl.glVertex3f( 1000, -_halfSize[1],-1000 ) |
|---|
| 226 | gl.glVertex3f(-1000, -_halfSize[1],-1000 ) |
|---|
| 227 | gl.glVertex3f(-1000, -_halfSize[1], 1000 ) |
|---|
| 228 | gl.glEnd() |
|---|
| 229 | gl.glEnable(gl.GL_LIGHTING) |
|---|