summaryrefslogtreecommitdiffstats
path: root/examples/datavisualization/texturesurface/doc/src/texturesurface.qdoc
blob: 5e1d3f656269ea0e4581a7ee9f93fe2a77cbeb43 (plain)
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
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd
** All rights reserved.
** For any questions to The Qt Company, please use contact form at http://qt.io
**
** This file is part of the Qt Data Visualization module.
**
** Licensees holding valid commercial license for Qt may use this file in
** accordance with the Qt License Agreement provided with the Software
** or, alternatively, in accordance with the terms contained in a written
** agreement between you and The Qt Company.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.io
**
****************************************************************************/

/*!
    \example texturesurface
    \title Textured Surface Example
    \ingroup qtdatavisualization_examples
    \brief Using texture with Q3DSurface.
    \since QtDataVisualization 1.2

    The textured surface example shows how to add an image as a texture for a surface. The example
    shows also how to:

    \list
        \li Create a surface series from an image
        \li Use custom input handler to enable zooming and panning
        \li Highlight an area of the surface
    \endlist

    \image texturesurface-example.png

    \section1 Texture to a surface series

    The image to be set as a texture to a surface can be set using QSurface3DSeries::setTextureFile().
    In this example we have added a check box to control if the texture is set or not. The
    following code extract is for reacting to the check box selections. The image in this
    example is read from the resource file where it is as a JPG file. Setting an empty file
    with the method clears the texture, and the surface uses the gradients or colors from the theme.

    \snippet texturesurface/surfacegraph.cpp 0

    \section1 Topographic surface series

    The topographic data for this example is obtained from National Land Survey of Finland. It
    provides a product called \c{Elevation Model 2 m}, which was suitable for our needs. We selected
    Levi fell to be shown. The accuracy of the data was well beyond our needs and therefore it
    is compressed and encoded into a PNG file. The height value from the original ASCII data is
    encoded into RGB format using a multiplier, which you will see later on a code extract.
    The multiplier is calculated simply by dividing the largest 24 bit value with the highest point
    in Finland.

    Qt Data Visualization has a special proxy for height map image files, but it converts
    only one byte values. So to utilize the bigger accuracy on the data from National Land
    Survey of Finland, we read the data from the PNG file and decode it into QSurface3DSeries.
    The following code samples show how this is done.

    First the encoding multiplier.
    \snippet texturesurface/topographicseries.cpp 0

    And then the actual decoding.
    \snippet texturesurface/topographicseries.cpp 1

    \section1 Use custom input handler to enable zooming and panning

    For the panning the implementation is similar to the \l{Axis Range Dragging With Labels Example}.
    The difference is that in this example we follow only dragging of X and Z axis and we don't
    allow dragging the surface outside the graph. The control for this is very simple and done as
    on the following example for the X axis.

    \snippet texturesurface/custominputhandler.cpp 0

    For the zooming we catch the \c wheelEvent and adjust the X and Y axis ranges according to delta
    value on QWheelEvent. The Y axis is also adjusted so that the aspect ratio between Y axis and
    XZ plane stays the same, and we don't get silly looking graph with height exaggerated too much.

    \snippet texturesurface/custominputhandler.cpp 1

    In this case we want to control the zoom level so that it won't get too near to or far from the
    surface. For instance, if the value for the X axis gets below the allowed, i.e. zooming gets too
    far, the value is set to the minimum allowed value. If the range is going to below the range
    minimum, both ends of the axis are adjusted so that the range stays at the limit.

    \snippet texturesurface/custominputhandler.cpp 2

    \section1 Highlight an area of the surface

    The main idea on creating a highlight on the surface is to create a copy of the series and add
    a bit of offset to the y value. On this example the class \c HighlightSeries implements the
    creation of the copy on its \c handlePositionChange method. Firstly the \c HighlightSeries
    needs to get the pointer to the original series and then it starts to listen the
    QSurface3DSeries::selectedPointChanged signal.

    \snippet texturesurface/highlightseries.cpp 0

    When the signal arrives, first thing is to check that the position is valid. Then the ranges
    for the  copied area are calculated and checked that they stay within the bounds. Finally
    we simply fill the data array of the highlight series with the range from the data array of
    topography series.

    \snippet texturesurface/highlightseries.cpp 1

    \section1 A gradient to the highlight series

    Since the \c HighlightSeries is QSurface3DSeries, we can use all the decoration methods series can
    have. In this example we added a gradient to emphasize the elevation. Because the suitable gradient
    style depends on the range of the Y axis and we change the range when zooming, we need to adjust
    the gradient color positions as the range change.

    For the gradient color positions we define proportional values.

    \snippet texturesurface/highlightseries.cpp 2

    The gradient modification is done on \c handleGradientChange method and we connect it to react to
    changes on Y axis.

    \snippet texturesurface/surfacegraph.cpp 1

    When a change on Y axis max value happens, we calculate the gradient color positions.

    \snippet texturesurface/highlightseries.cpp 3
*/