glLineWidth & Broken Lines


I have been trying to use glLineWidth to draw a line based on the mouse XY.
Using ofLine, when you increase glLineWidth over 1 the line is visibly made up of segments and not a continuous line.

This makes sense actually but I tried using this:

	for (int i = 0; i < vx.size(); i++) {  
		glVertex2f(vx[i], vy[i]);  
		//vectors with the coordinates from the mouse  

and still the same although GL_LINE_STRIP is supposed to draw a continuous strip I believe.

This is what it looks like without antialias (although that image has been resized it seems):

And with antialias:

The black line is actually ofCurve taken from the SVN, which appears to do the same. I guess you guys might have something in mind for dealing with line widths? ofLineWidth perhaps?

Thanks in advance,


PS, this is using pre release v0.02 and X-Code

hi -

yeah this is a big problem with opengl…-zation.htm
How do I turn on wide-line endpoint capping or mitering?

one solution is to draw circles of different widths wherever the vertices are. This can help smooth things out.

I’m sure there are other solutions - I’ll look around for some mitering algorithms

another solution is this one, which takes an array of points (ofVec3f - you can find this in the ofAddons svn or on chris sugrue’s website) and calculates tangents to draw a quad strip. it smooths out the angles a bit because especially with mouse drawn graphics you get alot of 90 degree angle changes (and 45 degress, etc) because of the integer input system. the width of the line is stored in the “Z” component of the ofVec3f
(which holds x,y,z)… it’s not perfect, but it could look better then opengl…

void renderStroke(ofVec3f * pts, int nPts, bool bUseZasWidth, float width){  
	if (nPts < 3) return;  
	float 		angleSmooth;  
	float 		prevAngle;  
	ofVec3f 	tangent;  
	ofVec3f 	pta, ptb;  
	for (int i = 0; i < nPts-1; i++){  
		float angle 	= atan2(pts[i+1].y - pts[i].y, pts[i+1].x - pts[i].x);  
		if (i == 0){  
			angleSmooth = angle;  
		} else {  
			float diff = (angle - angleSmooth);  
			while (diff < -PI) diff += TWO_PI;  
			while (diff > PI) diff -= TWO_PI;  
			angleSmooth = 0.5f * angleSmooth +   
						  0.5f * (angleSmooth + diff);  
		tangent.set(-sin(angleSmooth), cos(angleSmooth),0);  
		if (bUseZasWidth == true){  
			pta = pts[i] + tangent * pts[i]->z/2.0f;  
			ptb = pts[i] - tangent * pts[i]->z/2.0f;  
		} else {  
			pta = pts[i] + tangent * width/2.0f;  
			ptb = pts[i] - tangent * width/2.0f;  
		glVertex2f(pta.x, pta.y);  
		glVertex2f(ptb.x, ptb.y);  

I hope that helps - I’ll keep researching line drawing approaches…

take care!

Thanks for that Zach.

Yeah its a little bit surprising actually. Even the suggestion to turn off depth testing does not make a difference.

I’ve got ofVec3f (from here right?), but am at a bit of a loss as to how to use it. Do you mind posting a quick example of how I get the X Y and Z into it?



Hunting around for someways to handle mitering I came across the 2d graphics library Cairo. Just looking through the samples it seems you can do some pretty nice 2d rendering with it and also output vector/raster files.

More info here. It is open-source, cross-platform… What do you think?

hmm-- it’s definitely worth checking out ! thanks for pointing it out. I’ve downloaded the win32 development version and will give it a run…

I’ve also used anti grain geometry ( and livarot ( for 2-d rendering into images -

there is also amanith -, that uses opengl as a basis.

take care,

amanith infact looks good doesn’t it.
Let me know how it turns out with that.

Thanks again!