Skip to content

Commit

Permalink
Adds point size support
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumeblanc committed Nov 10, 2023
1 parent f53d846 commit d48902e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
33 changes: 25 additions & 8 deletions include/flip/imdraw.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -74,13 +76,28 @@ 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};
vertex_.color = {_r, _g, _b, _a};
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};
Expand All @@ -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();
}

Expand Down
12 changes: 12 additions & 0 deletions samples/imdraw/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
9 changes: 6 additions & 3 deletions src/impl/imdrawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand All @@ -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<const ImVertex> _vertices, sg_image _image,
Expand Down

0 comments on commit d48902e

Please sign in to comment.