绘制不同颜色的折线的最佳做法是什么?

时间:2014-02-13 17:00:04

标签: graphics opengl-es 3d opengl-es-2.0

我想绘制折线,每条折线由不同数量的点组成,每个都有自己的颜色。

在这种情况下,最佳做法是什么 - 我应该如何收集信息并将其发送到GPU?

1 个答案:

答案 0 :(得分:2)

将所有条带分解为GL_LINES并通过单个glDrawArrays() / glDrawElements()电话进行渲染。

对于glDrawElements()案例,您可以像这样分解条带:

#include <vector>
using namespace std;

#include <glm/glm.hpp>
using namespace glm;

struct LineBatch
{
    void AddStrip( const vector< vec2 >& strip, const vec3& color )
    {
        if( strip.size() < 2 )
            return;

        for( size_t i = 1; i < strip.size(); ++i )
        {
            indices.push_back( (GLushort)verts.size() + (GLushort)(i-1) );
            indices.push_back( (GLushort)verts.size() + (GLushort)(i-0) );
        }

        for( size_t i = 0; i < strip.size(); ++i )
        {
            Vertex temp;
            temp.pos = strip[i];
            temp.color = color;
            verts.push_back( temp );
        }
    }

    void Render()
    {
        ...
        glDrawElements( GL_LINES, indices.size(), GL_UNSIGNED_SHORT, &indices[0] );
        ...
    }

private:
    struct Vertex
    {
        vec2 pos;
        vec3 color;
    };
    vector< Vertex > verts;
    vector< GLushort > indices;
};