OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
material.cpp
Go to the documentation of this file.
1#include "material.h"
2
3#include "utils/parser.h"
4#include "resources.h"
5
6using namespace Tempest;
7
8static Tempest::Color toColor(zenkit::Color v) {
9 Tempest::Color c(float(v.r)/255.f, float(v.g)/255.f, float(v.b)/255.f, float(v.a)/255.f);
10 return c;
11 }
12
13Material::Material(const zenkit::Material& m, bool enableAlphaTest) {
14 tex = Resources::loadTexture(m.texture);
15 if(tex==nullptr) {
16 if(!m.texture.empty()) {
17 tex = Resources::loadTexture("DEFAULT.TGA");
18 } else {
20 enableAlphaTest &= (m.color.a!=255);
21 }
22 }
23
24 loadFrames(m);
25
26 alpha = loadAlphaFunc(m.alpha_func,m.group,m.color.a,tex,enableAlphaTest);
27 if(alpha==Water && m.name=="OWODWFALL_WATERFALL_01") {
28 // NOTE: waterfall heuristics
29 alpha = Solid;
30 }
31
32 if(m.texture_anim_map_mode!=zenkit::AnimationMapping::NONE && tex!=nullptr) {
33 auto texAniMapDir = m.texture_anim_map_dir;
34 if(texAniMapDir.x!=0.f)
35 texAniMapDirPeriod.x = int(1.f/texAniMapDir.x);
36 if(texAniMapDir.y!=0.f)
37 texAniMapDirPeriod.y = int(1.f/texAniMapDir.y);
38 }
39
40 if(m.wave_mode!=zenkit::WaveMode::NONE)
41 waveMaxAmplitude = m.wave_max_amplitude;
42
43 if(m.environment_mapping!=0)
44 ; // envMapping = m.environment_mapping_strength;
45 }
46
47Material::Material(const zenkit::VisualDecal& decal) {
48 tex = Resources::loadTexture(decal.name);
49 if(tex==nullptr && !decal.name.empty())
50 tex = Resources::loadTexture("DEFAULT.TGA");
51 loadFrames(decal.name, decal.texture_anim_fps);
52
53 alpha = loadAlphaFunc(decal.alpha_func, zenkit::MaterialGroup::UNDEFINED, decal.alpha_weight, tex, true);
54 alphaWeight = float(decal.alpha_weight)/255.f;
55 }
56
57Material::Material(const zenkit::IParticleEffect& src) {
58 tex = Resources::loadTexture(src.vis_name_s);
59 loadFrames(src.vis_name_s, src.vis_tex_ani_fps);
60
61 //TODO: visTexAniIsLooping
62 alpha = Parser::loadAlpha(src.vis_alpha_func_s);
63 }
64
65bool Material::operator ==(const Material& other) const {
66 return tex==other.tex &&
67 frames==other.frames &&
68 alpha==other.alpha &&
69 alphaWeight==other.alphaWeight &&
72 isGhost==other.isGhost &&
75 }
76
77bool Material::isSolid() const {
79 }
80
84
88
94
98
100 // if(alpha!=Material::Water)
101 // return false;
102 // return waveMaxAmplitude!=0.f;
103 return (alpha==Material::Water);
104 }
105
106Material::AlphaFunc Material::loadAlphaFunc(zenkit::AlphaFunction zenAlpha,
107 zenkit::MaterialGroup matGroup,
108 uint8_t clrAlpha,
109 const Tempest::Texture2d* tex,
110 bool enableAlphaTest) {
112 switch(zenAlpha) {
113 case zenkit::AlphaFunction::BLEND:
115 break;
116 case zenkit::AlphaFunction::ADD:
118 break;
119 case zenkit::AlphaFunction::SUBTRACT:
120 // FIXME: no such materials in game found
122 break;
123 case zenkit::AlphaFunction::MULTIPLY:
125 break;
126 case zenkit::AlphaFunction::MULTIPLY_ALT:
128 break;
129 case zenkit::AlphaFunction::DEFAULT:
130 case zenkit::AlphaFunction::NONE:
132 break;
133 }
134
135 if(matGroup == zenkit::MaterialGroup::WATER)
137
138 if(clrAlpha!=255 && alpha==Material::AlphaFunc::Solid) {
140 }
141
143 if(tex!=nullptr && tex->format()==Tempest::TextureFormat::DXT1 && clrAlpha==255) {
145 }
146 }
147
149 // castle wall in G1 (OW_DIRTDECAL.TGA) has alpha==0, set it to mul for now
150 // if(clrAlpha==0) {
151 // alpha = Material::AlphaFunc::Multiply;
152 // }
153 }
154
155 if(alpha == Material::AlphaFunc::AlphaTest && !enableAlphaTest) {
157 }
158 return alpha;
159 }
160
161void Material::loadFrames(const zenkit::Material& m) {
162 loadFrames(m.texture, m.texture_anim_fps);
163 }
164
165void Material::loadFrames(const std::string_view fr, float fps) {
167 if(frames.empty())
168 return;
169 if(fps > 0)
170 texAniFPSInv = uint64_t(1000.0f / fps); else
171 texAniFPSInv = 1000/std::max<size_t>(frames.size(),1);
172 }
float envMapping
Definition material.h:36
bool isShadowmapRequired() const
Definition material.h:47
float waveMaxAmplitude
Definition material.h:35
std::vector< const Tempest::Texture2d * > frames
Definition material.h:29
float alphaWeight
Definition material.h:31
Material()=default
bool isGhost
Definition material.h:34
bool isForwardShading() const
Definition material.h:45
bool isTextureInShadowPass() const
Definition material.h:48
bool isTesselated() const
Definition material.h:44
Tempest::Point texAniMapDirPeriod
Definition material.h:32
@ AdditiveLight
Definition material.h:25
@ Multiply2
Definition material.h:23
@ Multiply
Definition material.h:22
@ Transparent
Definition material.h:24
@ AlphaTest
Definition material.h:19
const Tempest::Texture2d * tex
Definition material.h:28
AlphaFunc alpha
Definition material.h:30
bool operator==(const Material &other) const
Definition material.cpp:65
bool isSceneInfoRequired() const
Definition material.h:46
uint64_t texAniFPSInv
Definition material.h:33
bool isSolid() const
Definition material.cpp:77
static const Tempest::Texture2d * loadTexture(std::string_view name, bool forceMips=false)
static auto loadTextureAnim(std::string_view name) -> std::vector< const Tempest::Texture2d * >
static Tempest::Color toColor(zenkit::Color v)
Definition material.cpp:8
Material::AlphaFunc loadAlpha(std::string_view src)
Definition parser.cpp:44