Skip to content

Commit

Permalink
fix color picker color converting
Browse files Browse the repository at this point in the history
  • Loading branch information
hotstreams committed Aug 30, 2024
1 parent bedf060 commit d99b694
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 73 deletions.
5 changes: 0 additions & 5 deletions include/limitless/renderer/color_picker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,16 @@ namespace Limitless {
};
std::list<PickData> data;

std::unordered_map<uint64_t, std::shared_ptr<Buffer>> instanced_buffers;

Framebuffer framebuffer;

static uint32_t convert(glm::uvec3 color) noexcept;
static glm::vec3 convert(uint32_t id) noexcept;

void process(Context& ctx);
public:
explicit ColorPicker(Renderer& renderer);

void onPick(Context& ctx, glm::uvec2 coords, std::function<void(uint32_t)> callback);

void update(Scene &scene, const Camera &camera) override;

void render(InstanceRenderer &renderer, Scene &scene, Context &ctx, const Assets &assets, const Camera &camera, UniformSetter &setter) override;

void onFramebufferChange(glm::uvec2 size) override;
Expand Down
21 changes: 6 additions & 15 deletions shaders/pipeline/color_picker.frag
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,16 @@ ENGINE::MATERIALDEPENDENT

#include "../interface_block/fragment.glsl"
#include "../shading/shading_mctx.glsl"
#include "../instance/instance_fs.glsl"

out vec3 color;

uniform vec3 color_id;

// INSTANCED MODEL
#if defined (ENGINE_MATERIAL_INSTANCED_MODEL)
layout (std430) buffer color_buffer {
vec3 _colors[];
};
#endif
//

vec3 getColorId() {
#if defined (ENGINE_MATERIAL_INSTANCED_MODEL)
return _colors[getInstanceId()];
#else
return color_id;
#endif
uint id = getId();
uint r = (id & 0x000000FF) >> 0;
uint g = (id & 0x0000FF00) >> 8;
uint b = (id & 0x00FF0000) >> 16;
return vec3(r, g, b) / 255.0;
}

void main() {
Expand Down
57 changes: 4 additions & 53 deletions src/limitless/renderer/color_picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@

using namespace Limitless;

glm::vec3 ColorPicker::convert(uint32_t id) noexcept {
const auto r = (id & 0x000000FF) >> 0;
const auto g = (id & 0x0000FF00) >> 8;
const auto b = (id & 0x00FF0000) >> 16;
return glm::vec3{r, g, b} / 255.0f;
}

uint32_t ColorPicker::convert(glm::uvec3 color) noexcept {
return color.r + color.g * 256 + color.b * 256 * 256;
}
Expand All @@ -39,50 +32,17 @@ void ColorPicker::process(Context& ctx) {

ctx.setPixelStore(PixelStore::UnpackAlignment, 1);

glm::uvec3 color {};
glReadPixels(info.coords.x, info.coords.y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &color[0]);
uint8_t color[3];
glReadPixels(info.coords.x, info.coords.y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, color);

framebuffer.unbind();

info.callback(convert(color));
info.callback(convert({color[0], color[1], color[2]}));

data.pop_front();
}
}

void ColorPicker::update([[maybe_unused]] Scene& scene, [[maybe_unused]] const Camera &camera) {
auto& frustum_culling = renderer.getInstanceRenderer().getFrustumCulling();
//TODO: buffer changed check?
// remove old (removed from scene) buffers
for (const auto& instance: frustum_culling.getVisibleInstances()) {
if (instance->getInstanceType() == InstanceType::Instanced) {
if (instanced_buffers.count(instance->getId()) == 0) {
instanced_buffers[instance->getId()] = Buffer::builder()
.target(Buffer::Type::ShaderStorage)
.usage(Buffer::Usage::DynamicDraw)
.access(Buffer::MutableAccess::WriteOrphaning)
.data(nullptr)
.size(sizeof(glm::vec3))
.build("color_buffer", *Context::getCurrentContext());
} else {
auto instances = frustum_culling.getVisibleModelInstanced(static_cast<InstancedInstance&>(*instance));
std::vector<glm::vec3> colors;
colors.reserve(instances.size());
for (const auto& inst: instances) {
colors.emplace_back(convert(inst->getId()));
}
// ensure buffer size
auto& buffer = instanced_buffers[instance->getId()];
auto size = sizeof(glm::vec3) * colors.size();
if (buffer->getSize() < size) {
buffer->resize(size);
}
buffer->mapData(colors.data(), size);
}
}
}
}

void ColorPicker::render(InstanceRenderer& renderer, [[maybe_unused]] Scene &scene, Context &ctx, const Assets &assets, [[maybe_unused]] const Camera &camera, UniformSetter &setter) {
if (data.empty()) {
return;
Expand All @@ -98,16 +58,7 @@ void ColorPicker::render(InstanceRenderer& renderer, [[maybe_unused]] Scene &sce

framebuffer.clear();

auto isetter = [&](ShaderProgram& shader, const Instance& instance) {
if (instance.getInstanceType() == InstanceType::Instanced) {
instanced_buffers[instance.getId()]->bindBase(ctx.getIndexedBuffers().getBindingPoint(IndexedBuffer::Type::ShaderStorage, "color_buffer"));
return;
}

shader.setUniform("color_id", convert(instance.getId()));
};

renderer.renderScene({ctx, assets, ShaderType::ColorPicker, ms::Blending::Opaque, setter, {isetter}});
renderer.renderScene({ctx, assets, ShaderType::ColorPicker, ms::Blending::Opaque, setter});

for (auto& pick: data) {
if (!pick.sync.isAlreadyPlaced()) {
Expand Down

0 comments on commit d99b694

Please sign in to comment.