diff --git a/examples/02-metaballs/metaballs.cpp b/examples/02-metaballs/metaballs.cpp index 990f4957a..24db1be33 100644 --- a/examples/02-metaballs/metaballs.cpp +++ b/examples/02-metaballs/metaballs.cpp @@ -408,6 +408,7 @@ uint32_t triangulate( , const float* _xyz , const Grid* _val[8] , float _iso + , float _scale ) { uint8_t cubeindex = 0; @@ -445,9 +446,9 @@ uint32_t triangulate( } } - float dr = _rgb[3] - _rgb[0]; - float dg = _rgb[4] - _rgb[1]; - float db = _rgb[5] - _rgb[2]; + const float dr = _rgb[3] - _rgb[0]; + const float dg = _rgb[4] - _rgb[1]; + const float db = _rgb[5] - _rgb[2]; uint32_t num = 0; const int8_t* indices = s_indices[cubeindex]; @@ -456,13 +457,13 @@ uint32_t triangulate( const float* vertex = verts[uint8_t(indices[ii])]; float* xyz = (float*)_result; - xyz[0] = _xyz[0] + vertex[0]; - xyz[1] = _xyz[1] + vertex[1]; - xyz[2] = _xyz[2] + vertex[2]; + xyz[0] = _xyz[0] + vertex[0]*_scale; + xyz[1] = _xyz[1] + vertex[1]*_scale; + xyz[2] = _xyz[2] + vertex[2]*_scale; - xyz[3] = vertex[3]; - xyz[4] = vertex[4]; - xyz[5] = vertex[5]; + xyz[3] = vertex[3]*_scale; + xyz[4] = vertex[4]*_scale; + xyz[5] = vertex[5]*_scale; uint32_t rr = uint8_t( (_rgb[0] + vertex[0]*dr)*255.0f); uint32_t gg = uint8_t( (_rgb[1] + vertex[1]*dg)*255.0f); @@ -482,7 +483,8 @@ uint32_t triangulate( return num; } -#define DIMS 32 +constexpr uint32_t kMaxDims = 32; +constexpr float kMaxDimsF = float(kMaxDims); class ExampleMetaballs : public entry::AppI { @@ -531,7 +533,7 @@ public: // Create program from shaders. m_program = bgfx::createProgram(vsh, fsh, true /* destroy shaders when program is destroyed */); - m_grid = new Grid[DIMS*DIMS*DIMS]; + m_grid = new Grid[kMaxDims*kMaxDims*kMaxDims]; m_timeOffset = bx::getHPCounter(); imguiCreate(); @@ -554,9 +556,8 @@ public: bool update() override { - const uint32_t ypitch = DIMS; - const uint32_t zpitch = DIMS*DIMS; - const float invdim = 1.0f/float(DIMS-1); + const uint32_t ypitch = kMaxDims; + const uint32_t zpitch = kMaxDims*kMaxDims; if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) ) { @@ -572,6 +573,29 @@ public: showExampleDialog(this); + ImGui::SetNextWindowPos( + ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f) + , ImGuiCond_FirstUseEver + ); + ImGui::SetNextWindowSize( + ImVec2(m_width / 5.0f, m_height / 7.0f) + , ImGuiCond_FirstUseEver + ); + ImGui::Begin("Settings" + , NULL + , 0 + ); + static float iso = 0.75f; + ImGui::SliderFloat("ISO", &iso, 0.1f, 4.0f); + + static uint32_t numDims = kMaxDims; + ImGui::SliderInt("Size", (int*)&numDims, 8, kMaxDims); + ImGui::End(); + + const float numDimsF = float(numDims); + const float scale = kMaxDimsF/numDimsF; + const float invDim = 1.0f/(numDimsF-1.0f); + // Set view 0 default viewport. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) ); @@ -618,21 +642,21 @@ public: float sphere[numSpheres][4]; for (uint32_t ii = 0; ii < numSpheres; ++ii) { - sphere[ii][0] = bx::sin(time*(ii*0.21f)+ii*0.37f) * (DIMS * 0.5f - 8.0f); - sphere[ii][1] = bx::sin(time*(ii*0.37f)+ii*0.67f) * (DIMS * 0.5f - 8.0f); - sphere[ii][2] = bx::cos(time*(ii*0.11f)+ii*0.13f) * (DIMS * 0.5f - 8.0f); - sphere[ii][3] = 1.0f/(2.0f + (bx::sin(time*(ii*0.13f) )*0.5f+0.5f)*2.0f); + sphere[ii][0] = bx::sin(time*(ii*0.21f)+ii*0.37f) * (kMaxDimsF * 0.5f - 8.0f); + sphere[ii][1] = bx::sin(time*(ii*0.37f)+ii*0.67f) * (kMaxDimsF * 0.5f - 8.0f); + sphere[ii][2] = bx::cos(time*(ii*0.11f)+ii*0.13f) * (kMaxDimsF * 0.5f - 8.0f); + sphere[ii][3] = 1.0f/(3.0f + (bx::sin(time*(ii*0.13f) )*0.5f+0.5f)*0.9f ); } profUpdate = bx::getHPCounter(); - for (uint32_t zz = 0; zz < DIMS; ++zz) + for (uint32_t zz = 0; zz < numDims; ++zz) { - for (uint32_t yy = 0; yy < DIMS; ++yy) + for (uint32_t yy = 0; yy < numDims; ++yy) { - uint32_t offset = (zz*DIMS+yy)*DIMS; + uint32_t offset = (zz*kMaxDims+yy)*kMaxDims; - for (uint32_t xx = 0; xx < DIMS; ++xx) + for (uint32_t xx = 0; xx < numDims; ++xx) { uint32_t xoffset = offset + xx; @@ -641,12 +665,12 @@ public: for (uint32_t ii = 0; ii < numSpheres; ++ii) { const float* pos = sphere[ii]; - float dx = pos[0] - (-DIMS*0.5f + float(xx) ); - float dy = pos[1] - (-DIMS*0.5f + float(yy) ); - float dz = pos[2] - (-DIMS*0.5f + float(zz) ); - float invr = pos[3]; - float dot = dx*dx + dy*dy + dz*dz; - dot *= invr*invr; + float dx = pos[0] - (-kMaxDimsF*0.5f + float(xx)*scale); + float dy = pos[1] - (-kMaxDimsF*0.5f + float(yy)*scale); + float dz = pos[2] - (-kMaxDimsF*0.5f + float(zz)*scale); + float invR = pos[3]; + float dot = dx*dx + dy*dy + dz*dz; + dot *= bx::square(invR); dist *= dot; dist += prod; @@ -662,13 +686,13 @@ public: profNormal = bx::getHPCounter(); - for (uint32_t zz = 1; zz < DIMS-1; ++zz) + for (uint32_t zz = 1; zz < numDims-1; ++zz) { - for (uint32_t yy = 1; yy < DIMS-1; ++yy) + for (uint32_t yy = 1; yy < numDims-1; ++yy) { - uint32_t offset = (zz*DIMS+yy)*DIMS; + uint32_t offset = (zz*kMaxDims+yy)*kMaxDims; - for (uint32_t xx = 1; xx < DIMS-1; ++xx) + for (uint32_t xx = 1; xx < numDims-1; ++xx) { uint32_t xoffset = offset + xx; @@ -691,31 +715,31 @@ public: PosNormalColorVertex* vertex = (PosNormalColorVertex*)tvb.data; - for (uint32_t zz = 0; zz < DIMS-1 && numVertices+12 < maxVertices; ++zz) + for (uint32_t zz = 0; zz < numDims-1 && numVertices+12 < maxVertices; ++zz) { float rgb[6]; - rgb[2] = zz*invdim; - rgb[5] = (zz+1)*invdim; + rgb[2] = zz*invDim; + rgb[5] = (zz+1)*invDim; - for (uint32_t yy = 0; yy < DIMS-1 && numVertices+12 < maxVertices; ++yy) + for (uint32_t yy = 0; yy < numDims-1 && numVertices+12 < maxVertices; ++yy) { - uint32_t offset = (zz*DIMS+yy)*DIMS; + uint32_t offset = (zz*kMaxDims+yy)*kMaxDims; - rgb[1] = yy*invdim; - rgb[4] = (yy+1)*invdim; + rgb[1] = yy*invDim; + rgb[4] = (yy+1)*invDim; - for (uint32_t xx = 0; xx < DIMS-1 && numVertices+12 < maxVertices; ++xx) + for (uint32_t xx = 0; xx < numDims-1 && numVertices+12 < maxVertices; ++xx) { uint32_t xoffset = offset + xx; - rgb[0] = xx*invdim; - rgb[3] = (xx+1)*invdim; + rgb[0] = xx*invDim; + rgb[3] = (xx+1)*invDim; float pos[3] = { - -DIMS*0.5f + float(xx), - -DIMS*0.5f + float(yy), - -DIMS*0.5f + float(zz) + -kMaxDimsF*0.5f + float(xx)*scale, + -kMaxDimsF*0.5f + float(yy)*scale, + -kMaxDimsF*0.5f + float(zz)*scale, }; const Grid* grid = m_grid; @@ -730,7 +754,15 @@ public: &grid[xoffset ], }; - uint32_t num = triangulate( (uint8_t*)vertex, PosNormalColorVertex::ms_layout.getStride(), rgb, pos, val, 0.5f); + uint32_t num = triangulate( + (uint8_t*)vertex + , PosNormalColorVertex::ms_layout.getStride() + , rgb + , pos + , val + , iso + , scale + ); vertex += num; numVertices += num; } @@ -756,7 +788,7 @@ public: // Display stats. ImGui::SetNextWindowPos( - ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f) + ImVec2(m_width - m_width / 5.0f - 10.0f, m_height / 7.0f + 30.0f) , ImGuiCond_FirstUseEver ); ImGui::SetNextWindowSize(