root/OpenSceneGraph/trunk/src/osg/DrawPixels.cpp @ 12912

Revision 12912, 3.5 kB (checked in by robert, 2 years ago)

Added support for using GL_UNPACK_ROW_LENGTH in conjunction with texture's + osg::Image via new RowLength?
parameter in osg::Image. To support this Image::setData(..) now has a new optional rowLength parameter which
defaults to 0, which provides the original behaviour, Image::setRowLength(int) and int Image::getRowLength() are also provided.

With the introduction of RowLength? support in osg::Image it is now possible to create a sub image where
the t size of the image are smaller than the row length, useful for when you have a large image on the CPU
and which to use a small portion of it on the GPU. However, when these sub images are created the data
within the image is no longer contiguous so data access can no longer assume that all the data is in
one block. The new method Image::isDataContiguous() enables the user to check whether the data is contiguous,
and if not one can either access the data row by row using Image::data(column,row,image) accessor, or use the
new Image::DataIterator? for stepping through each block on memory assocatied with the image.

To support the possibility of non contiguous osg::Image usage of image objects has had to be updated to
check DataContiguous? and handle the case or use access via the DataIerator? or by row by row. To achieve
this a relatively large number of files has had to be modified, in particular the texture classes and
image plugins that doing writing.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under 
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13#include <osg/DrawPixels>
14
15using namespace osg;
16
17DrawPixels::DrawPixels()
18{
19    // turn off display lists right now, just incase we want to modify the projection matrix along the way.
20    setSupportsDisplayList(false);
21   
22    _position.set(0.0f,0.0f,0.0f);
23   
24    _useSubImage = false;
25    _offsetX = 0;
26    _offsetY = 0;
27    _width = 0;
28    _height = 0;
29}
30
31DrawPixels::DrawPixels(const DrawPixels& drawimage,const CopyOp& copyop):
32    Drawable(drawimage,copyop),
33    _position(drawimage._position),
34    _image(drawimage._image),
35    _useSubImage(drawimage._useSubImage),
36    _offsetX(drawimage._offsetX),
37    _offsetY(drawimage._offsetY),
38    _width(drawimage._width),
39    _height(drawimage._height)
40{
41}
42
43DrawPixels::~DrawPixels()
44{
45    // image will delete itself thanks to ref_ptr :-)
46}
47
48void DrawPixels::setPosition(const osg::Vec3& position)
49{
50    _position = position;
51    dirtyBound();
52}
53
54void DrawPixels::setSubImageDimensions(unsigned int offsetX,unsigned int offsetY,unsigned int width,unsigned int height)
55{
56    _useSubImage = true;
57    _offsetX = offsetX;
58    _offsetY = offsetY;
59    _width = width;
60    _height = height;
61}
62
63void DrawPixels::getSubImageDimensions(unsigned int& offsetX,unsigned int& offsetY,unsigned int& width,unsigned int& height) const
64{
65    offsetX = _offsetX;
66    offsetY = _offsetY;
67    width = _width;
68    height = _height;
69}
70
71
72BoundingBox DrawPixels::computeBound() const
73{
74    // really needs to be dependent of view position and projection... will implement simple version right now.
75    BoundingBox bbox;
76    float diagonal = 0.0f;
77    if (_useSubImage)
78    {
79        diagonal = sqrtf(_width*_width+_height*_height);
80    }
81    else
82    {
83        diagonal = sqrtf(_image->s()*_image->s()+_image->t()*_image->t());
84    }
85   
86    bbox.expandBy(_position-osg::Vec3(diagonal,diagonal,diagonal));
87    bbox.expandBy(_position+osg::Vec3(diagonal,diagonal,diagonal));
88    return bbox;
89}
90
91void DrawPixels::drawImplementation(RenderInfo&) const
92{
93#ifdef OSG_GL1_AVAILABLE
94    glRasterPos3f(_position.x(),_position.y(),_position.z());
95
96    if (_useSubImage)
97    {
98        const GLvoid* pixels = _image->data(_offsetX,_offsetY);
99        glPixelStorei(GL_UNPACK_ALIGNMENT,_image->getPacking());
100        glPixelStorei(GL_UNPACK_ROW_LENGTH,_image->s());
101        glDrawPixels(_width,_height,
102                     (GLenum)_image->getPixelFormat(),
103                     (GLenum)_image->getDataType(),
104                     pixels);
105        glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
106    }
107    else
108    {
109        glPixelStorei(GL_UNPACK_ALIGNMENT,_image->getPacking());
110        glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
111        glDrawPixels(_image->s(), _image->t(),
112                     (GLenum)_image->getPixelFormat(),
113                     (GLenum)_image->getDataType(),
114                     _image->data() );
115    }
116#else
117    OSG_NOTICE<<"Warning: DrawPixels::drawImplementation(RenderInfo&) - not supported."<<std::endl;
118#endif
119}
Note: See TracBrowser for help on using the browser.