gl_point在Android上绘制为正方形



下面是一个使用VBO绘制6个点的简单QT程序。这些点在安卓系统上显示为正方形,在桌面上显示为单个像素。当我在顶点着色器中更改gl_PointSize时,正方形在Android上更改为适当的大小,但在桌面上保持单个像素。

example.pro

QT += core gui widgets opengl
TARGET = example
TEMPLATE = app
SOURCES = main.cpp
HEADERS = main.h

main.h

#include <QGLWidget>
#include <QGLFunctions>
#include <QGLShader>
class glview : public QGLWidget, protected QGLFunctions
{
    Q_OBJECT
public:
    explicit glview(QWidget *parent = 0);
protected:
    void initializeGL();
    void resizeGL(int w, int h);
    void paintGL();
private:
    quint32 vbo_id[1];
    QGLShaderProgram *program;
};

main.cpp

#include <QApplication>
#include "main.h"
struct vrtx {
    GLfloat x;
    GLfloat y;
    GLfloat z;
    GLfloat r;
    GLfloat g;
    GLfloat b;
}__attribute__((packed)) geomrtry[] = {
    //   x, y, z   r, g, b
    {1, 1, 0, 1, 0, 0},
    {1.5, 2, 0, 0, 1, 0},
    {2, 1, 0, 0, 0, 1},
    {3, 1, 0, 1, 0, 1},
    {3.5, 2, 0, 1, 1, 0},
    {4, 1, 0, 0, 1, 1},
};
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    glview widget;
    widget.show();
    return app.exec();
}
glview::glview(QWidget *parent) : QGLWidget(parent)
{
}
void glview::initializeGL()
{
    initializeGLFunctions();
    qglClearColor(Qt::white);
    QGLShader *vshader = new QGLShader(QGLShader::Vertex, this);
    const char *vsrc =
        "attribute highp vec4 vertex;n"
        "attribute mediump vec4 colour;n"
        "varying mediump vec4 f_colour;n"
        "uniform mediump mat4 matrix;n"
        "void main(void)n"
        "{n"
        "    gl_Position = matrix * vertex;n"
        "    f_colour = colour;n"
        "    gl_PointSize = 12.0;n"
        "}n";
    vshader->compileSourceCode(vsrc);
    QGLShader *fshader = new QGLShader(QGLShader::Fragment, this);
    const char *fsrc =
        "varying mediump vec4 f_colour;n"
        "void main(void)n"
        "{n"
        "    gl_FragColor = f_colour;n"
        "}n";
    fshader->compileSourceCode(fsrc);
    program = new QGLShaderProgram(this);
    program->addShader(vshader);
    program->addShader(fshader);
    program->link();
    glGenBuffers(1, vbo_id);
    glBindBuffer(GL_ARRAY_BUFFER, vbo_id[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(geomrtry), geomrtry, GL_STATIC_DRAW);
    glEnable(GL_DEPTH_TEST);
}
void glview::resizeGL(int w, int h)
{
    glViewport(0, 0, w, h);
}
void glview::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    QMatrix4x4 matrix;
    matrix.ortho(0, 5, 0, 3, -1, 1);
    program->bind();
    program->setUniformValue("matrix", matrix);
    glBindBuffer(GL_ARRAY_BUFFER, vbo_id[0]);
    int vertexLocation = program->attributeLocation("vertex");
    program->enableAttributeArray(vertexLocation);
    glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, sizeof(struct vrtx), 0);
    int colourLocation = program->attributeLocation("colour");
    program->enableAttributeArray(colourLocation);
    glVertexAttribPointer(colourLocation, 3, GL_FLOAT, GL_FALSE, sizeof(struct vrtx), ((char*)NULL + 12));
    //glDrawArrays(GL_TRIANGLES, 0, sizeof(geomrtry) / sizeof(struct vrtx));
    glDrawArrays(GL_POINTS, 0, sizeof(geomrtry) / sizeof(struct vrtx));
    glFlush();
}

您正在使用的功能,其中渲染点的大小是从顶点着色器中内置的gl_PointSize变量设置中获取的。这个功能在ES 2.0中是默认的。

桌面OpenGL中也有相同的功能,但默认情况下是禁用的。可以通过调用:

glEnable(GL_PROGRAM_POINT_SIZE);

如果此设置未启用,桌面OpenGL使用glPointSize() API调用设置的点大小

相关内容

  • 没有找到相关文章

最新更新