diff --git a/examples/28-wireframe/fs_wf_vertex_pulling.sc b/examples/28-wireframe/fs_wf_vertex_pulling.sc new file mode 100644 index 000000000..2fe9194ef --- /dev/null +++ b/examples/28-wireframe/fs_wf_vertex_pulling.sc @@ -0,0 +1,15 @@ +$input v_color + +/* + * Copyright 2026 Alessandro Muntoni. All rights reserved. + * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE + */ + +#include "../common/common.sh" +#include "uniforms.sh" + +void main() +{ + gl_FragColor = v_color; +} + diff --git a/examples/28-wireframe/uniforms.sh b/examples/28-wireframe/uniforms.sh index 934c41c6a..d4e1c71f3 100644 --- a/examples/28-wireframe/uniforms.sh +++ b/examples/28-wireframe/uniforms.sh @@ -10,3 +10,5 @@ uniform vec4 u_params[3]; #define u_wfOpacity u_params[1].w #define u_drawEdges u_params[2].x #define u_wfThickness u_params[2].y +#define u_wfStride u_params[2].z + diff --git a/examples/28-wireframe/varying.def.sc b/examples/28-wireframe/varying.def.sc index c0c52962a..008acd134 100644 --- a/examples/28-wireframe/varying.def.sc +++ b/examples/28-wireframe/varying.def.sc @@ -1,7 +1,9 @@ vec3 v_view : TEXCOORD0 = vec3(0.0, 0.0, 0.0); vec3 v_bc : TEXCOORD1 = vec3(0.0, 0.0, 0.0); vec3 v_normal : NORMAL = vec3(0.0, 0.0, 1.0); +vec4 v_color : COLOR0 = vec4(0.0, 0.0, 0.0, 0.0); vec3 a_position : POSITION; vec3 a_color1 : COLOR1; vec3 a_normal : NORMAL; + diff --git a/examples/28-wireframe/vs_wf_vertex_pulling.sc b/examples/28-wireframe/vs_wf_vertex_pulling.sc new file mode 100644 index 000000000..e890fa69c --- /dev/null +++ b/examples/28-wireframe/vs_wf_vertex_pulling.sc @@ -0,0 +1,117 @@ +$output v_color + +/* + * Copyright 2026 Alessandro Muntoni. All rights reserved. + * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE + */ + +#include "../common/common.sh" +#include "uniforms.sh" + +#include + +BUFFER_RO(positions, float, 0); +BUFFER_RO(indices, uint, 1); + +uint getIndex(uint i) +{ + uint idx32 = indices[i / 2u]; + if (i % 2u == 0u) + { + return idx32 & 0xFFFFu; + } + else + { + return idx32 >> 16u; + } +} + +vec3 getPosition(uint index) +{ + uint strideFloats = uint(u_wfStride); + uint offset = index * strideFloats; + return vec3( + positions[offset + 0u], + positions[offset + 1u], + positions[offset + 2u] + ); +} + +#define NEAR_EPSILON 0.001 +#define LENGTH_EPSILON 0.0001 + +void main() +{ + uint lineIndex = gl_VertexID / 6u; + uint localVertex = gl_VertexID % 6u; + + uint edgeIndex = lineIndex % 3u; + uint triFirstIndex = lineIndex - edgeIndex; + + uint idx0_loc = triFirstIndex + edgeIndex; + uint idx1_loc = triFirstIndex + ((edgeIndex + 1u) % 3u); + + uint vertexIndex0 = getIndex(idx0_loc); + uint vertexIndex1 = getIndex(idx1_loc); + + vec3 p0 = getPosition(vertexIndex0); + vec3 p1 = getPosition(vertexIndex1); + + vec4 p0_NDC = mul(u_modelViewProj, vec4(p0, 1.0)); + vec4 p1_NDC = mul(u_modelViewProj, vec4(p1, 1.0)); + + vec4 c0 = vec4(u_wfColor, u_wfOpacity); + vec4 c1 = vec4(u_wfColor, u_wfOpacity); + + vec4 clippedP0 = p0_NDC; + vec4 clippedP1 = p1_NDC; + vec4 clippedColor0 = c0; + vec4 clippedColor1 = c1; + + if (p0_NDC.w < NEAR_EPSILON) + { + float t = (NEAR_EPSILON - p0_NDC.w) / (p1_NDC.w - p0_NDC.w); + clippedP0 = mix(p0_NDC, p1_NDC, t); + clippedColor0 = mix(c0, c1, t); + } + if (p1_NDC.w < NEAR_EPSILON) + { + float t = (NEAR_EPSILON - p1_NDC.w) / (p0_NDC.w - p1_NDC.w); + clippedP1 = mix(p1_NDC, p0_NDC, t); + clippedColor1 = mix(c1, c0, t); + } + + const vec2 offsets[6] = + { + vec2(-1.0, -1.0), vec2(-1.0, 1.0), vec2( 1.0, -1.0), + vec2( 1.0, -1.0), vec2(-1.0, 1.0), vec2( 1.0, 1.0) + }; + vec2 offset = offsets[localVertex]; + vec2 uv = offset * 0.5 + 0.5; + + vec4 p = mix(clippedP0, clippedP1, uv.x); + vec4 color = mix(clippedColor0, clippedColor1, uv.x); + + vec2 ndp0 = clippedP0.xy / clippedP0.w; + vec2 ndp1 = clippedP1.xy / clippedP1.w; + vec2 screenDir = ndp1 - ndp0; + float screenDirLen = length(screenDir); + + if (screenDirLen > LENGTH_EPSILON) + { + vec2 T = screenDir / screenDirLen; + vec2 N = vec2(-T.y, T.x); + + float pixelsToNDCX = 2.0 / u_viewRect.z; + float pixelsToNDCY = 2.0 / u_viewRect.w; + + float side = 2.0 * uv.y - 1.0; + vec2 offsetNDC = N * (0.5 * u_wfThickness) * side * vec2(pixelsToNDCX, pixelsToNDCY); + + p.xy = p.xy + (offsetNDC * p.w); + } + + v_color = color; + gl_Position = p; +} + diff --git a/examples/28-wireframe/wireframe.cpp b/examples/28-wireframe/wireframe.cpp index a7fa7b617..a22e696c4 100644 --- a/examples/28-wireframe/wireframe.cpp +++ b/examples/28-wireframe/wireframe.cpp @@ -17,6 +17,7 @@ struct DrawMode WireframeShaded, Wireframe, Shaded, + VertexPullingWireframe, }; }; @@ -239,6 +240,7 @@ struct Uniforms m_wfColor[3] = 1.0f; m_drawEdges = 0.0f; m_wfThickness = 1.5f; + m_wfStride = 0.0f; u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, NumVec4); } @@ -259,7 +261,7 @@ struct Uniforms { /*0*/struct { float m_camPos[3], m_unused0; }; /*1*/struct { float m_wfColor[4]; }; - /*2*/struct { float m_drawEdges, m_wfThickness, m_unused2[2]; }; + /*2*/struct { float m_drawEdges, m_wfThickness, m_wfStride, m_unused2; }; }; float m_params[NumVec4*4]; @@ -310,8 +312,9 @@ public: , 0 ); - m_wfProgram = loadProgram("vs_wf_wireframe", "fs_wf_wireframe"); - m_meshProgram = loadProgram("vs_wf_mesh", "fs_wf_mesh"); + m_wfProgram = loadProgram("vs_wf_wireframe", "fs_wf_wireframe"); + m_vpProgram = loadProgram("vs_wf_vertex_pulling", "fs_wf_vertex_pulling"); + m_meshProgram = loadProgram("vs_wf_mesh", "fs_wf_mesh"); m_uniforms.init(); @@ -342,6 +345,7 @@ public: m_meshes[2].destroy(); bgfx::destroy(m_wfProgram); + bgfx::destroy(m_vpProgram); bgfx::destroy(m_meshProgram); m_uniforms.destroy(); @@ -399,6 +403,7 @@ public: ImGui::RadioButton("Wireframe + Shaded", &m_drawMode, 0); ImGui::RadioButton("Wireframe", &m_drawMode, 1); ImGui::RadioButton("Shaded", &m_drawMode, 2); + ImGui::RadioButton("Vertex Pulling Wireframe", &m_drawMode, 3); const bool wfEnabled = (DrawMode::Shaded != m_drawMode); if ( wfEnabled ) @@ -461,6 +466,7 @@ public: bgfx::setViewTransform(0, view, proj); m_uniforms.m_drawEdges = (DrawMode::WireframeShaded == m_drawMode) ? 1.0f : 0.0f; + m_uniforms.m_wfStride = (float)(m_meshes[m_meshSelection].m_mesh->m_layout.getStride() / 4); m_uniforms.submit(); if (DrawMode::Wireframe == m_drawMode) @@ -475,6 +481,18 @@ public: ; meshSubmit(m_meshes[m_meshSelection].m_mesh, 0, m_wfProgram, m_meshes[m_meshSelection].m_mtx, state); } + else if (DrawMode::VertexPullingWireframe == m_drawMode) + { + uint64_t state = 0 + | BGFX_STATE_WRITE_RGB + | BGFX_STATE_WRITE_A + | BGFX_STATE_WRITE_Z + | BGFX_STATE_CULL_CCW + | BGFX_STATE_MSAA + | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA) + ; + drawVertexPullingWireframe(m_meshes[m_meshSelection].m_mesh, 0, m_vpProgram, m_meshes[m_meshSelection].m_mtx, state); + } else { uint64_t state = 0 @@ -498,9 +516,31 @@ public: return false; } + void drawVertexPullingWireframe(const Mesh* mesh, bgfx::ViewId _id, bgfx::ProgramHandle _program, const float* _mtx, uint64_t _state) + { + bgfx::setTransform(_mtx); + bgfx::setState(_state); + for (const Group& group : mesh->m_groups) + { + bgfx::setBuffer(0, group.m_vbh, bgfx::Access::Read); + bgfx::setBuffer(1, group.m_ibh, bgfx::Access::Read); + + // 6 vertices per line + bgfx::setVertexCount(group.m_numIndices * 6); + bgfx::submit( + _id + , _program + , 0 + , BGFX_DISCARD_INDEX_BUFFER + | BGFX_DISCARD_VERTEX_STREAMS + ); + } + } + entry::MouseState m_mouseState; bgfx::ProgramHandle m_wfProgram; + bgfx::ProgramHandle m_vpProgram; bgfx::ProgramHandle m_meshProgram; uint32_t m_width; @@ -530,3 +570,4 @@ ENTRY_IMPLEMENT_MAIN( , "Drawing wireframe mesh." , "https://bkaradzic.github.io/bgfx/examples.html#wireframe" ); + diff --git a/examples/common/bgfx_utils.cpp b/examples/common/bgfx_utils.cpp index 5bdbff647..65e469bb2 100644 --- a/examples/common/bgfx_utils.cpp +++ b/examples/common/bgfx_utils.cpp @@ -472,7 +472,7 @@ void Mesh::load(bx::ReaderSeekerI* _reader, bool _ramcopy) bx::memCopy(group.m_vertices, mem->data, mem->size); } - group.m_vbh = bgfx::createVertexBuffer(mem, m_layout); + group.m_vbh = bgfx::createVertexBuffer(mem, m_layout, BGFX_BUFFER_COMPUTE_READ | BGFX_BUFFER_COMPUTE_FORMAT_32X1 | BGFX_BUFFER_COMPUTE_TYPE_FLOAT); } break; @@ -506,7 +506,7 @@ void Mesh::load(bx::ReaderSeekerI* _reader, bool _ramcopy) bx::memCopy(group.m_vertices, mem->data, mem->size); } - group.m_vbh = bgfx::createVertexBuffer(mem, m_layout); + group.m_vbh = bgfx::createVertexBuffer(mem, m_layout, BGFX_BUFFER_COMPUTE_READ | BGFX_BUFFER_COMPUTE_FORMAT_32X1 | BGFX_BUFFER_COMPUTE_TYPE_FLOAT); } break; @@ -523,7 +523,7 @@ void Mesh::load(bx::ReaderSeekerI* _reader, bool _ramcopy) bx::memCopy(group.m_indices, mem->data, mem->size); } - group.m_ibh = bgfx::createIndexBuffer(mem); + group.m_ibh = bgfx::createIndexBuffer(mem, BGFX_BUFFER_COMPUTE_READ | BGFX_BUFFER_COMPUTE_FORMAT_32X1 | BGFX_BUFFER_COMPUTE_TYPE_UINT); } break; @@ -799,3 +799,4 @@ bgfx::RendererType::Enum getType(const bx::StringView& _name) return bgfx::RendererType::Count; } +