Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
kicad-source-mirror
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
Elphel
kicad-source-mirror
Commits
20c86db7
Commit
20c86db7
authored
Jul 16, 2013
by
Maciej Suminski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1 pixel wide lines using OpenGL shaders.
parent
64122ae0
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
89 additions
and
56 deletions
+89
-56
opengl_gal.cpp
common/gal/opengl/opengl_gal.cpp
+42
-49
shader.frag
common/gal/opengl/shader.frag
+10
-5
shader.vert
common/gal/opengl/shader.vert
+37
-2
No files found.
common/gal/opengl/opengl_gal.cpp
View file @
20c86db7
...
...
@@ -783,23 +783,24 @@ void OPENGL_GAL::DrawCircle( const VECTOR2D& aCenterPoint, double aRadius )
color4
(
fillColor
.
r
,
fillColor
.
g
,
fillColor
.
b
,
fillColor
.
a
);
/* Draw a triangle that contains the circle, then shade it leaving only the circle.
Parameters given to setShader are relative coordinates of the triangle's vertices.
Parameters given to setShader are indices of the triangle's vertices
(if you want to understand more, check the vertex shader source [shader.vert]).
Shader uses this coordinates to determine if fragments are inside the circle or not.
v2
/\
//\\
v0 /_\/_\ v1
*/
setShader
(
SHADER_FILLED_CIRCLE
,
-
sqrt
(
3.0
f
),
-
1.0
f
);
vertex3
(
aCenterPoint
.
x
-
aRadius
*
sqrt
(
3.0
f
),
// v0
aCenterPoint
.
y
-
aRadius
,
layerDepth
);
setShader
(
SHADER_FILLED_CIRCLE
,
1.0
);
vertex3
(
aCenterPoint
.
x
-
aRadius
*
sqrt
(
3.0
f
),
aCenterPoint
.
y
-
aRadius
,
layerDepth
);
// v0
setShader
(
SHADER_FILLED_CIRCLE
,
sqrt
(
3.0
f
),
-
1.0
f
);
vertex3
(
aCenterPoint
.
x
+
aRadius
*
sqrt
(
3.0
f
),
// v1
aCenterPoint
.
y
-
aRadius
,
layerDepth
);
setShader
(
SHADER_FILLED_CIRCLE
,
2.0
);
vertex3
(
aCenterPoint
.
x
+
aRadius
*
sqrt
(
3.0
f
),
aCenterPoint
.
y
-
aRadius
,
layerDepth
);
// v1
setShader
(
SHADER_FILLED_CIRCLE
,
0.0
f
,
2.0
f
);
vertex3
(
aCenterPoint
.
x
,
aCenterPoint
.
y
+
aRadius
*
2.0
f
,
layerDepth
);
// v2
setShader
(
SHADER_FILLED_CIRCLE
,
3.0
);
vertex3
(
aCenterPoint
.
x
,
aCenterPoint
.
y
+
aRadius
*
2.0
f
,
layerDepth
);
// v2
}
if
(
isStrokeEnabled
)
...
...
@@ -807,30 +808,25 @@ void OPENGL_GAL::DrawCircle( const VECTOR2D& aCenterPoint, double aRadius )
color4
(
strokeColor
.
r
,
strokeColor
.
g
,
strokeColor
.
b
,
strokeColor
.
a
);
/* Draw a triangle that contains the circle, then shade it leaving only the circle.
Parameters given to setShader are relative coordinates of the triangle's vertices
Parameters given to setShader are indices of the triangle's vertices
(if you want to understand more, check the vertex shader source [shader.vert]).
and the line width. Shader uses this coordinates to determine if fragments are inside
the circle or not. Width parameter has to be passed as a ratio of inner radius
to outer radius.
the circle or not.
v2
/\
//\\
v0 /_\/_\ v1
*/
float
outerRadius
=
aRadius
+
(
lineWidth
/
2.0
f
);
float
innerRadius
=
aRadius
-
(
lineWidth
/
2.0
f
);
float
relWidth
=
innerRadius
/
outerRadius
;
setShader
(
SHADER_STROKED_CIRCLE
,
1.0
,
aRadius
,
lineWidth
);
vertex3
(
aCenterPoint
.
x
-
aRadius
*
sqrt
(
3.0
f
),
aCenterPoint
.
y
-
aRadius
,
layerDepth
);
// v0
setShader
(
SHADER_STROKED_CIRCLE
,
-
sqrt
(
3.0
f
),
-
1.0
f
,
rel
Width
);
vertex3
(
aCenterPoint
.
x
-
outerRadius
*
sqrt
(
3.0
f
),
// v0
aCenterPoint
.
y
-
outerRadius
,
layerDepth
);
setShader
(
SHADER_STROKED_CIRCLE
,
2.0
,
aRadius
,
line
Width
);
vertex3
(
aCenterPoint
.
x
+
aRadius
*
sqrt
(
3.0
f
),
aCenterPoint
.
y
-
aRadius
,
layerDepth
);
// v1
setShader
(
SHADER_STROKED_CIRCLE
,
sqrt
(
3.0
f
),
-
1.0
f
,
relWidth
);
vertex3
(
aCenterPoint
.
x
+
outerRadius
*
sqrt
(
3.0
f
),
// v1
aCenterPoint
.
y
-
outerRadius
,
layerDepth
);
setShader
(
SHADER_STROKED_CIRCLE
,
0.0
f
,
2.0
f
,
relWidth
);
vertex3
(
aCenterPoint
.
x
,
// v2
aCenterPoint
.
y
+
outerRadius
*
2.0
f
,
layerDepth
);
setShader
(
SHADER_STROKED_CIRCLE
,
3.0
,
aRadius
,
lineWidth
);
vertex3
(
aCenterPoint
.
x
,
aCenterPoint
.
y
+
aRadius
*
2.0
f
,
layerDepth
);
// v2
}
return
;
...
...
@@ -931,20 +927,21 @@ void OPENGL_GAL::drawFilledSemiCircle( const VECTOR2D& aCenterPoint, double aRad
Rotate
(
aAngle
);
/* Draw a triangle that contains the semicircle, then shade it to leave only the semicircle.
Parameters given to setShader are relative coordinates of the triangle's vertices.
Shader uses this coordinates to determine if fragments are inside the semicircle or not.
v2
/\
/__\
v0 //__\\ v1
Parameters given to setShader are indices of the triangle's vertices
(if you want to understand more, check the vertex shader source [shader.vert]).
Shader uses this coordinates to determine if fragments are inside the semicircle or not.
v2
/\
/__\
v0 //__\\ v1
*/
setShader
(
SHADER_FILLED_CIRCLE
,
-
3.0
f
/
sqrt
(
3.0
f
),
0
.0
f
);
setShader
(
SHADER_FILLED_CIRCLE
,
4
.0
f
);
vertex3
(
-
aRadius
*
3.0
f
/
sqrt
(
3.0
f
),
0.0
f
,
layerDepth
);
// v0
setShader
(
SHADER_FILLED_CIRCLE
,
3.0
f
/
sqrt
(
3.0
f
),
0
.0
f
);
setShader
(
SHADER_FILLED_CIRCLE
,
5
.0
f
);
vertex3
(
aRadius
*
3.0
f
/
sqrt
(
3.0
f
),
0.0
f
,
layerDepth
);
// v1
setShader
(
SHADER_FILLED_CIRCLE
,
0.0
f
,
2
.0
f
);
setShader
(
SHADER_FILLED_CIRCLE
,
6
.0
f
);
vertex3
(
0.0
f
,
aRadius
*
2.0
f
,
layerDepth
);
// v2
Restore
();
...
...
@@ -973,26 +970,22 @@ void OPENGL_GAL::drawStrokedSemiCircle( const VECTOR2D& aCenterPoint, double aRa
Rotate
(
aAngle
);
/* Draw a triangle that contains the semicircle, then shade it to leave only the semicircle.
Parameters given to setShader are relative coordinat
es of the triangle's vertices
and the line width. Shader uses this coordinates to determine if fragments are insid
e
the semicircle or not. Width parameter has to be passed as a ratio of inner radius
to outer radius
.
v2
/\
/__\
v0 //__\\ v1
Parameters given to setShader are indic
es of the triangle's vertices
(if you want to understand more, check the vertex shader source [shader.vert]), th
e
radius and the line width. Shader uses this coordinates to determine if fragments are
inside the semicircle or not
.
v2
/\
/__\
v0 //__\\ v1
*/
float
outerRadius
=
aRadius
;
float
innerRadius
=
aRadius
-
lineWidth
;
float
relWidth
=
innerRadius
/
outerRadius
;
setShader
(
SHADER_STROKED_CIRCLE
,
-
3.0
f
/
sqrt
(
3.0
f
),
0.0
f
,
relWidth
);
setShader
(
SHADER_STROKED_CIRCLE
,
4.0
f
,
aRadius
,
lineWidth
);
vertex3
(
-
aRadius
*
3.0
f
/
sqrt
(
3.0
f
),
0.0
f
,
layerDepth
);
// v0
setShader
(
SHADER_STROKED_CIRCLE
,
3.0
f
/
sqrt
(
3.0
f
),
0.0
f
,
rel
Width
);
setShader
(
SHADER_STROKED_CIRCLE
,
5.0
f
,
aRadius
,
line
Width
);
vertex3
(
aRadius
*
3.0
f
/
sqrt
(
3.0
f
),
0.0
f
,
layerDepth
);
// v1
setShader
(
SHADER_STROKED_CIRCLE
,
0.0
f
,
2.0
f
,
rel
Width
);
setShader
(
SHADER_STROKED_CIRCLE
,
6.0
f
,
aRadius
,
line
Width
);
vertex3
(
0.0
f
,
aRadius
*
2.0
f
,
layerDepth
);
// v2
Restore
();
...
...
common/gal/opengl/shader.frag
View file @
20c86db7
...
...
@@ -32,20 +32,25 @@ const float SHADER_FILLED_CIRCLE = 2.0;
const
float
SHADER_STROKED_CIRCLE
=
3
.
0
;
varying
vec4
shaderParams
;
varying
vec2
circleCoords
;
void
filledCircle
(
vec2
aCoord
)
{
if
(
dot
(
aCoord
,
aCoord
)
<
1
.
0
)
if
(
dot
(
aCoord
,
aCoord
)
<
1
.
0
f
)
gl_FragColor
=
gl_Color
;
else
discard
;
}
void
strokedCircle
(
vec2
aCoord
,
float
aWidth
)
void
strokedCircle
(
vec2
aCoord
,
float
a
Radius
,
float
a
Width
)
{
float
outerRadius
=
aRadius
;
float
innerRadius
=
aRadius
-
aWidth
;
float
relWidth
=
innerRadius
/
outerRadius
;
if
(
(
dot
(
aCoord
,
aCoord
)
<
1
.
0
)
&&
(
dot
(
aCoord
,
aCoord
)
>
aWidth
*
a
Width
)
)
(
dot
(
aCoord
,
aCoord
)
>
relWidth
*
rel
Width
)
)
gl_FragColor
=
gl_Color
;
else
discard
;
...
...
@@ -56,11 +61,11 @@ void main()
{
if
(
shaderParams
[
0
]
==
SHADER_FILLED_CIRCLE
)
{
filledCircle
(
vec2
(
shaderParams
[
1
],
shaderParams
[
2
]
)
);
filledCircle
(
circleCoords
);
}
else
if
(
shaderParams
[
0
]
==
SHADER_STROKED_CIRCLE
)
{
strokedCircle
(
vec2
(
shaderParams
[
1
],
shaderParams
[
2
]
)
,
shaderParams
[
3
]
);
strokedCircle
(
circleCoords
,
shaderParams
[
2
]
,
shaderParams
[
3
]
);
}
else
{
...
...
common/gal/opengl/shader.vert
View file @
20c86db7
...
...
@@ -31,8 +31,12 @@ const float SHADER_LINE = 1.0;
const
float
SHADER_FILLED_CIRCLE
=
2
.
0
;
const
float
SHADER_STROKED_CIRCLE
=
3
.
0
;
// Minimum line width
const
float
MIN_WIDTH
=
1
.
0
;
attribute
vec4
attrShaderParams
;
varying
vec4
shaderParams
;
varying
vec2
circleCoords
;
void
main
()
{
...
...
@@ -45,8 +49,8 @@ void main()
float
worldScale
=
gl_ModelViewMatrix
[
0
][
0
];
float
scale
;
// Make lines appear to be at least 1 pixel wid
th
if
(
worldScale
*
lineWidth
<
1
.
0
)
// Make lines appear to be at least 1 pixel wid
e
if
(
worldScale
*
lineWidth
<
MIN_WIDTH
)
scale
=
1
.
0
/
(
worldScale
*
lineWidth
);
else
scale
=
1
.
0
;
...
...
@@ -54,6 +58,37 @@ void main()
gl_Position
=
gl_ModelViewProjectionMatrix
*
(
gl_Vertex
+
vec4
(
shaderParams
.
yz
*
scale
,
0
.
0
,
0
.
0
)
);
}
else
if
(
(
shaderParams
[
0
]
==
SHADER_STROKED_CIRCLE
)
||
(
shaderParams
[
0
]
==
SHADER_FILLED_CIRCLE
)
)
{
// Compute relative circle coordinates basing on indices
// Circle
if
(
shaderParams
[
1
]
==
1
.
0
f
)
circleCoords
=
vec2
(
-
sqrt
(
3
.
0
f
),
-
1
.
0
f
);
else
if
(
shaderParams
[
1
]
==
2
.
0
f
)
circleCoords
=
vec2
(
sqrt
(
3
.
0
f
),
-
1
.
0
f
);
else
if
(
shaderParams
[
1
]
==
3
.
0
f
)
circleCoords
=
vec2
(
0
.
0
f
,
2
.
0
f
);
// Semicircle
else
if
(
shaderParams
[
1
]
==
4
.
0
f
)
circleCoords
=
vec2
(
-
3
.
0
f
/
sqrt
(
3
.
0
f
),
0
.
0
f
);
else
if
(
shaderParams
[
1
]
==
5
.
0
f
)
circleCoords
=
vec2
(
3
.
0
f
/
sqrt
(
3
.
0
f
),
0
.
0
f
);
else
if
(
shaderParams
[
1
]
==
6
.
0
f
)
circleCoords
=
vec2
(
0
.
0
f
,
2
.
0
f
);
// Make the line appear to be at least 1 pixel wide
float
lineWidth
=
shaderParams
[
3
];
float
worldScale
=
gl_ModelViewMatrix
[
0
][
0
];
float
scale
;
// Make lines appear to be at least 1 pixel width
if
(
worldScale
*
lineWidth
<
MIN_WIDTH
)
shaderParams
[
3
]
=
shaderParams
[
3
]
/
(
worldScale
*
lineWidth
);
gl_Position
=
ftransform
();
}
else
{
// Pass through the coordinates like in the fixed pipeline
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment