forked from OpenMW/openmw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaterial.cpp
207 lines (165 loc) · 7.63 KB
/
material.cpp
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
#include "material.hpp"
#include <iostream>
#include <stdexcept>
#include <osg/Depth>
#include <osg/TexEnvCombine>
#include <osg/Texture2D>
#include <osg/TexMat>
#include <osg/Material>
#include <components/shader/shadermanager.hpp>
namespace Terrain
{
osg::ref_ptr<osg::TexMat> getBlendmapTexMat(int blendmapScale)
{
static std::map<int, osg::ref_ptr<osg::TexMat> > texMatMap;
osg::ref_ptr<osg::TexMat> texMat = texMatMap[blendmapScale];
if (!texMat)
{
texMat = new osg::TexMat;
osg::Matrixf matrix;
float scale = (blendmapScale/(static_cast<float>(blendmapScale)+1.f));
matrix.preMultTranslate(osg::Vec3f(0.5f, 0.5f, 0.f));
matrix.preMultScale(osg::Vec3f(scale, scale, 1.f));
matrix.preMultTranslate(osg::Vec3f(-0.5f, -0.5f, 0.f));
texMatMap[blendmapScale] = texMat;
}
return texMat;
}
osg::ref_ptr<osg::TexMat> getLayerTexMat(float layerTileSize)
{
static std::map<float, osg::ref_ptr<osg::TexMat> > texMatMap;
osg::ref_ptr<osg::TexMat> texMat = texMatMap[layerTileSize];
if (!texMat)
{
texMat = new osg::TexMat;
texMat->setMatrix(osg::Matrix::scale(osg::Vec3f(layerTileSize,layerTileSize,1.f)));
texMatMap[layerTileSize] = texMat;
}
return texMat;
}
osg::ref_ptr<osg::Depth> getEqualDepth()
{
static osg::ref_ptr<osg::Depth> depth;
if (!depth)
{
depth = new osg::Depth;
depth->setFunction(osg::Depth::EQUAL);
}
return depth;
}
FixedFunctionTechnique::FixedFunctionTechnique(const std::vector<TextureLayer>& layers,
const std::vector<osg::ref_ptr<osg::Texture2D> >& blendmaps, int blendmapScale, float layerTileSize)
{
bool firstLayer = true;
int i=0;
for (std::vector<TextureLayer>::const_iterator it = layers.begin(); it != layers.end(); ++it)
{
osg::ref_ptr<osg::StateSet> stateset (new osg::StateSet);
if (!firstLayer)
{
stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
stateset->setAttributeAndModes(getEqualDepth(), osg::StateAttribute::ON);
}
int texunit = 0;
if(!firstLayer)
{
osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(i++);
stateset->setTextureAttributeAndModes(texunit, blendmap.get());
// This is to map corner vertices directly to the center of a blendmap texel.
stateset->setTextureAttributeAndModes(texunit, getBlendmapTexMat(blendmapScale));
static osg::ref_ptr<osg::TexEnvCombine> texEnvCombine;
if (!texEnvCombine)
{
texEnvCombine = new osg::TexEnvCombine;
texEnvCombine->setCombine_RGB(osg::TexEnvCombine::REPLACE);
texEnvCombine->setSource0_RGB(osg::TexEnvCombine::PREVIOUS);
}
stateset->setTextureAttributeAndModes(texunit, texEnvCombine, osg::StateAttribute::ON);
++texunit;
}
// Add the actual layer texture multiplied by the alpha map.
osg::ref_ptr<osg::Texture2D> tex = it->mDiffuseMap;
stateset->setTextureAttributeAndModes(texunit, tex.get());
stateset->setTextureAttributeAndModes(texunit, getLayerTexMat(layerTileSize), osg::StateAttribute::ON);
firstLayer = false;
addPass(stateset);
}
}
ShaderTechnique::ShaderTechnique(Shader::ShaderManager& shaderManager, bool forcePerPixelLighting, bool clampLighting, const std::vector<TextureLayer>& layers,
const std::vector<osg::ref_ptr<osg::Texture2D> >& blendmaps, int blendmapScale, float layerTileSize)
{
bool firstLayer = true;
int i=0;
for (std::vector<TextureLayer>::const_iterator it = layers.begin(); it != layers.end(); ++it)
{
osg::ref_ptr<osg::StateSet> stateset (new osg::StateSet);
if (!firstLayer)
{
stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
stateset->setAttributeAndModes(getEqualDepth(), osg::StateAttribute::ON);
}
int texunit = 0;
stateset->setTextureAttributeAndModes(texunit, it->mDiffuseMap);
stateset->setTextureAttributeAndModes(texunit, getLayerTexMat(layerTileSize), osg::StateAttribute::ON);
stateset->addUniform(new osg::Uniform("diffuseMap", texunit));
if(!firstLayer)
{
++texunit;
osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(i++);
stateset->setTextureAttributeAndModes(texunit, blendmap.get());
stateset->setTextureAttributeAndModes(texunit, getBlendmapTexMat(blendmapScale));
stateset->addUniform(new osg::Uniform("blendMap", texunit));
}
if (it->mNormalMap)
{
++texunit;
stateset->setTextureAttributeAndModes(texunit, it->mNormalMap);
stateset->addUniform(new osg::Uniform("normalMap", texunit));
}
Shader::ShaderManager::DefineMap defineMap;
defineMap["forcePPL"] = forcePerPixelLighting ? "1" : "0";
defineMap["clamp"] = clampLighting ? "1" : "0";
defineMap["normalMap"] = (it->mNormalMap) ? "1" : "0";
defineMap["blendMap"] = !firstLayer ? "1" : "0";
defineMap["colorMode"] = "2";
defineMap["specularMap"] = it->mSpecular ? "1" : "0";
defineMap["parallax"] = (it->mNormalMap && it->mParallax) ? "1" : "0";
osg::ref_ptr<osg::Shader> vertexShader = shaderManager.getShader("terrain_vertex.glsl", defineMap, osg::Shader::VERTEX);
osg::ref_ptr<osg::Shader> fragmentShader = shaderManager.getShader("terrain_fragment.glsl", defineMap, osg::Shader::FRAGMENT);
if (!vertexShader || !fragmentShader)
throw std::runtime_error("Unable to create shader");
stateset->setAttributeAndModes(shaderManager.getProgram(vertexShader, fragmentShader));
firstLayer = false;
addPass(stateset);
}
}
Effect::Effect(bool useShaders, bool forcePerPixelLighting, bool clampLighting, Shader::ShaderManager* shaderManager, const std::vector<TextureLayer> &layers, const std::vector<osg::ref_ptr<osg::Texture2D> > &blendmaps,
int blendmapScale, float layerTileSize)
: mShaderManager(shaderManager)
, mUseShaders(useShaders)
, mForcePerPixelLighting(forcePerPixelLighting)
, mClampLighting(clampLighting)
, mLayers(layers)
, mBlendmaps(blendmaps)
, mBlendmapScale(blendmapScale)
, mLayerTileSize(layerTileSize)
{
selectTechnique(0);
}
bool Effect::define_techniques()
{
try
{
if (mUseShaders && mShaderManager)
addTechnique(new ShaderTechnique(*mShaderManager, mForcePerPixelLighting, mClampLighting, mLayers, mBlendmaps, mBlendmapScale, mLayerTileSize));
else
addTechnique(new FixedFunctionTechnique(mLayers, mBlendmaps, mBlendmapScale, mLayerTileSize));
}
catch (std::exception& e)
{
std::cerr << "Error: " << e.what() << std::endl;
addTechnique(new FixedFunctionTechnique(mLayers, mBlendmaps, mBlendmapScale, mLayerTileSize));
}
return true;
}
}