Make a single large buffer ahead of time and put things in it as
needed. The glDrawArrays method takes first and count arguments, so
you never have to draw the entire buffer, just whatever part of it you
are currently using.

Note that you can draw multiple lines per draw call if your lines are
all in the same buffer as well, which also helps performance. Often
graphics hardware can't reach its rated peak performance unless you
batch large numbers of vertices into fewer draw calls like this.
Sharing buffers also cuts down on calls to set the pointers like
glVertexPointer.

If you ever exceed the capacity you'll have to make a new larger
buffer and put the previous' data into the front, but that's much
better than having to make buffers every frame.

On Feb 21, 9:55 am, M <[email protected]> wrote:
> Hi guys,
>
> I'm getting closer to wrapping up my game project but facing an
> interesting "issue" now with the game so looking for help on how to do
> this.
>
> I need to draw lines (lasers) in orthogonal 2D coordinate space using
> OpenGL but I cannot nail down a quick way to do so. Usually I draw
> everything using gl.glDrawArrays(); and it's proven to be fairly good
> way of rendering everything in my tile based world. But the lasers are
> giving me a headache. I need to be able to draw line between two
> points and the two points can be anywhere on the screen. This makes it
> next to impossible to predefine the lines. If I only had to do
> horizontal / vertical lines I could easily solve this but since the
> two points can be anywhere on the screen and I need to be able to draw
> a line between them no matter the angle / direction.
>
> My current implementation is a really lousy one and definitely hits
> the FPS a bit too heavily.
> When ever I need to draw a line between two points (which is quite
> often) I call a "shoot" method that takes in two positions (start and
> end of the line) and draws a line between them. It defines the
> vertices everytime I call shoot.
>
> I define them by doing the whole thing:
>
> ////////////////////////// CODE
> START ////////////////////////////////////////
> // PASTEBIN LINK FOR THE SAME CODE :http://pastebin.com/m4faa750b
>
> float lineVertices[] = { pos1.x, pos1.y, pos2.x, pos.y };
> ByteBuffer bb = ByteBuffer.allocateDirect(lineVertices.length * 4);
> bb.order(ByteOrder.nativeOrder());
> line = bb.asFloatBuffer();
> line.put(lineVertices);
> line.position(0);
>
> ////////////////////////// CODE
> END    ////////////////////////////////////////
>
> And after that I can render it using the gl.glDrawArrays(); easily.
>
> Anyone got any better solution? Any way of predefining all the
> possible lines or other fancy stuff to optimize it a bit. It's obvious
> that this current implementation is horrible. Causes excessive GC and
> just slows things down.
>
> So looking ways of drawing simple lines between two arbitary points on
> screen as fast as possible because it happens often.

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

Reply via email to