Smooth line drawing, glsl, shader, openframeworks, sketch - ofApp.cpp. ... I draw the lines as one big vboMesh, each line has 2 polygons, ...
Skiptocontent
Allgists
BacktoGitHub
Signin
Signup
Sign in
Sign up
{{message}}
Instantlysharecode,notes,andsnippets.
gwio/ofApp.cpp
CreatedMay13,2018
Star
1
Fork
0
Star
Code
Revisions
1
Stars
1
Embed
Whatwouldyouliketodo?
Embed
Embedthisgistinyourwebsite.
Share
Copysharablelinkforthisgist.
Clonevia
HTTPS
ClonewithGitorcheckoutwithSVNusingtherepository’swebaddress.
LearnmoreaboutcloneURLs
DownloadZIP
Smoothlinedrawing,glsl,shader,openframeworks,sketch
Raw
ofApp.cpp
ThisfilecontainsbidirectionalUnicodetextthatmaybeinterpretedorcompileddifferentlythanwhatappearsbelow.Toreview,openthefileinaneditorthatrevealshiddenUnicodecharacters.
LearnmoreaboutbidirectionalUnicodecharacters
Showhiddencharacters
/*IdrawthelinesasonebigvboMesh,eachlinehas2polygons,theyareconstructedfromthesamevertexpositionand
andextrudedlaterinthevertexshader*/
lineMesh.addVertex(lineStart);
lineMesh.addVertex(lineEnd);
lineMesh.addVertex(lineEnd);
lineMesh.addVertex(lineStart);
//...lineMesh.addIndex(..)...addingtheindexorder
//thisisthenormalvectortooffsettheverticeslater
ofVec2fnormalLine=(lineStart-lineEnd).getPerpendicular();
/*normalLinegetsintoavector,2floatsforeachvertex,itsjustimportantthateverymeshvertexhasown
normal"linewidth"vectorbothpositiveandnegativedirection*/
lineNormalVec.push_back(normalLine.x);
lineNormalVec.push_back(normalLine.y);
/*ThelineNormalVecisthenusedascustomattributefortheshader*/
shader.setupShaderFromFile(GL_VERTEX_SHADER,"shader.vert");
shader.setupShaderFromFile(GL_FRAGMENT_SHADER,"shader.frag");
shader.bindDefaults();
//0-4arealreadyusedbyof
shader.bindAttribute(5,"line_Normals");
shader.linkProgram();
intshaderAttPos=shader.getAttributeLocation("line_Normals");
lineMesh.getVbo().setAttributeData(shaderAttPos,lineNormalVec[0],2,lineNormalVec.size(),GL_STATIC_DRAW,sizeof(float)*2);
//thelinesarethedrawnintoafbo
shader.begin();
fbo.begin();
ofClear(0,0,0,255);
shader.setUniform1f("time",ofGetElapsedTimef()/4.0);
//notsurewhatthe"right"settingsarebuttheglBlendEquation(max)isimportant,
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ZERO);
//thisworksonios;glBlendEquationOES(GL_MAX_EXT);
glBlendEquation(GL_MAX);
drawLineMesh();
fbo.end();
shader.end();
//Ithenlaterthisfboandprocessitwithanothershaderforcoloringandlinecaps
Raw
shader.frag
ThisfilecontainsbidirectionalUnicodetextthatmaybeinterpretedorcompileddifferentlythanwhatappearsbelow.Toreview,openthefileinaneditorthatrevealshiddenUnicodecharacters.
LearnmoreaboutbidirectionalUnicodecharacters
Showhiddencharacters
/*thismakesthelinesmooth,agradient,basedonthefragmentpositioninsidethelinepolygon.Iusepow()to
tomodifythegradientcurve,hardergradientforbiggerwidth,smoothercurveforthinlines.Alsothereareprobably
betterwaysthenusingpow()forthis.*/
invec2frag_pos;
voidmain()
{
vec2pos;
pos.x=gl_FragCoord.x;
pos.y=gl_FragCoord.y;
vec2xy=frag_pos-gl_FragCoord.xy;
//floatalpha=pow(length(frag_pos),abs(sin(time)*6)+2);
floatalpha=pow(length(frag_pos),5);
vec4tempOut=vec4(0,0,0,0);
outputColor=vec4(1.0-alpha,1.0-alpha,1.0-alpha,1.0);
}
Raw
shader.vert
ThisfilecontainsbidirectionalUnicodetextthatmaybeinterpretedorcompileddifferentlythanwhatappearsbelow.Toreview,openthefileinaneditorthatrevealshiddenUnicodecharacters.
LearnmoreaboutbidirectionalUnicodecharacters
Showhiddencharacters
#version150
uniformmat4modelViewProjectionMatrix;
uniformfloattime;
invec2line_Normals;
invec4position;
invec2texcoord;
outvec2texCoordVarying;
outvec2frag_pos;
voidmain(){
//thenormalvecotrisscaledbythedesiredlinewidthandappliedtoeachvertex
vec4delta=vec4(line_Normals*"lineWidht",0,0);
vec4pos=modelViewProjectionMatrix*(position+delta);
texCoordVarying=vec2(texcoord.x,texcoord.y);
frag_pos=line_Normals;
gl_Position=pos;
}
Raw
shader2.frag
ThisfilecontainsbidirectionalUnicodetextthatmaybeinterpretedorcompileddifferentlythanwhatappearsbelow.Toreview,openthefileinaneditorthatrevealshiddenUnicodecharacters.
LearnmoreaboutbidirectionalUnicodecharacters
Showhiddencharacters
/*Ithenuseasecondshadertofillthegapsbetweenthelineswithcircles,withasimilargradient,
thisisabittrickyandtotallynotperfect,butdecentenough;)*/
floatradius=(18.0*abs(sin(time))+1.0)*1.08;
//floatradius=(2.5)*1.08;
vec4imageC=texture(net,texCoordVarying);
vec4addCircle=vec4(1.0,1.0,1.0,1.0);
floatalpha=0.0;
for(inti=0;i