开发者

How to draw multiple line segments of different colours in OpenGL-ES on Android

开发者 https://www.devze.com 2023-01-22 22:02 出处:网络
I\'m creating an application that needs to draw m开发者_运维百科ultiple line segments, each one starting in one colour and ending in another.

I'm creating an application that needs to draw m开发者_运维百科ultiple line segments, each one starting in one colour and ending in another. At the moment I have the code below within my calculating loop but this seems like overkill to achieve what I want especially if the line could be hundreds of segments long.

I'm realtively new to OpenGL so would appreciate some thoughts on how best to approach this.

Thanks.

    vertexBuffer.position(0);
    vertexBuffer.put(xyz[0]);
    vertexBuffer.put(xyz[1]);
    vertexBuffer.put(xyz[2]/);
    vertexBuffer.put(nextxyz[0]);
    vertexBuffer.put(nextxyz[1]/;
    vertexBuffer.put(nextxyz[2]);
    vertexBuffer.position(0);

    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
    gl.glDrawArrays(GL10.GL_LINES, 0, 2);
    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);


That is the wrong use of vertex arrays. They are there to draw multiple primitives (lines in your case) in one draw call and you are only drawing one line each call.

Just pack all the line's vertices (and colors of course) into one vertex array (and color array), so that vertex 2*i and vertex 2*i+1 make up line i and then draw all the lines in one go with

glDrawArrays(GL_LINES, 0, 2*numLines);

If the line segments are all connected, you can also use a linestrip, where the end vertex of a line is used as the start vertex of the next line (but keep in mind that in this case the end vertex of a line also has the same color as the start vertex of the next one, of course). This way you only need numLines+1 vertices and can draw the linestrip with

glDrawArray(GL_LINE_STRIP, 0, numLines+1);

EDIT: And like Profet said, when all the line segments make up a single connected straight line, you can just use one single line with a 1D texture or a fragment shader (although that might be a bit heavy for a beginner) for varying the color along the line.

EDIT: Your code suggests you want all the line segments to use the same start and end colors. In this case you cannot use a linestrip, of course. Packing all the vertices into one array for drawing all segments with a single call (the first suggestion) would mean to repeat the colors for every line, but it might still be faster than doing multiple glDrawArrays calls, escpecially if you build the vertex and color arrays only once. Using unsigned byte colors instead of float (rarely neccessary for colors) might also help to improve performance in this case, as then you only have 16 byte per vertex to send to the GPU (which is also very alignment friendly).


I don't exactly understand what is your problem, but I agree that drawing hundreds of segments is not the best solution...

What do you want to do ?

  • Are these segments all aligned on a single line ?
  • Can you replace all these lines by a single texture drawn in a quad ?
  • Can you calculate the color of each pixel from a specific data, like point's coordinates ? In this case you could use a fragment shader to color each pixel.
0

精彩评论

暂无评论...
验证码 换一张
取 消