Skip to content

Commit

Permalink
Merge pull request #893 from pbdot/ec-variable-line-width
Browse files Browse the repository at this point in the history
Adding variable width line rendering
  • Loading branch information
pbdot authored Feb 13, 2025
2 parents 55cd77a + 770e1ef commit b20e7f2
Show file tree
Hide file tree
Showing 7 changed files with 641 additions and 577 deletions.
2 changes: 2 additions & 0 deletions source_files/edge/r_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ class RenderState

virtual void LineWidth(float width) = 0;

virtual float GetLineWidth() = 0;

virtual void DeleteTexture(const GLuint *tex_id) = 0;

virtual void FrontFace(GLenum wind) = 0;
Expand Down
5 changes: 5 additions & 0 deletions source_files/edge/render/gl/gl_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ class GLRenderState : public RenderState
glLineWidth(line_width_);
}

float GetLineWidth()
{
return line_width_;
}

void DeleteTexture(const GLuint *tex_id)
{
if (tex_id && *tex_id > 0)
Expand Down
1 change: 0 additions & 1 deletion source_files/edge/render/sokol/shaders/world.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ void main()

layout(binding=1) uniform state {
int flags;
int layer;
float alpha_test;
int clipplanes;
int fog_mode;
Expand Down
1,113 changes: 549 additions & 564 deletions source_files/edge/render/sokol/shaders/world.h

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions source_files/edge/render/sokol/sokol_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2797,11 +2797,9 @@ typedef struct {

typedef struct {
int flags;
int layer;
float alpha_test;
int clipplanes;
int fog_mode;
uint8_t _pad_20[12];
float fog_color[4];
float fog_density;
float fog_start;
Expand Down Expand Up @@ -4577,8 +4575,7 @@ SOKOL_API_IMPL void sgl_end(void) {
bool fragment_uniforms_dirty = ctx->fragment_uniforms_dirty;
if (fragment_uniforms_dirty) {
ctx->fragment_uniforms_dirty = false;
_sgl_fragment_uniform_t* current = _sgl_next_fragment_uniform(ctx);
current->layer = ctx->layer_id;
_sgl_fragment_uniform_t* current = _sgl_next_fragment_uniform(ctx);
_sgl_fragment_uniform_t* next = &ctx->fragment_uniforms.ptr[ctx->fragment_uniforms.next];
*next = *current;
}
Expand Down
11 changes: 10 additions & 1 deletion source_files/edge/render/sokol/sokol_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class SokolRenderState : public RenderState

void Reset()
{
line_width_ = 1.0f;

for (int32_t i = 0; i < kMaxClipPlane; i++)
{
clip_planes_[i].enabled_ = false;
Expand Down Expand Up @@ -288,7 +290,12 @@ class SokolRenderState : public RenderState

void LineWidth(float width)
{
EPI_UNUSED(width);
line_width_ = width;
}

float GetLineWidth()
{
return line_width_;
}

void DeleteTexture(const GLuint *tex_id)
Expand Down Expand Up @@ -770,6 +777,8 @@ class SokolRenderState : public RenderState
GLint texture_wrap_s_;
GLint texture_wrap_t_;

float line_width_;

struct EClipPlane
{
bool enabled_;
Expand Down
81 changes: 74 additions & 7 deletions source_files/edge/render/sokol/sokol_units.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ static void RenderFlush()
for (int32_t i = 0; i < current_render_unit; i++)
{
ec_frame_stats.draw_render_units++;

RendererUnit *unit = &local_units[i];

// assume unit will require a command
Expand Down Expand Up @@ -538,16 +538,83 @@ void RenderCurrentUnits(void)
else if (unit->shape == GL_LINES)
{
sgl_disable_texture();
sgl_begin_lines();
const RendererVertex *V = local_verts + unit->first;

for (int v_idx = 0, v_last_idx = unit->count; v_idx < v_last_idx; v_idx++, V++)
float state_width = render_state->GetLineWidth();

// Use native lines if possible, not this does not do AA smoothing, and if enabled will need to use alternate rending below
if (AlmostEquals(state_width, 1.0f))
{
sgl_v3f_c4b(V->position.X, V->position.Y, V->position.Z, epi::GetRGBARed(V->rgba),
epi::GetRGBAGreen(V->rgba), epi::GetRGBABlue(V->rgba), epi::GetRGBAAlpha(V->rgba));
sgl_begin_lines();
const RendererVertex *V = local_verts + unit->first;

for (int v_idx = 0, v_last_idx = unit->count; v_idx < v_last_idx; v_idx++, V++)
{
sgl_v3f_c4b(V->position.X, V->position.Y, V->position.Z, epi::GetRGBARed(V->rgba),
epi::GetRGBAGreen(V->rgba), epi::GetRGBABlue(V->rgba), epi::GetRGBAAlpha(V->rgba));
}

sgl_end();
}
else
{

sgl_end();
// This does not currently do AA smoothing
// https://github.com/pbdot/Lines
// see cpu_lines.h for AA shader, once multishader support is in
// so can have a shader specifically for lines

sgl_begin_triangles();

const RendererVertex *V = local_verts + unit->first;

float line_width = HMM_MAX(1.0f, state_width);

for (int v_idx = 0; v_idx < unit->count; v_idx += 2)
{
const RendererVertex *src_v0 = V + v_idx;
const RendererVertex *src_v1 = src_v0 + 1;

// use first vertice color
uint8_t red = epi::GetRGBARed(src_v0->rgba);
uint8_t green = epi::GetRGBAGreen(src_v0->rgba);
uint8_t blue = epi::GetRGBABlue(src_v0->rgba);
uint8_t alpha = epi::GetRGBAAlpha(src_v0->rgba);

HMM_Vec2 v0 = {src_v0->position[0], src_v0->position[1]};
HMM_Vec2 v1 = {src_v1->position[0], src_v1->position[1]};

HMM_Vec2 line_vector = HMM_SubV2(v1, v0);
float line_length = HMM_LenV2(line_vector);
HMM_Vec2 dir = HMM_NormV2(line_vector);
HMM_Vec2 normal = {-dir.Y, dir.X};

HMM_Vec2 a1 = {v0.X - normal.X, v0.Y - normal.Y};
HMM_Vec2 a0 = {v0.X + normal.X, v0.Y + normal.Y};

HMM_Vec2 b1 = {v1.X - normal.X, v1.Y - normal.Y};
HMM_Vec2 b0 = {v1.X + normal.X, v1.Y + normal.Y};

sgl_v3f_t4f_c4b(a0.X, a0.Y, src_v0->position.Z, -line_width, -0.5f * line_length, line_width,
0.5f * line_length, red, green, blue, alpha);

sgl_v3f_t4f_c4b(a1.X, a1.Y, src_v0->position.Z, line_width, -0.5f * line_length, line_width,
0.5f * line_length, red, green, blue, alpha);

sgl_v3f_t4f_c4b(b0.X, b0.Y, src_v1->position.Z, -line_width, 0.5f * line_length, line_width,
0.5f * line_length, red, green, blue, alpha);

sgl_v3f_t4f_c4b(a1.X, a1.Y, src_v0->position.Z, line_width, -0.5f * line_length, line_width,
0.5f * line_length, red, green, blue, alpha);

sgl_v3f_t4f_c4b(b0.X, b0.Y, src_v1->position.Z, -line_width, 0.5f * line_length, line_width,
0.5f * line_length, red, green, blue, alpha);

sgl_v3f_t4f_c4b(b1.X, b1.Y, src_v1->position.Z, line_width, 0.5f * line_length, line_width,
0.5f * line_length, red, green, blue, alpha);
}

sgl_end();
}

continue;
}
Expand Down

0 comments on commit b20e7f2

Please sign in to comment.