Try this. These are the shaders from the video I made above
Vertex
uniform mat4 mvp;
uniform int vid_offset = 0;
uniform vec3 pos_offset;
out VS_OUT
{
vec4 color;
vec2 texCoord;
} vs_out;
void main(void)
{
vec4 vertices[] = vec4[](vec4(-0.5, -0.5, 0.0, 1.0),
vec4( 0.5, -0.5, 0.0, 1.0),
vec4( 0.5, 0.5, 0.0, 1.0),
vec4(-0.5, 0.5, 0.0, 1.0));
vec2 texcoords[] = vec2[](vec2(0.0, 0.0),
vec2(1.0, 0.0),
vec2(1.0, 1.0),
vec2(0.0, 1.0));
vec4 colors[] = vec4[](vec4(1.0, 0.0, 0.0, 1.0),
vec4(0.0, 1.0, 0.0, 1.0),
vec4(0.0, 0.0, 0.0, 1.0),
vec4(1.0, 1.0, 1.0, 1.0));
vertices[0].xyz += pos_offset;
gl_Position = mvp * vertices[(gl_VertexID + vid_offset) % 4];
vs_out.color = colors[(gl_VertexID + vid_offset) % 4];
vs_out.texCoord = texcoords[(gl_VertexID + vid_offset) % 4];
}
Geometry
#version 450 core
layout (lines_adjacency) in;
layout (triangle_strip, max_vertices = 4) out;
in VS_OUT
{
vec4 color;
vec2 texCoord;
} gs_in[4];
out GS_OUT
{
noperspective vec2 v[4];
noperspective float area[4];
flat float oneOverW[4];
flat float depth[4]; // optional for quad depth emulation
flat vec4 color[4]; // our regular vertex attribs
flat vec2 texCoord[4];
} gs_out;
double area(dvec2 a, dvec2 b)
{
return a.x*b.y - a.y*b.x;
}
void main(void)
{
int i, j, j_next;
vec2 v[4];
for (i=0; i<4; i++) {
float oneOverW = 1.0 / gl_in[i].gl_Position.w;
v[i] = gl_in[i].gl_Position.xy * oneOverW;
gs_out.oneOverW[i] = oneOverW;
gs_out.depth[i] = (((gl_in[i].gl_Position.z * oneOverW) + 1.0) / 2.0) * oneOverW;
gs_out.color[i] = gs_in[i].color * oneOverW;
gs_out.texCoord[i] = gs_in[i].texCoord * oneOverW;
}
for (i=0; i<4; i++) {
// Mapping of polygon vertex order to triangle strip vertex order.
//
// Quad (lines adjacency) Triangle strip
// vertex order: vertex order:
//
// 1----2 1----3
// | | ===> | \ |
// | | | \ |
// 0----3 0----2
//
int reorder[4] = int[]( 0, 1, 3, 2 );
int ii = reorder[i];
dvec2 vector[4];
for (j=0; j<4; j++) {
vector[j] = dvec2(v[j]) - dvec2(v[ii]);
gs_out.v[j] = vec2(vector[j]);
}
for (j=0; j<4; j++) {
j_next = (j+1) % 4;
gs_out.area[j] = float(area(vector[j], vector[j_next])); // if we use float for area it's possible to run out of precision
}
gl_Position = gl_in[ii].gl_Position;
EmitVertex();
}
}
Fragment
#version 450 core
uniform sampler2D tex1;
uniform bool texturesEnabled;
in GS_OUT
{
noperspective vec2 v[4];
noperspective float area[4];
flat float oneOverW[4];
flat float depth[4]; // optional for quad depth emulation
flat vec4 color[4]; // our regular vertex attribs
flat vec2 texCoord[4];
} fs_in;
// interpolated variables
vec4 fsColor;
vec2 fsTexCoord;
// out
out vec4 color;
void QuadInterpolation()
{
uint i, i_next, i_prev;
vec2 s[4];
float A[4];
for (i=0; i<4; i++) {
s[i] = fs_in.v[i];
A[i] = fs_in.area[i];
}
float D[4];
float r[4];
for (i=0; i<4; i++) {
i_next = (i+1)%4;
D[i] = dot(s[i], s[i_next]);
r[i] = length(s[i]);
if (fs_in.oneOverW[i] < 0) { // is w[i] negative?
r[i] = -r[i];
}
}
float t[4];
for (i=0; i<4; i++) {
i_next = (i+1)%4;
if(A[i]==0.0) {
t[i] = 0; // check for zero area + div by zero
}
else {
t[i] = (r[i]*r[i_next] - D[i]) / A[i];
}
}
float uSum = 0;
float u[4];
for (i=0; i<4; i++) {
i_prev = (i-1)%4;
u[i] = (t[i_prev] + t[i]) / r[i];
uSum += u[i];
}
float lambda[4];
for (i=0; i<4; i++) {
lambda[i] = u[i] / uSum;
}
/* Discard fragments when all the weights are neither all negative nor all positive. */
int lambdaSignCount = 0;
for (i=0; i<4; i++) {
if (fs_in.oneOverW[i] < 0) {
if (lambda[i] > 0) {
lambdaSignCount--;
} else {
lambdaSignCount++;
}
}
else {
if (lambda[i] < 0) {
lambdaSignCount--;
} else {
lambdaSignCount++;
}
}
}
if (lambdaSignCount != 4) {
if(!gl_HelperInvocation) { // we need this for mipmap calculation with textures otherwise edge pixels fail
discard;
}
}
float interp_oneOverW = 0.0;
float depth = 0.0;
fsColor = vec4(0.0);
fsTexCoord = vec2(0.0);
for (i=0; i<4; i++) {
interp_oneOverW += lambda[i] * fs_in.oneOverW[i];
depth += lambda[i] * fs_in.depth[i];
fsColor += lambda[i] * fs_in.color[i];
fsTexCoord += lambda[i] * fs_in.texCoord[i];
}
fsColor /= interp_oneOverW;
fsTexCoord /= interp_oneOverW;
depth /= interp_oneOverW;
// write depth value
gl_FragDepth = depth;
}
void main(void)
{
QuadInterpolation();
if(texturesEnabled) {
color = texture(tex1,fsTexCoord);
}
else {
color = fsColor;
}
}
You want to use glDrawArrays with GL_LINE_ADJACENCY