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
15 using namespace osg;
16
DrawPixels()17 DrawPixels::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
DrawPixels(const DrawPixels & drawimage,const CopyOp & copyop)31 DrawPixels::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
~DrawPixels()43 DrawPixels::~DrawPixels()
44 {
45 // image will delete itself thanks to ref_ptr :-)
46 }
47
setPosition(const osg::Vec3 & position)48 void DrawPixels::setPosition(const osg::Vec3& position)
49 {
50 _position = position;
51 dirtyBound();
52 }
53
setSubImageDimensions(unsigned int offsetX,unsigned int offsetY,unsigned int width,unsigned int height)54 void 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
getSubImageDimensions(unsigned int & offsetX,unsigned int & offsetY,unsigned int & width,unsigned int & height) const63 void 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
computeBoundingBox() const72 BoundingBox DrawPixels::computeBoundingBox() 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
drawImplementation(RenderInfo &) const91 void 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 }
120
121