Skip to content
Open
6 changes: 5 additions & 1 deletion src/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ RenderableMaterial::RenderableMaterial(Qt3DCore::QNode *parent, VertexShaderType
shader3->setGeometryShaderCode(
Qt3DRender::QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/shaders/line_tesselator.geom"))));
break;
case (GeometryShaderType::LineStipple):
shader3->setGeometryShaderCode(
Qt3DRender::QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/shaders/line_stipple.geom"))));
break;
default:
throw(std::runtime_error("Unhandled geometry shader type.\n"));
}
Expand Down Expand Up @@ -122,4 +126,4 @@ void RenderableMaterial::setSpecular(QColor specular)
{
specular_ = specular;
specularParameter_->setValue(specular_);
}
}
5 changes: 3 additions & 2 deletions src/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class RenderableMaterial : public Qt3DRender::QMaterial
enum class GeometryShaderType
{
None,
LineTesselator
LineTesselator,
LineStipple
};
// Fragment Shader Types
enum class FragmentShaderType
Expand Down Expand Up @@ -56,4 +57,4 @@ class RenderableMaterial : public Qt3DRender::QMaterial
// Set specular colour component
void setSpecular(QColor specular);
};
} // namespace Mildred
} // namespace Mildred
1 change: 1 addition & 0 deletions src/shaders.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<file>shaders/phongpervertex.frag</file>
<file>shaders/monochrome.frag</file>
<file>shaders/line_tesselator.geom</file>
<file>shaders/line_stipple.geom</file>
</qresource>
</RCC>
79 changes: 79 additions & 0 deletions src/shaders/line_stipple.geom
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#version 330

layout(lines) in;
layout(triangle_strip, max_vertices = 4) out;

// Input Vertex Data
in worldData
{
vec3 position;
vec3 normal;
vec4 color;
}
vertices[];

// Output Fragment Data
out fragData
{
vec3 position;
vec3 normal;
vec4 color;
}
frag;

uniform vec2 viewportSize;
uniform float lineWidth = 1.5;

// Copy clip distances for specified input vertex
void applyClipping(int inVertexID)
{
gl_ClipDistance[0] = gl_in[inVertexID].gl_ClipDistance[0];
gl_ClipDistance[1] = gl_in[inVertexID].gl_ClipDistance[1];
gl_ClipDistance[2] = gl_in[inVertexID].gl_ClipDistance[2];
gl_ClipDistance[3] = gl_in[inVertexID].gl_ClipDistance[3];
gl_ClipDistance[4] = gl_in[inVertexID].gl_ClipDistance[4];
gl_ClipDistance[5] = gl_in[inVertexID].gl_ClipDistance[5];
}

void main()
{
vec4 p1 = gl_in[0].gl_Position;
vec4 p2 = gl_in[1].gl_Position;

vec2 dir = normalize((p2.xy - p1.xy) * viewportSize);
vec2 offset = vec2(-dir.y, dir.x) * lineWidth / viewportSize;

// Emit the four corners of our two triangles
gl_Position = p1 + vec4(offset.xy * p1.w, 0.0, 0.0);
applyClipping(0);
frag.position = vec3(gl_Position);
frag.color = vertices[0].color;
frag.normal = vertices[1].normal;
EmitVertex();
gl_Position = p1 - vec4(offset.xy * p1.w, 0.0, 0.0);
applyClipping(0);
frag.position = vec3(gl_Position);
frag.color = vertices[0].color;
frag.normal = vertices[0].normal;
EmitVertex();
gl_Position = p2 + vec4(offset.xy * p2.w, 0.0, 0.0);
applyClipping(1);
frag.position = vec3(gl_Position);
frag.color = vertices[1].color;
frag.normal = vertices[1].normal;
EmitVertex();
gl_Position = p2 - vec4(offset.xy * p2.w, 0.0, 0.0);
applyClipping(1);
frag.position = vec3(gl_Position);
frag.color = vertices[1].color;
frag.normal = vertices[1].normal;

// To view line-stipple output, remove comment bars (//) from the following two lines
//gl_Position = p1 + vec4(offset.xy * p1.w, 0.0, 0.0);
//applyClipping(0);

EmitVertex();

EndPrimitive();

}
4 changes: 2 additions & 2 deletions src/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Data1DEntity *MildredWidget::addData1D(std::string_view tag)

// Add a material
auto *material = createMaterial(entity, RenderableMaterial::VertexShaderType::ClippedToDataVolume,
RenderableMaterial::GeometryShaderType::LineTesselator,
RenderableMaterial::GeometryShaderType::LineStipple,
RenderableMaterial::FragmentShaderType::PerVertexPhong);
entity->setDataMaterial(material);
entity->setErrorMaterial(material);
Expand All @@ -214,4 +214,4 @@ DisplayGroup *MildredWidget::addDisplayGroup()
auto newGroup = displayGroups_.emplace_back(std::make_shared<DisplayGroup>());

return newGroup.get();
}
}