I am trying to draw a dashed line in OpenGl using a texture equally spaced along the path as the dashes. I can get a solid line, but that wont work for this project. Could someone help point me in the right direction?
Asked
Active
Viewed 2.2k times
3 Answers
19
Dotted or dashed line in OpenGL is called stippled.
glPushAttrib(GL_ENABLE_BIT);
# glPushAttrib is done to return everything to normal after drawing
glLineStipple(1, 0xAAAA); # [1]
glEnable(GL_LINE_STIPPLE);
glBegin(GL_LINES);
glVertex3f(-.5,.5,-.5);
glVertex3f(.5,.5,-.5);
glEnd();
glPopAttrib();
0xAAAA is the parameter you want to experiment with.
(Sourced from here)
-
I think the link is supposed to be http://3dengine.org/Drawing_dotted_lines_(OpenGL) Which also got broken, so http://tinyurl.com/4nzdy3e – Ben Voigt Mar 16 '11 at 05:25
-
@user661855: The content of the code is pasted in my updated answer. – visakh7 Mar 16 '11 at 05:29
-
I'm using this code for an iPhone app, is there an iPhone alternative to glLineStipple? – user661855 Mar 16 '11 at 05:38
-
@user661855: Maybe this other question would help: http://stackoverflow.com/questions/1937444/drawing-stippled-lines-using-opengl-es – Ben Voigt Mar 16 '11 at 06:06
0
The best way would be to use glBegin(GL_POINTS); and spread out the points in a linear fashion.

atx
- 4,831
- 3
- 26
- 40
0
A dashed line is just a series of colinear line segments with gaps in between. If you want texturing, you can emit a bunch of quads. You can control the texture coordinates so that each dash is a piece of the texture, or each dash encompasses the whole texture, or any other repetition pattern you like.
What have you tried so far? Show the code that's not working and the output graphic it creates. Then explain what you want to be different.

Ben Voigt
- 277,958
- 43
- 419
- 720
-
The path I have to get the texture on is drawn by the user. I use the - (void)touchesBegan:(NSSet *)touches, - (void)touchesMoved:(NSSet *)touches, and - (void)touchesEnded:(NSSet *)touches to track where the user draws. I draw a solid line using.... – user661855 Mar 16 '11 at 05:18
-
@user661855: You can edit your question, if you have a lot of details to add (and you do). Code doesn't fit well in comments. – Ben Voigt Mar 16 '11 at 05:25
-
@user661885: My recommendation would be to draw a solid line while the user is tracing, then when they stop touching the screen, replace it with a bunch of quads laid out along the diagonal. Or are you trying to follow a path that isn't a straight line? – Ben Voigt Mar 16 '11 at 05:28
-
-
@user661885: And you're trying to texture it? Or was texturing just one idea for making it dashed? – Ben Voigt Mar 16 '11 at 05:33
-
The texturing is one idea, yes. I have a texture of an image file of a shape that is used to draw the solid line along where the user traces. But instead of the solid line, I want the dashes to be the shape image. – user661855 Mar 16 '11 at 05:49
-
@user661885: I think you're going to have to take each point reported by the touch API, filter out noise where the finger moved slightly backward or out of line, and then calculate the length of each segment. When the cumulative length reaches a dash length, stop drawing. When the cumulative length exceeds the dash length plus a gap size, start drawing again. And so on. – Ben Voigt Mar 16 '11 at 05:52
-
I did try this earlier, but I forgot to filter out the noise! Thanks for your help. I will try again! – user661855 Mar 16 '11 at 06:00