5eeb63b7128a8d2f931775c60d89fbd2c74ca2c2
[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 <Python.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_colShift(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_colShift = colIdx << 3;
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 || !PyInt_Check(value))
76         {
77                 PyErr_SetString(PyExc_TypeError, "The value must be a int");
78                 return -1;
79         }
80         // set color index
81         getFilter(self)->setColor((unsigned short)(PyInt_AsLong(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 == NULL || !PyFloat_Check(value))
98         {
99                 PyErr_SetString(PyExc_TypeError, "The value must be a float");
100                 return -1;
101         }
102         // set depth
103         getFilter(self)->setDepth(float(PyFloat_AsDouble(value)));
104         // success
105         return 0;
106 }
107
108
109 // attributes structure
110 static PyGetSetDef filterNormalGetSets[] =
111
112         {"colorIdx", (getter)getColor, (setter)setColor, "index of color used to calculate normal (0 - red, 1 - green, 2 - blue)", NULL},
113         {"depth", (getter)getDepth, (setter)setDepth, "depth of relief", NULL},
114         // attributes from FilterBase class
115         {"previous", (getter)Filter_getPrevious, (setter)Filter_setPrevious, "previous pixel filter", NULL},
116         {NULL}
117 };
118
119 // define python type
120 PyTypeObject FilterNormalType =
121
122         PyObject_HEAD_INIT(NULL)
123         0,                         /*ob_size*/
124         "VideoTexture.FilterNormal",   /*tp_name*/
125         sizeof(PyFilter),          /*tp_basicsize*/
126         0,                         /*tp_itemsize*/
127         (destructor)Filter_dealloc,/*tp_dealloc*/
128         0,                         /*tp_print*/
129         0,                         /*tp_getattr*/
130         0,                         /*tp_setattr*/
131         0,                         /*tp_compare*/
132         0,                         /*tp_repr*/
133         0,                         /*tp_as_number*/
134         0,                         /*tp_as_sequence*/
135         0,                         /*tp_as_mapping*/
136         0,                         /*tp_hash */
137         0,                         /*tp_call*/
138         0,                         /*tp_str*/
139         0,                         /*tp_getattro*/
140         0,                         /*tp_setattro*/
141         0,                         /*tp_as_buffer*/
142         Py_TPFLAGS_DEFAULT,        /*tp_flags*/
143         "Filter for Blue Screen objects",       /* tp_doc */
144         0,                             /* tp_traverse */
145         0,                             /* tp_clear */
146         0,                             /* tp_richcompare */
147         0,                             /* tp_weaklistoffset */
148         0,                             /* tp_iter */
149         0,                             /* tp_iternext */
150         NULL,                /* tp_methods */
151         0,                   /* tp_members */
152         filterNormalGetSets,           /* tp_getset */
153         0,                         /* tp_base */
154         0,                         /* tp_dict */
155         0,                         /* tp_descr_get */
156         0,                         /* tp_descr_set */
157         0,                         /* tp_dictoffset */
158         (initproc)Filter_init<FilterNormal>,     /* tp_init */
159         0,                         /* tp_alloc */
160         Filter_allocNew,           /* tp_new */
161 };
162