File: DrawImplicitSurface.cpp

package info (click to toggle)
libwildmagic 5.17%2Bcleaned1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 90,112 kB
  • sloc: cpp: 215,940; csh: 637; sh: 91; makefile: 39
file content (193 lines) | stat: -rw-r--r-- 6,316 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
// 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.1 (2012/07/07)

#include "DrawImplicitSurface.h"
#include "Function.h"

WM5_WINDOW_APPLICATION(DrawImplicitSurface);

//----------------------------------------------------------------------------
DrawImplicitSurface::DrawImplicitSurface ()
    :
    WindowApplication2("SampleMathematics/DrawImplicitSurface", 0, 0, 256,
        256, Float4(1.0f, 1.0f, 1.0f, 1.0f)),
    mTracer(F, DF, 256, 256)
{
    mNumSamples = 100;
    mBlur = false;
    mSize = GetWidth();
}
//----------------------------------------------------------------------------
bool DrawImplicitSurface::OnInitialize ()
{
    if (!WindowApplication2::OnInitialize())
    {
        return false;
    }

    // Initialize camera and view frustum.
    mTracer.Location = Vector3f(2.0f, 0.0f, 0.0f);
    mTracer.DVector = Vector3f(-1.0f, 0.0f, 0.0f);
    mTracer.UVector = Vector3f(0.0f, 1.0f, 0.0f);
    mTracer.RVector = mTracer.DVector.Cross(mTracer.UVector);
    mTracer.Near = 0.1f;
    mTracer.Far = 10.0f;
    mTracer.HalfWidth = 2.0f*mTracer.Near;  // 90 degree horizontal FOV
    mTracer.HalfHeight = 2.0f*mTracer.Near;  // 90 degree vertical FOV

    // The light direction will be the camera direction so that we can see
    // the surface from all camera locations.

    // Draw the level surface.
    mTracer.DrawSurface(mNumSamples, mTracer.DVector, mBlur);
    OnDisplay();
    return true;
}
//----------------------------------------------------------------------------
void DrawImplicitSurface::OnDisplay ()
{
    ClearScreen();

    const float* image = mTracer.GetImage();
    for (int y = 0, i = 0; y < mTracer.GetHeight(); ++y)
    {
        for (int x = 0; x < mTracer.GetWidth(); ++x, ++i)
        {
            unsigned char value = (unsigned char)(255.0f*image[i]);
            SetPixel(x, y, ColorRGB(value, value, value));
        }
    }

    WindowApplication2::OnDisplay();
}
//----------------------------------------------------------------------------
bool DrawImplicitSurface::OnKeyDown (unsigned char key, int x, int y)
{
    if (WindowApplication2::OnKeyDown(key, x, y))
    {
        return true;
    }

    switch (key)
    {
    case '+':  // increase ray sample size
    case '=':
        mNumSamples += 100;
        mTracer.DrawSurface(mNumSamples, mTracer.DVector, mBlur);
        OnDisplay();
        return true;
    case '-':  // decrease ray sample size
    case '_':
        if (mNumSamples > 100)
        {
            mNumSamples -= 100;
            mTracer.DrawSurface(mNumSamples, mTracer.DVector, mBlur);
            OnDisplay();
        }
        return true;
    case 'b':  // toggle blur of output image
    case 'B':
        mBlur = !mBlur;
        mTracer.DrawSurface(mNumSamples, mTracer.DVector, mBlur);
        OnDisplay();
        return true;
    }

    return false;
}
//----------------------------------------------------------------------------
bool DrawImplicitSurface::OnSpecialKeyDown (int key, int, int)
{
    // TODO:  These are chosen for the specific functions in this
    // application.  Allow the application to modify these, either by key
    // strokes or automatically.
    const float trnDelta = 0.25f, rotDelta = 0.1f;
    bool moved = false;
    float length;
    Matrix3f rot;

    if (key == KEY_UP_ARROW)
    {
        // Translate forward in camera direction.
        mTracer.Location += trnDelta*mTracer.DVector;
        moved = true;
    }
    else if (key == KEY_DOWN_ARROW)
    {
        // Translate backward in camera direction.
        mTracer.Location -= trnDelta*mTracer.DVector;
        moved = true;
    }
    else if (key == KEY_F1)
    {
        // Rotate about camera right, move up on view sphere.
        length = mTracer.Location.Length();
        rot.MakeRotation(mTracer.RVector, rotDelta);
        mTracer.DVector = rot*mTracer.DVector;
        mTracer.UVector = rot*mTracer.UVector;
        mTracer.Location = -length*mTracer.DVector;
        moved = true;
    }
    else if (key == KEY_F2)
    {
        // Rotate about camera right, move down on view sphere.
        length = mTracer.Location.Length();
        rot.MakeRotation(mTracer.RVector, -rotDelta);
        mTracer.DVector = rot*mTracer.DVector;
        mTracer.UVector = rot*mTracer.UVector;
        mTracer.Location = -length*mTracer.DVector;
        moved = true;
    }
    else if (key == KEY_F3)
    {
        // Rotate about camera up, move right on view sphere.
        length = mTracer.Location.Length();
        rot.MakeRotation(mTracer.UVector, rotDelta);
        mTracer.DVector = rot*mTracer.DVector;
        mTracer.RVector = rot*mTracer.RVector;
        mTracer.Location = -length*mTracer.DVector;
        moved = true;
    }
    else if (key == KEY_F4)
    {
        // Rotate about camera up, move left on view sphere.
        length = mTracer.Location.Length();
        rot.MakeRotation(mTracer.UVector, -rotDelta);
        mTracer.DVector = rot*mTracer.DVector;
        mTracer.RVector = rot*mTracer.RVector;
        mTracer.Location = -length*mTracer.DVector;
        moved = true;
    }
    else if (key == KEY_F5)
    {
        // Rotate about camera direction, roll counterclockwise.
        length = mTracer.Location.Length();
        rot.MakeRotation(mTracer.DVector, rotDelta);
        mTracer.UVector = rot*mTracer.UVector;
        mTracer.RVector = rot*mTracer.RVector;
        moved = true;
    }
    else if (key == KEY_F6)
    {
        // Rotate about camera direction, roll clockwise.
        length = mTracer.Location.Length();
        rot.MakeRotation(mTracer.DVector, -rotDelta);
        mTracer.UVector = rot*mTracer.UVector;
        mTracer.RVector = rot*mTracer.RVector;
        moved = true;
    }

    if (moved)
    {
        mTracer.DrawSurface(mNumSamples, mTracer.DVector, mBlur);
        OnDisplay();
    }

    return true;
}
//----------------------------------------------------------------------------