一点帮助移植本教程

时间:2011-07-28 12:08:40

标签: android opengl-es porting

我正在尝试使用this tutorial中的代码,但它的语言错误(OPEN-GL),所以我尽可能地将它移植到OPENGL-ES中。这就是我到目前为止所做的:

public void coordinates (GL10 gl, int x, int y, int width, int height){
        int[] viewport = new int[4];
        gl.glGetIntegerv(GL11.GL_VIEWPORT, viewport, 0);
        int[] modelview = new int[16];
        gl.glGetIntegerv(GL11.GL_MODELVIEW_MATRIX, modelview, 0);
        int[] projection = new int[16];
        gl.glGetIntegerv(GL11.GL_PROJECTION_MATRIX, projection, 0);

        sety = viewport[3] - sety;
}

glReadPixel命令有点抛出,因为它不接受这个

gl.glReadPixels(setx, sety, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &setz);

错误指出无法解析GL_DEPTH_COMPONENT,GL_FLOAT和& setz。什么是OPENGL-ES替代品?

本教程的最后一部分,第5步,是整个教程的全新方法还是汇编?

注意:setx和sety是在我的代码顶部调用的私有浮动

编辑:我也遇到了使用gluUnProject命令的问题,它似乎需要比教程中的opengl版本更多的值。

(在opengl-es中)

 GLU.gluUnProject(winX, winY, winZ, model, modelOffset, project, projectOffset, view, viewOffset, obj, objOffset)

(和教程中)

gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

编辑:

好吧,我想我已经解决了我自己的问题,但我仍然遇到问题。

gl.glReadPixels 

需要这些变量

glReadPixels(int, int, int, int, int, int, Buffer)

哪个好,但

GLU.gluUnproject

需要x坐标(由readpixels作为缓冲区返回)为float。

同样在NeHe教程中,UnProject会返回三个浮点数,但这里似乎只留有空间,所以我不知道如何获取变量。这是我目前的glUnproject代码,它与已接受的布局不匹配。

GLU.gluUnProject(setx, newsety, setz, modelview, 0, projection, 0, viewport, 0, posx, 0, posy, 0, posz, 0);

这些论点就是这样的

 (float, float, buffer, int[], int, int[], int, int[], int, float, int, float, int, float, int)]

什么时候应该

(float, float, float, float[], int, float[], int, int[], int, float[], int)

编辑:

对,我决定取消readpixel命令,因为我的游戏是2D,将z设置为0应该可以正常工作

所以缓冲问题已经消失

我将一些变量更改为浮点数,GLU.gluUnProject现在不返回任何错误。我仍然不知道如何返回posx,posy,posz变量。

这是我的代码。

 public void Vector3 (GL11 gl, int x, int y){
        int[] viewport = new int[4];
        float[] modelview = new float[16];
        float[] projection = new float[16];
        float winx, winy, winz;
        float posx, posy, posz;
        float[] Vector3 = new float[3];

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

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

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

        return Vector3(posx, posy, posz);


    } 

setx,sety,setz是全局变量,返回Vector3(posx,posy,posz); line当前返回错误。

你的帮助将不胜感激。

编辑:好的想想我明白了

这是一些代码(我包含了运动事件方法,因为那是当前问题所在,而draw方法是给出一些上下文)

public synchronized void randomMethod(MotionEvent event){
        if (event.getAction() == MotionEvent.ACTION_DOWN){
        setx = event.getX();
        sety = event.getY();
        Vector3(null);
        }
    }

    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[] Vector3 = new float[3];

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

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


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

        posx = Vector3[1];
        posy = Vector3[2];
        posz = Vector3[3];


    }



 @Override
public void onDrawFrame(GL10 gl) {
         gl.glClear(GL10.GL_COLOR_BUFFER_BIT |
            GL10.GL_DEPTH_BUFFER_BIT);

             gl.glLoadIdentity();
             gl.glPushMatrix();
    gl.glTranslatef(posx, posy, posz);
    gl.glScalef(100, 100, 0);
    square.draw(gl);        
    gl.glPopMatrix();
 }

我的错误日志显示

07-30 10:41:03.220: ERROR/AndroidRuntime(340): Uncaught handler: thread main exiting due to uncaught exception
07-30 10:41:03.270: ERROR/AndroidRuntime(340): java.lang.NullPointerException
07-30 10:41:03.270: ERROR/AndroidRuntime(340):     at android.app.ui.GLSurfaceRenderer.Vector3(GLSurfaceRenderer.java:51)
07-30 10:41:03.270: ERROR/AndroidRuntime(340):     at android.app.ui.GLSurfaceRenderer.randomMethod(GLSurfaceRenderer.java:40)
07-30 10:41:03.270: ERROR/AndroidRuntime(340):     at android.app.ui.Practice.onTouchEvent(Practice.java:41)
07-30 10:41:03.270: ERROR/AndroidRuntime(340):     at android.app.Activity.dispatchTouchEvent(Activity.java:2064)
07-30 10:41:03.270: ERROR/AndroidRuntime(340):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
07-30 10:41:03.270: ERROR/AndroidRuntime(340):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1690)
07-30 10:41:03.270: ERROR/AndroidRuntime(340):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 10:41:03.270: ERROR/AndroidRuntime(340):     at android.os.Looper.loop(Looper.java:123)
07-30 10:41:03.270: ERROR/AndroidRuntime(340):     at android.app.ActivityThread.main(ActivityThread.java:4310)
07-30 10:41:03.270: ERROR/AndroidRuntime(340):     at java.lang.reflect.Method.invokeNative(Native Method)
07-30 10:41:03.270: ERROR/AndroidRuntime(340):     at java.lang.reflect.Method.invoke(Method.java:521)
 07-30 10:41:03.270: ERROR/AndroidRuntime(340):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
 07-30 10:41:03.270: ERROR/AndroidRuntime(340):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
 07-30 10:41:03.270: ERROR/AndroidRuntime(340):     at dalvik.system.NativeStart.main(Native Method)

我有一种预感,问题可能是由于我在randomMethod中调用Vector3的方式引起的,但我无法想象我还能如何实现同样的事情(可能是null参数)。

1 个答案:

答案 0 :(得分:2)

这是你的空参数。

Vector3期待GL11的一个实例,但你提供的是null,这意味着它涉及到

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

gl = NULL你得到一个 空指针

如果您这样做,那么您可以纯粹获得Z坐标,那么您将不得不将此方法放置在可以访问GL11的地方