C++ glsl qt着色器程序
你能帮我写一个正确的着色器程序(glsl)让QT painterPath适应SceneGraph吗。节点的几何体已三角形化,但我无法为阴影笔刷样式编写正确的着色器材质C++ glsl qt着色器程序,c++,qt,opengl,C++,Qt,Opengl,你能帮我写一个正确的着色器程序(glsl)让QT painterPath适应SceneGraph吗。节点的几何体已三角形化,但我无法为阴影笔刷样式编写正确的着色器材质 1. pathNode.cpp #include <QtQuick/QSGSimpleMaterialShader> #include <QtQuick/QSGTexture> #include <QtQuick/QQuickWindow> #include <private/qtri
1. pathNode.cpp
#include <QtQuick/QSGSimpleMaterialShader>
#include <QtQuick/QSGTexture>
#include <QtQuick/QQuickWindow>
#include <private/qtriangulator_p.h>
#include <QPainterPath>
#include "pathNode.h"
#define TEXTURE_SIZE 64
struct BrushMaterial
{
~BrushMaterial() {
delete texture;
}
QColor color;
QSGTexture* texture;
};
class BrushShader : public QSGSimpleMaterialShader<BrushMaterial>
{
QSG_DECLARE_SIMPLE_SHADER(BrushShader, BrushMaterial)
public:
BrushShader() :
id_color(-1),
id_texture(-1),
id_textureSize(-1)
{
setShaderSourceFile(QOpenGLShader::Vertex, ":/shaders/brush.vsh");
setShaderSourceFile(QOpenGLShader::Fragment, ":/shaders/brush.fsh");
}
QList<QByteArray> attributes() const
{
return QList<QByteArray>() << "aVertex" << "aTexCoord";
}
void updateState(const BrushMaterial *m, const BrushMaterial *)
{
// Set the color
program()->setUniformValue(id_color, m->color);
// Bind the texture and set program to use texture unit 0 (the default)
m->texture->bind();
// Then set the texture size so we can adjust the texture coordinates accordingly in the
// vertex shader..
QSize s = m->texture->textureSize();
program()->setUniformValue(id_textureSize, QSizeF(1.0 / s.width(), 1.0 / s.height()));
}
void resolveUniforms()
{
id_texture = program()->uniformLocation("texture");
id_textureSize = program()->uniformLocation("textureSize");
id_color = program()->uniformLocation("color");
// We will only use texture unit 0, so set it only once.
program()->setUniformValue(id_texture, 0);
}
private:
int id_color;
int id_texture;
int id_textureSize;
};
PathNode::PathNode(QSGTexture* texture,const QColor& color)
: m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 0, 0, GL_UNSIGNED_INT)
{
setGeometry(&m_geometry);
m_geometry.setDrawingMode(GL_TRIANGLES);
QSGSimpleMaterial<BrushMaterial>* m = BrushShader::createMaterial();
m->state()->texture = texture;
m->state()->color = color;
m->setFlag(QSGMaterial::Blending);
setMaterial(m);
setFlag(OwnsMaterial, true);
}
void PathNode::update(const QPainterPath& path)
{
const QTriangleSet triangles{ qTriangulate(path) };
// Fill vertex buffer
m_geometry.allocate(triangles.vertices.size() / 2, triangles.indices.size());
QSGGeometry::TexturedPoint2D *vertex = m_geometry.vertexDataAsTexturedPoint2D();
for (int i = 0; i < m_geometry.vertexCount(); ++i)
vertex[i].set(triangles.vertices.at(2 * i), triangles.vertices.at(2 * i + 1), 1, 0);
// Fill index buffer
uint *indices = m_geometry.indexDataAsUInt();
if (triangles.indices.type() != QVertexIndexVector::UnsignedInt)
qFatal("Unexpected geometry index type");
memcpy(indices, triangles.indices.data(), triangles.indices.size() * sizeof(*indices));
markDirty(QSGNode::DirtyGeometry);
}
uniform sampler2D texture;
uniform lowp float qt_Opacity;
uniform lowp vec4 color;
varying highp vec2 vTexCoord;
void main()
{
lowp vec4 shade = texture2D(texture, vTexCoord) ;
lowp vec4 c = vec4(color.xyz * shade.xyz, 1.0);
gl_FragColor = c * qt_Opacity;
}
链接到github代码:只需为项目指定透明颜色即可 在main.cpp的第50行中,更改:
fillBrush.setColor(Qt::red);
致:
这是我得到的结果:。或者我不明白你想得到什么?我需要用红线填充路径,但没有背景。现在我有红色的填充,带有较轻的十字线。对不起,你能为你想要的结果制作一个模型图片吗?主要目标是像QBrush样式一样填充节点。
uniform sampler2D texture;
uniform lowp float qt_Opacity;
uniform lowp vec4 color;
varying highp vec2 vTexCoord;
void main()
{
lowp vec4 shade = texture2D(texture, vTexCoord) ;
lowp vec4 c = vec4(color.xyz * shade.xyz, 1.0);
gl_FragColor = c * qt_Opacity;
}
fillBrush.setColor(Qt::red);
fillBrush.setColor(Qt::transparent);