GLES 2.0在Android上将GL_POINTS绘制为方块?

时间:2014-07-12 16:41:38

标签: qt opengl-es-2.0 qt5

下面是一个简单的QT程序,使用VBO绘制6个点。这些点在Android上显示为方块,在桌面上显示为单个像素。当我在顶点着色器中更改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();
}

1 个答案:

答案 0 :(得分:3)

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

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

启用它
glEnable(GL_PROGRAM_POINT_SIZE);

如果未启用此设置,桌面OpenGL将使用通过glPointSize() API调用设置的磅值。