1

This is the super simple version of the question I posted earlier (Which I think is too complicated)

How do I draw a Line in OpenGL ES 2.0 Using as a reference a stroke on the Touch Screen?

For example If i draw a square with my finger on the screen, i want it to be drawn on the screen with OpenGL.

I have tried researching a lot but no luck so far.

(I only now how to draw objects which already have fixed vertex arrays, no idea of how to draw one with constantly changing array nor how to implement it)

bernie
  • 9,820
  • 5
  • 62
  • 92
Pochi
  • 13,391
  • 3
  • 64
  • 104

1 Answers1

1

You should use vertex buffer objects (VBOs) as the backing OpenGL structure for your vertex data. Then, the gesture must be converted to a series of positions (I don't know how that happens on your platform). These positions must then be pushed to the VBO with glBufferSubData if the existing VBO is large enough or glBufferData if the existing VBO is too small.

Using VBOs to draw lines or any other OpenGL shape is easy and many tutorials exist to accomplish it.

update

Based on your other question, you seem to be almost there! You already create VBOs like I mentioned but they are probably not large enough. The current size is sizeof(Vertices) as specified in glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

You need to change the size given to glBufferData to something large enough to hold all the original vertices + those added later. You should also use GL_STREAM as the last argument (read up on the function).

To add a new vertex, use something like this :

glBufferSubData(GL_ARRAY_BUFFER, current_nb_vertices*3*sizeof(float), nb_vertices_to_add, newVertices);
current_nb_vertices += nb_vertices_to_add;
//...
// drawing lines
glDrawArrays(GL_LINE_STRIP, 0, current_nb_vertices);

You don't need the indices in the element array to draw lines.

Community
  • 1
  • 1
bernie
  • 9,820
  • 5
  • 62
  • 92
  • Hello, the problem here is that i dont realy understand where to implement it (or how to). I have to change the geometry, so i have to keep adding vertexes to the original array right? – Pochi Dec 07 '11 at 07:25
  • Here is a link to my other question (the complete version of this one) – Pochi Dec 07 '11 at 10:21
  • (http://stackoverflow.com/questions/8409071/opengl-es-2-0-on-ios-5-drawing-a-line-using-the-motion-manager) – Pochi Dec 07 '11 at 10:21
  • oo thanks! just a question, when and how should i call the method to redraw with the new array of line points? i create this NSMutablearray at touchesmoved, and i copy it (or want to) at touch ended (so that i can display the whole stroke/figure) – Pochi Dec 08 '11 at 01:23
  • I don't know anything about the portable device specifics sorry. I only know the OpenGL side. The `glDrawArrays` I put is a replacement for your `glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);` since you don't need indexes to draw a line loop. Normally you should redraw your scene when the content changes I guess? – bernie Dec 08 '11 at 03:28
  • Hi Chiquis,I am facing this problem in android opengl es2.0 for Autocad App developing.I had successfully dawned line and circles.when i run my app in google nexus7, its fine. when i run my app in samsung galaxy note II, when I draw line means, the previous line erased. this is my problem.. – harikrishnan Jun 26 '13 at 11:35
  • HI Chiquis,please see this link to understand my problem clearly..Please help me if you can.. http://stackoverflow.com/questions/17187032/why-my-opengl-output-differs-for-various-devices http://stackoverflow.com/questions/17229066/is-opengl-development-gpu-dependant/17230475?noredirect=1#17230475 – harikrishnan Jun 26 '13 at 11:35