OpenGL何时使用几何着色器

时间:2014-03-21 18:43:12

标签: c++ qt opengl shader

我目前有一个OpenGL精灵绘图类,它缓冲了一堆精灵数据,然后用glDrawElements转储它。问题是,创建进入缓冲区的精灵是很麻烦的,因为我有大量的参数传递到缓冲区,着色器的冗余度更高。我想知道是否可以通过仅使用要点,位置,方向,纹理坐标等加载缓冲区来减少CPU负载......然后让几何着色器将该无意义转换为片段着色器的四边形。

如果有不同的答案,我已经添加了违规方法,以便您可以看到我的意思:

void Machine::draw(key num, BoundingBox loc, float angle){
    SpriteSyncData*    props;
    VertexAttribArray* vdata;
    GLushort*          idata;
    SpriteProperties*  sprite_props;
    int                sliceW;
    int                sliceH;

    sprite_props = &spriteList[num];
    props = &spriteToSync[sprite_props->atlas];

    props->size++;
    if(props->size > props->capacity){
        props->capacity += COARSE_MEM_SCALE;
        props->data   = (VertexAttribArray*) realloc((void*) props->data,   (sizeof(VertexAttribArray)*4) * props->capacity);
        props->i_data = (GLushort*)          realloc((void*) props->i_data, (sizeof(GLushort)*4) * props->capacity);
    }

    vdata = props->data   + (props->size - 1) * 4;
    idata = props->i_data + (props->size - 1) * 4;

    sliceW = sprite_props->location.x1 - sprite_props->location.x0;
    sliceH = sprite_props->location.y1 - sprite_props->location.y0;

    if(sprite_props->flags & DRAW_TILED){
        vdata[0].p = QVector3D(loc.x1, loc.y0, UNIFORM_DEPTH);
        vdata[1].p = QVector3D(loc.x0, loc.y0, UNIFORM_DEPTH);
        vdata[2].p = QVector3D(loc.x0, loc.y1, UNIFORM_DEPTH);
        vdata[3].p = QVector3D(loc.x1, loc.y1, UNIFORM_DEPTH);
        vdata[0].s = QVector2D(((float) (loc.x1 - loc.x0)) / sliceW,
                               ((float) (loc.y1 - loc.y0)) / sliceH);
        vdata[0].r = QVector2D(0, 0);
        vdata[1].r = vdata[0].r;
        vdata[2].r = vdata[0].r;
        vdata[3].r = vdata[0].r;
    }
    else{
        vdata[0].p = QVector3D(loc.x0 + sliceW, loc.y0,          UNIFORM_DEPTH);
        vdata[1].p = QVector3D(loc.x0,          loc.y0,          UNIFORM_DEPTH);
        vdata[2].p = QVector3D(loc.x0,          loc.y0 + sliceH, UNIFORM_DEPTH);
        vdata[3].p = QVector3D(loc.x0 + sliceW, loc.y0 + sliceH, UNIFORM_DEPTH);
        vdata[0].s = QVector2D(1, 1);
        vdata[0].r = QVector2D(sliceW, sliceH);
        vdata[1].r = vdata[0].r;
        vdata[2].r = vdata[0].r;
        vdata[3].r = vdata[0].r;
    }
    vdata[0].t = QVector2D(sprite_props->texCoords[2], sprite_props->texCoords[1]);
    vdata[1].t = QVector2D(sprite_props->texCoords[0], sprite_props->texCoords[1]);
    vdata[2].t = QVector2D(sprite_props->texCoords[0], sprite_props->texCoords[3]);
    vdata[3].t = QVector2D(sprite_props->texCoords[2], sprite_props->texCoords[3]);
    vdata[1].s = vdata[0].s;
    vdata[2].s = vdata[0].s;
    vdata[3].s = vdata[0].s;

    vdata[0].s_lo = QVector2D(sprite_props->texCoords[0], sprite_props->texCoords[1]);
    vdata[0].s_hi = QVector2D(sprite_props->texCoords[2] - sprite_props->texCoords[0],
                              sprite_props->texCoords[3] - sprite_props->texCoords[1]);
    vdata[1].s_lo = vdata[0].s_lo;
    vdata[1].s_hi = vdata[0].s_hi;
    vdata[2].s_lo = vdata[0].s_lo;
    vdata[2].s_hi = vdata[0].s_hi;
    vdata[3].s_lo = vdata[0].s_lo;
    vdata[3].s_hi = vdata[0].s_hi;

    vdata[0].o = (vdata[1].p + vdata[3].p) * 0.5;
    vdata[1].o = vdata[0].o;
    vdata[2].o = vdata[0].o;
    vdata[3].o = vdata[0].o;

    vdata[0].a = angle;
    vdata[1].a = angle;
    vdata[2].a = angle;
    vdata[3].a = angle;

    idata[0] = (props->size - 1) * 4;
    idata[1] = idata[0] + 1;
    idata[2] = idata[0] + 2;
    idata[3] = idata[0] + 3;

}

0 个答案:

没有答案