سؤال

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.

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top