-1

enter image description hereI am very new to Open GL. My motto is to create freehand drawing using open GL. This I am trying to achieve through connecting vertices on Drag. My problem is as soon I tap anywhere on screen a line is drawn from center os screen to that point. I am not able to figure out why? Please help.

This is what I am doing to draw :

@Override
public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    glUniform4f(uColorLocation, 1.0f, 0.0f, 0.0f, 1.0f);

    vertexData.put(toFloatarray(points));
    vertexData.clear();

    for (int i = last_size; i < points.size(); i++) {

        glDrawArrays(GL_LINES, i , 2);
        last_size = points.size();

    }

}

I know for loop is not the best way and this is the issue but I am not able to get over it.

EDIT:

This is how I am adding points to array

public void handleTouchDrag(float normalizedX, float normalizedY) {
    points.add(normalizedX);
    points.add(normalizedY);

}

where :

final float normalizedX = (event.getX() / (float) v.getWidth()) * 2 - 1;
final float normalizedY = -((event.getY() / (float) v.getHeight()) * 2 - 1);
Payal
  • 903
  • 1
  • 9
  • 28

1 Answers1

1

You are feeding the function with only one vertex and requesting to draw a line segment, and OpenGL decides to consider the second point to be origin.

You can use

 glDrawArrays(GL_LINE_STRIP, 0, points.size());

And this will draw lines to previous point.

codetiger
  • 2,650
  • 20
  • 37
  • Better remove the for loop and try this glDrawArrays(GL_LINE_STRIP, 0, points.size()); I've also edited my code in the answer. – codetiger Aug 09 '16 at 04:31
  • I have uploaded screenshot of result that I am getting both ways, with or without For loop..I only dragged and drew the bottom line but the center one connected the bottom one is drawing on its own. Please help – Payal Aug 09 '16 at 04:42
  • If you are drawing GL_LINES, you are suppose to give 2 vertices (x1, y1, x2, y2). and GL_LINE_STRIP will drawling from previous vertex. – codetiger Aug 09 '16 at 04:45