material.cpp 3.93 KB
Newer Older
1
2
#include "material.h" 

3
#include <imgui/imgui_glfw.h>
Johannes Braun's avatar
Johannes Braun committed
4
#include <util/messages.h>
's avatar
committed
5

6
#include <core/state.h>
7
#include <core/message_tags.h>
8

9
namespace glare::core
's avatar
committed
10
{
11
12
13
	std::atomic<size_t> Material::m_current_id = 0;

	Material::Material(std::string name)
14
		: m_material_id(m_current_id++), m_name(name)
15
16
17
	{ }

	Material::~Material()
's avatar
committed
18
19
	{

20
	}
's avatar
committed
21

22
23
24
25
26
27
	void Material::gui()
	{
		ImGui::PushID(m_name.c_str());
		ImGui::Title("Material: ");
		ImGui::SameLine();
		ImGui::Text(m_name.c_str());
's avatar
committed
28

29
30
31
32
		ImGui::SameLine();
		if (ImGui::Button("Edit...", ImVec2(ImGui::GetContentRegionAvailWidth(), 0)))
		{
			ImGui::OpenPopup("Material_parameter_popup");
's avatar
committed
33
34
		}

35
		if (ImGui::BeginPopup("Material_parameter_popup"))
36
		{
37
			const static auto colorOption = [](const std::string &name, MaterialParameter3f &param)
38
			{
39
40
41
42
43
44
45
				glm::vec4 val = glm::vec4(param.value, 1);
				ImGui::ColorButton(*reinterpret_cast<ImVec4*>(&val));
				param.value = val.xyz;
				ImGui::SameLine();
				return ImGui::DragFloat3(name.c_str(), reinterpret_cast<float*>(&param.value), 0.01f, 0.f, 1.f);
			};
			const static auto floatOption = [](const std::string &name, MaterialParameter1f &param)
46
			{
47
48
49
50
51
52
53
54
55
56
57
58
59
60
				return ImGui::DragFloat(name.c_str(), &param.value, 0.001f, 0.f, 1.f);
			};

			bool changed = false;

			changed |= colorOption("Base Color", base);
			ImGui::Spacing();
			changed |= floatOption("Roughness", roughness);
			changed |= floatOption("Metallic", metallic);
			changed |= floatOption("Transmissive", transmissive);
			changed |= ImGui::DragFloat("Emissive", &emission.value, 0.001f, 0.f, 1000.f);
			ImGui::Spacing();
			changed |= ImGui::DragFloat("IOR", &index_of_refraction, 0.001f, 0.1f, 10.f);

Johannes Braun's avatar
Johannes Braun committed
61
62
			if (changed)
				Context::current().messages().id(tags::material).push();
63
			ImGui::EndPopup();
64
		}
65
66
		ImGui::PopID();
	}
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
	void Material::uniformUpdate(const Program& program, const std::string& name) const
	{
		program.uniform(name + ".base.value", base.value);
		if (base.texture)
			program.uniform(name + ".base.texture", base.texture->makeTextureResident());
		program.uniform(name + ".base.texture_available", static_cast<uint32_t>(static_cast<bool>(base.texture)));

		program.uniform(name + ".roughness.value", roughness.value);
		if (roughness.texture)
			program.uniform(name + ".roughness.texture", roughness.texture->makeTextureResident());
		program.uniform(name + ".roughness.texture_available", static_cast<uint32_t>(static_cast<bool>(roughness.texture)));

		program.uniform(name + ".metallic.value", metallic.value);
		if (metallic.texture)
			program.uniform(name + ".metallic.texture", metallic.texture->makeTextureResident());
		program.uniform(name + ".metallic.texture_available", static_cast<uint32_t>(static_cast<bool>(metallic.texture)));

		program.uniform(name + ".transmission.value", transmissive.value);
		if (transmissive.texture)
			program.uniform(name + ".transmission.texture", transmissive.texture->makeTextureResident());
		program.uniform(name + ".transmission.texture_available", static_cast<uint32_t>(static_cast<bool>(transmissive.texture)));

		program.uniform(name + ".emission.value", emission.value);
		if (emission.texture)
			program.uniform(name + ".emission.texture", emission.texture->makeTextureResident());
		program.uniform(name + ".emission.texture_available", static_cast<uint32_t>(static_cast<bool>(emission.texture)));

		if (map_normal)
			program.uniform(name + ".normal_map", map_normal->makeTextureResident());
		if (map_displacement)
			program.uniform(name + ".displacement_map", map_displacement->makeTextureResident());
		program.uniform(name + ".has_displacement_map", static_cast<bool>(map_displacement));
		program.uniform(name + ".has_normal_map", static_cast<bool>(emission.texture));

		program.uniform(name + ".ior", index_of_refraction);
	}
's avatar
committed
104

105
106
107
108
	const std::string& Material::getName() const
	{
		return m_name;
	}
's avatar
committed
109

110
111
112
	size_t Material::id() const
	{
		return m_material_id;
's avatar
committed
113
114
	}
}