File: BlendedTerrainEffect.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 (171 lines) | stat: -rw-r--r-- 6,108 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
// 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/04)

#include "BlendedTerrainEffect.h"
#include "Wm5Environment.h"
#include "Wm5PVWMatrixConstant.h"
#include "Wm5Texture1D.h"
using namespace Wm5;

WM5_IMPLEMENT_RTTI(Wm5, VisualEffect, BlendedTerrainEffect);
WM5_IMPLEMENT_STREAM(BlendedTerrainEffect);
WM5_IMPLEMENT_FACTORY(BlendedTerrainEffect);

//----------------------------------------------------------------------------
BlendedTerrainEffect::BlendedTerrainEffect (const std::string& effectFile)
    :
    VisualEffect(effectFile)
{
    // TODO:  Once WmfxCompiler parses the Cg FX files, we will not need to
    // set the sampler state.
    PixelShader* pshader = GetPixelShader(0, 0);

    // GrassSampler
    pshader->SetFilter(0, Shader::SF_LINEAR_LINEAR);
    pshader->SetCoordinate(0, 0, Shader::SC_REPEAT);
    pshader->SetCoordinate(0, 1, Shader::SC_REPEAT);

    // StoneSampler
    pshader->SetFilter(1, Shader::SF_LINEAR_LINEAR);
    pshader->SetCoordinate(1, 0, Shader::SC_REPEAT);
    pshader->SetCoordinate(1, 1, Shader::SC_REPEAT);

    // BlendSampler
    pshader->SetFilter(2, Shader::SF_LINEAR);
    pshader->SetCoordinate(2, 0, Shader::SC_REPEAT);

    // CloudSampler
    pshader->SetFilter(3, Shader::SF_LINEAR_LINEAR);
    pshader->SetCoordinate(3, 0, Shader::SC_REPEAT);
    pshader->SetCoordinate(3, 1, Shader::SC_REPEAT);

    // Create a 1-dimensional texture whose intensities are proportional to
    // height.
    const int numTexels = 256;
    mBlendTexture = new0 Texture1D(Texture::TF_L8, numTexels, 1);
    unsigned char* data = (unsigned char*)mBlendTexture->GetData(0);
    for (int i = 0; i < numTexels; ++i)
    {
        *data++ = (unsigned char)i;
    }
}
//----------------------------------------------------------------------------
BlendedTerrainEffect::~BlendedTerrainEffect ()
{
}
//----------------------------------------------------------------------------
VisualEffectInstance* BlendedTerrainEffect::CreateInstance (
    ShaderFloat* flowDirection, ShaderFloat* powerFactor,
    Texture2D* grassTexture, Texture2D* stoneTexture, Texture2D* cloudTexture)
{
    VisualEffectInstance* instance = new0 VisualEffectInstance(this, 0);
    instance->SetVertexConstant(0, 0, new0 PVWMatrixConstant());
    instance->SetVertexConstant(0, 1, flowDirection);
    instance->SetPixelConstant(0, 0, powerFactor);
    instance->SetPixelTexture(0, 0, grassTexture);
    instance->SetPixelTexture(0, 1, stoneTexture);
    instance->SetPixelTexture(0, 2, mBlendTexture);
    instance->SetPixelTexture(0, 3, cloudTexture);
    return instance;
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// Name support.
//----------------------------------------------------------------------------
Object* BlendedTerrainEffect::GetObjectByName (const std::string& name)
{
    Object* found = VisualEffect::GetObjectByName(name);
    if (found)
    {
        return found;
    }

    WM5_GET_OBJECT_BY_NAME(mBlendTexture, name, found);

    return 0;
}
//----------------------------------------------------------------------------
void BlendedTerrainEffect::GetAllObjectsByName (const std::string& name,
    std::vector<Object*>& objects)
{
    VisualEffect::GetAllObjectsByName(name, objects);

    WM5_GET_ALL_OBJECTS_BY_NAME(mBlendTexture, name, objects);
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// Streaming support.
//----------------------------------------------------------------------------
BlendedTerrainEffect::BlendedTerrainEffect (LoadConstructor value)
    :
    VisualEffect(value)
{
}
//----------------------------------------------------------------------------
void BlendedTerrainEffect::Load (InStream& source)
{
    WM5_BEGIN_DEBUG_STREAM_LOAD(source);

    VisualEffect::Load(source);

    source.ReadPointer(mBlendTexture);

    WM5_END_DEBUG_STREAM_LOAD(BlendedTerrainEffect, source);
}
//----------------------------------------------------------------------------
void BlendedTerrainEffect::Link (InStream& source)
{
    VisualEffect::Link(source);

    source.ResolveLink(mBlendTexture);
}
//----------------------------------------------------------------------------
void BlendedTerrainEffect::PostLink ()
{
    VisualEffect::PostLink();

    //VisualPass* pass = mTechniques[0]->GetPass(0);
    //VertexShader* vshader = pass->GetVertexShader();
    //PixelShader* pshader = pass->GetPixelShader();
    //vshader->SetBaseRegisters(msVRegisters);
    //vshader->SetPrograms(msVPrograms);
    //pshader->SetBaseRegisters(msPRegisters);
    //pshader->SetTextureUnits(msPTextureUnits);
    //pshader->SetPrograms(msPPrograms);
}
//----------------------------------------------------------------------------
bool BlendedTerrainEffect::Register (OutStream& target) const
{
    if (VisualEffect::Register(target))
    {
        target.Register(mBlendTexture);
        return true;
    }
    return false;
}
//----------------------------------------------------------------------------
void BlendedTerrainEffect::Save (OutStream& target) const
{
    WM5_BEGIN_DEBUG_STREAM_SAVE(target);

    VisualEffect::Save(target);

    target.WritePointer(mBlendTexture);

    WM5_END_DEBUG_STREAM_SAVE(BlendedTerrainEffect, target);
}
//----------------------------------------------------------------------------
int BlendedTerrainEffect::GetStreamingSize () const
{
    int size = VisualEffect::GetStreamingSize();
    size += WM5_POINTERSIZE(mBlendTexture);
    return size;
}
//----------------------------------------------------------------------------