Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone,

I am currently working on a Live Wallpaper app for android that uses OpenGLES. I have come across some trouble though. I drew a cube and added motion to it, but know I want to add a touch event so when the user touches/drags their finger on the screen the cube will rotate with their finger. I have been following some example code in a book I have been reading but it doesn't seem to work, whenever I run my app and try to drag my finger across the screen nothing happens. So first it tells me to make an onTouch() or in the renderer class, like this:
C#
@Override
    public boolean onTouch(View v, MotionEvent event) {
        for (int i = 0; i < event.getPointerCount(); i++)
        {
            if (event.getPointerId(i) == 0) {
                touchX = (event.getX(i) / surfaceWidth) * 480;
                touchY = (event.getY(i) / surfaceHeight) * 800;
            }
        }
        return true;
    }

btw here are the float variables:
C#
//Set onTouch
float surfaceWidth;
float surfaceHeight;
float touchX;
float touchY;


then it tells me to set the surfaceWidth/Height as the dimensions in the onSurfaceChanged() like this:
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);
    surfaceWidth = width;
    surfaceHeight = height;
}

That is all it tells me to do, but I cant help but thinking that I should add something to the onDrawFrame() since moving an object requires redrawing the frames. If it is helpful here is a little snippet from my onDrawFrame():
@Override
public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    GLU.gluLookAt(gl, 0, 0, 3, 0, 0, 0, 0, 2, 0);

    //Draw Cube
    gl.glPushMatrix();
    square.draw(gl);
    gl.glPopMatrix();

Thank you to anyone who can help me, I know this may seem like a pretty simple problem but I do not really have any experience with the onTouch() or OpenGL, which is why I am reading this book, thanks again!
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900