File: VolumeFog.cpp

package info (click to toggle)
libwildmagic 5.13-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 89,572 kB
  • ctags: 26,264
  • sloc: cpp: 210,924; csh: 637; sh: 95; makefile: 36
file content (281 lines) | stat: -rw-r--r-- 9,578 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// 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 "VolumeFog.h"
#include "VolumeFogEffect.h"

WM5_WINDOW_APPLICATION(VolumeFog);

//----------------------------------------------------------------------------
VolumeFog::VolumeFog ()
    :
    WindowApplication3("SampleGraphics/VolumeFog", 0, 0, 640, 480,
        Float4(0.0f, 0.5f, 0.75f, 1.0f)),
        mTextColor(0.0f, 0.0f, 0.0f, 1.0f)
{
    Environment::InsertDirectory(ThePath + "Shaders/");
}
//----------------------------------------------------------------------------
bool VolumeFog::OnInitialize ()
{
    if (!WindowApplication3::OnInitialize())
    {
        return false;
    }

    // Set up the camera.
    mCamera->SetFrustum(60.0f, GetAspectRatio(), 0.01f, 100.0f);
    APoint camPosition(0.0f, -9.0f, 1.5f);
    AVector camDVector(0.0f, 1.0f, 0.0f);
    AVector camUVector(0.0f, 0.0f, 1.0f);
    AVector camRVector = camDVector.Cross(camUVector);
    mCamera->SetFrame(camPosition, camDVector, camUVector, camRVector);

    CreateScene();

    // Initial update of objects.
    mScene->Update();

    // Initial culling of scene.
    mCuller.SetCamera(mCamera);
    mCuller.ComputeVisibleSet(mScene);

    InitializeCameraMotion(0.005f, 0.002f);
    InitializeObjectMotion(mScene);
    return true;
}
//----------------------------------------------------------------------------
void VolumeFog::OnTerminate ()
{
    mScene = 0;
    mMesh = 0;
    mScreenCamera = 0;
    mScreenPolygon = 0;

    WindowApplication3::OnTerminate();
}
//----------------------------------------------------------------------------
void VolumeFog::OnIdle ()
{
    MeasureTime();

    bool fogNeedsUpdate = false;
    if (MoveCamera())
    {
        mCuller.ComputeVisibleSet(mScene);
        fogNeedsUpdate = true;
    }

    if (MoveObject())
    {
        mScene->Update();
        mCuller.ComputeVisibleSet(mScene);
        fogNeedsUpdate = true;
    }

    if (fogNeedsUpdate)
    {
        UpdateFog();
    }

    if (mRenderer->PreDraw())
    {
        mRenderer->ClearDepthBuffer();

        // Draw fogged terrain.
        mRenderer->SetCamera(mCamera);
        mRenderer->Draw(mCuller.GetVisibleSet());

        // Draw background image.
        mRenderer->SetCamera(mScreenCamera);
        mRenderer->Draw(mScreenPolygon);

        DrawFrameRate(8, GetHeight()-8, mTextColor);

        mRenderer->PostDraw();
        mRenderer->DisplayColorBuffer();
    }

    UpdateFrameCount();
}
//----------------------------------------------------------------------------
void VolumeFog::CreateScene ()
{
    // Create a screen-space camera for the background image.
    mScreenCamera = ScreenTarget::CreateCamera();

    // Create a screen polygon for the background image.
    VertexFormat* vformat = VertexFormat::Create(2,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0);

    mScreenPolygon = ScreenTarget::CreateRectangle(vformat, GetWidth(),
        GetHeight(), 0.0f, 1.0f, 0.0f, 1.0f, 1.0f);

    std::string skyName = Environment::GetPathR("BlueSky.wmtf");
    Texture2D* skyTexture = Texture2D::LoadWMTF(skyName);
    Texture2DEffect* skyEffect = new0 Texture2DEffect();
    mScreenPolygon->SetEffectInstance(skyEffect->CreateInstance(skyTexture));

    // Create the scene graph for the terrain.
    mScene = new0 Node();

    // Begin with a flat height field.
    vformat = VertexFormat::Create(3,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT4, 0,
        VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0);

    mMesh = StandardMesh(vformat).Rectangle(64, 64, 8.0f, 8.0f);
    mScene->AttachChild(mMesh);

    // Set the heights based on a precomputed height field.  Also create a
    // texture image to go with the height field.
    std::string heightFieldName = Environment::GetPathR("HeightField.wmtf");
    Texture2D* heightTexture = Texture2D::LoadWMTF(heightFieldName);
    unsigned char* data = (unsigned char*)heightTexture->GetData(0);
    Float4 white(1.0f, 1.0f, 1.0f, 0.0f);
    VertexBufferAccessor vba(mMesh);
    for (int i = 0; i < vba.GetNumVertices(); ++i)
    {
        unsigned char value = *data;
        float height = 3.0f*value/255.0f + 0.05f*Mathf::SymmetricRandom();

        *data++ = (unsigned char)Mathf::IntervalRandom(32.0f, 64.0f);
        *data++ = 3*(128 - value/2)/4;
        *data++ = 0;
        *data++ = 255;

        vba.Position<Float3>(i)[2] = height;

        // The fog color is white.  The alpha channel is filled in by the
        // function UpdateFog().
        vba.Color<Float4>(0, i) = white;
    }

    UpdateFog();

    std::string effectFile = Environment::GetPathR("VolumeFog.wmfx");
    VolumeFogEffect* effect = new0 VolumeFogEffect(effectFile);
    mMesh->SetEffectInstance(effect->CreateInstance(heightTexture));
}
//----------------------------------------------------------------------------
void VolumeFog::UpdateFog ()
{
    // Compute the fog factors based on the intersection of a slab bounded
    // by z = zbot and z = ztop with rays emanating from the camera location
    // to each vertex in the height field.
    const float zBot = 0.0f;  // slab bottom
    const float zTop = 0.5f;  // slab top
    const float fogConstant = 8.0f;  // fog = length/(length + fogconstant)
    float tBot, tTop, invDirZ, length;

    // Having the camera below the height field is not natural, so we just
    // leave the fog values the way they are.
    APoint camPosition = mCamera->GetPosition();
    if (camPosition[2] <= zBot)
    {
        return;
    }

    VertexBufferAccessor vba(mMesh);
    for (int i = 0; i < vba.GetNumVertices(); ++i)
    {
        // The ray is E+t*D, where D = V-E with E the eye point and V the
        // vertex.  The ray reaches the vertex at t = 1.
        APoint heightPosition = vba.Position<Float3>(i);

        AVector direction = heightPosition - camPosition;
        float tVmE = direction.Normalize();
        float alpha;

        if (camPosition[2] >= zTop)
        {
            if (direction[2] >= -Mathf::ZERO_TOLERANCE)
            {
                // The ray never intersects the slab, so no fog at vertex.
                alpha = 0.0f;
            }
            else
            {
                // The ray intersects the slab.  Compute the length of the
                // intersection and map it to a fog factor.
                invDirZ = 1.0f/direction[2];
                tTop = (zTop - camPosition[2])*invDirZ;
                if (tTop < tVmE)
                {
                    // The eye is above the slab; the vertex is in or below.
                    tBot = (zBot - camPosition[2])*invDirZ;
                    if (tBot < tVmE)
                    {
                        // The vertex is below the slab.  Clamp to bottom.
                        length = tBot - tTop;
                        alpha = length/(length + fogConstant);
                    }
                    else
                    {
                        // The vertex is inside the slab.
                        length = tVmE - tTop;
                        alpha = length/(length + fogConstant);
                    }
                }
                else
                {
                    // The eye and vertex are above the slab, so no fog.
                    alpha = 0.0f;
                }
            }
        }
        else  // position[2] in (z0,z1)
        {
            if (direction[2] >= Mathf::ZERO_TOLERANCE)
            {
                // The ray intersects the top of the slab.
                invDirZ = 1.0f/direction[2];
                tTop = (zTop - camPosition[2])*invDirZ;
                if (tTop < tVmE)
                {
                    // The vertex is above the slab.
                    alpha = tTop/(tTop + fogConstant);
                }
                else
                {
                    // The vertex is on or inside the slab.
                    alpha = tVmE/(tVmE + fogConstant);
                }
            }
            else if (direction[2] <= -Mathf::ZERO_TOLERANCE)
            {
                // The ray intersects the bottom of the slab.
                invDirZ = 1.0f/direction[2];
                tBot = (zBot - camPosition[2])*invDirZ;
                if (tBot < tVmE)
                {
                    // The vertex is below the slab.
                    alpha = tBot/(tBot + fogConstant);
                }
                else
                {
                    // The vertex is on or inside the slab.
                    alpha = tVmE/(tVmE + fogConstant);
                }
            }
            else
            {
                // The ray is parallel to the slab, so both eye and vertex
                // are inside the slab.
                alpha = tVmE/(tVmE + fogConstant);
            }
        }

        vba.Color<Float4>(0, i)[3] = alpha;
    }

    mRenderer->Update(mMesh->GetVertexBuffer());
}
//----------------------------------------------------------------------------