Texture.cc 5.11 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

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

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

    osg::ref_ptr<osg::Vec2Array> vertices(new osg::Vec2Array(4));
    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));
    geometry->setTexCoordArray(id, textureCoords);

    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 76 77 78 79 80 81 82
}

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 &&
83
        sourceURL != image->getSourceURL())
84 85 86 87 88 89 90 91
    {
        sourceURL = image->getSourceURL();
    }

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

92
        if (texture2D->getImage() != NULL)
93
        {
94 95 96 97 98 99 100 101 102
            texture2D->getImage()->setImage(image->getWidth(),
                                            image->getHeight(),
                                            1,
                                            GL_RGB,
                                            GL_RGB,
                                            GL_UNSIGNED_BYTE,
                                            image->getImageData(),
                                            osg::Image::NO_DELETE);
            texture2D->getImage()->dirty();
103 104 105 106
        }
    }
}

107
osg::ref_ptr<osg::Geometry>
108 109 110
Texture::draw(float x1, float y1, float x2, float y2,
              bool smoothInterpolation) const
{
111
    return draw(x1, y1, x2, y1, x2, y2, x1, y2, smoothInterpolation);
112 113
}

114
osg::ref_ptr<osg::Geometry>
115 116 117 118
Texture::draw(float x1, float y1, float x2, float y2,
              float x3, float y3, float x4, float y4,
              bool smoothInterpolation) const
{
119 120 121 122 123 124 125 126 127 128 129
    osg::Vec2Array* vertices =
            static_cast<osg::Vec2Array*>(geometry->getVertexArray());
    (*vertices)[0].set(x1, y1);
    (*vertices)[1].set(x2, y2);
    (*vertices)[2].set(x3, y3);
    (*vertices)[3].set(x4, y4);

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

131 132
    if (state == REQUESTED)
    {
133 134
        drawarrays->set(osg::PrimitiveSet::LINES, 0, 4);
        (*colors)[0].set(0.0f, 0.0f, 1.0f, 1.0f);
135
        
136 137 138
        geometry->getOrCreateStateSet()->
                setTextureAttributeAndModes(id, texture2D, osg::StateAttribute::OFF);

139
        return geometry;
140 141 142 143
    }

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

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

156 157
    geometry->getOrCreateStateSet()->
            setTextureAttributeAndModes(id, texture2D, osg::StateAttribute::ON);
158

159
    return geometry;
160
}