2016-07-10 09:41:38 +03:00
|
|
|
/*
|
2016-07-10 09:42:03 +03:00
|
|
|
* Copyright 2016 Joseph Cherlin. All rights reserved.
|
2016-07-10 09:41:38 +03:00
|
|
|
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "bgfx_utils.h"
|
|
|
|
#include "imgui/imgui.h"
|
|
|
|
#include <bx/rng.h>
|
|
|
|
#include <map>
|
|
|
|
|
2017-06-26 07:44:04 +03:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2016-07-10 09:41:38 +03:00
|
|
|
#define RENDER_PASS_SHADING 0 // Default forward rendered geo with simple shading
|
|
|
|
#define RENDER_PASS_ID 1 // ID buffer for picking
|
|
|
|
#define RENDER_PASS_BLIT 2 // Blit GPU render target to CPU texture
|
|
|
|
|
|
|
|
#define ID_DIM 8 // Size of the ID buffer
|
|
|
|
|
|
|
|
class ExamplePicking : public entry::AppI
|
|
|
|
{
|
2017-06-26 07:44:04 +03:00
|
|
|
public:
|
2019-08-18 00:40:38 +03:00
|
|
|
ExamplePicking(const char* _name, const char* _description, const char* _url)
|
|
|
|
: entry::AppI(_name, _description, _url)
|
2017-06-26 07:44:04 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-07-15 10:17:29 +03:00
|
|
|
void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
|
2016-07-10 09:41:38 +03:00
|
|
|
{
|
|
|
|
Args args(_argc, _argv);
|
|
|
|
|
2017-06-30 08:23:18 +03:00
|
|
|
m_width = _width;
|
|
|
|
m_height = _height;
|
2017-06-26 07:44:04 +03:00
|
|
|
m_debug = BGFX_DEBUG_NONE;
|
2016-07-10 21:10:44 +03:00
|
|
|
m_reset = BGFX_RESET_VSYNC;
|
2016-07-10 09:41:38 +03:00
|
|
|
|
2018-04-18 01:42:18 +03:00
|
|
|
bgfx::Init init;
|
|
|
|
init.type = args.m_type;
|
|
|
|
init.vendorId = args.m_pciId;
|
|
|
|
init.resolution.width = m_width;
|
|
|
|
init.resolution.height = m_height;
|
|
|
|
init.resolution.reset = m_reset;
|
|
|
|
bgfx::init(init);
|
2016-07-10 09:41:38 +03:00
|
|
|
|
|
|
|
// Enable debug text.
|
|
|
|
bgfx::setDebug(m_debug);
|
|
|
|
|
|
|
|
// Set up screen clears
|
|
|
|
bgfx::setViewClear(RENDER_PASS_SHADING
|
2016-07-10 09:42:03 +03:00
|
|
|
, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
|
2016-07-10 09:41:38 +03:00
|
|
|
, 0x303030ff
|
|
|
|
, 1.0f
|
|
|
|
, 0
|
|
|
|
);
|
|
|
|
|
2016-07-10 09:42:03 +03:00
|
|
|
// ID buffer clears to black, which represnts clicking on nothing (background)
|
2016-07-10 09:41:38 +03:00
|
|
|
bgfx::setViewClear(RENDER_PASS_ID
|
2016-07-10 09:42:03 +03:00
|
|
|
, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
|
2016-07-10 09:41:38 +03:00
|
|
|
, 0x000000ff
|
|
|
|
, 1.0f
|
|
|
|
, 0
|
|
|
|
);
|
|
|
|
|
2016-07-10 09:42:03 +03:00
|
|
|
// Create uniforms
|
2016-07-10 21:10:44 +03:00
|
|
|
u_tint = bgfx::createUniform("u_tint", bgfx::UniformType::Vec4); // Tint for when you click on items
|
|
|
|
u_id = bgfx::createUniform("u_id", bgfx::UniformType::Vec4); // ID for drawing into ID buffer
|
2016-07-10 09:41:38 +03:00
|
|
|
|
|
|
|
// Create program from shaders.
|
2016-07-10 21:10:44 +03:00
|
|
|
m_shadingProgram = loadProgram("vs_picking_shaded", "fs_picking_shaded"); // Blinn shading
|
|
|
|
m_idProgram = loadProgram("vs_picking_shaded", "fs_picking_id"); // Shader for drawing into ID buffer
|
2016-07-10 09:42:03 +03:00
|
|
|
|
2016-07-10 21:10:44 +03:00
|
|
|
static const char* meshPaths[] =
|
|
|
|
{
|
|
|
|
"meshes/orb.bin",
|
|
|
|
"meshes/column.bin",
|
|
|
|
"meshes/bunny.bin",
|
|
|
|
"meshes/cube.bin",
|
|
|
|
"meshes/tree.bin",
|
|
|
|
"meshes/hollowcube.bin",
|
|
|
|
};
|
|
|
|
|
|
|
|
static const float meshScale[] =
|
|
|
|
{
|
|
|
|
0.5f,
|
|
|
|
0.05f,
|
|
|
|
0.5f,
|
|
|
|
0.25f,
|
|
|
|
0.05f,
|
|
|
|
0.05f,
|
|
|
|
};
|
2016-07-10 09:42:03 +03:00
|
|
|
|
2016-07-10 10:10:57 +03:00
|
|
|
m_highlighted = UINT32_MAX;
|
|
|
|
m_reading = 0;
|
|
|
|
m_currFrame = UINT32_MAX;
|
|
|
|
m_fov = 3.0f;
|
|
|
|
m_cameraSpin = false;
|
|
|
|
|
2016-07-10 09:42:03 +03:00
|
|
|
bx::RngMwc mwc; // Random number generator
|
2016-07-10 21:10:44 +03:00
|
|
|
for (uint32_t ii = 0; ii < 12; ++ii)
|
2016-07-10 09:42:03 +03:00
|
|
|
{
|
2016-07-10 21:10:44 +03:00
|
|
|
m_meshes[ii] = meshLoad(meshPaths[ii % BX_COUNTOF(meshPaths)]);
|
|
|
|
m_meshScale[ii] = meshScale[ii % BX_COUNTOF(meshPaths)];
|
2016-07-10 09:42:03 +03:00
|
|
|
// For the sake of this example, we'll give each mesh a random color, so the debug output looks colorful.
|
|
|
|
// In an actual app, you'd probably just want to count starting from 1
|
2016-07-10 21:10:44 +03:00
|
|
|
uint32_t rr = mwc.gen() % 256;
|
|
|
|
uint32_t gg = mwc.gen() % 256;
|
|
|
|
uint32_t bb = mwc.gen() % 256;
|
|
|
|
m_idsF[ii][0] = rr / 255.0f;
|
|
|
|
m_idsF[ii][1] = gg / 255.0f;
|
|
|
|
m_idsF[ii][2] = bb / 255.0f;
|
|
|
|
m_idsF[ii][3] = 1.0f;
|
|
|
|
m_idsU[ii] = rr + (gg << 8) + (bb << 16) + (255u << 24);
|
2016-07-10 09:42:03 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 09:41:38 +03:00
|
|
|
m_timeOffset = bx::getHPCounter();
|
|
|
|
|
2016-07-10 09:42:03 +03:00
|
|
|
// Set up ID buffer, which has a color target and depth buffer
|
2016-08-20 07:05:37 +03:00
|
|
|
m_pickingRT = bgfx::createTexture2D(ID_DIM, ID_DIM, false, 1, bgfx::TextureFormat::RGBA8, 0
|
2016-07-10 21:32:20 +03:00
|
|
|
| BGFX_TEXTURE_RT
|
2018-08-21 03:12:26 +03:00
|
|
|
| BGFX_SAMPLER_MIN_POINT
|
|
|
|
| BGFX_SAMPLER_MAG_POINT
|
|
|
|
| BGFX_SAMPLER_MIP_POINT
|
|
|
|
| BGFX_SAMPLER_U_CLAMP
|
|
|
|
| BGFX_SAMPLER_V_CLAMP
|
2016-07-10 09:42:03 +03:00
|
|
|
);
|
2016-08-20 07:05:37 +03:00
|
|
|
m_pickingRTDepth = bgfx::createTexture2D(ID_DIM, ID_DIM, false, 1, bgfx::TextureFormat::D24S8, 0
|
2016-07-10 09:42:03 +03:00
|
|
|
| BGFX_TEXTURE_RT
|
2018-08-21 03:12:26 +03:00
|
|
|
| BGFX_SAMPLER_MIN_POINT
|
|
|
|
| BGFX_SAMPLER_MAG_POINT
|
|
|
|
| BGFX_SAMPLER_MIP_POINT
|
|
|
|
| BGFX_SAMPLER_U_CLAMP
|
|
|
|
| BGFX_SAMPLER_V_CLAMP
|
2016-07-10 09:42:03 +03:00
|
|
|
);
|
|
|
|
|
2016-07-10 21:32:20 +03:00
|
|
|
// CPU texture for blitting to and reading ID buffer so we can see what was clicked on.
|
|
|
|
// Impossible to read directly from a render target, you *must* blit to a CPU texture
|
|
|
|
// first. Algorithm Overview: Render on GPU -> Blit to CPU texture -> Read from CPU
|
|
|
|
// texture.
|
2016-08-20 07:05:37 +03:00
|
|
|
m_blitTex = bgfx::createTexture2D(ID_DIM, ID_DIM, false, 1, bgfx::TextureFormat::RGBA8, 0
|
2016-07-10 21:10:44 +03:00
|
|
|
| BGFX_TEXTURE_BLIT_DST
|
|
|
|
| BGFX_TEXTURE_READ_BACK
|
2018-08-21 03:12:26 +03:00
|
|
|
| BGFX_SAMPLER_MIN_POINT
|
|
|
|
| BGFX_SAMPLER_MAG_POINT
|
|
|
|
| BGFX_SAMPLER_MIP_POINT
|
|
|
|
| BGFX_SAMPLER_U_CLAMP
|
|
|
|
| BGFX_SAMPLER_V_CLAMP
|
2016-07-10 09:42:03 +03:00
|
|
|
);
|
|
|
|
|
2016-07-10 21:10:44 +03:00
|
|
|
bgfx::TextureHandle rt[2] =
|
|
|
|
{
|
|
|
|
m_pickingRT,
|
|
|
|
m_pickingRTDepth
|
|
|
|
};
|
2016-07-10 09:42:03 +03:00
|
|
|
m_pickingFB = bgfx::createFrameBuffer(BX_COUNTOF(rt), rt, true);
|
|
|
|
|
|
|
|
imguiCreate();
|
2016-07-10 09:41:38 +03:00
|
|
|
}
|
|
|
|
|
2017-07-15 10:17:29 +03:00
|
|
|
int shutdown() override
|
2016-07-10 09:41:38 +03:00
|
|
|
{
|
2016-07-10 21:10:44 +03:00
|
|
|
for (uint32_t ii = 0; ii < 12; ++ii)
|
2016-07-10 09:42:03 +03:00
|
|
|
{
|
2016-07-10 21:10:44 +03:00
|
|
|
meshUnload(m_meshes[ii]);
|
2016-07-10 09:42:03 +03:00
|
|
|
}
|
2016-07-10 09:41:38 +03:00
|
|
|
|
|
|
|
// Cleanup.
|
2017-07-18 08:29:43 +03:00
|
|
|
bgfx::destroy(m_shadingProgram);
|
|
|
|
bgfx::destroy(m_idProgram);
|
2016-07-10 09:41:38 +03:00
|
|
|
|
2017-07-18 08:29:43 +03:00
|
|
|
bgfx::destroy(u_tint);
|
|
|
|
bgfx::destroy(u_id);
|
2016-07-10 09:41:38 +03:00
|
|
|
|
2017-07-18 08:29:43 +03:00
|
|
|
bgfx::destroy(m_pickingFB);
|
|
|
|
bgfx::destroy(m_pickingRT);
|
|
|
|
bgfx::destroy(m_pickingRTDepth);
|
|
|
|
bgfx::destroy(m_blitTex);
|
2016-07-10 09:41:38 +03:00
|
|
|
|
2016-07-10 09:42:03 +03:00
|
|
|
imguiDestroy();
|
2016-07-10 09:41:38 +03:00
|
|
|
|
|
|
|
// Shutdown bgfx.
|
|
|
|
bgfx::shutdown();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-15 10:17:29 +03:00
|
|
|
bool update() override
|
2016-07-10 09:41:38 +03:00
|
|
|
{
|
2016-07-10 21:32:20 +03:00
|
|
|
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
|
2016-07-10 09:41:38 +03:00
|
|
|
{
|
2017-07-23 03:40:27 +03:00
|
|
|
// Draw UI
|
|
|
|
imguiBeginFrame(
|
|
|
|
m_mouseState.m_mx
|
|
|
|
, m_mouseState.m_my
|
|
|
|
, (m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0)
|
|
|
|
| (m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0)
|
|
|
|
| (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
|
|
|
|
, m_mouseState.m_mz
|
|
|
|
, uint16_t(m_width)
|
|
|
|
, uint16_t(m_height)
|
|
|
|
);
|
2016-07-10 09:41:38 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
const bgfx::Caps* caps = bgfx::getCaps();
|
|
|
|
bool blitSupport = 0 != (caps->supported & BGFX_CAPS_TEXTURE_BLIT);
|
2016-07-10 09:41:38 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
showExampleDialog(this
|
|
|
|
, !blitSupport
|
|
|
|
? "BGFX_CAPS_TEXTURE_BLIT is not supported."
|
|
|
|
: NULL
|
|
|
|
);
|
2016-07-10 09:41:38 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
if (blitSupport)
|
|
|
|
{
|
|
|
|
ImGui::SetNextWindowPos(
|
2017-12-02 08:04:27 +03:00
|
|
|
ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
|
|
|
|
, ImGuiCond_FirstUseEver
|
|
|
|
);
|
|
|
|
ImGui::SetNextWindowSize(
|
|
|
|
ImVec2(m_width / 5.0f, m_height / 2.0f)
|
|
|
|
, ImGuiCond_FirstUseEver
|
2017-07-23 03:40:27 +03:00
|
|
|
);
|
|
|
|
ImGui::Begin("Settings"
|
|
|
|
, NULL
|
2017-12-02 08:04:27 +03:00
|
|
|
, 0
|
2017-07-23 03:40:27 +03:00
|
|
|
);
|
2016-07-10 09:42:03 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
ImGui::Image(m_pickingRT, ImVec2(m_width / 5.0f - 16.0f, m_width / 5.0f - 16.0f) );
|
|
|
|
ImGui::SliderFloat("Field of view", &m_fov, 1.0f, 60.0f);
|
|
|
|
ImGui::Checkbox("Spin Camera", &m_cameraSpin);
|
2016-07-10 09:42:03 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
ImGui::End();
|
2016-09-25 21:25:11 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
bgfx::setViewFrameBuffer(RENDER_PASS_ID, m_pickingFB);
|
2016-09-25 21:25:11 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
float time = (float)( (bx::getHPCounter() - m_timeOffset) / double(bx::getHPFrequency() ) );
|
2016-09-25 21:25:11 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
// Set up matrices for basic forward renderer
|
|
|
|
const float camSpeed = 0.25;
|
|
|
|
float cameraSpin = (float)m_cameraSpin;
|
|
|
|
float eyeDist = 2.5f;
|
2018-11-17 08:54:20 +03:00
|
|
|
|
|
|
|
const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
|
|
|
|
const bx::Vec3 eye =
|
2017-07-23 03:40:27 +03:00
|
|
|
{
|
2018-01-14 02:33:50 +03:00
|
|
|
-eyeDist * bx::sin(time*cameraSpin*camSpeed),
|
2017-07-23 03:40:27 +03:00
|
|
|
0.0f,
|
2018-01-14 02:33:50 +03:00
|
|
|
-eyeDist * bx::cos(time*cameraSpin*camSpeed),
|
2017-07-23 03:40:27 +03:00
|
|
|
};
|
2016-09-25 21:25:11 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
float view[16];
|
|
|
|
bx::mtxLookAt(view, eye, at);
|
2016-07-10 09:42:03 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
float proj[16];
|
|
|
|
bx::mtxProj(proj, 60.0f, float(m_width) / float(m_height), 0.1f, 100.0f, caps->homogeneousDepth);
|
2016-07-10 09:42:03 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
// Set up view rect and transform for the shaded pass
|
|
|
|
bgfx::setViewRect(RENDER_PASS_SHADING, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
|
|
|
bgfx::setViewTransform(RENDER_PASS_SHADING, view, proj);
|
2016-09-25 21:25:11 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
// Set up picking pass
|
|
|
|
float viewProj[16];
|
|
|
|
bx::mtxMul(viewProj, view, proj);
|
2016-07-10 09:42:03 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
float invViewProj[16];
|
|
|
|
bx::mtxInverse(invViewProj, viewProj);
|
2016-07-10 09:42:03 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
// Mouse coord in NDC
|
|
|
|
float mouseXNDC = ( m_mouseState.m_mx / (float)m_width ) * 2.0f - 1.0f;
|
|
|
|
float mouseYNDC = ((m_height - m_mouseState.m_my) / (float)m_height) * 2.0f - 1.0f;
|
2016-07-10 09:42:03 +03:00
|
|
|
|
2019-01-04 02:16:29 +03:00
|
|
|
const bx::Vec3 pickEye = bx::mulH({ mouseXNDC, mouseYNDC, 0.0f }, invViewProj);
|
|
|
|
const bx::Vec3 pickAt = bx::mulH({ mouseXNDC, mouseYNDC, 1.0f }, invViewProj);
|
2016-07-10 09:42:03 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
// Look at our unprojected point
|
|
|
|
float pickView[16];
|
2019-01-04 02:16:29 +03:00
|
|
|
bx::mtxLookAt(pickView, pickEye, pickAt);
|
2016-07-10 09:42:03 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
// Tight FOV is best for picking
|
|
|
|
float pickProj[16];
|
|
|
|
bx::mtxProj(pickProj, m_fov, 1, 0.1f, 100.0f, caps->homogeneousDepth);
|
2016-07-10 09:42:03 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
// View rect and transforms for picking pass
|
|
|
|
bgfx::setViewRect(RENDER_PASS_ID, 0, 0, ID_DIM, ID_DIM);
|
|
|
|
bgfx::setViewTransform(RENDER_PASS_ID, pickView, pickProj);
|
2016-07-10 09:42:03 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
// Now that our passes are set up, we can finally draw each mesh
|
2016-07-10 09:42:03 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
// Picking highlights a mesh so we'll set up this tint color
|
|
|
|
const float tintBasic[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
|
|
|
|
const float tintHighlighted[4] = { 0.3f, 0.3f, 2.0f, 1.0f };
|
2016-07-10 21:32:20 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
for (uint32_t mesh = 0; mesh < 12; ++mesh)
|
|
|
|
{
|
|
|
|
const float scale = m_meshScale[mesh];
|
|
|
|
|
|
|
|
// Set up transform matrix for each mesh
|
|
|
|
float mtx[16];
|
|
|
|
bx::mtxSRT(mtx
|
|
|
|
, scale, scale, scale
|
|
|
|
, 0.0f
|
|
|
|
, time*0.37f*(mesh % 2 ? 1.0f : -1.0f)
|
|
|
|
, 0.0f
|
|
|
|
, (mesh % 4) - 1.5f
|
|
|
|
, (mesh / 4) - 1.25f
|
|
|
|
, 0.0f
|
|
|
|
);
|
|
|
|
|
|
|
|
// Submit mesh to both of our render passes
|
|
|
|
// Set uniform based on if this is the highlighted mesh
|
|
|
|
bgfx::setUniform(u_tint
|
|
|
|
, mesh == m_highlighted
|
|
|
|
? tintHighlighted
|
|
|
|
: tintBasic
|
|
|
|
);
|
|
|
|
meshSubmit(m_meshes[mesh], RENDER_PASS_SHADING, m_shadingProgram, mtx);
|
|
|
|
|
|
|
|
// Submit ID pass based on mesh ID
|
|
|
|
bgfx::setUniform(u_id, m_idsF[mesh]);
|
|
|
|
meshSubmit(m_meshes[mesh], RENDER_PASS_ID, m_idProgram, mtx);
|
2016-07-10 09:42:03 +03:00
|
|
|
}
|
2016-07-10 21:32:20 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
// If the user previously clicked, and we're done reading data from GPU, look at ID buffer on CPU
|
|
|
|
// Whatever mesh has the most pixels in the ID buffer is the one the user clicked on.
|
|
|
|
if (m_reading == m_currFrame)
|
2016-07-10 09:42:03 +03:00
|
|
|
{
|
2017-07-23 03:40:27 +03:00
|
|
|
m_reading = 0;
|
|
|
|
std::map<uint32_t, uint32_t> ids; // This contains all the IDs found in the buffer
|
|
|
|
uint32_t maxAmount = 0;
|
|
|
|
for (uint8_t *x = m_blitData; x < m_blitData + ID_DIM * ID_DIM * 4;)
|
2016-07-10 09:42:03 +03:00
|
|
|
{
|
2017-07-23 03:40:27 +03:00
|
|
|
uint8_t rr = *x++;
|
|
|
|
uint8_t gg = *x++;
|
|
|
|
uint8_t bb = *x++;
|
|
|
|
uint8_t aa = *x++;
|
|
|
|
|
|
|
|
if (bgfx::RendererType::Direct3D9 == caps->rendererType)
|
2016-07-10 09:42:03 +03:00
|
|
|
{
|
2017-07-23 03:40:27 +03:00
|
|
|
// Comes back as BGRA
|
|
|
|
uint8_t temp = rr;
|
|
|
|
rr = bb;
|
|
|
|
bb = temp;
|
2016-07-10 09:42:03 +03:00
|
|
|
}
|
2017-07-23 03:40:27 +03:00
|
|
|
|
|
|
|
if (0 == (rr|gg|bb) ) // Skip background
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t hashKey = rr + (gg << 8) + (bb << 16) + (aa << 24);
|
|
|
|
std::map<uint32_t, uint32_t>::iterator mapIter = ids.find(hashKey);
|
|
|
|
uint32_t amount = 1;
|
|
|
|
if (mapIter != ids.end() )
|
|
|
|
{
|
|
|
|
amount = mapIter->second + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ids[hashKey] = amount; // Amount of times this ID (color) has been clicked on in buffer
|
|
|
|
maxAmount = maxAmount > amount
|
|
|
|
? maxAmount
|
|
|
|
: amount
|
|
|
|
;
|
2016-07-10 09:42:03 +03:00
|
|
|
}
|
2016-07-10 21:32:20 +03:00
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
uint32_t idKey = 0;
|
|
|
|
m_highlighted = UINT32_MAX;
|
|
|
|
if (maxAmount)
|
2016-07-10 09:42:03 +03:00
|
|
|
{
|
2017-07-23 03:40:27 +03:00
|
|
|
for (std::map<uint32_t, uint32_t>::iterator mapIter = ids.begin(); mapIter != ids.end(); mapIter++)
|
|
|
|
{
|
|
|
|
if (mapIter->second == maxAmount)
|
|
|
|
{
|
|
|
|
idKey = mapIter->first;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t ii = 0; ii < 12; ++ii)
|
2016-07-10 09:42:03 +03:00
|
|
|
{
|
2017-07-23 03:40:27 +03:00
|
|
|
if (m_idsU[ii] == idKey)
|
|
|
|
{
|
|
|
|
m_highlighted = ii;
|
|
|
|
break;
|
|
|
|
}
|
2016-07-10 09:42:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-23 03:40:27 +03:00
|
|
|
// Start a new readback?
|
|
|
|
if (!m_reading
|
|
|
|
&& m_mouseState.m_buttons[entry::MouseButton::Left])
|
|
|
|
{
|
|
|
|
// Blit and read
|
|
|
|
bgfx::blit(RENDER_PASS_BLIT, m_blitTex, 0, 0, m_pickingRT);
|
|
|
|
m_reading = bgfx::readTexture(m_blitTex, m_blitData);
|
|
|
|
}
|
2016-07-10 09:42:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
imguiEndFrame();
|
2016-07-10 09:41:38 +03:00
|
|
|
|
|
|
|
// Advance to next frame. Rendering thread will be kicked to
|
|
|
|
// process submitted rendering primitives.
|
|
|
|
m_currFrame = bgfx::frame();
|
|
|
|
|
2017-06-30 09:19:20 +03:00
|
|
|
return true;
|
2016-07-10 09:41:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-26 07:44:04 +03:00
|
|
|
entry::MouseState m_mouseState;
|
|
|
|
|
2016-07-10 09:41:38 +03:00
|
|
|
uint32_t m_width;
|
|
|
|
uint32_t m_height;
|
|
|
|
uint32_t m_debug;
|
|
|
|
uint32_t m_reset;
|
2016-07-10 09:42:03 +03:00
|
|
|
int64_t m_timeOffset;
|
2016-07-10 09:41:38 +03:00
|
|
|
|
2016-07-10 09:42:03 +03:00
|
|
|
Mesh* m_meshes[12];
|
|
|
|
float m_meshScale[12];
|
|
|
|
float m_idsF[12][4];
|
|
|
|
uint32_t m_idsU[12];
|
2016-07-10 10:10:57 +03:00
|
|
|
uint32_t m_highlighted;
|
2016-07-10 09:42:03 +03:00
|
|
|
|
|
|
|
// Resource handles
|
2016-07-10 09:41:38 +03:00
|
|
|
bgfx::ProgramHandle m_shadingProgram;
|
2016-07-10 09:42:03 +03:00
|
|
|
bgfx::ProgramHandle m_idProgram;
|
2016-07-10 09:41:38 +03:00
|
|
|
bgfx::UniformHandle u_tint;
|
2016-07-10 09:42:03 +03:00
|
|
|
bgfx::UniformHandle u_id;
|
|
|
|
bgfx::TextureHandle m_pickingRT;
|
|
|
|
bgfx::TextureHandle m_pickingRTDepth;
|
|
|
|
bgfx::TextureHandle m_blitTex;
|
|
|
|
bgfx::FrameBufferHandle m_pickingFB;
|
|
|
|
|
|
|
|
uint8_t m_blitData[ID_DIM*ID_DIM * 4]; // Read blit into this
|
|
|
|
|
2016-07-10 10:10:57 +03:00
|
|
|
uint32_t m_reading;
|
|
|
|
uint32_t m_currFrame;
|
2016-07-10 09:42:03 +03:00
|
|
|
|
2016-07-10 10:10:57 +03:00
|
|
|
float m_fov;
|
|
|
|
bool m_cameraSpin;
|
2016-07-10 09:41:38 +03:00
|
|
|
};
|
|
|
|
|
2017-06-26 07:44:04 +03:00
|
|
|
} // namespace
|
|
|
|
|
2019-08-17 23:25:39 +03:00
|
|
|
ENTRY_IMPLEMENT_MAIN(
|
2019-08-18 00:40:38 +03:00
|
|
|
ExamplePicking
|
|
|
|
, "30-picking"
|
|
|
|
, "Mouse picking via GPU texture readback."
|
|
|
|
, "https://bkaradzic.github.io/bgfx/examples.html#picking"
|
|
|
|
);
|