| 1 | # PySoy's bodies.Camera 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 Camera(Body) : |
|---|
| 21 | '''soy.bodies.Camera |
|---|
| 22 | |
|---|
| 23 | A camera is an invisible object in 3d space through which the scene can |
|---|
| 24 | be rendered through. It must be attached to a soy.widgets.Projector or |
|---|
| 25 | other rendering class to be activated. |
|---|
| 26 | ''' |
|---|
| 27 | |
|---|
| 28 | ########################################################################## |
|---|
| 29 | # |
|---|
| 30 | # Python Functions |
|---|
| 31 | # |
|---|
| 32 | |
|---|
| 33 | def __cinit__(self, scene=None, |
|---|
| 34 | position=None, rotation=None, velocity=None, |
|---|
| 35 | model=None, shape=None, |
|---|
| 36 | wireframe=0, |
|---|
| 37 | *args, **keywords) : |
|---|
| 38 | cdef int _i |
|---|
| 39 | self._fovy = 45.0 |
|---|
| 40 | self._znear = 1.0 |
|---|
| 41 | self._zfar = 100.0 |
|---|
| 42 | self.mass = 0 |
|---|
| 43 | self._wire = wireframe |
|---|
| 44 | self._rpt = 0 |
|---|
| 45 | for _i from 0 <= _i < 16 : |
|---|
| 46 | self._rtimes[_i] = 0.0 |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | ############################################################################ |
|---|
| 50 | # |
|---|
| 51 | # Properties |
|---|
| 52 | # |
|---|
| 53 | |
|---|
| 54 | property fps : |
|---|
| 55 | def __get__(self) : |
|---|
| 56 | cdef double f1, f2 |
|---|
| 57 | f2 = self._rtimes[self._rpt] |
|---|
| 58 | f1 = self._rtimes[(self._rpt + 1) % 16] |
|---|
| 59 | if f1 == 0.0 or f2 == 0.0 or f1 == f2 : |
|---|
| 60 | return 0.0 |
|---|
| 61 | else : |
|---|
| 62 | return 1 / (f2 - f1) * 15 |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | property wireframe : |
|---|
| 66 | def __get__(self) : |
|---|
| 67 | return self._wire |
|---|
| 68 | def __set__(self,value) : |
|---|
| 69 | self._wire = value |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | property lens : |
|---|
| 73 | '''Camera Lens |
|---|
| 74 | |
|---|
| 75 | This is the (vertical) angle which the lens can view. Defaults to 45.0 |
|---|
| 76 | ''' |
|---|
| 77 | def __get__(self) : |
|---|
| 78 | return self._fovy |
|---|
| 79 | |
|---|
| 80 | def __set__(self, value) : |
|---|
| 81 | self._fovy = value |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | property direction : |
|---|
| 85 | '''Camera Direction |
|---|
| 86 | |
|---|
| 87 | Direction in which camera is looking |
|---|
| 88 | ''' |
|---|
| 89 | def __get__(self) : |
|---|
| 90 | return ( self._rotation[2], self._rotation[6], self._rotation[10] ) |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | property right : |
|---|
| 94 | '''Camera Right |
|---|
| 95 | |
|---|
| 96 | Direction of the 'right' side of the camera |
|---|
| 97 | ''' |
|---|
| 98 | def __get__(self): |
|---|
| 99 | return ( self._rotation[0], self._rotation[4], self._rotation[8] ) |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | property up : |
|---|
| 103 | '''Camera Up |
|---|
| 104 | |
|---|
| 105 | Direction of the up vector of the camera |
|---|
| 106 | ''' |
|---|
| 107 | def __get__(self): |
|---|
| 108 | return ( self._rotation[1], self._rotation[5], self._rotation[9] ) |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | ############################################################################ |
|---|
| 112 | # |
|---|
| 113 | # WindowLoop Functions |
|---|
| 114 | # |
|---|
| 115 | |
|---|
| 116 | cdef void _project(self, gl.GLdouble _aspect) : |
|---|
| 117 | cdef gl.GLfloat _mtx[16] |
|---|
| 118 | # |
|---|
| 119 | # Bail now if in the abyss |
|---|
| 120 | if self._scene is None : |
|---|
| 121 | return |
|---|
| 122 | # |
|---|
| 123 | # Check if wireframe mode is turned on |
|---|
| 124 | if self._wire == 1 : |
|---|
| 125 | #gl.glEnable (gl.GL_LINE_SMOOTH) |
|---|
| 126 | #gl.glEnable (gl.GL_BLEND) |
|---|
| 127 | #gl.glBlendFunc (gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) ## Anti-Aliasing maybe? |
|---|
| 128 | #gl.glHint (gl.GL_LINE_SMOOTH_HINT, gl.GL_DONT_CARE) |
|---|
| 129 | #gl.glLineWidth (3) |
|---|
| 130 | gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE) |
|---|
| 131 | else : |
|---|
| 132 | gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL) |
|---|
| 133 | #gl.glDisable (gl.GL_LINE_SMOOTH) |
|---|
| 134 | #gl.glDisable (gl.GL_BLEND) |
|---|
| 135 | #gl.glBlendFunc (gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) |
|---|
| 136 | #gl.glHint (gl.GL_LINE_SMOOTH_HINT, gl.GL_DONT_CARE) |
|---|
| 137 | #gl.glLineWidth (1) |
|---|
| 138 | # |
|---|
| 139 | # Now we apply an inverse matrix to translate to the Scene's origin |
|---|
| 140 | gl.glMatrixMode(gl.GL_MODELVIEW) |
|---|
| 141 | gl.glLoadIdentity() |
|---|
| 142 | _mtx[0] = self._rotation[0] |
|---|
| 143 | _mtx[1] = self._rotation[1] |
|---|
| 144 | _mtx[2] = self._rotation[2] |
|---|
| 145 | _mtx[3] = 0.0 |
|---|
| 146 | _mtx[4] = self._rotation[4] |
|---|
| 147 | _mtx[5] = self._rotation[5] |
|---|
| 148 | _mtx[6] = self._rotation[6] |
|---|
| 149 | _mtx[7] = 0.0 |
|---|
| 150 | _mtx[8] = self._rotation[8] |
|---|
| 151 | _mtx[9] = self._rotation[9] |
|---|
| 152 | _mtx[10] = self._rotation[10] |
|---|
| 153 | _mtx[11] = 0.0 |
|---|
| 154 | _mtx[12] = 0.0 |
|---|
| 155 | _mtx[13] = 0.0 |
|---|
| 156 | _mtx[14] = 0.0 |
|---|
| 157 | _mtx[15] = 1.0 |
|---|
| 158 | gl.glLoadMatrixf(_mtx) |
|---|
| 159 | gl.glTranslatef(-self._position[0], -self._position[1], -self._position[2]) |
|---|
| 160 | self._scene._render(self._fovy, _aspect, self._znear, self._zfar) |
|---|
| 161 | # |
|---|
| 162 | # Framerate calc |
|---|
| 163 | self._rpt = (self._rpt + 1) % 16 |
|---|
| 164 | self._rtimes[self._rpt] = soy._internals._time() |
|---|