0

I am using a simple GLsurfaceview to draw a simple white square covering most of the screen. when OnTouchEvent() is triggered the square should expand and de-expand according to the point pressed on screen. This is done by changing the vertex positions accordingly.

public void onDrawFrame(GL10 gl) {
    // Set GL_MODELVIEW transformation mode
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();   // reset the matrix to its default state
    // When using GL_MODELVIEW, you must set the camera view
    GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
    square.draw(gl, x);
}

The Square.draw() method:

 public void draw(GL10 gl,float x) {
    Log.d("touch", "Square.draw() called");
    vertexBuffer.put(0,-(x/480)*2);
    vertexBuffer.put(6,-(x/480)*2);

    //Set the face rotation
    gl.glFrontFace(GL10.GL_CW);

    //Point to our vertex buffer
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

    //Enable vertex buffer
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

    //Draw the vertices as triangle strip
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

    //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

The GlsurfaceView containing the onTouchEvent:

public boolean onTouchEvent(MotionEvent e) {
    float x = e.getX();
    mRenderer.x = x;

    requestRender();
    return true;
}

My initial thought was each time the screen is pressed the onTouchEvent() is triggered. I then pass the x coordinate to the renderer and from there to the draw method to change the vertexBuffer. This doesnt seem to work for two reasons: 1. once i've covered the whole screen theres no going back no matter where i press. 2. the point i press does not translate well. I have tried using glTranslatef but might have done it wrong.

I would like to stick to this method of changing the vertex positions rather than using glTranslatef.

genpfault
  • 51,148
  • 11
  • 85
  • 139
JY2k
  • 2,879
  • 1
  • 31
  • 60

1 Answers1

0

I suggest you clear the screen before redrawing it. It doesn't matter if you modify the vertex positions themself or have it done at transformation (with glTranslate). You need a clean surface to draw on. Add a

glClear(GL_COLOR_BUFFER_BIT);

at the beginning of the drawing function.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • That did solve the 1st problem. however my bigger problem is still that i need to be able to properly reposition a vertex according to a pressed point.... – JY2k Sep 06 '13 at 12:03
  • @JonathanYarkoni: Of what type is `vertexBuffer`. That method put could be *inserting* a new element, instead of replacing it with the value you desire. – datenwolf Sep 06 '13 at 12:22
  • nope. because it does react only not quite as expected. My finger moves more than the corresponding vertex does on the screen. – JY2k Sep 06 '13 at 12:28
  • @JonathanYarkoni: Well then this is a mere scaling/projection issue. You got there a hardcoded 480 for the screen width in pixels; which is unlikely to be correct. Also the projection may make your viewing volume larger/smaller than the 0…1 range you currently assume. – datenwolf Sep 06 '13 at 12:44
  • Ok.. how do i go about fixing it then...? – JY2k Sep 06 '13 at 14:15
  • I found this answer: http://stackoverflow.com/questions/9342878/usage-of-gluunproject and it seems to be working for me only i cant get the glreadpixels to work without GLES20. can you maybe help me...? – JY2k Sep 06 '13 at 16:00
  • @JonathanYarkoni: gluUnproject is total overkill in your case. Essentially what you must do is introducing some sensible mapping between window coordinates and view space coordinates. As a first step I suggest you implement a proper mapping from pixel based pointer coordinates into normalized coordinates. For that you need to obtain the actual dimensions of your viewport. That constant 480 value is not what you want. You want to query the actual display dimensions and use those for the normalization. – datenwolf Sep 06 '13 at 16:11