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