sample1D 保留字,创建高度为1 的二维纹理
使用高度为1的OpenGL纹理2D对象的示例:
glTexStorage2D(GL_TEXTURE_2D, 8, GL_RGB8, 256, 1);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 256, 1, GL_RGB, GL_UNSIGNED_BYTE, palette);
并使用名为“ tex”的sampler2D对象在GLSL中进行相应的调用:
vec4 color = texture(tex, vec2(x, 1.0f));\n"
https://stackoverflow.com/questions/6210080/sampler1d-not-supported-in-nvidia-glsl
void set_color_palette() { // if (m_colorTexture.isCreated()) { // m_colorTexture.destroy(); // } // if (m_colorTexture.create()) { // m_colorTexture.setSize(m_palette.size()); // m_colorTexture.setMinMagFilters(QOpenGLTexture::Linear, QOpenGLTexture::Linear); // m_colorTexture.setWrapMode(QOpenGLTexture::ClampToEdge); // m_colorTexture.setFormat(QOpenGLTexture::RGB8_UNorm); // m_colorTexture.allocateStorage(); // m_colorTexture.setData(QOpenGLTexture::RGB, QOpenGLTexture::Float32, m_palette.data()); // } glGenTextures(1, &m_texturePalette); glBindTexture(GL_TEXTURE_2D, m_texturePalette); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_palette.size(), 1, 0, GL_RGB, GL_FLOAT, m_palette.data()); }
if (m_paletteTexture.isCreated()) { m_paletteTexture.destroy(); } if (m_paletteTexture.create()) { QImage img(m_palette.size(), 1, QImage::Format_RGB888); img.fill(QColor(0, 0, 0)); for (int i = 0 ; i < m_palette.size(); i++) { img.setPixel(i,0, m_palette[i]); } m_paletteTexture.setData(img); m_paletteTexture.setMinificationFilter(QOpenGLTexture::Linear); m_paletteTexture.setMagnificationFilter(QOpenGLTexture::Linear); m_paletteTexture.setWrapMode(QOpenGLTexture::ClampToEdge); }
生成文字纹理
QOpenGLTexture *MeasureSpecimen::genTexture(uint textureWidth, uint textureHeight, const QString &text, int size) { QOpenGLTexture *texture = new QOpenGLTexture(QOpenGLTexture::Target2D); QImage img(textureWidth, textureHeight, QImage::Format_RGBA8888); img.fill(QColor(0, 0, 0, 0)); QPainter painter; QFont font; painter.begin(&img); font.setPointSize(size); painter.setFont(font); QPen pen; pen.setColor(Qt::black); pen.setStyle(Qt::SolidLine); painter.setPen(pen); QTextOption option; option.setAlignment(Qt::AlignHCenter | Qt::AlignTop); option.setWrapMode(QTextOption::WordWrap); QRectF rect(0, 0, textureWidth, textureHeight); painter.drawText(rect, text, option); texture->setData(img); texture->setMinificationFilter(QOpenGLTexture::Linear); texture->setMagnificationFilter(QOpenGLTexture::Linear); texture->setWrapMode(QOpenGLTexture::Repeat); return texture; }

浙公网安备 33010602011771号