从抛出:IllegalArgumentException gluUnProject [英] IllegalArgumentException from gluUnProject

查看:321
本文介绍了从抛出:IllegalArgumentException gluUnProject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误信息

  08-30 19:20:17.774:ERROR / AndroidRuntime(4681):致命异常:GLThread 9
08-30 19:20:17.774:ERROR / AndroidRuntime(4681):java.lang.IllegalArgumentException异常:长度 - 偏移< ñ
08-30 19:20:17.774:ERROR / AndroidRuntime(4681):在android.opengl.Matrix.multiplyMV(本机方法)
08-30 19:20:17.774:ERROR / AndroidRuntime(4681):在android.opengl.GLU.gluUnProject(GLU.java:237)
08-30 19:20:17.774:ERROR / AndroidRuntime(4681):在android.app.ui.GLSurfaceRenderer.vector3(GLSurfaceRenderer.java:70)
08-30 19:20:17.774:ERROR / AndroidRuntime(4681):在android.app.ui.GLSurfaceRenderer.onDrawFrame(GLSurfaceRenderer.java:103)
08-30 19:20:17.774:ERROR / AndroidRuntime(4681):在android.opengl.GLSurfaceView $ GLThread.guardedRun(GLSurfaceView.java:1332)
08-30 19:20:17.774:ERROR / AndroidRuntime(4681):在android.opengl.GLSurfaceView $ GLThread.run(GLSurfaceView.java:1116)

有了这个code:

 进口android.opengl.GLU;
进口android.opengl.GLSurfaceView.Renderer;
导入android.util *。
公共类GLSurfaceRenderer实现渲染{
公共静态浮动SETX,SETY;
私有静态浮动POSX,铭文,posz = 100;
私人双速;
私有静态浮动旋转;
私有静态浮动statrotation;
公共静态布尔是pressed;私有静态FlatColoredSquare平方米;
私有静态FlatColoredSquare statSquare;
公共最后静态字符串变量=输入;
    公共GLSurfaceRenderer(){    方=新FlatColoredSquare();
    statSquare =新FlatColoredSquare();
    旋转=(浮点)Math.floor(的Math.random()* 361);
    速度= 0.1;
    }    公共无效的Vector3(GL11 GL){        INT []视=新INT [4];
        浮动[] =模型视图新的浮动[16];
        浮动[] =投影新的浮动[16];
        浮winx的,酒味,winz;
        浮动[] = newcoords新的浮动[3];        gl.glGetIntegerv(GL11.GL_VIEWPORT,视口,0);
        ((GL11)GL).glGetFloatv(GL11.GL_MODELVIEW_MATRIX,模型视图,0);
        ((GL11)GL).glGetFloatv(GL11.GL_PROJECTION_MATRIX,投影,0);        winx的=(浮点)SETX;
        酒味=(浮点)视[3] - SETY;
        winz = 0;        GLU.gluUnProject(winx的,酒味,winz,模型视图,0,投影,0,视口,0,newcoords,0);
        POSX =(int)的newcoords [1];
        POSY =(int)的newcoords [2];
        posz =(int)的newcoords [3];        Log.d(TAG的Vector3使用);
    }
 @覆盖
公共无效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(旋转,0,0,1);
    gl.glTranslatef((浮点)速度/ 10,0,0);
    square.draw(GL);
    gl.glPopMatrix();    gl.glPushMatrix();
    gl.glTranslatef(POSX,铭文,posz);
    gl.glRotatef(statrotation,0,0,1);
    statSquare.draw(GL);
    gl.glPopMatrix();
    statrotation ++;
    速度++;
    Log.d(TAG,帧中绘制);
    的Vector3((GL11)GL);}@覆盖
公共无效onSurfaceChanged(GL10 GL,诠释的宽度,高度INT){
    gl.glViewport(0,0,宽度,高度);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    GLU.gluOrtho2D(GL, - 宽度,宽度,-height,高度);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
}
@覆盖
公共无效onSurfaceCreated(GL10 GL,EGLConfig配置){
    gl.glClearColor(0.0,0.0,0.0,0.0);
    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);}
}

这是我试图打电话给我的Draw方法的的Vector3()方法,这似乎工作,因为它读取远远不够,挑选出一个错误的实际方法的通知。该错误是在该行抛出

  GLU.gluUnProject(winx的,酒味,winz,模型观察,0,投影,0,视,0,newcoords,0);

注意SETX和SETY应该等于0时为屏幕尚未触及被抛出了错误的位置。

可能错误是什么关系我glSurfaceView?

 进口android.content.Context;
进口android.opengl.GLSurfaceView;
导入android.util *。
进口android.view.MotionEvent;公共类输入扩展GLSurfaceView {公共GLSurfaceRenderer glSurfaceRenderer;
公众持股量SETX,SETY;
公共最后静态字符串变量=输入;
公共投入(上下文的背景下){
    超级(上下文);
    glSurfaceRenderer =新GLSurfaceRenderer();
    setRenderer(glSurfaceRenderer);
}
@覆盖
公共布尔onTouchEvent(MotionEvent事件){
    如果(event.getAction()== MotionEvent.ACTION_DOWN){
    SETX = event.getX();
    SETY = event.getY();
    Log.d(TAG,是pressed触发);
    }
    返回true;
}}


解决方案

我简单地看了看源$ C ​​$ C此功能,并且它似乎期待你的 newcoords 矢量有大小为4,而不是他们正在齐次坐标,那么你也将用W来划分,让您的实际坐标。

请注意,在你的code另一个错误,以及,你索引 newcoords 出呼叫后界至 gluUnProject 。第一个元素是 newcoords [0] ,而不是 newcoords [1]

我想,如果你使用这样的事情,它会更好地工作(我没有测试这虽然):

 浮动[] = newcoords新的浮动[4]; //注意大小4!
GLU.gluUnProject(...);
浮X = newcoords [0] / newcoords [3];
浮Y = newcoords [1] / newcoords [3];
浮Z = newcoords [2] / newcoords [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;
}

}

解决方案

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];

这篇关于从抛出:IllegalArgumentException gluUnProject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