main.cpp 4.51 KB
Newer Older
1
2
3
#include <core/base/program.h>
#include <core/rendering/texture_renderer.h>
#include <core/state.h>
4
#include <core/res/res.h>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include <util/files.h>
#include <util/images.h>
#include <iostream>

#include <imgui/imgui_glfw.h>

using namespace glare;

std::vector<std::string> extensions =
{
	".hdr", ".png"
};

float zoom = 1.f;

void on_scroll(double xoffset, double yoffset)
{
	zoom += 0.1f*float(yoffset);
	zoom = glm::max(zoom, 0.01f);
}

int main(int argc, char* argv[])
{
29
30
	core::Context::createAsCurrent(files::asset("/preferences/default.xml"));
	core::Context::current().callbacks().addScrollCallback("main", on_scroll);
31

32
	auto texture_renderer = core::TextureRenderer::makeCenterTexture();
33
34
35
36
37
	texture_renderer->addPreDrawCallback([](const core::Program& program)
	{
		program.uniform("u_scale", zoom);
	});

38
39
40
	std::shared_ptr<core::Texture> image;
	std::shared_ptr<core::Texture> heatmap_other_image;
	std::shared_ptr<core::Texture> heatmap;
41
42
43
44
	std::sort(extensions.begin(), extensions.end());

	core::Program heatmap_generator(files::shader("/heatmap/heatmap.comp"));

45
	core::Context::current().loop([&texture_renderer, &image, &heatmap_other_image, &heatmap, &heatmap_generator]()
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
	{
		static int current_tab = 0;

		if(image && (current_tab == 0 || (current_tab == 1 && !heatmap)))
			texture_renderer->draw(*image);
		else if (heatmap)
		{
			texture_renderer->draw(*heatmap);
		}

		ImGui::BeginTabbed("Images", std::array<std::string, 2>{"Load image", "Heatmap"}, current_tab);

		switch (current_tab)
		{
		default:
		case 0:
		{
			static auto root = files::asset("screenshots");

			// Go to parent directory
			if (ImGui::Button("..", ImVec2(ImGui::GetContentRegionAvailWidth(), 32)))
				root = root.parent_path();

			for (auto& p : fs::directory_iterator(root)) {
				fs::path path = p;

				// Either draw directory button or...
				if (is_directory(path) && ImGui::Button(path.filename().string().c_str(), ImVec2(ImGui::GetContentRegionAvailWidth(), 40)))
					root = path;
				// ... draw a button which loads a dae file.
				else if (std::binary_search(extensions.begin(), extensions.end(), path.extension().string()) && ImGui::FeatureButton(path.filename().string().c_str(), ImVec2(ImGui::GetContentRegionAvailWidth(), 32)))
				{
78
					image = core::global_resources::textures.get(path);
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
					zoom = 1.f;
				}
			}
		}
			break;
		case 1:
		{
			static auto root = files::asset("screenshots");

			ImGui::Title("Select an image to generate a heatmap...");
			if (heatmap)
			{
				static char buf[64] = {};
				ImGui::InputText("File name", buf, 128);
				static bool hdr = false;
				ImGui::Checkbox("Save as HDR", &hdr);
				if (ImGui::FeatureButton("Save", ImVec2(ImGui::GetContentRegionAvailWidth(), 0)))
				{
					std::string name = std::string(buf) + (hdr?".hdr":".png");
					fs::path output = root / name;

100
101
102
103
					if (hdr)
						heatmap->get<float>().save(output);
					else
						heatmap->get<uint8_t>().save(output);
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
				}
				ImGui::Separator();
			}

			// Go to parent directory
			if (ImGui::Button("..", ImVec2(ImGui::GetContentRegionAvailWidth(), 32)))
				root = root.parent_path();

			for (auto& p : fs::directory_iterator(root)) {
				fs::path path = p;

				// Either draw directory button or...
				if (is_directory(path) && ImGui::Button(path.filename().string().c_str(), ImVec2(ImGui::GetContentRegionAvailWidth(), 40)))
					root = path;
				// ... draw a button which loads a dae file.
				else if (std::binary_search(extensions.begin(), extensions.end(), path.extension().string()) && ImGui::FeatureButton(path.filename().string().c_str(), ImVec2(ImGui::GetContentRegionAvailWidth(), 32)))
				{
121
					heatmap_other_image = core::global_resources::textures.get(path);
122
123
124
125
					if (!image)
					{
						Log_Error << "Please select a source image first!";
					}
126
					else if (heatmap_other_image->width() != image->width() || heatmap_other_image->height() != image->height())
127
128
129
130
131
					{
						Log_Error << "Selected an image with different dimensions!";
					}
					else
					{
132
						heatmap = std::make_shared<core::Texture>(core::Image<float>({ image->width(), image->height() }, 4));
133
134

						heatmap_generator.use();
135
136
137
						heatmap_generator.uniform("u_first_image", image->imageAddress(gl::Access::eReadOnly));
						heatmap_generator.uniform("u_second_image", heatmap_other_image->imageAddress(gl::Access::eReadOnly));
						heatmap_generator.uniform("u_result_heatmap", heatmap->imageAddress(gl::Access::eWriteOnly));
138

139
						heatmap_generator.dispatch2D(image->width(), 24, image->height(), 24);
140
141
142
143
144
145
146
147
148
149
150
					}
					zoom = 1.f;
				}
			}
		}
			break;
		}

		ImGui::EndTabbed();
	});
}