Texture.cc 8.3 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 36 37 38
#include "Texture.h"

Texture::Texture()
    : _is3D(false)
{
39 40 41 42 43 44 45 46 47
    texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
    texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
    GLuint id;
    glGenTextures(1, &id);
    t->setID(id);
    glBindTexture(GL_TEXTURE_2D, id);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
48 49 50 51 52 53 54 55 56
}

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

void
57
Texture::setId(unsigned int _id)
58
{
59
    id = _id;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
}

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

    if (image->getState() != WebImage::UNINITIALIZED &&
        (sourceURL != image->getSourceURL() ||
         _is3D != image->is3D()))
    {
        sourceURL = image->getSourceURL();
        _is3D = image->is3D();
    }

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

        if (image->getWidth() != imageWidth ||
            image->getHeight() != imageHeight)
        {
            imageWidth = image->getWidth();
            textureWidth = 32;
            while (textureWidth < imageWidth)
            {
                textureWidth *= 2;
            }
            imageHeight = image->getHeight();
            textureHeight = 32;
            while (textureHeight < imageHeight)
            {
                textureHeight *= 2;
            }

            maxU = static_cast<double>(imageWidth)
                   / static_cast<double>(textureWidth);
            maxV = static_cast<double>(imageHeight)
                   / static_cast<double>(textureHeight);

100 101 102 103
            osg::ref_ptr<osg::Image> image;
            image->setImage(textureWidth, textureHeight, 8, 3, GL_RGBA, GL_UNSIGNED_BYTES, NULL, osg::Image::USE_NEW_DELETE);

            texture2D->
104 105 106 107 108 109 110 111 112 113 114 115 116
            glBindTexture(GL_TEXTURE_2D, id);
            glTexImage2D(GL_TEXTURE_2D, 0, 3, textureWidth, textureHeight,
                         0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
        }

        glBindTexture(GL_TEXTURE_2D, id);
        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, imageWidth, imageHeight,
                        GL_RGBA, GL_UNSIGNED_BYTE, image->getImageData());

        heightModel = image->getHeightModel();
    }
}

117
osg::ref_ptr<osg::Geometry>
118 119 120
Texture::draw(float x1, float y1, float x2, float y2,
              bool smoothInterpolation) const
{
121
    return draw(x1, y1, x2, y1, x2, y2, x1, y2, smoothInterpolation);
122 123
}

