Texture.cc 5.39 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

    geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,
61
                              0, 4));
62 63 64 65 66 67 68

    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

    osg::ref_ptr<osg::LineWidth> linewidth(new osg::LineWidth);
    linewidth->setWidth(2.0f);
    geometry->getOrCreateStateSet()->
73
    setAttributeAndModes(linewidth, osg::StateAttribute::ON);
74
    geometry->getOrCreateStateSet()->
75
    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
        sourceURL = image->getSourceURL();
    }

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

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

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

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

    osg::DrawArrays* drawarrays =
133
        static_cast<osg::DrawArrays*>(geometry->getPrimitiveSet(0));
134
    osg::Vec4Array* colors =
135
        static_cast<osg::Vec4Array*>(geometry->getColorArray());
136

137
    if (state == REQUESTED) {
138
        drawarrays->set(osg::PrimitiveSet::LINE_LOOP, 0, 4);
139
        (*colors)[0].set(0.0f, 0.0f, 1.0f, 1.0f);
140

141
        geometry->getOrCreateStateSet()->
142
        setTextureAttributeAndModes(0, texture2D, osg::StateAttribute::OFF);
143

144
        return geometry;
145 146
    }

147
    if (smoothInterpolation) {
148 149
        texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
        texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
150
    } else {
151 152
        texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
        texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
153 154
    }

155 156
    drawarrays->set(osg::PrimitiveSet::POLYGON, 0, 4);
    (*colors)[0].set(1.0f, 1.0f, 1.0f, 1.0f);
157

158
    geometry->getOrCreateStateSet()->
159
    setTextureAttributeAndModes(0, texture2D, osg::StateAttribute::ON);
160

161
    return geometry;
162
}