~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/gameengine/VideoTexture/ImageViewport.cpp

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
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
 
*/
 
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
 */
22
26
 
23
27
/** \file gameengine/VideoTexture/ImageViewport.cpp
24
28
 *  \ingroup bgevideotex
26
30
 
27
31
// implementation
28
32
 
29
 
#include <PyObjectPlus.h>
 
33
#include "PyObjectPlus.h"
30
34
#include <structmember.h>
31
35
 
32
36
#include "GL/glew.h"
50
54
        
51
55
        //glGetIntegerv(GL_VIEWPORT, m_viewport);
52
56
        // create buffer for viewport image
 
57
        // Warning: this buffer is also used to get the depth buffer as an array of
 
58
        //          float (1 float = 4 bytes per pixel)
53
59
        m_viewportImage = new BYTE [4 * getViewportSize()[0] * getViewportSize()[1]];
54
60
        // set attributes
55
61
        setWhole(false);
57
63
 
58
64
// destructor
59
65
ImageViewport::~ImageViewport (void)
60
 
{ delete [] m_viewportImage; }
 
66
{
 
67
        delete [] m_viewportImage;
 
68
}
61
69
 
62
70
 
63
71
// use whole viewport to capture image
81
89
        setPosition();
82
90
}
83
91
 
84
 
void ImageViewport::setCaptureSize (short * size)
 
92
void ImageViewport::setCaptureSize (short size[2])
85
93
{
86
94
        m_whole = false;
87
95
        if (size == NULL) 
101
109
}
102
110
 
103
111
// set position of capture rectangle
104
 
void ImageViewport::setPosition (GLint * pos)
 
112
void ImageViewport::setPosition (GLint pos[2])
105
113
{
106
114
        // if new position is not provided, use existing position
107
115
        if (pos == NULL) pos = m_position;
123
131
                // reset image
124
132
                init(m_capSize[0], m_capSize[1]);
125
133
        // if texture wasn't initialized
126
 
        if (!m_texInit)
127
 
        {
 
134
        if (!m_texInit) {
128
135
                // initialize it
129
136
                loadTexture(texId, m_image, m_size);
130
137
                m_texInit = true;
131
138
        }
132
139
        // if texture can be directly created
133
140
        if (texId != 0 && m_pyfilter == NULL && m_capSize[0] == calcSize(m_capSize[0])
134
 
                && m_capSize[1] == calcSize(m_capSize[1]) && !m_flip)
 
141
            && m_capSize[1] == calcSize(m_capSize[1]) && !m_flip && !m_zbuff && !m_depth)
135
142
        {
136
143
                // just copy current viewport to texture
137
144
                glBindTexture(GL_TEXTURE_2D, texId);
140
147
                m_avail = false;
141
148
        }
142
149
        // otherwise copy viewport to buffer, if image is not available
143
 
        else if (!m_avail)
144
 
        {
145
 
                // get frame buffer data
146
 
                if (m_alpha)
147
 
                {
148
 
                        glReadPixels(m_upLeft[0], m_upLeft[1], (GLsizei)m_capSize[0], (GLsizei)m_capSize[1], GL_RGBA,
149
 
                                     GL_UNSIGNED_BYTE, m_viewportImage);
 
150
        else if (!m_avail) {
 
151
                if (m_zbuff) {
 
152
                        // Use read pixels with the depth buffer
 
153
                        // *** misusing m_viewportImage here, but since it has the correct size
 
154
                        //     (4 bytes per pixel = size of float) and we just need it to apply
 
155
                        //     the filter, it's ok
 
156
                        glReadPixels(m_upLeft[0], m_upLeft[1], (GLsizei)m_capSize[0], (GLsizei)m_capSize[1],
 
157
                                GL_DEPTH_COMPONENT, GL_FLOAT, m_viewportImage);
150
158
                        // filter loaded data
151
 
                        FilterRGBA32 filt;
152
 
                        filterImage(filt, m_viewportImage, m_capSize);
 
159
                        FilterZZZA filt;
 
160
                        filterImage(filt, (float *)m_viewportImage, m_capSize);
153
161
                }
154
 
                else
155
 
                {
156
 
                        glReadPixels(m_upLeft[0], m_upLeft[1], (GLsizei)m_capSize[0], (GLsizei)m_capSize[1], GL_RGB,
157
 
                                     GL_UNSIGNED_BYTE, m_viewportImage);
158
 
                        // filter loaded data
159
 
                        FilterRGB24 filt;
160
 
                        filterImage(filt, m_viewportImage, m_capSize);
 
162
                else {
 
163
 
 
164
                        if (m_depth) {
 
165
                                // Use read pixels with the depth buffer
 
166
                                // See warning above about m_viewportImage.
 
167
                                glReadPixels(m_upLeft[0], m_upLeft[1], (GLsizei)m_capSize[0], (GLsizei)m_capSize[1],
 
168
                                        GL_DEPTH_COMPONENT, GL_FLOAT, m_viewportImage);
 
169
                                // filter loaded data
 
170
                                FilterDEPTH filt;
 
171
                                filterImage(filt, (float *)m_viewportImage, m_capSize);
 
172
                        }
 
173
                        else {
 
174
 
 
175
                                // get frame buffer data
 
176
                                if (m_alpha) {
 
177
                                        glReadPixels(m_upLeft[0], m_upLeft[1], (GLsizei)m_capSize[0], (GLsizei)m_capSize[1], GL_RGBA,
 
178
                                                GL_UNSIGNED_BYTE, m_viewportImage);
 
179
                                        // filter loaded data
 
180
                                        FilterRGBA32 filt;
 
181
                                        filterImage(filt, m_viewportImage, m_capSize);
 
182
                                }
 
183
                                else {
 
184
                                        glReadPixels(m_upLeft[0], m_upLeft[1], (GLsizei)m_capSize[0], (GLsizei)m_capSize[1], GL_RGB,
 
185
                                                GL_UNSIGNED_BYTE, m_viewportImage);
 
186
                                        // filter loaded data
 
187
                                        FilterRGB24 filt;
 
188
                                        filterImage(filt, m_viewportImage, m_capSize);
 
189
                                }
 
190
                        }
161
191
                }
162
192
        }
163
193
}
165
195
 
166
196
 
167
197
// cast Image pointer to ImageViewport
168
 
inline ImageViewport * getImageViewport (PyImage * self)
 
198
inline ImageViewport * getImageViewport (PyImage *self)
169
199
{ return static_cast<ImageViewport*>(self->m_image); }
170
200
 
171
201
 
173
203
 
174
204
 
175
205
// get whole
176
 
PyObject * ImageViewport_getWhole (PyImage * self, void * closure)
 
206
PyObject *ImageViewport_getWhole (PyImage *self, void *closure)
177
207
{
178
208
        if (self->m_image != NULL && getImageViewport(self)->getWhole()) Py_RETURN_TRUE;
179
209
        else Py_RETURN_FALSE;
180
210
}
181
211
 
182
212
// set whole
183
 
int ImageViewport_setWhole (PyImage * self, PyObject * value, void * closure)
 
213
int ImageViewport_setWhole (PyImage *self, PyObject *value, void *closure)
184
214
{
185
215
        // check parameter, report failure
186
216
        if (value == NULL || !PyBool_Check(value))
203
233
}
204
234
 
205
235
// get alpha
206
 
PyObject * ImageViewport_getAlpha (PyImage * self, void * closure)
 
236
PyObject *ImageViewport_getAlpha (PyImage *self, void *closure)
207
237
{
208
238
        if (self->m_image != NULL && getImageViewport(self)->getAlpha()) Py_RETURN_TRUE;
209
239
        else Py_RETURN_FALSE;
210
240
}
211
241
 
212
242
// set whole
213
 
int ImageViewport_setAlpha (PyImage * self, PyObject * value, void * closure)
 
243
int ImageViewport_setAlpha (PyImage *self, PyObject *value, void *closure)
214
244
{
215
245
        // check parameter, report failure
216
246
        if (value == NULL || !PyBool_Check(value))
226
256
 
227
257
 
228
258
// get position
229
 
static PyObject * ImageViewport_getPosition (PyImage * self, void * closure)
 
259
static PyObject *ImageViewport_getPosition (PyImage *self, void *closure)
230
260
{
231
 
        return Py_BuildValue("(ii)", getImageViewport(self)->getPosition()[0],
232
 
                getImageViewport(self)->getPosition()[1]);
 
261
        GLint *pos = getImageViewport(self)->getPosition();
 
262
        PyObject *ret = PyTuple_New(2);
 
263
        PyTuple_SET_ITEM(ret, 0, PyLong_FromLong(pos[0]));
 
264
        PyTuple_SET_ITEM(ret, 1, PyLong_FromLong(pos[1]));
 
265
        return ret;
233
266
}
234
267
 
235
268
// set position
236
 
static int ImageViewport_setPosition (PyImage * self, PyObject * value, void * closure)
 
269
static int ImageViewport_setPosition (PyImage *self, PyObject *value, void *closure)
237
270
{
238
271
        // check validity of parameter
239
 
        if (value == NULL || !PySequence_Check(value) || PySequence_Size(value) != 2
240
 
                || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 0))
241
 
                || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 1)))
 
272
        if (value == NULL ||
 
273
            !(PyTuple_Check(value) || PyList_Check(value)) ||
 
274
            PySequence_Fast_GET_SIZE(value) != 2 ||
 
275
            !PyLong_Check(PySequence_Fast_GET_ITEM(value, 0)) ||
 
276
            !PyLong_Check(PySequence_Fast_GET_ITEM(value, 1)))
242
277
        {
243
278
                PyErr_SetString(PyExc_TypeError, "The value must be a sequence of 2 ints");
244
279
                return -1;
245
280
        }
246
281
        // set position
247
 
        GLint pos [] = {
248
 
                GLint(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 0))),
249
 
                        GLint(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 1)))
 
282
        GLint pos[2] = {
 
283
            GLint(PyLong_AsLong(PySequence_Fast_GET_ITEM(value, 0))),
 
284
            GLint(PyLong_AsLong(PySequence_Fast_GET_ITEM(value, 1)))
250
285
        };
251
286
        getImageViewport(self)->setPosition(pos);
252
287
        // success
254
289
}
255
290
 
256
291
// get capture size
257
 
PyObject * ImageViewport_getCaptureSize (PyImage * self, void * closure)
 
292
PyObject *ImageViewport_getCaptureSize (PyImage *self, void *closure)
258
293
{
259
 
        return Py_BuildValue("(ii)", getImageViewport(self)->getCaptureSize()[0],
260
 
                getImageViewport(self)->getCaptureSize()[1]);
 
294
        short *size = getImageViewport(self)->getCaptureSize();
 
295
        PyObject *ret = PyTuple_New(2);
 
296
        PyTuple_SET_ITEM(ret, 0, PyLong_FromLong(size[0]));
 
297
        PyTuple_SET_ITEM(ret, 1, PyLong_FromLong(size[1]));
 
298
        return ret;
261
299
}
262
300
 
263
301
// set capture size
264
 
int ImageViewport_setCaptureSize (PyImage * self, PyObject * value, void * closure)
 
302
int ImageViewport_setCaptureSize (PyImage *self, PyObject *value, void *closure)
265
303
{
266
304
        // check validity of parameter
267
 
        if (value == NULL || !PySequence_Check(value) || PySequence_Size(value) != 2
268
 
                || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 0))
269
 
                || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 1)))
 
305
        if (value == NULL ||
 
306
            !(PyTuple_Check(value) || PyList_Check(value)) ||
 
307
            PySequence_Fast_GET_SIZE(value) != 2 ||
 
308
            !PyLong_Check(PySequence_Fast_GET_ITEM(value, 0)) ||
 
309
            !PyLong_Check(PySequence_Fast_GET_ITEM(value, 1)))
270
310
        {
271
311
                PyErr_SetString(PyExc_TypeError, "The value must be a sequence of 2 ints");
272
312
                return -1;
273
313
        }
274
314
        // set capture size
275
 
        short size [] = {
276
 
                short(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 0))),
277
 
                        short(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 1)))
 
315
        short size[2] = {
 
316
            short(PyLong_AsLong(PySequence_Fast_GET_ITEM(value, 0))),
 
317
            short(PyLong_AsLong(PySequence_Fast_GET_ITEM(value, 1)))
278
318
        };
279
319
        try
280
320
        {
310
350
        {(char*)"size", (getter)Image_getSize, NULL, (char*)"image size", NULL},
311
351
        {(char*)"scale", (getter)Image_getScale, (setter)Image_setScale, (char*)"fast scale of image (near neighbor)", NULL},
312
352
        {(char*)"flip", (getter)Image_getFlip, (setter)Image_setFlip, (char*)"flip image vertically", NULL},
 
353
        {(char*)"zbuff", (getter)Image_getZbuff, (setter)Image_setZbuff, (char*)"use depth buffer as texture", NULL},
 
354
        {(char*)"depth", (getter)Image_getDepth, (setter)Image_setDepth, (char*)"get depth information from z-buffer as array of float", NULL},
313
355
        {(char*)"filter", (getter)Image_getFilter, (setter)Image_setFilter, (char*)"pixel filter", NULL},
314
356
        {NULL}
315
357
};