gpt4 book ai didi

android - 来自 gluUnProject 的 IllegalArgumentException

转载 作者:太空狗 更新时间:2023-10-29 13:39:44 26 4
gpt4 key购买 nike

我收到此错误消息

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)

使用这段代码:

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

}






}

请注意,我已尝试在我的 Draw 方法中调用 vector3() 方法,该方法似乎有效,因为它读取的内容足够多,可以找出实际方法中的错误。在这一行抛出错误

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

请注意,由于尚未触摸屏幕,在抛出错误时 setx 和 sety 应等于 0。

错误可能与我的 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;
}

}

最佳答案

我已经简要查看了此函数的源代码,它似乎期望您的 newcoords 向量的大小为 4 而不是 3。它们是齐次坐标,因此您还必须除以 w 得到您的实际坐标。

请注意,您的代码中还有另一个错误,您在调用 gluUnProject 后索引 newcoords 超出范围。第一个元素是 newcoords[0],而不是 newcoords[1]

我认为如果你使用这样的东西,效果会更好(虽然我没有测试过):

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

关于android - 来自 gluUnProject 的 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7248019/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com