124
osg::ref_ptr<osg::Geometry>
125 126 127 128
Texture::draw(float x1, float y1, float x2, float y2,
              float x3, float y3, float x4, float y4,
              bool smoothInterpolation) const
{
129 130 131
    osg::ref_ptr<osg::Geometry> geometry(new osg::Geometry);
    osg::ref_ptr<osg::StateSet> stateset(new osg::StateSet);

132 133
    if (state == REQUESTED)
    {
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        osg::ref_ptr<osg::Vec2Array> vertices(new osg::Vec2Array);
        vertices->push_back(osg::Vec2(x1, y1));
        vertices->push_back(osg::Vec2(x2, y2));
        vertices->push_back(osg::Vec2(x3, y3));
        vertices->push_back(osg::Vec2(x4, y4));

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

        geometry->setVertexArray(vertices);

        osg::ref_ptr<osg::Vec4Array> color(new osg::Vec4Array);
        color->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
        geometry->setColorArray(color);
        geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
        
        return geometry;
151 152
    }

153
    stateset->setTextureAttributeAndModes(id, texture2D);
154 155 156 157

    float dx, dy;
    if (smoothInterpolation)
    {
158 159
        texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
        texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
160 161 162 163 164
        dx = 1.0f / (2.0f * textureWidth);
        dy = 1.0f / (2.0f * textureHeight);
    }
    else
    {
165 166
        texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
        texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
167 168 169 170 171 172 173
        dx = 0.0f;
        dy = 0.0f;
    }

    glColor3f(1.0f, 1.0f, 1.0f);
    if (!_is3D)
    {
174 175 176 177 178 179 180 181
        osg::ref_ptr<osg::Vec2Array> tc = new osg::Vec2Array;

        geometry->setTexCoordArray(id, tc);
        tc->push_back(osg::Vec2(dx, maxV - dy));
        tc->push_back(osg::Vec2(maxU - dx, maxV - dy));
        tc->push_back(osg::Vec2(maxU - dx, dy));
        tc->push_back(osg::Vec2(dx, dy));

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        glBegin(GL_QUADS);
        glTexCoord2f(dx, maxV - dy);
        glVertex3f(x1, y1, 0.0f);
        glTexCoord2f(maxU - dx, maxV - dy);
        glVertex3f(x2, y2, 0.0f);
        glTexCoord2f(maxU - dx, dy);
        glVertex3f(x3, y3, 0.0f);
        glTexCoord2f(dx, dy);
        glVertex3f(x4, y4, 0.0f);
        glEnd();
    }
    else
    {
        float scaleX = 1.0f / static_cast<float>(heightModel.size() - 1);

        for (int32_t i = 0; i < heightModel.size() - 1; ++i)
        {
            float scaleI = scaleX * static_cast<float>(i);

            float scaleY =
                    1.0f / static_cast<float>(heightModel[i].size() - 1);

            float x1i = x1 + scaleI * (x4 - x1);
            float x1f = x2 + scaleI * (x3 - x2);
            float x2i = x1i + scaleX * (x4 - x1);
            float x2f = x1f + scaleX * (x3 - x2);

            for (int32_t j = 0; j < heightModel[i].size() - 1; ++j)
            {
                float scaleJ = scaleY * static_cast<float>(j);

                float y1i = y1 + scaleJ * (y2 - y1);
                float y1f = y4 + scaleJ * (y3 - y4);
                float y2i = y1i + scaleY * (y2 - y1);
                float y2f = y1f + scaleY * (y3 - y4);

                float nx1 = x1i + scaleJ * (x1f - x1i);
                float nx2 = x1i + (scaleJ + scaleY) * (x1f - x1i);
                float nx3 = x2i + (scaleJ + scaleY) * (x2f - x2i);
                float nx4  = x2i + scaleJ * (x2f - x2i);
                float ny1 = y1i + scaleI * (y1f - y1i);
                float ny2 = y2i + scaleI * (y2f - y2i);
                float ny3 = y2i + (scaleI + scaleX) * (y2f - y2i);
                float ny4 = y1i + (scaleI + scaleX) * (y1f - y1i);

                glBegin(GL_QUADS);
                glTexCoord2f(dx + scaleJ * (maxU - dx * 2.0f),
                             dy + (1.0f - scaleI) * (maxV - dy * 2.0f));
                glVertex3f(nx1, ny1, -static_cast<float>(heightModel[i][j]));
                glTexCoord2f(dx + (scaleJ + scaleY) * (maxU - dx * 2.0f),
                             dy + (1.0f - scaleI) * (maxV - dy * 2.0f));
                glVertex3f(nx2, ny2, -static_cast<float>(heightModel[i][j + 1]));
                glTexCoord2f(dx + (scaleJ + scaleY) * (maxU - dx * 2.0f),
                             dy + (1.0f - scaleI - scaleX) * (maxV - dy * 2.0f));
                glVertex3f(nx3, ny3, -static_cast<float>(heightModel[i + 1][j + 1]));
                glTexCoord2f(dx + scaleJ * (maxU - dx * 2.0f),
                             dy + (1.0f - scaleI - scaleX) * (maxV - dy * 2.0f));
                glVertex3f(nx4, ny4, -static_cast<float>(heightModel[i + 1][j]));

                glEnd();
            }
        }
    }
}

bool
Texture::is3D(void) const
{
    return _is3D;
}