From 3f0134c96728aae97b8ecb08d6c86dfe36fcf3b7 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Mon, 20 Sep 2021 16:54:41 -0700 Subject: [PATCH 01/11] Initial commit of drawing lines as triangles --- lib/gl/renderer_core.cpp | 235 ++++++++++++++++++++++++++++++++++-- lib/gl/renderer_core.hpp | 8 ++ lib/gl/shaders/default.vert | 50 ++++++++ lib/gl/types.hpp | 2 + 4 files changed, 283 insertions(+), 12 deletions(-) diff --git a/lib/gl/renderer_core.cpp b/lib/gl/renderer_core.cpp index ad3dee63..967d01b8 100644 --- a/lib/gl/renderer_core.cpp +++ b/lib/gl/renderer_core.cpp @@ -45,6 +45,9 @@ const std::vector CoreGLDevice::unif_list = "useClipPlane", "clipPlane", "containsText", + "expandLines", + "lineWidth", + "aspectRatio", "modelViewMatrix", "projectionMatrix", "textProjMatrix", @@ -66,6 +69,84 @@ const std::vector CoreGLDevice::unif_list = "alphaTex" }; +struct alignas(16) CoreGLDevice::LineVertex +{ + std::array vtx; + int orientation; + std::array prev; + std::array next; + + static constexpr int layout = LAYOUT_EXT_LINE_VTX; + + static void Setup() + { + constexpr LineVertex base{}; + const size_t vtx_offset = (size_t)(&base.vtx) - (size_t)(&base); + const size_t orient_offset = (size_t)(&base.orientation) - (size_t)(&base); + const size_t prev_offset = (size_t)(&base.prev) - (size_t)(&base); + const size_t next_offset = (size_t)(&base.next) - (size_t)(&base); + + glEnableVertexAttribArray(ATTR_VERTEX); + glEnableVertexAttribArray(ATTR_LINE_ORIENT); + glEnableVertexAttribArray(ATTR_LINE_PREV); + glEnableVertexAttribArray(ATTR_LINE_NEXT); + + glVertexAttribPointer(ATTR_VERTEX, 3, GL_FLOAT, false, sizeof(LineVertex), (void*)vtx_offset); + glVertexAttribPointer(ATTR_LINE_ORIENT, 1, GL_INT, false, sizeof(LineVertex), (void*)orient_offset); + glVertexAttribPointer(ATTR_LINE_PREV, 3, GL_FLOAT, false, sizeof(LineVertex), (void*)prev_offset); + glVertexAttribPointer(ATTR_LINE_NEXT, 3, GL_FLOAT, false, sizeof(LineVertex), (void*)next_offset); + } + + static void Finish() + { + glDisableVertexAttribArray(ATTR_VERTEX); + glDisableVertexAttribArray(ATTR_LINE_ORIENT); + glDisableVertexAttribArray(ATTR_LINE_PREV); + glDisableVertexAttribArray(ATTR_LINE_NEXT); + } +}; + +struct alignas(16) CoreGLDevice::LineColorVertex +{ + std::array vtx; + std::array color; + int orientation; + std::array prev; + std::array next; + + static constexpr int layout = LAYOUT_EXT_LINE_VTX_COLOR; + + static void Setup() + { + constexpr LineColorVertex base{}; + const size_t vtx_offset = (size_t)(&base.vtx) - (size_t)(&base); + const size_t color_offset = (size_t)(&base.color) - (size_t)(&base); + const size_t orient_offset = (size_t)(&base.orientation) - (size_t)(&base); + const size_t prev_offset = (size_t)(&base.prev) - (size_t)(&base); + const size_t next_offset = (size_t)(&base.next) - (size_t)(&base); + glEnableVertexAttribArray(ATTR_VERTEX); + glEnableVertexAttribArray(ATTR_COLOR); + glEnableVertexAttribArray(ATTR_LINE_ORIENT); + glEnableVertexAttribArray(ATTR_LINE_PREV); + glEnableVertexAttribArray(ATTR_LINE_NEXT); + + glVertexAttribPointer(ATTR_VERTEX, 3, GL_FLOAT, false, sizeof(LineColorVertex), (void*)vtx_offset); + glVertexAttribPointer(ATTR_COLOR, 4, GL_UNSIGNED_INT, true, sizeof(LineColorVertex), (void*)color_offset); + glVertexAttribPointer(ATTR_LINE_ORIENT, 1, GL_INT, false, sizeof(LineColorVertex), (void*)orient_offset); + glVertexAttribPointer(ATTR_LINE_PREV, 3, GL_FLOAT, false, sizeof(LineColorVertex), (void*)prev_offset); + glVertexAttribPointer(ATTR_LINE_NEXT, 3, GL_FLOAT, false, sizeof(LineColorVertex), (void*)next_offset); + } + + static void Finish() + { + glDisableVertexAttribArray(ATTR_VERTEX); + glDisableVertexAttribArray(ATTR_COLOR); + glDisableVertexAttribArray(ATTR_LINE_ORIENT); + glDisableVertexAttribArray(ATTR_LINE_PREV); + glDisableVertexAttribArray(ATTR_LINE_NEXT); + } +}; + template void setupVtxAttrLayout() { @@ -96,7 +177,10 @@ bool CoreGLDevice::compileShaders() { CoreGLDevice::ATTR_TEXT_VERTEX, "textVertex"}, { CoreGLDevice::ATTR_NORMAL, "normal"}, { CoreGLDevice::ATTR_COLOR, "color"}, - { CoreGLDevice::ATTR_TEXCOORD0, "texCoord0"} + { CoreGLDevice::ATTR_TEXCOORD0, "texCoord0"}, + { CoreGLDevice::ATTR_LINE_ORIENT, "line_orientation"}, + { CoreGLDevice::ATTR_LINE_PREV, "line_prev_vtx"}, + { CoreGLDevice::ATTR_LINE_NEXT, "line_next_vtx"} }; if (!default_prgm.create(DEFAULT_VS, DEFAULT_FS, attribMap, 1)) @@ -246,22 +330,116 @@ void CoreGLDevice::setClipPlaneEqn(const std::array &eqn) void CoreGLDevice::bufferToDevice(array_layout layout, IVertexBuffer &buf) { - if (buf.getHandle() == 0) + if (buf.getShape() == GL_LINES && + (layout == Vertex::layout || layout == VertexColor::layout)) { + if (buf.getHandle() == 0) + { + if (buf.count() == 0) { return; } + GLuint handle[2]; + glGenBuffers(2, &handle[0]); + buf.setHandle(vbos.size()); + vbos.emplace_back(VBOData{handle[0], handle[1], GL_TRIANGLES, 0, layout}); + } + glBindBuffer(GL_ARRAY_BUFFER, vbos[buf.getHandle()].vert_buf); + glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_STATIC_DRAW); if (buf.count() == 0) { return; } - GLuint handle; - glGenBuffers(1, &handle); - buf.setHandle(vbos.size()); - vbos.emplace_back(VBOData{handle, 0, buf.getShape(), buf.count(), layout}); + // Create extended vertex data for generating shader-expanded lines + if (layout == Vertex::layout) + { + std::vector ext_data(buf.count() * 2); + auto pts = static_cast&>(buf).begin(); + for (size_t i = 0; i < buf.count(); i++) + { + const Vertex& pt = *(pts + i); + ext_data[2*i].vtx = pt.coord; + ext_data[2*i].orientation = 1; + ext_data[2*i+1].vtx = pt.coord; + ext_data[2*i+1].orientation = -1; + if (i >= 1) + { + ext_data[2*(i-1)].next = pt.coord; + ext_data[2*(i-1) + 1].next = pt.coord; + } + if (i + 1 < buf.count()) + { + ext_data[2*(i+1)].prev = pt.coord; + ext_data[2*(i+1)+1].prev = pt.coord; + } + } + ext_data[0].prev = ext_data[0].vtx; + ext_data[1].prev = ext_data[1].vtx; + int last_vtx = 2*(buf.count() - 1); + ext_data[last_vtx].next = ext_data[last_vtx].vtx; + ext_data[last_vtx+1].next = ext_data[last_vtx+1].vtx; + glBufferData(GL_ARRAY_BUFFER, ext_data.size() * sizeof(LineVertex), + ext_data.data(), GL_STATIC_DRAW); + } + else if (layout == VertexColor::layout) + { + std::vector ext_data(buf.count() * 2); + auto pts = static_cast&>(buf).begin(); + for (size_t i = 0; i < buf.count(); i++) + { + const VertexColor& pt = *(pts + i); + ext_data[2*i].vtx = pt.coord; + ext_data[2*i].color = pt.color; + ext_data[2*i].orientation = 1; + ext_data[2*i+1].vtx = pt.coord; + ext_data[2*i+1].color = pt.color; + ext_data[2*i+1].orientation = -1; + if (i >= 1) + { + ext_data[2*(i-1)].next = pt.coord; + ext_data[2*(i-1) + 1].next = pt.coord; + } + if (i + 1 < buf.count()) + { + ext_data[2*(i+1)].prev = pt.coord; + ext_data[2*(i+1)+1].prev = pt.coord; + } + } + ext_data[0].prev = ext_data[0].vtx; + ext_data[1].prev = ext_data[1].vtx; + int last_vtx = 2*(buf.count() - 1); + ext_data[last_vtx].next = ext_data[last_vtx].vtx; + ext_data[last_vtx+1].next = ext_data[last_vtx+1].vtx; + glBufferData(GL_ARRAY_BUFFER, ext_data.size() * sizeof(LineColorVertex), + ext_data.data(), GL_STATIC_DRAW); + } + // Create indices to generate triangles for our lines + std::vector index_data(buf.count() * 6); + for (size_t it = 0; it < buf.count() * 2; it++) + { + index_data[3*it] = it; + index_data[3*it+1] = it+1; + index_data[3*it+2] = it+2; + } // iterate over triangles + + // Upload index data to GPU + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[buf.getHandle()].elem_buf); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_data.size() * sizeof(int), + index_data.data(), GL_STATIC_DRAW); + + vbos[buf.getHandle()].count = index_data.size(); } else { - vbos[buf.getHandle()].count = buf.count(); + if (buf.getHandle() == 0) + { + if (buf.count() == 0) { return; } + GLuint handle; + glGenBuffers(1, &handle); + buf.setHandle(vbos.size()); + vbos.emplace_back(VBOData{handle, 0, buf.getShape(), buf.count(), layout}); + vbos[buf.getHandle()].count = buf.count(); + } + glBindBuffer(GL_ARRAY_BUFFER, vbos[buf.getHandle()].vert_buf); + glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, buf.count() * buf.getStride(), + buf.getData(), GL_STATIC_DRAW); } - glBindBuffer(GL_ARRAY_BUFFER, vbos[buf.getHandle()].vert_buf); - glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_STATIC_DRAW); - glBufferData(GL_ARRAY_BUFFER, buf.count() * buf.getStride(), - buf.getData(), GL_STATIC_DRAW); } void CoreGLDevice::bufferToDevice(array_layout layout, IIndexedBuffer& buf) @@ -360,6 +538,34 @@ void CoreGLDevice::drawDeviceBufferImpl(GLenum shape, int count, bool indexed) clearVtxAttrLayout(); } +void CoreGLDevice::drawExtendedLineImpl(array_layout type, int count) +{ + // Set up uniforms + glUniform1i(uniforms["expandLines"], true); + glUniform1f(uniforms["lineWidth"], 1.0); + glUniform1f(uniforms["aspectRatio"], (float)vp_width / vp_height); + // Set up attributes + glVertexAttrib3f(CoreGLDevice::ATTR_NORMAL, 0.f, 0.f, 1.f); + if (type == LineVertex::layout) + { + LineVertex::Setup(); + } + else if (type == LineColorVertex::layout) + { + LineColorVertex::Setup(); + } + glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, (void*)0); + if (type == LineVertex::layout) + { + LineVertex::Finish(); + } + else if (type == LineColorVertex::layout) + { + LineColorVertex::Finish(); + } + glUniform1i(uniforms["expandLines"], false); +} + void CoreGLDevice::drawDeviceBuffer(int hnd) { if (hnd == 0) { return; } @@ -372,7 +578,8 @@ void CoreGLDevice::drawDeviceBuffer(int hnd) indexed = true; } if (vbos[hnd].layout == Vertex::layout - || vbos[hnd].layout == VertexNorm::layout) + || vbos[hnd].layout == VertexNorm::layout + || vbos[hnd].layout == LineVertex::layout) { glVertexAttrib4fv(ATTR_COLOR, static_color.data()); } @@ -398,6 +605,10 @@ void CoreGLDevice::drawDeviceBuffer(int hnd) case VertexNormTex::layout: drawDeviceBufferImpl(shape, count, indexed); break; + case LineVertex::layout: + case LineColorVertex::layout: + drawExtendedLineImpl(vbos[hnd].layout, count); + break; default: cerr << "WARNING: Unhandled vertex layout " << vbos[hnd].layout << endl; } diff --git a/lib/gl/renderer_core.hpp b/lib/gl/renderer_core.hpp index 75a0b3f9..a03651b1 100644 --- a/lib/gl/renderer_core.hpp +++ b/lib/gl/renderer_core.hpp @@ -27,6 +27,9 @@ class CoreGLDevice : public GLDevice ATTR_NORMAL, ATTR_COLOR, ATTR_TEXCOORD0, + ATTR_LINE_ORIENT, + ATTR_LINE_PREV, + ATTR_LINE_NEXT, NUM_ATTRS }; @@ -59,6 +62,9 @@ class CoreGLDevice : public GLDevice array_layout layout; }; + struct LineVertex; + struct LineColorVertex; + std::vector vbos; bool compileShaders(); @@ -67,6 +73,8 @@ class CoreGLDevice : public GLDevice template void drawDeviceBufferImpl(GLenum shape, int count, bool indexed); + void drawExtendedLineImpl(array_layout type, int count); + void processTriangleXfbBuffer(CaptureBuffer& cbuf, const vector& verts); void processLineXfbBuffer(CaptureBuffer& cbuf, diff --git a/lib/gl/shaders/default.vert b/lib/gl/shaders/default.vert index c33127fb..59b3a943 100644 --- a/lib/gl/shaders/default.vert +++ b/lib/gl/shaders/default.vert @@ -16,8 +16,16 @@ attribute vec4 color; attribute vec3 normal; attribute vec2 texCoord0; +attribute float line_orientation; +attribute vec3 line_prev_vtx; +attribute vec3 line_next_vtx; + uniform bool containsText; +uniform bool expandLines; +uniform float lineWidth; +uniform float aspectRatio; + uniform mat4 modelViewMatrix; uniform mat4 projectionMatrix; uniform mat4 textProjMatrix; @@ -52,4 +60,46 @@ void main() vec4 textOffset = textProjMatrix * vec4(textVertex, 0.0, 0.0); gl_Position += vec4((textOffset.xy * pos.w), -0.005, 0.0); } + if (expandLines) + { + // Compute screen-space coordinates for current and previous segments + mat4 mvp = projectionMatrix * modelViewMatrix; + vec4 prev_clip = mvp * vec4(line_prev_vtx, 1.0); + vec4 next_clip = mvp * vec4(line_next_vtx, 1.0); + + vec2 curr_scrn = pos.xy / pos.w; + vec2 prev_scrn = prev_clip.xy / prev_clip.w; + vec2 next_scrn = next_clip.xy / next_clip.w; + + // Correct for current aspect ratio + curr_scrn.x *= aspectRatio; + prev_scrn.x *= aspectRatio; + next_scrn.x *= aspectRatio; + + float width = lineWidth; + + // Get direction and normal of line segment + vec2 dir; + if (vertex == line_prev_vtx) + { + dir = normalize(next_scrn - curr_scrn); + } + else if (vertex == line_next_vtx) + { + dir = normalize(curr_scrn - prev_scrn); + } + else + { + dir = normalize(curr_scrn - prev_scrn); + vec2 perp = vec2(-dir.y, dir.x); + + dir += normalize(next_scrn - curr_scrn); + dir = normalize(dir); + vec2 miter = vec2(-dir.y, dir.x); + width = lineWidth / dot(miter, perp); + } + vec2 line_normal = vec2(-dir.y, dir.x); + vec4 offset = vec4(line_normal * line_orientation * width / 2.0, 0.0, 0.0); + gl_Position += offset; + } })" diff --git a/lib/gl/types.hpp b/lib/gl/types.hpp index fcdb9598..ea18bb52 100644 --- a/lib/gl/types.hpp +++ b/lib/gl/types.hpp @@ -175,6 +175,8 @@ enum array_layout LAYOUT_VTX_TEXTURE0, LAYOUT_VTX_NORMAL_COLOR, LAYOUT_VTX_NORMAL_TEXTURE0, + LAYOUT_EXT_LINE_VTX, + LAYOUT_EXT_LINE_VTX_COLOR, NUM_LAYOUTS }; From 02003e148761cd26c426783f978830bfbc1663ef Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 23 Sep 2021 10:30:39 -0700 Subject: [PATCH 02/11] Some fixes --- lib/gl/renderer_core.cpp | 74 +++++++++++++++++++++---------------- lib/gl/shaders/default.vert | 1 + lib/gl/types.hpp | 3 +- 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/lib/gl/renderer_core.cpp b/lib/gl/renderer_core.cpp index 967d01b8..b88ff976 100644 --- a/lib/gl/renderer_core.cpp +++ b/lib/gl/renderer_core.cpp @@ -76,7 +76,7 @@ struct alignas(16) CoreGLDevice::LineVertex std::array prev; std::array next; - static constexpr int layout = LAYOUT_EXT_LINE_VTX; + static constexpr array_layout layout = LAYOUT_EXT_LINE_VTX; static void Setup() { @@ -114,7 +114,7 @@ struct alignas(16) CoreGLDevice::LineColorVertex std::array prev; std::array next; - static constexpr int layout = LAYOUT_EXT_LINE_VTX_COLOR; + static constexpr array_layout layout = LAYOUT_EXT_LINE_VTX_COLOR; static void Setup() { @@ -335,11 +335,14 @@ void CoreGLDevice::bufferToDevice(array_layout layout, IVertexBuffer &buf) { if (buf.getHandle() == 0) { + array_layout ext_layout; + if (layout == Vertex::layout) { ext_layout = LineVertex::layout; } + else if (layout == VertexColor::layout) { ext_layout = LineColorVertex::layout; } if (buf.count() == 0) { return; } GLuint handle[2]; glGenBuffers(2, &handle[0]); buf.setHandle(vbos.size()); - vbos.emplace_back(VBOData{handle[0], handle[1], GL_TRIANGLES, 0, layout}); + vbos.emplace_back(VBOData{handle[0], handle[1], GL_TRIANGLES, 0, ext_layout}); } glBindBuffer(GL_ARRAY_BUFFER, vbos[buf.getHandle()].vert_buf); glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_STATIC_DRAW); @@ -356,22 +359,23 @@ void CoreGLDevice::bufferToDevice(array_layout layout, IVertexBuffer &buf) ext_data[2*i].orientation = 1; ext_data[2*i+1].vtx = pt.coord; ext_data[2*i+1].orientation = -1; - if (i >= 1) + if (i % 2 == 0) { - ext_data[2*(i-1)].next = pt.coord; - ext_data[2*(i-1) + 1].next = pt.coord; + // first node in the segment + ext_data[2*(i+1)].prev = pt.coord; + ext_data[2*(i+1) + 1].prev = pt.coord; + ext_data[2*i].prev = pt.coord; + ext_data[2*i+1].prev = pt.coord; } - if (i + 1 < buf.count()) + else { - ext_data[2*(i+1)].prev = pt.coord; - ext_data[2*(i+1)+1].prev = pt.coord; + // last node in the segment + ext_data[2*(i-1)].next = pt.coord; + ext_data[2*(i-1) + 1].next = pt.coord; + ext_data[2*i].next = pt.coord; + ext_data[2*i+1].next = pt.coord; } } - ext_data[0].prev = ext_data[0].vtx; - ext_data[1].prev = ext_data[1].vtx; - int last_vtx = 2*(buf.count() - 1); - ext_data[last_vtx].next = ext_data[last_vtx].vtx; - ext_data[last_vtx+1].next = ext_data[last_vtx+1].vtx; glBufferData(GL_ARRAY_BUFFER, ext_data.size() * sizeof(LineVertex), ext_data.data(), GL_STATIC_DRAW); } @@ -388,32 +392,37 @@ void CoreGLDevice::bufferToDevice(array_layout layout, IVertexBuffer &buf) ext_data[2*i+1].vtx = pt.coord; ext_data[2*i+1].color = pt.color; ext_data[2*i+1].orientation = -1; - if (i >= 1) + if (i % 2 == 0) { - ext_data[2*(i-1)].next = pt.coord; - ext_data[2*(i-1) + 1].next = pt.coord; + // first node in the segment + ext_data[2*(i+1)].prev = pt.coord; + ext_data[2*(i+1) + 1].prev = pt.coord; + ext_data[2*i].prev = pt.coord; + ext_data[2*i+1].prev = pt.coord; } - if (i + 1 < buf.count()) + else { - ext_data[2*(i+1)].prev = pt.coord; - ext_data[2*(i+1)+1].prev = pt.coord; + // last node in the segment + ext_data[2*(i-1)].next = pt.coord; + ext_data[2*(i-1) + 1].next = pt.coord; + ext_data[2*i].next = pt.coord; + ext_data[2*i+1].next = pt.coord; } } - ext_data[0].prev = ext_data[0].vtx; - ext_data[1].prev = ext_data[1].vtx; - int last_vtx = 2*(buf.count() - 1); - ext_data[last_vtx].next = ext_data[last_vtx].vtx; - ext_data[last_vtx+1].next = ext_data[last_vtx+1].vtx; glBufferData(GL_ARRAY_BUFFER, ext_data.size() * sizeof(LineColorVertex), ext_data.data(), GL_STATIC_DRAW); } // Create indices to generate triangles for our lines - std::vector index_data(buf.count() * 6); - for (size_t it = 0; it < buf.count() * 2; it++) + std::vector index_data(buf.count() * 3); + for (size_t it = 0; it < buf.count() / 2; it++) { - index_data[3*it] = it; - index_data[3*it+1] = it+1; - index_data[3*it+2] = it+2; + index_data[6*it] = 4*it; + index_data[6*it+1] = 4*it+1; + index_data[6*it+2] = 4*it+2; + + index_data[6*it+3] = 4*it+1; + index_data[6*it+4] = 4*it+2; + index_data[6*it+5] = 4*it+3; } // iterate over triangles // Upload index data to GPU @@ -540,9 +549,11 @@ void CoreGLDevice::drawDeviceBufferImpl(GLenum shape, int count, bool indexed) void CoreGLDevice::drawExtendedLineImpl(array_layout type, int count) { + // Set polygon offset to pull lines towards camera + glPolygonOffset(0, 0); // Set up uniforms glUniform1i(uniforms["expandLines"], true); - glUniform1f(uniforms["lineWidth"], 1.0); + glUniform1f(uniforms["lineWidth"], 8.0 / vp_height); glUniform1f(uniforms["aspectRatio"], (float)vp_width / vp_height); // Set up attributes glVertexAttrib3f(CoreGLDevice::ATTR_NORMAL, 0.f, 0.f, 1.f); @@ -564,6 +575,7 @@ void CoreGLDevice::drawExtendedLineImpl(array_layout type, int count) LineColorVertex::Finish(); } glUniform1i(uniforms["expandLines"], false); + glPolygonOffset(0, 0); } void CoreGLDevice::drawDeviceBuffer(int hnd) diff --git a/lib/gl/shaders/default.vert b/lib/gl/shaders/default.vert index 59b3a943..c6a1ece4 100644 --- a/lib/gl/shaders/default.vert +++ b/lib/gl/shaders/default.vert @@ -99,6 +99,7 @@ void main() width = lineWidth / dot(miter, perp); } vec2 line_normal = vec2(-dir.y, dir.x); + line_normal.x /= aspectRatio; vec4 offset = vec4(line_normal * line_orientation * width / 2.0, 0.0, 0.0); gl_Position += offset; } diff --git a/lib/gl/types.hpp b/lib/gl/types.hpp index ea18bb52..600fc79b 100644 --- a/lib/gl/types.hpp +++ b/lib/gl/types.hpp @@ -57,8 +57,7 @@ class Handle { if (this != &other) { - hnd = other.hnd; - other.hnd = 0; + std::swap(hnd, other.hnd); } return *this; } From e761ac7ec1770de265bff0d36b1f3e260597e91e Mon Sep 17 00:00:00 2001 From: Max Yang Date: Sun, 26 Jun 2022 00:34:32 -0700 Subject: [PATCH 03/11] Fix line length calculation, glPolygonOffset() reset after line draw --- lib/gl/renderer.hpp | 4 +++- lib/gl/renderer_core.cpp | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/gl/renderer.hpp b/lib/gl/renderer.hpp index 3de73ff3..284532ea 100644 --- a/lib/gl/renderer.hpp +++ b/lib/gl/renderer.hpp @@ -104,6 +104,8 @@ class GLDevice std::array static_color; + float line_w; + protected: TextureHandle passthrough_texture; @@ -139,7 +141,7 @@ class GLDevice void disableBlend() { glDisable(GL_BLEND); } void enableDepthWrite() { glDepthMask(GL_TRUE); } void disableDepthWrite() { glDepthMask(GL_FALSE); } - void setLineWidth(float w) { glLineWidth(w); } + void setLineWidth(float w) { line_w = w; glLineWidth(w); } virtual void init(); virtual DeviceType getType() = 0; diff --git a/lib/gl/renderer_core.cpp b/lib/gl/renderer_core.cpp index b88ff976..9b313cf2 100644 --- a/lib/gl/renderer_core.cpp +++ b/lib/gl/renderer_core.cpp @@ -553,7 +553,7 @@ void CoreGLDevice::drawExtendedLineImpl(array_layout type, int count) glPolygonOffset(0, 0); // Set up uniforms glUniform1i(uniforms["expandLines"], true); - glUniform1f(uniforms["lineWidth"], 8.0 / vp_height); + glUniform1f(uniforms["lineWidth"], 2 * line_w / vp_width); glUniform1f(uniforms["aspectRatio"], (float)vp_width / vp_height); // Set up attributes glVertexAttrib3f(CoreGLDevice::ATTR_NORMAL, 0.f, 0.f, 1.f); @@ -575,7 +575,7 @@ void CoreGLDevice::drawExtendedLineImpl(array_layout type, int count) LineColorVertex::Finish(); } glUniform1i(uniforms["expandLines"], false); - glPolygonOffset(0, 0); + glPolygonOffset(1, 1); } void CoreGLDevice::drawDeviceBuffer(int hnd) From 6af7353a52f83d8ffed3df96ca8846f392932eac Mon Sep 17 00:00:00 2001 From: Max Yang Date: Sun, 26 Jun 2022 00:40:33 -0700 Subject: [PATCH 04/11] Add comments --- lib/gl/renderer_core.cpp | 1 + lib/gl/shaders/default.vert | 2 ++ 2 files changed, 3 insertions(+) diff --git a/lib/gl/renderer_core.cpp b/lib/gl/renderer_core.cpp index 9b313cf2..c923b54c 100644 --- a/lib/gl/renderer_core.cpp +++ b/lib/gl/renderer_core.cpp @@ -575,6 +575,7 @@ void CoreGLDevice::drawExtendedLineImpl(array_layout type, int count) LineColorVertex::Finish(); } glUniform1i(uniforms["expandLines"], false); + // Reset polygon offset to default value glPolygonOffset(1, 1); } diff --git a/lib/gl/shaders/default.vert b/lib/gl/shaders/default.vert index c6a1ece4..c56bd10b 100644 --- a/lib/gl/shaders/default.vert +++ b/lib/gl/shaders/default.vert @@ -60,6 +60,8 @@ void main() vec4 textOffset = textProjMatrix * vec4(textVertex, 0.0, 0.0); gl_Position += vec4((textOffset.xy * pos.w), -0.005, 0.0); } + // Below code adapted from: + // https://github.com/mattdesl/webgl-lines if (expandLines) { // Compute screen-space coordinates for current and previous segments From 2ab307d4a60805e631529ed8a1e214650bb018e4 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Sun, 26 Jun 2022 01:39:17 -0700 Subject: [PATCH 05/11] Update tests/data submodule --- tests/data | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/data b/tests/data index 1fc29549..77196e20 160000 --- a/tests/data +++ b/tests/data @@ -1 +1 @@ -Subproject commit 1fc29549fc8d76daea4d8354ec53ade80ce93d49 +Subproject commit 77196e20da34e1756b226162ade6eac1de3841e5 From 5bba175b275667297ce3f4f30fa037528b6d3025 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Sun, 26 Jun 2022 16:06:35 -0700 Subject: [PATCH 06/11] Fix attempt to draw invalid buffer when toggling shading mode --- lib/gl/renderer_core.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/gl/renderer_core.cpp b/lib/gl/renderer_core.cpp index c923b54c..ae56bc91 100644 --- a/lib/gl/renderer_core.cpp +++ b/lib/gl/renderer_core.cpp @@ -442,6 +442,9 @@ void CoreGLDevice::bufferToDevice(array_layout layout, IVertexBuffer &buf) glGenBuffers(1, &handle); buf.setHandle(vbos.size()); vbos.emplace_back(VBOData{handle, 0, buf.getShape(), buf.count(), layout}); + } + else + { vbos[buf.getHandle()].count = buf.count(); } glBindBuffer(GL_ARRAY_BUFFER, vbos[buf.getHandle()].vert_buf); From 253f36b81baa46b97fc9c13839f37b4c909b9b79 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Sun, 26 Jun 2022 17:23:54 -0700 Subject: [PATCH 07/11] Avoid an extra type conversion from a GL_INT buffer attribute to a float shader attribute --- lib/gl/renderer_core.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/gl/renderer_core.cpp b/lib/gl/renderer_core.cpp index ae56bc91..19ee0d3b 100644 --- a/lib/gl/renderer_core.cpp +++ b/lib/gl/renderer_core.cpp @@ -72,7 +72,7 @@ const std::vector CoreGLDevice::unif_list = struct alignas(16) CoreGLDevice::LineVertex { std::array vtx; - int orientation; + float orientation; std::array prev; std::array next; @@ -92,7 +92,7 @@ struct alignas(16) CoreGLDevice::LineVertex glEnableVertexAttribArray(ATTR_LINE_NEXT); glVertexAttribPointer(ATTR_VERTEX, 3, GL_FLOAT, false, sizeof(LineVertex), (void*)vtx_offset); - glVertexAttribPointer(ATTR_LINE_ORIENT, 1, GL_INT, false, sizeof(LineVertex), (void*)orient_offset); + glVertexAttribPointer(ATTR_LINE_ORIENT, 1, GL_FLOAT, false, sizeof(LineVertex), (void*)orient_offset); glVertexAttribPointer(ATTR_LINE_PREV, 3, GL_FLOAT, false, sizeof(LineVertex), (void*)prev_offset); glVertexAttribPointer(ATTR_LINE_NEXT, 3, GL_FLOAT, false, sizeof(LineVertex), (void*)next_offset); } @@ -110,7 +110,7 @@ struct alignas(16) CoreGLDevice::LineColorVertex { std::array vtx; std::array color; - int orientation; + float orientation; std::array prev; std::array next; @@ -132,7 +132,7 @@ struct alignas(16) CoreGLDevice::LineColorVertex glVertexAttribPointer(ATTR_VERTEX, 3, GL_FLOAT, false, sizeof(LineColorVertex), (void*)vtx_offset); glVertexAttribPointer(ATTR_COLOR, 4, GL_UNSIGNED_INT, true, sizeof(LineColorVertex), (void*)color_offset); - glVertexAttribPointer(ATTR_LINE_ORIENT, 1, GL_INT, false, sizeof(LineColorVertex), (void*)orient_offset); + glVertexAttribPointer(ATTR_LINE_ORIENT, 1, GL_FLOAT, false, sizeof(LineColorVertex), (void*)orient_offset); glVertexAttribPointer(ATTR_LINE_PREV, 3, GL_FLOAT, false, sizeof(LineColorVertex), (void*)prev_offset); glVertexAttribPointer(ATTR_LINE_NEXT, 3, GL_FLOAT, false, sizeof(LineColorVertex), (void*)next_offset); } From 35f38c9855dcadeddc4436fea8bf4c431167804d Mon Sep 17 00:00:00 2001 From: Max Yang Date: Sun, 26 Jun 2022 23:03:42 -0700 Subject: [PATCH 08/11] Account for perspective divide when computing line offset --- lib/gl/shaders/default.vert | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gl/shaders/default.vert b/lib/gl/shaders/default.vert index c56bd10b..6701610a 100644 --- a/lib/gl/shaders/default.vert +++ b/lib/gl/shaders/default.vert @@ -102,7 +102,7 @@ void main() } vec2 line_normal = vec2(-dir.y, dir.x); line_normal.x /= aspectRatio; - vec4 offset = vec4(line_normal * line_orientation * width / 2.0, 0.0, 0.0); + vec4 offset = vec4(line_normal * line_orientation * width * pos.w / 2.0, 0.0, 0.0); gl_Position += offset; } })" From 5e72bd09e9406946b1070dc5279db79c93c4de5c Mon Sep 17 00:00:00 2001 From: Max Yang Date: Sun, 26 Jun 2022 23:08:09 -0700 Subject: [PATCH 09/11] Revert "Update tests/data submodule" This reverts commit 2ab307d4a60805e631529ed8a1e214650bb018e4. --- tests/data | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/data b/tests/data index 77196e20..1fc29549 160000 --- a/tests/data +++ b/tests/data @@ -1 +1 @@ -Subproject commit 77196e20da34e1756b226162ade6eac1de3841e5 +Subproject commit 1fc29549fc8d76daea4d8354ec53ade80ce93d49 From 36ad37543af0d4db707fd8da55465198d7622cdb Mon Sep 17 00:00:00 2001 From: Max Yang Date: Sun, 26 Jun 2022 23:27:36 -0700 Subject: [PATCH 10/11] Update tests/data submodule --- tests/data | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/data b/tests/data index 1fc29549..764d2f51 160000 --- a/tests/data +++ b/tests/data @@ -1 +1 @@ -Subproject commit 1fc29549fc8d76daea4d8354ec53ade80ce93d49 +Subproject commit 764d2f51017b82f27ca8bc59249b53741b9903a0 From 2a56dd9f4fadca095c50b488a98f6c6c1a218c2f Mon Sep 17 00:00:00 2001 From: Max Yang Date: Sun, 26 Jun 2022 23:45:34 -0700 Subject: [PATCH 11/11] Update tests/data submodule --- tests/data | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/data b/tests/data index 764d2f51..cb71613e 160000 --- a/tests/data +++ b/tests/data @@ -1 +1 @@ -Subproject commit 764d2f51017b82f27ca8bc59249b53741b9903a0 +Subproject commit cb71613e2149cb6586567d9dc5dd1f7a63132bea