Texture.cc 5.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Definition of the class Texture.
 *
 *   @author Lionel Heng <hengli@student.ethz.ch>
 *
 */

32 33
#include <osg/LineWidth>

34 35
#include "Texture.h"

36 37 38 39
Texture::Texture(unsigned int _id)
    : id(_id)
    , texture2D(new osg::Texture2D)
    , geometry(new osg::Geometry)
40
{
41 42
    texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
    texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
43 44 45 46 47 48 49

    texture2D->setDataVariance(osg::Object::DYNAMIC);
    texture2D->setResizeNonPowerOfTwoHint(false);

    osg::ref_ptr<osg::Image> image = new osg::Image;
    texture2D->setImage(image);

50
    osg::ref_ptr<osg::Vec3dArray> vertices(new osg::Vec3dArray(4));
51 52 53 54 55 56 57
    geometry->setVertexArray(vertices);

    osg::ref_ptr<osg::Vec2Array> textureCoords = new osg::Vec2Array;
    textureCoords->push_back(osg::Vec2(0.0f, 1.0f));
    textureCoords->push_back(osg::Vec2(1.0f, 1.0f));
    textureCoords->push_back(osg::Vec2(1.0f, 0.0f));
    textureCoords->push_back(osg::Vec2(0.0f, 0.0f));
58
    geometry->setTexCoordArray(0, textureCoords);
59 60 61 62 63 64 65 66 67 68

    geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,
                                                  0, 4));

    osg::ref_ptr<osg::Vec4Array> colors(new osg::Vec4Array);
    colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
    geometry->setColorArray(colors);
    geometry->setColorBinding(osg::Geometry::BIND_OVERALL);

    geometry->setUseDisplayList(false);
69 70 71 72 73 74 75

    osg::ref_ptr<osg::LineWidth> linewidth(new osg::LineWidth);
    linewidth->setWidth(2.0f);
    geometry->getOrCreateStateSet()->
            setAttributeAndModes(linewidth, osg::StateAttribute::ON);
    geometry->getOrCreateStateSet()->
            setMode(GL_LIGHTING, osg::StateAttribute::OFF);
76 77 78 79 80 81 82 83 84 85 86 87 88 89
}

const QString&
Texture::getSourceURL(void) const
{
    return sourceURL;
}

void
Texture::sync(const WebImagePtr& image)
{
    state = static_cast<State>(image->getState());

    if (image->getState() != WebImage::UNINITIALIZED &&
90
        sourceURL != image->getSourceURL())
91 92 93 94 95 96 97 98
    {
        sourceURL = image->getSourceURL();
    }

    if (image->getState() == WebImage::READY && image->getSyncFlag())
    {
        image->setSyncFlag(false);

99
        if (texture2D->getImage() != NULL)
100
        {
101 102 103
            texture2D->getImage()->setImage(image->getWidth(),
                                            image->getHeight(),
                                            1,
104 105
                                            GL_RGBA,
                                            GL_RGBA,
106 107 108 109
                                            GL_UNSIGNED_BYTE,
                                            image->getImageData(),
                                            osg::Image::NO_DELETE);
            texture2D->getImage()->dirty();
110 111 112 113
        }
    }
}

114
osg::ref_ptr<osg::Geometry>
115
Texture::draw(double x1, double y1, double x2, double y2,
116
              double z,
117 118
              bool smoothInterpolation) const
{
119
    return draw(x1, y1, x2, y1, x2, y2, x1, y2, z, smoothInterpolation);
120 121
}

122
osg::ref_ptr<osg::Geometry>
123 124
Texture::draw(double x1, double y1, double x2, double y2,
              double x3, double y3, double x4, double y4,
125
              double z,
126 127
              bool smoothInterpolation) const
{
128 129
    osg::Vec3dArray* vertices =
            static_cast<osg::Vec3dArray*>(geometry->getVertexArray());
130 131 132 133
    (*vertices)[0].set(x1, y1, z);
    (*vertices)[1].set(x2, y2, z);
    (*vertices)[2].set(x3, y3, z);
    (*vertices)[3].set(x4, y4, z);
134 135 136 137 138

    osg::DrawArrays* drawarrays =
            static_cast<osg::DrawArrays*>(geometry->getPrimitiveSet(0));
    osg::Vec4Array* colors =
            static_cast<osg::Vec4Array*>(geometry->getColorArray());
139

140 141
    if (state == REQUESTED)
    {
142
        drawarrays->set(osg::PrimitiveSet::LINE_LOOP, 0, 4);
143
        (*colors)[0].set(0.0f, 0.0f, 1.0f, 1.0f);
144
        
145
        geometry->getOrCreateStateSet()->
146
                setTextureAttributeAndModes(0, texture2D, osg::StateAttribute::OFF);
147

148
        return geometry;
149 150 151 152
    }

    if (smoothInterpolation)
    {
153 154
        texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
        texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
155 156 157
    }
    else
    {
158 159
        texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
        texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
160 161
    }

162 163
    drawarrays->set(osg::PrimitiveSet::POLYGON, 0, 4);
    (*colors)[0].set(1.0f, 1.0f, 1.0f, 1.0f);
164

165
    geometry->getOrCreateStateSet()->
166
            setTextureAttributeAndModes(0, texture2D, osg::StateAttribute::ON);
167

168
    return geometry;
169
}