I found a description in another thread but I am not sure how to implement it, but this is exactly what I am looking to try and do.
If you want to emulate OpenGL’s glLineStipple for thick lines, then they use a 16-bit pattern.
You can generate a 1D texture for your stipple texture, and map it onto your line geometry using the repeat factor as texture coordinates.
Question is how do I accomplish that? I have tried without success :(. I am trying the following but I am struggling to understand how to get this working.
widget code [using qt]
#include "glwidget.h"
#include "math.h"
#include <QGLShader>
#include <QGLShaderProgram>
#include <QKeyEvent>
namespace
{
const float GL_PI = 3.1415f;
}
GLWidget::GLWidget(QWidget *parent)
: QGLWidget(QGLFormat(QGL::DoubleBuffer | QGL::Rgba | QGL::DepthBuffer), parent)
, vShader(0)
, fShader(0)
, program(0)
, xRot(0.0f)
, yRot(0.0f)
{
}
GLWidget::~GLWidget()
{
delete program;
program = NULL;
delete fShader;
fShader = NULL;
delete vShader;
vShader = NULL;
}
void GLWidget::initializeGL()
{
vShader = new QGLShader(QGLShader::Vertex, context(), this);
vShader->compileSourceFile(":/shaders/vertex.vsh");
fShader = new QGLShader(QGLShader::Fragment, context(), this);
fShader->compileSourceFile(":/shaders/fragment.fsh");
program = new QGLShaderProgram(context(), this);
program->addShader(vShader);
program->addShader(fShader);
program->link();
// attribute
vertexAttribLoc = program->attributeLocation("a_vertex");
texCoordAttribLoc = program->attributeLocation("a_texCoord");
// vertex shader uniform
mvpMatrixUniLoc = program->uniformLocation("u_mvpMatrix");
GLfloat x = -80.0f;
GLfloat y = -90.0f;
GLfloat z = 0.0f;
// point 1
vertices.append(x);
vertices.append(y);
vertices.append(z);
// point 2
x = 80.0f;
vertices.append(x);
vertices.append(y);
vertices.append(z);
// fragment shader uniform
colorUniformLoc = program->uniformLocation("u_color");
samplerUniLoc = program->uniformLocation("u_sampler");
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
program->bind();
int tupleSize = 3;
program->setAttributeArray(vertexAttribLoc, GL_FLOAT, vertices.data(), tupleSize);
program->enableAttributeArray(vertexAttribLoc);
program->setAttributeValue(texCoordAttribLoc, 3);
program->enableAttributeArray(texCoordAttribLoc);
QVector3D color(0.0f, 1.0f, 0.0f);
program->setUniformValue(colorUniformLoc, color);
GLushort pattern = 0x5555;
program->setUniformValue(samplerUniLoc, pattern);
QMatrix4x4 lineModel = modelMatrix;
lineModel.rotate(xRot, 1.0f, 0.0f, 0.0f);
lineModel.rotate(yRot, 0.0f, 1.0f, 0.0f);
GLfloat sizes[2];
glGetFloatv(GL_ALIASED_LINE_WIDTH_RANGE, sizes);
GLfloat curSize = sizes[0];
for(GLfloat y = 20.0f; y < 200.0f; y += 20.0f)
{
QMatrix4x4 individualLineModel = lineModel;
individualLineModel.translate(0.0f, y, 0.0f);
mvpMatrix = projectionMatrix * viewMatrix * individualLineModel;
program->setUniformValue(mvpMatrixUniLoc, mvpMatrix);
glLineWidth(curSize);
glDrawArrays(GL_LINES, 0, vertices.size());
curSize += 1.0f;
}
swapBuffers();
}
void GLWidget::resizeGL(int w, int h)
{
GLfloat nRange = 100.0f;
GLfloat aspectRatio;
if(h == 0)
{
h = 1;
}
glViewport(0, 0, w, h);
projectionMatrix.setToIdentity();
aspectRatio = (GLfloat) w / (GLfloat) h;
if(w <= h)
{
projectionMatrix.ortho(-nRange, nRange, (-nRange * h) / w, (nRange * h) / w, -nRange, nRange);
}
else
{
projectionMatrix.ortho((-nRange * w) / h, (nRange * w) / h, -nRange, nRange, -nRange, nRange);
}
viewMatrix.setToIdentity();
modelMatrix.setToIdentity();
mvpMatrix = projectionMatrix * viewMatrix * modelMatrix;
}
void GLWidget::keyPressEvent(QKeyEvent *event)
{
switch(event->key())
{
case Qt::Key_Up:
xRot -= 5.0f;
break;
case Qt::Key_Down:
xRot += 5.0f;
break;
case Qt::Key_Left:
yRot -= 5.0f;
break;
case Qt::Key_Right:
yRot += 5.0f;
break;
case Qt::Key_Q:
case Qt::Key_Escape:
exit(0);
default:
break;
}
if(xRot > 356.0f)
{
xRot = 0.0f;
}
if(xRot < -1.0f)
{
xRot = 355.0f;
}
if(yRot > 356.0f)
{
yRot = 0.0f;
}
if(yRot < -1.0f)
{
yRot = 355.0f;
}
update();
}
vertex shader
attribute vec4 a_vertex;
attribute float a_textureCoord;
uniform mat4 u_mvpMatrix;
varying float v_textureCoord;
void main(void)
{
gl_Position = u_mvpMatrix * a_vertex;
v_textureCoord = a_textureCoord;
}
fragment
uniform vec3 u_color;
uniform sampler1D sampler;
varying float v_texCoord;
void main(void)
{
vec4 baseColor = vec4(u_color, 1.0);
vec4 texColor = texture1D(sampler, v_texCoord);
gl_FragColor = baseColor + texColor;
}
I seem to be crashing whenever I try to draw however if I take out the texColor from fragment shader and just use the baseColor, I don’t crash.