| 1 | # PySoy's bodies.Light 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$ |
|---|
| 19 | |
|---|
| 20 | cdef class Light (Body) : |
|---|
| 21 | '''soy.bodies.Light |
|---|
| 22 | |
|---|
| 23 | Lights provide illumination in your scene. Needs more work. |
|---|
| 24 | ''' |
|---|
| 25 | |
|---|
| 26 | ############################################################################ |
|---|
| 27 | # |
|---|
| 28 | # Python Functions |
|---|
| 29 | # |
|---|
| 30 | |
|---|
| 31 | def __cinit__(self, scene=None, |
|---|
| 32 | radius=180.0, ambient=None, diffuse=None, specular=None, |
|---|
| 33 | *args, **keywords) : |
|---|
| 34 | self.radius = radius |
|---|
| 35 | self.ambient = ambient |
|---|
| 36 | self.mass = 0 |
|---|
| 37 | self.diffuse = diffuse |
|---|
| 38 | self.specular = specular |
|---|
| 39 | if self._scene is not None : |
|---|
| 40 | self._scene._lights._append(<void*> self) |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | ############################################################################ |
|---|
| 44 | # |
|---|
| 45 | # Properties |
|---|
| 46 | # |
|---|
| 47 | |
|---|
| 48 | property ambient : |
|---|
| 49 | def __get__(self) : |
|---|
| 50 | return self._ambient |
|---|
| 51 | |
|---|
| 52 | def __set__(self, value) : |
|---|
| 53 | if value == None : |
|---|
| 54 | from soy.colors import black |
|---|
| 55 | self._ambient = black |
|---|
| 56 | elif isinstance(value, soy.colors.Color) : |
|---|
| 57 | self._ambient = value |
|---|
| 58 | else : |
|---|
| 59 | raise TypeError('color must be an instance of soy.colors.Color') |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | property diffuse : |
|---|
| 63 | def __get__(self) : |
|---|
| 64 | return self._diffuse |
|---|
| 65 | |
|---|
| 66 | def __set__(self, value) : |
|---|
| 67 | if value == None : |
|---|
| 68 | from soy.colors import gray |
|---|
| 69 | self._diffuse = gray |
|---|
| 70 | elif isinstance(value, soy.colors.Color) : |
|---|
| 71 | self._diffuse = value |
|---|
| 72 | else : |
|---|
| 73 | raise TypeError('color must be an instance of soy.colors.Color') |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | property radius : |
|---|
| 77 | def __get__(self) : |
|---|
| 78 | return self._radius |
|---|
| 79 | |
|---|
| 80 | def __set__(self, value) : |
|---|
| 81 | if type(value) != float and type(value) != int : |
|---|
| 82 | raise TypeError('Light.radius must be a float or int') |
|---|
| 83 | if value < 0.0 or value > 180.0 : |
|---|
| 84 | raise ValueError('Light.radius must be 0.0 - 180.0') |
|---|
| 85 | self._radius = value |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | property specular : |
|---|
| 89 | def __get__(self) : |
|---|
| 90 | return self._specular |
|---|
| 91 | |
|---|
| 92 | def __set__(self, value) : |
|---|
| 93 | if value == None : |
|---|
| 94 | from soy.colors import white |
|---|
| 95 | self._specular = white |
|---|
| 96 | elif isinstance(value, soy.colors.Color) : |
|---|
| 97 | self._specular = value |
|---|
| 98 | else : |
|---|
| 99 | raise TypeError('color must be an instance of soy.colors.Color') |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | ############################################################################ |
|---|
| 103 | # |
|---|
| 104 | # General C Functions |
|---|
| 105 | # |
|---|
| 106 | |
|---|
| 107 | cdef void _append(self) : |
|---|
| 108 | # |
|---|
| 109 | # this overrides Body._append to also append self to self._scene._lights |
|---|
| 110 | # |
|---|
| 111 | self._scene._bodies._append(<void*> self) |
|---|
| 112 | self._scene._lights._append(<void*> self) |
|---|
| 113 | # |
|---|
| 114 | ###################################### |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | cdef void _remove(self) : |
|---|
| 118 | # |
|---|
| 119 | # this overrides Body._remove to also remove self from self._scene._lights |
|---|
| 120 | # |
|---|
| 121 | self._scene._bodies._remove(<void*> self) |
|---|
| 122 | self._scene._lights._remove(<void*> self) |
|---|
| 123 | # |
|---|
| 124 | ###################################### |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | ############################################################################ |
|---|
| 128 | # |
|---|
| 129 | # WindowLoop Functions |
|---|
| 130 | # |
|---|
| 131 | |
|---|
| 132 | cdef void _on(self, int _id) nogil : |
|---|
| 133 | cdef gl.GLfloat _position[4] |
|---|
| 134 | # |
|---|
| 135 | # we do this only because glLightfv takes four parameters |
|---|
| 136 | _position[0] = self._position[0] |
|---|
| 137 | _position[1] = self._position[1] |
|---|
| 138 | _position[2] = self._position[2] |
|---|
| 139 | _position[3] = 1.0 |
|---|
| 140 | # |
|---|
| 141 | # enable the light of the given _id |
|---|
| 142 | gl.glEnable(_id) |
|---|
| 143 | gl.glLightfv(_id, gl.GL_AMBIENT, |
|---|
| 144 | (<soy.colors.Color> self._ambient)._rgba) |
|---|
| 145 | gl.glLightfv(_id, gl.GL_DIFFUSE, |
|---|
| 146 | (<soy.colors.Color> self._diffuse)._rgba) |
|---|
| 147 | gl.glLightfv(_id, gl.GL_SPECULAR, |
|---|
| 148 | (<soy.colors.Color> self._specular)._rgba) |
|---|
| 149 | gl.glLightfv(_id, gl.GL_POSITION, _position) |
|---|
| 150 | # |
|---|
| 151 | # for spotlights |
|---|
| 152 | gl.glLightf (_id, gl.GL_SPOT_CUTOFF, self._radius) |
|---|
| 153 | #if self._radius != 180.0 : |
|---|
| 154 | # TODO, Ticket #947 : |
|---|
| 155 | # gl.glLightf (_id, gl.GL_SPOT_EXPODENT, [[0-128, default 0]]) |
|---|
| 156 | # gl.glLightfv(_id, gl.GL_SPOT_DIRECTION, [[direction, as per Body]]) |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | cdef void _off(self, int _id) nogil : |
|---|
| 160 | gl.glDisable(_id) |
|---|