Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line Stipple Shader #68

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open

Line Stipple Shader #68

wants to merge 14 commits into from

Conversation

Pranavya-Vivek
Copy link
Collaborator

@Pranavya-Vivek Pranavya-Vivek commented Jul 24, 2023

This Line Stipple Shader takes three uniforms:

  • line_width: The width of the line in pixels.
  • line_stipple_factor: The number of pixels between each stipple.
  • line_stipple_pattern: A bit pattern that determines which pixels are stippled.

The shader uses the mod() function to determine if a pixel should be stippled. If the pixel's x-coordinate is divisible by the line_stipple_factor, then it is stippled.

The shader also uses the discard keyword to discard any pixels that are stippled.

The line_width, line_stipple_factor, and line_stipple_pattern uniforms have been set to enable the stippled line to be drawn on the output Example Binary.

Copy link
Member

@trisyoungs trisyoungs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I look forward to see if you've turned this into a working example - filtering vertices based on the position on screen could work, but I have my concerns with this approach (see comments below).

An alternative could be to modify the line_tesselator to output multiple triangle strips, rather than just one - the idea is the same, but working at the level of the input vertices.


void main() {
float step = 1.0 / line_stipple_factor;
float pattern = mod(position.x * step, line_stipple_pattern);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relying on the x-coordinate alone will surely break things for completely vertical lines, no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I see! I can add a second mod() function to check the modulo of the y-coordinate of the position vector with the line stipple pattern. So by changing the condition on line 14, it will allow stippling for vertical lines too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I don't think even that will be enough! We have to think in terms of stippling along the vector of the line we're drawing, rather than a cardinal (screen) direction, otherwise our results won't be consistent.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I’ve come up with a good way to do this using dot products, and by letting the shader take vertex position and texture coordinates as inputs, while outputting the fragment texture coordinates. I'll send you my idea as a commit once I run some tests.

src/material.cpp Outdated
Comment on lines 74 to 77
case (FragmentShaderType::LineStipple):
shader3->setFragmentShaderCode(
Qt3DRender::QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/shaders/line_stipple.frag"))));
break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the line stipple is to be a fragment shader then we would have to have a version for each type of colouring (e.g. Phong) we want to be "stipplable". Do we think this is the best approach?

Copy link
Collaborator Author

@Pranavya-Vivek Pranavya-Vivek Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay yes! I think I can implement a similar approach to the line stippling code, but using a geometry shader instead. Then we would only need one geometry shader, regardless of how the lines are coloured. I'll add the updated commit here;

@Pranavya-Vivek Pranavya-Vivek marked this pull request as ready for review August 4, 2023 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants