File: MapTextureToQuad.cpp

package info (click to toggle)
libwildmagic 5.17%2Bcleaned1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 90,124 kB
  • sloc: cpp: 215,940; csh: 637; sh: 91; makefile: 40
file content (253 lines) | stat: -rw-r--r-- 7,439 bytes parent folder | download | duplicates (3)
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
32
33
34
35
36
37
38
39
40
41
42
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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
252
253
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.0 (2010/01/01)

#include "MapTextureToQuad.h"

WM5_WINDOW_APPLICATION(MapTextureToQuad);

//----------------------------------------------------------------------------
MapTextureToQuad::MapTextureToQuad ()
    :
    WindowApplication2("SampleMathematics/MapTextureToQuad", 0, 0, 256, 256,
        Float4(1.0f, 1.0f, 1.0f, 1.0f))
{
    mTexture = 0;
    mMapping = 0;
    mMouseDown = false;
    mSelected = -1;
}
//----------------------------------------------------------------------------
bool MapTextureToQuad::OnInitialize ()
{
    if (!WindowApplication2::OnInitialize())
    {
        return false;
    }

    // The texture whose image will be drawn on the quadrilateral.
    std::string path = Environment::GetPathR("MagicianSmallReflectY.wmtf");
    mTexture = Texture2D::LoadWMTF(path);

    // The quadrilateral to which the image is perspectively mapped.  The
    // default is the original image rectangle (the initial mapping is the
    // identity).
    int dim0 = mTexture->GetDimension(0, 0);
    int dim1 = mTexture->GetDimension(1, 0);
    float xmax = dim0 - 1.0f;
    float ymax = dim1 - 1.0f;
    mVertex[0] = Vector2f(0.0f, 0.0f);
    mVertex[1] = Vector2f(xmax, 0.0f);
    mVertex[2] = Vector2f(xmax, ymax);
    mVertex[3] = Vector2f(0.0f, ymax);

    Vector2f offset(0.5f*(GetWidth() - dim0), 0.5f*(GetHeight() - dim1));
    for (int i = 0; i < 4; ++i)
    {
        mVertex[i] += offset;
    }

    CreateMapping();
    return true;
}
//----------------------------------------------------------------------------
void MapTextureToQuad::OnTerminate ()
{
    delete0(mTexture);
    delete0(mMapping);

    WindowApplication2::OnTerminate();
}
//----------------------------------------------------------------------------
bool MapTextureToQuad::OnMouseClick (int button, int state, int x, int y,
    unsigned int)
{
    if (button != MOUSE_LEFT_BUTTON)
    {
        return false;
    }

    if (state == MOUSE_DOWN)
    {
        mMouseDown = true;
        SelectVertex(Vector2f((float)x, (float)y));
        return true;
    }

    if (state == MOUSE_UP)
    {
        mMouseDown = false;
        if (mSelected >= 0
        &&  0 <= x && x < GetWidth()
        &&  0 <= y && y < GetHeight())
        {
            UpdateQuadrilateral(Vector2f((float)x, (float)y));
        }
        return true;
    }

    return false;
}
//----------------------------------------------------------------------------
bool MapTextureToQuad::OnMotion (int, int x, int y, unsigned int)
{
    if (mMouseDown)
    {
        if (mSelected >= 0
        &&  0 <= x && x < GetWidth()
        &&  0 <= y && y < GetHeight())
        {
            UpdateQuadrilateral(Vector2f((float)x, (float)y));
        }
        return true;
    }
    return false;
}
//----------------------------------------------------------------------------
void MapTextureToQuad::CreateMapping ()
{
    // Create the new perspective mapping from the *target* quadrilateral to
    // the *source* square bitmap.  The mapping is in this direction to avoid
    // holes in the drawn quadrilateral.
    delete0(mMapping);
#ifdef USE_HM_QUAD_TO_SQR
    mMapping = new0 HmQuadToSqrf(mVertex[0], mVertex[1], mVertex[2],
        mVertex[3]);
#else
    mMapping = new0 BiQuadToSqrf(mVertex[0], mVertex[1], mVertex[2],
        mVertex[3]);
#endif

    // Compute axis-aligned bounding box.
    int xmin = GetWidth(), xmax = 0, ymin = GetWidth(), ymax = 0;
    for (int i = 0; i < 4; ++i)
    {
        if (xmin > (int)mVertex[i].X())
        {
            xmin = (int)mVertex[i].X();
        }

        if (xmax < (int)mVertex[i].X())
        {
            xmax = (int)mVertex[i].X();
        }

        if (ymin > (int)mVertex[i].Y())
        {
            ymin = (int)mVertex[i].Y();
        }

        if (ymax < (int)mVertex[i].Y())
        {
            ymax = (int)mVertex[i].Y();
        }
    }

    // Draw perspective mapping of image (...inefficient drawing...).
    ClearScreen();
    int xSize = mTexture->GetDimension(0, 0);
    int ySize = mTexture->GetDimension(1, 0);
    unsigned char* data = (unsigned char*)mTexture->GetData(0);
    for (int trgY = ymin; trgY <= ymax; ++trgY)
    {
        Vector2f quad;
        quad.Y() = (float)trgY;

        int xStart = xmin;
        while (xStart <= xmax)
        {
            quad.X() = (float)xStart;
            if (PointInPolygon2f(4, mVertex).ContainsQuadrilateral(quad))
            {
                break;
            }
            ++xStart;
        }

        int xFinal = xmax;
        while (xFinal >= xmin)
        {
            quad.X() = (float)xFinal;
            if (PointInPolygon2f(4, mVertex).ContainsQuadrilateral(quad))
            {
                break;
            }
            --xFinal;
        }

        for (int trgX = xStart; trgX <= xFinal; ++trgX)
        {
            // Transform point to unit square.
            quad.X() = (float)trgX;
            Vector2f square = mMapping->Transform(quad);

            // Convert to bitmap coordinates (using clamping).
            int srcX = (int)((xSize - 1)*square.X());
            if (srcX < 0)
            {
                srcX = 0;
            }
            else if (srcX >= xSize)
            {
                srcX = xSize - 1;
            }

            int srcY = (int)((ySize - 1)*square.Y());
            if (srcY < 0)
            {
                srcY = 0;
            }
            else if (srcY >= ySize)
            {
                srcY = ySize - 1;
            }

            int srcIndex = 4*(srcX + xSize*srcY);
            unsigned char* srcRGBA = data + srcIndex;
            int trgIndex = trgX + GetWidth()*trgY;
            unsigned char* trgRGB = (unsigned char*)(mScreen + trgIndex);

            for (int i = 0; i < 3; ++i)
            {
                trgRGB[i] = srcRGBA[i];
            }
        }
    }
    OnDisplay();
}
//----------------------------------------------------------------------------
void MapTextureToQuad::SelectVertex (const Vector2f& position)
{
    // Identify vertex within 5 pixels of mouse click.
    const float pixelRange = 5.0f;
    mSelected = -1;
    for (int i = 0; i < 4; ++i)
    {
        Vector2f diff = position - mVertex[i];
        if (diff.Length() <= pixelRange)
        {
            mSelected = i;
            break;
        }
    }
}
//----------------------------------------------------------------------------
void MapTextureToQuad::UpdateQuadrilateral (const Vector2f& position)
{
    // Quadrilateral must remain convex.
    int prev = (mSelected > 0 ? mSelected - 1 : 3);
    int next = (mSelected < 3 ? mSelected + 1 : 0);
    Vector2f diff1 = position - mVertex[prev];
    Vector2f diff2 = mVertex[next] - position;
    float det = diff1.X()*diff2.Y() - diff1.Y()*diff2.X();
    if (det > 0.0f)
    {
        mVertex[mSelected] = position;
        CreateMapping();
    }
}
//----------------------------------------------------------------------------