svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender.git] / source / gameengine / VideoTexture / FilterNormal.cpp
1 /* $Id$
2 -----------------------------------------------------------------------------
3 This source file is part of VideoTexture library
4
5 Copyright (c) 2007 The Zdeno Ash Miklas
6
7 This program is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the Free Software
9 Foundation; either version 2 of the License, or (at your option) any later
10 version.
11
12 This program is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
19 http://www.gnu.org/copyleft/lesser.txt.
20 -----------------------------------------------------------------------------
21 */
22
23
24 #include <PyObjectPlus.h>
25 #include <structmember.h>
26
27 #include "FilterNormal.h"
28
29 #include "FilterBase.h"
30 #include "PyTypeList.h"
31
32 // implementation FilterNormal
33
34 // constructor
35 FilterNormal::FilterNormal (void) : m_colIdx(0)
36 {
37         // set default depth
38         setDepth(4);
39 }
40
41 // set color shift
42 void FilterNormal::setColor (unsigned short colIdx)
43 {
44         // check validity of index
45         if (colIdx < 3)
46                 // set color shift
47                 m_colIdx = colIdx;
48 }
49
50 // set depth
51 void FilterNormal::setDepth (float depth)
52 {
53         m_depth = depth;
54         m_depthScale = depth / depthScaleKoef;
55 }
56
57
58 // cast Filter pointer to FilterNormal
59 inline FilterNormal * getFilter (PyFilter * self)
60 { return static_cast<FilterNormal*>(self->m_filter); }
61
62
63 // python methods and get/sets
64
65 // get index of color used to calculate normal
66 static PyObject * getColor (PyFilter * self, void * closure)
67 {
68         return Py_BuildValue("H", getFilter(self)->getColor());
69 }
70
71 // set index of color used to calculate normal
72 static int setColor (PyFilter * self, PyObject * value, void * closure)
73 {
74         // check validity of parameter
75         if (value == NULL || !PyLong_Check(value))
76         {
77                 PyErr_SetString(PyExc_TypeError, "filt.colorIdx = int: VideoTexture.FilterNormal, expected the value must be a int");
78                 return -1;
79         }
80         // set color index
81         getFilter(self)->setColor((unsigned short)(PyLong_AsSsize_t(value)));
82         // success
83         return 0;
84 }
85
86
87 // get depth
88 static PyObject * getDepth (PyFilter * self, void * closure)
89 {
90         return Py_BuildValue("f", getFilter(self)->getDepth());
91 }
92
93 // set depth
94 static int setDepth (PyFilter * self, PyObject * value, void * closure)
95 {
96         // check validity of parameter
97         if (value)
98         {
99                 float depth= (float)PyFloat_AsDouble(value);
100                 if ((depth==-1 && PyErr_Occurred()) == 0) /* no error converting to a float? */
101                 {
102                         // set depth
103                         getFilter(self)->setDepth(depth);
104                         // success
105                         return 0;
106                 }
107         }
108
109         PyErr_SetString(PyExc_TypeError, "filt.depth = float: VideoTexture.FilterNormal, expected the value must be a float");
110         return -1;
111 }
112
113
114 // attributes structure
115 static PyGetSetDef filterNormalGetSets[] =
116
117         {(char*)"colorIdx", (getter)getColor, (setter)setColor, (char*)"index of color used to calculate normal (0 - red, 1 - green, 2 - blue)", NULL},
118         {(char*)"depth", (getter)getDepth, (setter)setDepth, (char*)"depth of relief", NULL},
119         // attributes from FilterBase class
120         {(char*)"previous", (getter)Filter_getPrevious, (setter)Filter_setPrevious, (char*)"previous pixel filter", NULL},
121         {NULL}
122 };
123
124 // define python type
125 PyTypeObject FilterNormalType =
126
127 #if (PY_VERSION_HEX >= 0x02060000)
128         PyVarObject_HEAD_INIT(NULL, 0)
129 #else
130         /* python 2.5 and below */
131         PyObject_HEAD_INIT( NULL )  /* required py macro */
132         0,                         /*ob_size*/
133 #endif
134         "VideoTexture.FilterNormal",   /*tp_name*/
135         sizeof(PyFilter),          /*tp_basicsize*/
136         0,                         /*tp_itemsize*/
137         (destructor)Filter_dealloc,/*tp_dealloc*/
138         0,                         /*tp_print*/
139         0,                         /*tp_getattr*/
140         0,                         /*tp_setattr*/
141         0,                         /*tp_compare*/
142         0,                         /*tp_repr*/
143         0,                         /*tp_as_number*/
144         0,                         /*tp_as_sequence*/
145         0,                         /*tp_as_mapping*/
146         0,                         /*tp_hash */
147         0,                         /*tp_call*/
148         0,                         /*tp_str*/
149         0,                         /*tp_getattro*/
150         0,                         /*tp_setattro*/
151         0,                         /*tp_as_buffer*/
152         Py_TPFLAGS_DEFAULT,        /*tp_flags*/
153         "Filter for Blue Screen objects",       /* tp_doc */
154         0,                             /* tp_traverse */
155         0,                             /* tp_clear */
156         0,                             /* tp_richcompare */
157         0,                             /* tp_weaklistoffset */
158         0,                             /* tp_iter */
159         0,                             /* tp_iternext */
160         NULL,                /* tp_methods */
161         0,                   /* tp_members */
162         filterNormalGetSets,           /* tp_getset */
163         0,                         /* tp_base */
164         0,                         /* tp_dict */
165         0,                         /* tp_descr_get */
166         0,                         /* tp_descr_set */
167         0,                         /* tp_dictoffset */
168         (initproc)Filter_init<FilterNormal>,     /* tp_init */
169         0,                         /* tp_alloc */
170         Filter_allocNew,           /* tp_new */
171 };
172