VideoTexture module.
[blender-staging.git] / source / gameengine / VideoTexture / ImageRender.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 // implementation
24
25 #include <Python.h>
26 #include <structmember.h>
27
28 #include <KX_BlenderCanvas.h>
29 #include <KX_BlenderRenderTools.h>
30 #include <RAS_IRasterizer.h>
31 #include <RAS_OpenGLRasterizer.h>
32 #include <KX_WorldInfo.h>
33 #include <KX_Light.h>
34
35 #include "ImageRender.h"
36
37 #include "ImageBase.h"
38 #include "BlendType.h"
39 #include "Exception.h"
40
41
42 // constructor
43 ImageRender::ImageRender (KX_Scene * scene, KX_Camera * camera) : m_scene(scene),
44 m_camera(camera)
45 {
46         // create screen area
47         m_area.winrct.xmin = m_upLeft[0];
48         m_area.winrct.ymin = m_upLeft[1];
49         m_area.winx = m_size[0];
50         m_area.winy = m_size[1];
51         // create canvas
52         m_canvas = new KX_BlenderCanvas(&m_area);
53         // create render tools
54         m_rendertools = new KX_BlenderRenderTools();
55         // create rasterizer
56         m_rasterizer = new RAS_OpenGLRasterizer(m_canvas);
57         m_rasterizer->Init();
58         // initialize background colour
59         setBackground(0, 0, 255);
60         // refresh lights
61         refreshLights();
62 }
63
64 // destructor
65 ImageRender::~ImageRender (void)
66 {
67         // release allocated objects
68         delete m_rasterizer;
69         delete m_rendertools;
70         delete m_canvas;
71 }
72
73
74 // set background color
75 void ImageRender::setBackground (unsigned char red, unsigned char green, unsigned char blue)
76 {
77         m_background[0] = red;
78         m_background[1] = green;
79         m_background[2] = blue;
80         m_rasterizer->SetBackColor(m_background[0], m_background[1], m_background[2], 1.0);
81 }
82
83
84 // capture image from viewport
85 void ImageRender::calcImage (unsigned int texId)
86 {
87         // setup camera
88         bool cameraPasive = !m_camera->GetViewport();
89         // render scene
90         Render();
91         // reset camera
92         if (cameraPasive) m_camera->EnableViewport(false);
93         // get image from viewport
94         ImageViewport::calcImage(texId);
95 }
96
97
98 // refresh lights
99 void ImageRender::refreshLights (void)
100 {
101         // clear lights list
102         //m_rendertools->RemoveAllLights();
103         // set lights
104         //for (int idx = 0; idx < scene->GetLightList()->GetCount(); ++idx)
105         //  m_rendertools->AddLight(((KX_LightObject*)(scene->GetLightList()->GetValue(idx)))->GetLightData());
106 }
107
108
109
110 // cast Image pointer to ImageRender
111 inline ImageRender * getImageRender (PyImage * self)
112 { return static_cast<ImageRender*>(self->m_image); }
113
114
115 // python methods
116
117 // Blender Scene type
118 BlendType<KX_Scene> sceneType ("KX_Scene");
119 // Blender Camera type
120 BlendType<KX_Camera> cameraType ("KX_Camera");
121
122
123 static ExceptionID SceneInvalid, CameraInvalid;
124 static ExpDesc SceneInvalidDesc (SceneInvalid, "Scene object is invalid");
125 static ExpDesc CameraInvalidDesc (CameraInvalid, "Camera object is invalid");
126
127 // object initialization
128 static int ImageRender_init (PyObject * pySelf, PyObject * args, PyObject * kwds)
129 {
130         // parameters - scene object
131         PyObject * scene;
132         // camera object
133         PyObject * camera;
134         // parameter keywords
135         static char *kwlist[] = {"sceneObj", "cameraObj", NULL};
136         // get parameters
137         if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &scene, &camera))
138                 return -1;
139         try
140         {
141                 // get scene pointer
142                 KX_Scene * scenePtr (NULL);
143                 if (scene != NULL) scenePtr = sceneType.checkType(scene);
144                 // throw exception if scene is not available
145                 if (scenePtr == NULL) THRWEXCP(SceneInvalid, S_OK);
146
147                 // get camera pointer
148                 KX_Camera * cameraPtr (NULL);
149                 if (camera != NULL) cameraPtr = cameraType.checkType(camera);
150                 // throw exception if camera is not available
151                 if (cameraPtr == NULL) THRWEXCP(CameraInvalid, S_OK);
152
153                 // get pointer to image structure
154                 PyImage * self = reinterpret_cast<PyImage*>(pySelf);
155                 // create source object
156                 if (self->m_image != NULL) delete self->m_image;
157                 self->m_image = new ImageRender(scenePtr, cameraPtr);
158         }
159         catch (Exception & exp)
160         {
161                 exp.report();
162                 return -1;
163         }
164         // initialization succeded
165         return 0;
166 }
167
168
169 // get background color
170 PyObject * getBackground (PyImage * self, void * closure)
171 {
172         return Py_BuildValue("[BBB]", getImageRender(self)->getBackground()[0],
173                 getImageRender(self)->getBackground()[1], getImageRender(self)->getBackground()[2]);
174 }
175
176 // set color
177 static int setBackground (PyImage * self, PyObject * value, void * closure)
178 {
179         // check validity of parameter
180         if (value == NULL || !PySequence_Check(value) || PySequence_Length(value) != 3
181                 || !PyInt_Check(PySequence_Fast_GET_ITEM(value, 0))
182                 || !PyInt_Check(PySequence_Fast_GET_ITEM(value, 1))
183                 || !PyInt_Check(PySequence_Fast_GET_ITEM(value, 2)))
184         {
185                 PyErr_SetString(PyExc_TypeError, "The value must be a sequence of 3 ints");
186                 return -1;
187         }
188         // set background color
189         getImageRender(self)->setBackground((unsigned char)(PyInt_AsLong(PySequence_Fast_GET_ITEM(value, 0))),
190                 (unsigned char)(PyInt_AsLong(PySequence_Fast_GET_ITEM(value, 1))),
191                 (unsigned char)(PyInt_AsLong(PySequence_Fast_GET_ITEM(value, 2))));
192         // success
193         return 0;
194 }
195
196
197 // methods structure
198 static PyMethodDef imageRenderMethods[] =
199 { // methods from ImageBase class
200         {"refresh", (PyCFunction)Image_refresh, METH_NOARGS, "Refresh image - invalidate its current content"},
201         {NULL}
202 };
203 // attributes structure
204 static PyGetSetDef imageRenderGetSets[] =
205
206         {"background", (getter)getBackground, (setter)setBackground, "background color", NULL},
207         // attributes from ImageBase class
208         {"image", (getter)Image_getImage, NULL, "image data", NULL},
209         {"size", (getter)Image_getSize, NULL, "image size", NULL},
210         {"scale", (getter)Image_getScale, (setter)Image_setScale, "fast scale of image (near neighbour)",       NULL},
211         {"flip", (getter)Image_getFlip, (setter)Image_setFlip, "flip image vertically", NULL},
212         {"filter", (getter)Image_getFilter, (setter)Image_setFilter, "pixel filter", NULL},
213         {NULL}
214 };
215
216
217 // define python type
218 PyTypeObject ImageRenderType =
219
220         PyObject_HEAD_INIT(NULL)
221         0,                         /*ob_size*/
222         "VideoTexture.ImageRender",   /*tp_name*/
223         sizeof(PyImage),          /*tp_basicsize*/
224         0,                         /*tp_itemsize*/
225         (destructor)Image_dealloc, /*tp_dealloc*/
226         0,                         /*tp_print*/
227         0,                         /*tp_getattr*/
228         0,                         /*tp_setattr*/
229         0,                         /*tp_compare*/
230         0,                         /*tp_repr*/
231         0,                         /*tp_as_number*/
232         0,                         /*tp_as_sequence*/
233         0,                         /*tp_as_mapping*/
234         0,                         /*tp_hash */
235         0,                         /*tp_call*/
236         0,                         /*tp_str*/
237         0,                         /*tp_getattro*/
238         0,                         /*tp_setattro*/
239         0,                         /*tp_as_buffer*/
240         Py_TPFLAGS_DEFAULT,        /*tp_flags*/
241         "Image source from render",       /* tp_doc */
242         0,                             /* tp_traverse */
243         0,                             /* tp_clear */
244         0,                             /* tp_richcompare */
245         0,                             /* tp_weaklistoffset */
246         0,                             /* tp_iter */
247         0,                             /* tp_iternext */
248         imageRenderMethods,    /* tp_methods */
249         0,                   /* tp_members */
250         imageRenderGetSets,          /* tp_getset */
251         0,                         /* tp_base */
252         0,                         /* tp_dict */
253         0,                         /* tp_descr_get */
254         0,                         /* tp_descr_set */
255         0,                         /* tp_dictoffset */
256         (initproc)ImageRender_init,     /* tp_init */
257         0,                         /* tp_alloc */
258         Image_allocNew,           /* tp_new */
259 };
260
261