This repository was archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmaterial.cpp
More file actions
119 lines (110 loc) · 5.04 KB
/
Copy pathmaterial.cpp
File metadata and controls
119 lines (110 loc) · 5.04 KB
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
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "precompiled.h"
#include "fplbase/flatbuffer_utils.h"
#include "fplbase/material.h"
#include "fplbase/renderer.h"
#include "fplbase/utilities.h"
#include "materials_generated.h"
namespace fplbase {
static_assert(
kBlendModeOff == static_cast<BlendMode>(matdef::BlendMode_OFF) &&
kBlendModeTest == static_cast<BlendMode>(matdef::BlendMode_TEST) &&
kBlendModeAlpha == static_cast<BlendMode>(matdef::BlendMode_ALPHA) &&
kBlendModeAdd == static_cast<BlendMode>(matdef::BlendMode_ADD) &&
kBlendModeAddAlpha == static_cast<BlendMode>(matdef::BlendMode_ADDALPHA) &&
kBlendModeMultiply == static_cast<BlendMode>(matdef::BlendMode_MULTIPLY) &&
kBlendModePreMultipliedAlpha ==
static_cast<BlendMode>(matdef::BlendMode_PREMULTIPLIEDALPHA),
"BlendMode enums in material.h and material.fbs must match.");
static_assert(kBlendModeCount == kBlendModePreMultipliedAlpha + 2,
"Please update static_assert above with new enum values.");
static_assert(
kFormatAuto == static_cast<TextureFormat>(matdef::TextureFormat_AUTO) &&
kFormat8888 == static_cast<TextureFormat>(matdef::TextureFormat_F_8888) &&
kFormat888 == static_cast<TextureFormat>(matdef::TextureFormat_F_888) &&
kFormat5551 == static_cast<TextureFormat>(matdef::TextureFormat_F_5551) &&
kFormat565 == static_cast<TextureFormat>(matdef::TextureFormat_F_565) &&
kFormatLuminance == static_cast<TextureFormat>(matdef::TextureFormat_F_8) &&
kFormatLuminanceAlpha ==
static_cast<TextureFormat>(matdef::TextureFormat_F_88) &&
kFormatASTC == static_cast<TextureFormat>(matdef::TextureFormat_ASTC) &&
kFormatPKM == static_cast<TextureFormat>(matdef::TextureFormat_PKM) &&
kFormatKTX == static_cast<TextureFormat>(matdef::TextureFormat_KTX) &&
kFormatNative == static_cast<TextureFormat>(matdef::TextureFormat_NATIVE),
"TextureFormat enums in material.h and materials.fbs must match.");
static_assert(kFormatCount == kFormatLuminanceAlpha + 1,
"Please update static_assert above with new enum values.");
void Material::Set(Renderer &renderer) {
renderer.SetBlendMode(blend_mode_);
for (size_t i = 0; i < textures_.size(); i++) textures_[i]->Set(i);
}
void Material::DeleteTextures() {
for (size_t i = 0; i < textures_.size(); i++) textures_[i]->Delete();
}
Material *Material::LoadFromMaterialDef(const matdef::Material *matdef,
const TextureLoaderFn &tlf) {
if (matdef) {
auto mat = new Material();
mat->set_blend_mode(static_cast<BlendMode>(matdef->blendmode()));
for (size_t i = 0; i < matdef->texture_filenames()->size(); i++) {
flatbuffers::uoffset_t index = static_cast<flatbuffers::uoffset_t>(i);
auto format =
matdef->desired_format() && i < matdef->desired_format()->size()
? static_cast<TextureFormat>(matdef->desired_format()->Get(index))
: kFormatAuto;
auto tex = tlf(
matdef->texture_filenames()->Get(index)->c_str(), format,
(matdef->mipmaps() ? kTextureFlagsUseMipMaps : kTextureFlagsNone) |
(matdef->is_cubemap() && matdef->is_cubemap()->Get(index)
? kTextureFlagsIsCubeMap
: kTextureFlagsNone) |
(matdef->wrapmode() == matdef::TextureWrap_CLAMP
? kTextureFlagsClampToEdge
: kTextureFlagsNone));
if (!tex) {
delete mat;
return nullptr;
}
mat->textures().push_back(tex);
auto original_size =
matdef->original_size() && index < matdef->original_size()->size()
? LoadVec2i(matdef->original_size()->Get(index))
: tex->size();
tex->set_original_size(original_size);
}
return mat;
}
return nullptr;
}
Material *Material::LoadFromMaterialDef(const char *filename,
const TextureLoaderFn &tlf) {
const matdef::Material *def = nullptr;
std::string flatbuf;
if (LoadFile(filename, &flatbuf)) {
flatbuffers::Verifier verifier(
reinterpret_cast<const uint8_t *>(flatbuf.c_str()), flatbuf.length());
assert(matdef::VerifyMaterialBuffer(verifier));
def = matdef::GetMaterial(flatbuf.c_str());
}
Material *mat = LoadFromMaterialDef(def, tlf);
if (!mat) {
RendererBase::Get()->set_last_error(std::string("Couldn\'t load: ")
+ filename);
} else {
mat->set_filename(filename);
}
return mat;
}
} // namespace fplbase