diff --git a/include/flip/imdraw.h b/include/flip/imdraw.h index 0c7e8b0..61204fe 100644 --- a/include/flip/imdraw.h +++ b/include/flip/imdraw.h @@ -28,9 +28,10 @@ struct ImMode { }; struct ImVertex { - HMM_Vec3 position = {0, 0, 0}; - Color color = {1, 1, 1, 1}; - HMM_Vec2 uv = {0, 0}; + HMM_Vec3 position = {0, 0, 0}; // Vertex position + Color color = {1, 1, 1, 1}; // Vertex color + HMM_Vec2 uv = {0, 0}; // Vertex texture coordinate + float size = 1.f; // Point size primitive }; // RAII to begin/end an immediate mode draw @@ -57,6 +58,7 @@ class ImDraw { void color(float _r, float _g, float _b, float _a) { vertex_.color = {_r, _g, _b, _a}; } + void size(float _size) { vertex_.size = _size; } // Modifies current (internal) vertex and submit point. void vertex(const HMM_Vec3& _xyz) { @@ -74,6 +76,13 @@ class ImDraw { Submit(); } + void vertex(const HMM_Vec3& _xyz, const Color& _color, float _size) { + vertex_.position = _xyz; + vertex_.color = _color; + vertex_.size = _size; + Submit(); + } + void vertex(float _x, float _y, float _z, float _r, float _g, float _b, float _a) { vertex_.position = {_x, _y, _z}; @@ -81,6 +90,14 @@ class ImDraw { Submit(); } + void vertex(float _x, float _y, float _z, float _r, float _g, float _b, + float _a, float _size) { + vertex_.position = {_x, _y, _z}; + vertex_.color = {_r, _g, _b, _a}; + vertex_.size = _size; + Submit(); + } + void vertex(float _x, float _y, float _z, float _u, float _v) { vertex_.position = {_x, _y, _z}; vertex_.uv = {_u, _v}; @@ -93,18 +110,18 @@ class ImDraw { Submit(); } - void vertex(const HMM_Vec3& _xyz, const HMM_Vec2& _uv, const Color& _color) { + void vertex(const HMM_Vec3& _xyz, const Color& _color, const HMM_Vec2& _uv) { vertex_.position = _xyz; - vertex_.uv = _uv; vertex_.color = _color; + vertex_.uv = _uv; Submit(); } - void vertex(float _x, float _y, float _z, float _u, float _v, float _r, - float _g, float _b, float _a) { + void vertex(float _x, float _y, float _z, float _r, float _g, float _b, + float _a, float _u, float _v) { vertex_.position = {_x, _y, _z}; - vertex_.uv = {_u, _v}; vertex_.color = {_r, _g, _b, _a}; + vertex_.uv = {_u, _v}; Submit(); } diff --git a/samples/imdraw/main.cpp b/samples/imdraw/main.cpp index beb9f5a..d2fa45e 100644 --- a/samples/imdraw/main.cpp +++ b/samples/imdraw/main.cpp @@ -53,6 +53,18 @@ class ImDraw : public flip::Application { drawer.vertex(1, -1, 0); } + // Red axis points + { + auto drawer = flip::ImDraw{ + _renderer, transform1_, {.type = SG_PRIMITIVETYPE_POINTS}}; + + drawer.color(flip::kRed); + drawer.size(10.f); + + drawer.vertex(0, 1, 0); + drawer.vertex(0, -1, 0); + } + return true; } diff --git a/src/impl/imdrawer.cpp b/src/impl/imdrawer.cpp index bae16d9..4d33b7f 100644 --- a/src/impl/imdrawer.cpp +++ b/src/impl/imdrawer.cpp @@ -17,10 +17,12 @@ ImDrawer::ImDrawer() { "layout(location=0) in vec3 position;\n" "layout(location=1) in vec4 color;\n" "layout(location=2) in vec2 uv;\n" + "layout(location=3) in float size;\n" "out vec2 vertex_uv;\n" "out vec4 vertex_color;\n" "void main() {\n" " gl_Position = mvp * vec4(position, 1.);\n" + " gl_PointSize = size;\n" " vertex_uv = uv;\n" " vertex_color = color;\n" "}\n"; @@ -75,10 +77,11 @@ void ImDrawer::Begin(const HMM_Mat4& _view_proj, const HMM_Mat4& _transform, if (it == pipelines_.end()) { sg_pipeline pip = sg_make_pipeline(sg_pipeline_desc{ .shader = shaders_[_mode.alpha_test].id(), - .layout = {.buffers = {{.stride = 36}}, + .layout = {.buffers = {{.stride = sizeof(ImVertex)}}, .attrs = {{.format = SG_VERTEXFORMAT_FLOAT3}, {.format = SG_VERTEXFORMAT_FLOAT4}, - {.format = SG_VERTEXFORMAT_FLOAT2}}}, + {.format = SG_VERTEXFORMAT_FLOAT2}, + {.format = SG_VERTEXFORMAT_FLOAT}}}, .depth = {.compare = _mode.z_compare, .write_enabled = _mode.z_write}, .colors = {{.blend = {.enabled = _mode.alpha_blending, .src_factor_rgb = SG_BLENDFACTOR_SRC_ALPHA, @@ -96,7 +99,7 @@ void ImDrawer::Begin(const HMM_Mat4& _view_proj, const HMM_Mat4& _transform, sg_apply_pipeline(it->second.id()); const auto mvp = _view_proj * _transform; - sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, {mvp.Elements[0], 64}); + sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, {mvp.Elements[0], sizeof(mvp)}); } void ImDrawer::End(std::span _vertices, sg_image _image,