First render

This commit is contained in:
NukeBird 2025-02-11 22:26:28 +03:00
parent fa569fad52
commit a4727881c5
6 changed files with 117 additions and 8 deletions

View file

@ -8,15 +8,15 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
string(APPEND resources_dir "${CMAKE_CURRENT_SOURCE_DIR}/data@data") string(APPEND resources_dir "${CMAKE_CURRENT_SOURCE_DIR}/data@data")
if (EMSCRIPTEN) if (EMSCRIPTEN)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Oz -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY --preload-file ${resources_dir} --shell-file=\"${CMAKE_CURRENT_SOURCE_DIR}/shell.html\"") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Oz -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=0 -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY --preload-file ${resources_dir} --shell-file=\"${CMAKE_CURRENT_SOURCE_DIR}/shell.html\"")
set(CMAKE_EXECUTABLE_SUFFIX ".html") set(CMAKE_EXECUTABLE_SUFFIX ".html")
endif () endif ()
file(GLOB_RECURSE PROJECT_SRC src/*.cpp src/*.hpp src/*.h) file(GLOB_RECURSE PROJECT_SRC src/*.cpp src/*.hpp src/*.h)
find_package(raylib) find_package(raylib)
#find_package(spdlog) find_package(glm)
find_package(nlohmann_json) find_package(nlohmann_json)
add_executable(${PROJECT_NAME} ${PROJECT_SRC}) add_executable(${PROJECT_NAME} ${PROJECT_SRC})
target_link_libraries(${PROJECT_NAME} raylib nlohmann_json::nlohmann_json) target_link_libraries(${PROJECT_NAME} raylib nlohmann_json::nlohmann_json glm::glm)

View file

@ -4,7 +4,7 @@
raylib/5.5 raylib/5.5
#spdlog/1.15.0 #spdlog/1.15.0
nlohmann_json/3.11.3 nlohmann_json/3.11.3
#glm/cci.20230113 glm/cci.20230113
#glfw/3.4 #glfw/3.4
#glew/2.2.0 #glew/2.2.0
#stb/cci.20240531 #stb/cci.20240531

BIN
data/img2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

BIN
data/img3.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

BIN
data/img4.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

View file

@ -1,4 +1,70 @@
#include <algorithm>
#include <glm/common.hpp>
#include <glm/ext/vector_uint2.hpp>
#include <glm/ext/vector_uint3.hpp>
#include <glm/glm.hpp>
#include <iostream>
#include <raylib.h> #include <raylib.h>
#include <vector>
#define MAX_PTS_PER_NODE 1
struct kdtree
{
kdtree(std::vector<glm::uvec2> &pts)
{
calc_aabb(pts);
if (pts.size() > MAX_PTS_PER_NODE)
{
int split_index = aabb_size.x > aabb_size.y ? 0 : 1;
std::sort(pts.begin(), pts.end(),
[split_index](const glm::uvec2 &a, const glm::uvec2 &b)
{
return a[split_index] < b[split_index];
});
{
std::vector<glm::uvec2> lpts(pts.begin(), pts.begin() + pts.size() / 2);
left = new kdtree(lpts);
}
{
std::vector<glm::uvec2> rpts(pts.begin() + pts.size() / 2, pts.end());
right = new kdtree(rpts);
}
}
}
void draw()
{
DrawRectangleLines(aabb_min.x, aabb_min.y, aabb_size.x, aabb_size.y, BLACK);
if(left && right)
{
left->draw();
right->draw();
}
}
void calc_aabb(const std::vector<glm::uvec2> &pts)
{
for (const auto &p : pts)
{
aabb_min = glm::min(aabb_min, p);
aabb_max = glm::max(aabb_max, p);
}
aabb_size = aabb_max - aabb_min;
}
kdtree *left = nullptr;
kdtree *right = nullptr;
glm::uvec2 aabb_min{999999};
glm::uvec2 aabb_max{000000};
glm::uvec2 aabb_size;
};
int main (int argc, char *argv[]) int main (int argc, char *argv[])
{ {
@ -7,10 +73,53 @@ int main (int argc, char *argv[])
InitWindow(img.width, img.height, ""); InitWindow(img.width, img.height, "");
SetTargetFPS(60); SetTargetFPS(60);
std::vector<glm::uvec2> pts;
for (int i = 0; i < img.width * img.height; ++i)
{
int x = i % img.width;
int y = i / img.width;
auto val = ((glm::u8vec3 *)img.data + i)->r < 10;
bool found = false;
for(int dy = -1; dy <= 1; ++dy)
{
for(int dx = -1; dx <= 1; ++dx)
{
auto x2 = x + dx;
auto y2 = y + dy;
auto index = y2 * img.width + x2;
if(index >= 0 && index < img.width * img.height)
{
auto v2 = ((glm::u8vec3 *)img.data + index)->r < 10;
if(val != v2)
{
found = true;
break;
}
}
}
if(found)
{
break;
}
}
if(found)
{
pts.emplace_back(x, y);
}
}
kdtree tree(pts);
while (!WindowShouldClose()) while (!WindowShouldClose())
{ {
BeginDrawing(); BeginDrawing();
ClearBackground(WHITE); ClearBackground(WHITE);
tree.draw();
EndDrawing(); EndDrawing();
} }