03c500a7e14e61b2821412c7b687b29ef4857008
[blender.git] / source / gameengine / VideoTexture / FilterNormal.cpp
1 /*
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 /** \file gameengine/VideoTexture/FilterNormal.cpp
24  *  \ingroup bgevideotex
25  */
26
27 #include "PyObjectPlus.h"
28 #include <structmember.h>
29
30 #include "FilterNormal.h"
31
32 #include "FilterBase.h"
33 #include "PyTypeList.h"
34
35 // implementation FilterNormal
36
37 // constructor
38 FilterNormal::FilterNormal (void) : m_colIdx(0)
39 {
40         // set default depth
41         setDepth(4);
42 }
43
44 // set color shift
45 void FilterNormal::setColor (unsigned short colIdx)
46 {
47         // check validity of index
48         if (colIdx < 3)
49                 // set color shift
50                 m_colIdx = colIdx;
51 }
52
53 // set depth
54 void FilterNormal::setDepth (float depth)
55 {
56         m_depth = depth;
57         m_depthScale = depth / depthScaleKoef;
58 }
59
60
61 // cast Filter pointer to FilterNormal
62 inline FilterNormal * getFilter (PyFilter *self)
63 { return static_cast<FilterNormal*>(self->m_filter); }
64
65
66 // python methods and get/sets
67
68 // get index of color used to calculate normal
69 static PyObject *getColor (PyFilter *self, void *closure)
70 {
71         return Py_BuildValue("H", getFilter(self)->getColor());
72 }
73
74 // set index of color used to calculate normal
75 static int setColor (PyFilter *self, PyObject *value, void *closure)
76 {
77         // check validity of parameter
78         if (value == NULL || !PyLong_Check(value))
79         {
80                 PyErr_SetString(PyExc_TypeError, "filt.colorIdx = int: VideoTexture.FilterNormal, expected the value must be a int");
81                 return -1;
82         }
83         // set color index
84         getFilter(self)->setColor((unsigned short)(PyLong_AsSsize_t(value)));
85         // success
86         return 0;
87 }
88
89
90 // get depth
91 static PyObject *getDepth (PyFilter *self, void *closure)
92 {
93         return Py_BuildValue("f", getFilter(self)->getDepth());
94 }
95
96 // set depth
97 static int setDepth (PyFilter *self, PyObject *value, void *closure)
98 {
99         // check validity of parameter
100         if (value)
101         {
102                 float depth= (float)PyFloat_AsDouble(value);
103                 if ((depth==-1 && PyErr_Occurred()) == 0) /* no error converting to a float? */
104                 {
105                         // set depth
106                         getFilter(self)->setDepth(depth);
107                         // success
108                         return 0;
109                 }
110         }
111
112         PyErr_SetString(PyExc_TypeError, "filt.depth = float: VideoTexture.FilterNormal, expected the value must be a float");
113         return -1;
114 }
115
116
117 // attributes structure
118 static PyGetSetDef filterNormalGetSets[] =
119
120         {(char*)"colorIdx", (getter)getColor, (setter)setColor, (char*)"index of color used to calculate normal (0 - red, 1 - green, 2 - blue)", NULL},
121         {(char*)"depth", (getter)getDepth, (setter)setDepth, (char*)"depth of relief", NULL},
122         // attributes from FilterBase class
123         {(char*)"previous", (getter)Filter_getPrevious, (setter)Filter_setPrevious, (char*)"previous pixel filter", NULL},
124         {NULL}
125 };
126
127 // define python type
128 PyTypeObject FilterNormalType =
129
130         PyVarObject_HEAD_INIT(NULL, 0)
131         "VideoTexture.FilterNormal",   /*tp_name*/
132         sizeof(PyFilter),          /*tp_basicsize*/
133         0,                         /*tp_itemsize*/
134         (destructor)Filter_dealloc,/*tp_dealloc*/
135         0,                         /*tp_print*/
136         0,                         /*tp_getattr*/
137         0,                         /*tp_setattr*/
138         0,                         /*tp_compare*/
139         0,                         /*tp_repr*/
140         0,                         /*tp_as_number*/
141         0,                         /*tp_as_sequence*/
142         0,                         /*tp_as_mapping*/
143         0,                         /*tp_hash */
144         0,                         /*tp_call*/
145         0,                         /*tp_str*/
146         0,                         /*tp_getattro*/
147         0,                         /*tp_setattro*/
148         0,                         /*tp_as_buffer*/
149         Py_TPFLAGS_DEFAULT,        /*tp_flags*/
150         "Filter for Blue Screen objects",       /* tp_doc */
151         0,                             /* tp_traverse */
152         0,                             /* tp_clear */
153         0,                             /* tp_richcompare */
154         0,                             /* tp_weaklistoffset */
155         0,                             /* tp_iter */
156         0,                             /* tp_iternext */
157         NULL,                /* tp_methods */
158         0,                   /* tp_members */
159         filterNormalGetSets,           /* tp_getset */
160         0,                         /* tp_base */
161         0,                         /* tp_dict */
162         0,                         /* tp_descr_get */
163         0,                         /* tp_descr_set */
164         0,                         /* tp_dictoffset */
165         (initproc)Filter_init<FilterNormal>,     /* tp_init */
166         0,                         /* tp_alloc */
167         Filter_allocNew,           /* tp_new */
168 };
169