3

I get this error message

08-30 19:20:17.774: ERROR/AndroidRuntime(4681): FATAL EXCEPTION: GLThread 9
08-30 19:20:17.774: ERROR/AndroidRuntime(4681): java.lang.IllegalArgumentException: length - offset < n
08-30 19:20:17.774: ERROR/AndroidRuntime(4681):     at android.opengl.Matrix.multiplyMV(Native Method)
08-30 19:20:17.774: ERROR/AndroidRuntime(4681):     at android.opengl.GLU.gluUnProject(GLU.java:237)
08-30 19:20:17.774: ERROR/AndroidRuntime(4681):     at android.app.ui.GLSurfaceRenderer.vector3(GLSurfaceRenderer.java:70)
08-30 19:20:17.774: ERROR/AndroidRuntime(4681):     at android.app.ui.GLSurfaceRenderer.onDrawFrame(GLSurfaceRenderer.java:103)
08-30 19:20:17.774: ERROR/AndroidRuntime(4681):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1332)
08-30 19:20:17.774: ERROR/AndroidRuntime(4681):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1116)

With this code:

import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.util.*;


public class GLSurfaceRenderer implements Renderer{


public static float setx, sety;
private static float posx, posy, posz = 100;
private double speed;
private static float rotation;
private static float statrotation;
public static boolean isPressed;

private static FlatColoredSquare square;
private static FlatColoredSquare statSquare;
public final static String TAG = "input";






    public GLSurfaceRenderer () {

    square = new FlatColoredSquare();
    statSquare = new FlatColoredSquare();
    rotation = (float) Math.floor(Math.random()*361);
    speed = 0.1;




    }

    public void vector3 (GL11 gl){

        int[] viewport = new int[4];
        float[] modelview = new float[16];
        float[] projection = new float[16];
        float winx, winy, winz;
        float[] newcoords = new float[3];

        gl.glGetIntegerv(GL11.GL_VIEWPORT, viewport, 0);
        ((GL11) gl).glGetFloatv(GL11.GL_MODELVIEW_MATRIX, modelview, 0);
        ((GL11) gl).glGetFloatv(GL11.GL_PROJECTION_MATRIX, projection, 0);

        winx = (float)setx;
        winy = (float)viewport[3] - sety;
        winz = 0;

        GLU.gluUnProject(winx, winy, winz, modelview, 0, projection, 0,   viewport, 0, newcoords, 0);
        posx = (int)newcoords[1];
        posy = (int)newcoords[2];
        posz = (int)newcoords[3];



        Log.d(TAG, "vector3 Used");
    }


 @Override
public void onDrawFrame(GL10 gl) {
         gl.glClear(GL10.GL_COLOR_BUFFER_BIT |
            GL10.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();
    gl.glScalef(100, 100, 0);
    gl.glPushMatrix();
    gl.glRotatef(rotation, 0, 0, 1);
    gl.glTranslatef((float) speed/10, 0, 0);    
    square.draw(gl);    
    gl.glPopMatrix();

    gl.glPushMatrix();
    gl.glTranslatef(posx, posy, posz);
    gl.glRotatef(statrotation,0,0,1);
    statSquare.draw(gl);
    gl.glPopMatrix();


    statrotation++;
    speed++;
    Log.d(TAG, "Frame Drawn");
    vector3((GL11) gl);

}   

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    GLU.gluOrtho2D(gl, -width, width, -height, height);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();


}




@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    gl.glShadeModel(GL10.GL_SMOOTH);
    gl.glClearDepthf(1.0f);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glDepthFunc(GL10.GL_LEQUAL);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);

}






} 

Notice that I have attempted to call the vector3() method in my Draw method which appears to work because it reads far enough to pick out an error in the actual method. The error is thrown at this line

GLU.gluUnProject(winx, winy, winz, modelview, 0, projection, 0,   viewport, 0, newcoords, 0);

Note that setx and sety should be equal to 0 at the point the error is thrown as the screen has not yet been touched.

Could the error be anything to do with my glSurfaceView?

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.*;
import android.view.MotionEvent;



public class Input extends GLSurfaceView {

public GLSurfaceRenderer glSurfaceRenderer;
public float setx, sety;
public final static String TAG = "input";


public Input(Context context) {
    super(context);


    glSurfaceRenderer = new GLSurfaceRenderer();
    setRenderer(glSurfaceRenderer);


}


@Override
public boolean onTouchEvent(MotionEvent event){
    if (event.getAction() == MotionEvent.ACTION_DOWN){
    setx = event.getX();
    sety = event.getY();


    Log.d(TAG, "isPressed triggered");
    }
    return true;
}

}
Jack
  • 2,625
  • 5
  • 33
  • 56

1 Answers1

2

I've briefly looked at the source code for this function, and it appears to expect your newcoords vector to have size 4 instead of 3. They are homogeneous coordinates, so you will also have to divide by w to get your actual coordinates.

Note that there's another error in your code as well, you're indexing newcoords out of bounds after the call to gluUnProject. The first element is newcoords[0], not newcoords[1].

I think if you use something like this, it would work better (I didn't test this though):

float[] newcoords = new float[4]; // Note size 4!!
GLU.gluUnProject(...);
float x = newcoords[0] / newcoords[3];
float y = newcoords[1] / newcoords[3];
float z = newcoords[2] / newcoords[3];
svdree
  • 13,298
  • 4
  • 28
  • 21
  • Oh my god I could kiss you =D I set a log tag to check and posx now returns "0.0" thank you. On thing though my setx and sety screen coordinates don't seem to be updating in the render thread (In other words I set logs in my onTouchEvent and they show setx and sety to change when the screen is pressed but my render class is not reading the same float values. Is there a way to share these values easily between classes – Jack Aug 31 '11 at 11:13
  • The documentation for GLSurfaceView has a section on how to pass events to the renderer, that's probably what you're looking for. – svdree Aug 31 '11 at 12:31
  • Never mind I've got to a stage where my screen coordinates a returning accurately through synchronized methods :) but my world coordinates are still returning zeros despite being supposedly updated every frame and my screen coordinates being recorded from within the renderer. Any ideas =/ – Jack Aug 31 '11 at 18:24
  • Hang on are you sure this is correct? The code runs fine without the division by newcoords[3] but returns nothing but zeros all the time. However with the division by newcoords[3] I get a divide by zero error. I expect that all the zeros are to do with something else in the code. – Jack Sep 03 '11 at 12:37
  • Yes, absolutely sure, I just tested it. One thing you have to watch out for is that the emulator returns zeroes for viewport and modelview and projection matrices. On a device (I tested on a Nexus S), the code should work. – svdree Sep 04 '11 at 13:48