Texture.cc 5.49 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
/*=====================================================================

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.
 *
28
 *   @author Lionel Heng <hengli@inf.ethz.ch>
29 30 31
 *
 */

32 33
#include <osg/LineWidth>

34 35
#include "Texture.h"

36 37 38 39
Texture::Texture(quint64 id)
 : mId(id)
 , mTexture2D(new osg::Texture2D)
 , mGeometry(new osg::Geometry)
40
{
41 42
    mTexture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
    mTexture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
43

44 45
    mTexture2D->setDataVariance(osg::Object::DYNAMIC);
    mTexture2D->setResizeNonPowerOfTwoHint(false);
46 47

    osg::ref_ptr<osg::Image> image = new osg::Image;
48
    mTexture2D->setImage(image);
49

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

    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
    mGeometry->setTexCoordArray(0, textureCoords);
59

60
    mGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,
61
                              0, 4));
62 63 64

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

68
    mGeometry->setUseDisplayList(false);
69 70 71

    osg::ref_ptr<osg::LineWidth> linewidth(new osg::LineWidth);
    linewidth->setWidth(2.0f);
72
    mGeometry->getOrCreateStateSet()->
73
    setAttributeAndModes(linewidth, osg::StateAttribute::ON);
74
    mGeometry->getOrCreateStateSet()->
75
    setMode(GL_LIGHTING, osg::StateAttribute::OFF);
76 77 78 79 80
}

const QString&
Texture::getSourceURL(void) const
{
81 82 83 84 85 86 87
    return mSourceURL;
}

void
Texture::setId(quint64 id)
{
    mId = id;
88 89 90 91 92
}

void
Texture::sync(const WebImagePtr& image)
{
93
    mState = static_cast<State>(image->getState());
94 95

    if (image->getState() != WebImage::UNINITIALIZED &&
96 97 98
        mSourceURL != image->getSourceURL())
    {
        mSourceURL = image->getSourceURL();
99 100
    }

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

105 106 107 108 109 110 111 112 113 114 115
        if (mTexture2D->getImage() != NULL)
        {
            mTexture2D->getImage()->setImage(image->getWidth(),
                                             image->getHeight(),
                                             1,
                                             GL_RGBA,
                                             GL_RGBA,
                                             GL_UNSIGNED_BYTE,
                                             image->getImageData(),
                                             osg::Image::NO_DELETE);
            mTexture2D->getImage()->dirty();
116 117 118 119
        }
    }
}

120
osg::ref_ptr<osg::Geometry>
121
Texture::draw(double x1, double y1, double x2, double y2,
122
              double z,
123 124
              bool smoothInterpolation) const
{
125
    return draw(x1, y1, x2, y1, x2, y2, x1, y2, z, smoothInterpolation);
126 127
}

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

    osg::DrawArrays* drawarrays =
142
        static_cast<osg::DrawArrays*>(mGeometry->getPrimitiveSet(0));
143
    osg::Vec4Array* colors =
144
        static_cast<osg::Vec4Array*>(mGeometry->getColorArray());
145

146 147
    if (mState == REQUESTED)
    {
148
        drawarrays->set(osg::PrimitiveSet::LINE_LOOP, 0, 4);
149
        (*colors)[0].set(0.0f, 0.0f, 1.0f, 1.0f);
150

151 152
        mGeometry->getOrCreateStateSet()->
        setTextureAttributeAndModes(0, mTexture2D, osg::StateAttribute::OFF);
153

154
        return mGeometry;
155 156
    }

157 158 159 160 161 162 163 164 165
    if (smoothInterpolation)
    {
        mTexture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
        mTexture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
    }
    else
    {
        mTexture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
        mTexture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
166 167
    }

168 169
    drawarrays->set(osg::PrimitiveSet::POLYGON, 0, 4);
    (*colors)[0].set(1.0f, 1.0f, 1.0f, 1.0f);
170

171 172
    mGeometry->getOrCreateStateSet()->
        setTextureAttributeAndModes(0, mTexture2D, osg::StateAttribute::ON);
173

174
    return mGeometry;
175
}