0

Basically i have an application for Android 1.5 with a GLSurfaceView class that shows a simple square polygon on the screen. I want to learn to add a new functionality, the functionality of moving the square touching it with the finger. I mean that when the user touches the square and moves the finger, the square should be moved with the finger, until the finger releases the screen.

I'm trying to use gluUnProject to obtain the OpenGL coordinates that matches the exact position of the finger, then, i will make a translatef to the polygon, and i will get the polygon moved to that position (i hope it)

The problem is that something is going wrong with gluUnProject, it is giving me this exception: java.lang.IllegalArgumentException: length - offset < n on the call to gluUnProject.

First of all, i'm passing 0 as Z win coordinate because i dont know what i have to pass as z win coordinate, because win doesn't have Z coordinates, only X and Y. I tested passing 1 on Z coordinate, and i'm getting the same exception.

float [] outputCoords=getOpenGLCoords(event.getX(), event.getY(), 0);
x=outputCoords[0];
y=outputCoords[1];
z=outputCoords[2];

. . .

public float[] getOpenGLCoords(float xWin,float yWin,float zWin)
{
    int screenW=SectionManager.instance.getDisplayWidth();
    int screenH=SectionManager.instance.getDisplayHeight();
    //CODE FOR TRANSLATING FROM SCREEN COORDINATES TO OPENGL COORDINATES
    mg.getCurrentProjection(MyGl);
    mg.getCurrentModelView(MyGl);
    float [] modelMatrix = new float[16];
    float [] projMatrix = new float[16];
    modelMatrix=mg.mModelView;
    projMatrix=mg.mProjection;          
    int [] mView = new int[4];
    mView[0] = 0;
    mView[1] = 0;
    mView[2] = screenW; //width
    mView[3] = screenH; //height
    float [] outputCoords = new float[3];
    GLU.gluUnProject(xWin, yWin, zWin, modelMatrix, 0, projMatrix, 0, mView, 0, outputCoords, 0);
    return outputCoords;
}
NullPointerException
  • 36,107
  • 79
  • 222
  • 382

1 Answers1

3

I answered the same question here; basically the gluUnproject function expects your outputCoords array to have size 4 instead of 3. Note that these are homogeneous coordinates, so you still have to divide the first 3 by the 4th one if you're doing perspective projection.

Community
  • 1
  • 1
svdree
  • 13,298
  • 4
  • 28
  • 21
  • can you explain me why i should divide by the fourth element? – NullPointerException Nov 07 '11 at 08:19
  • and also, wich Z value i have to pass to the gluUnProject function on the winZ value? – NullPointerException Nov 07 '11 at 08:33
  • @AndroidUser99 Because these are homogeneous coordinates `(x, y, z, w)` and you want the equivalent coordinates `(x/w, y/w, z/w, 1)`, which are in turn equivalent to a point `(x/w, y/w, z/w)` in affine 3-space. – Christian Rau Nov 07 '11 at 14:26
  • I understand homogeneous coordinates, but the signature for gluUnProject does not return 4 values, only 3. `gluUnProject(.../*snip*/..., GLdouble* outX, GLdouble* outY, GLdouble* outZ);` Where are you getting this 4th coordinate? – Olie Jun 06 '13 at 23:38
  • Oh, I see you're using some Android library thing, which uses a non-standard `gluUnProject()`. Just to be clear for future SO-spelunkers, the `gluUnProject()` that comes with OpenGL (no wrappers) just returns X, Y, Z -- no W. – Olie Jun 06 '13 at 23:41