移植本教程的一些帮助

发布于 2024-11-26 16:17:43 字数 6315 浏览 1 评论 0原文

我正在尝试使用本教程中的代码,但其语言错误(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 作为缓冲区返回)为浮点数。

同样在 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);行当前返回错误。

您的帮助将不胜感激,谢谢。

编辑:好的,我想我明白了,

这是一些代码(我包含了运动事件方法,因为这就是当前问题所在,并且绘制方法提供了一些上下文)

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 参数)。

I'm trying to use the code in this tutorial, but its in the wrong language (OPEN-GL) so i'm porting it into OPENGL-ES as best I can. This is what I have so far:

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

Got a bit thrown by the glReadPixel command because it doesn't accept this

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

Error states that GL_DEPTH_COMPONENT, GL_FLOAT and &setz can't be resolved. What is the OPENGL-ES alternative?

Also the last part of the tutorial, step 5, is that a whole new method or a compilation of the whole tutorial?

NOTE: setx and sety are private floats called at the top of my code

EDIT: I'm also having trouble with the gluUnProject command it seems to require more values than the opengl version in the tutorial.

(in opengl-es)

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

(and in the tutorial)

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

EDIT:

Okay I think I've pretty much solved my own question but I'm still having problems.

gl.glReadPixels 

requires these variables

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

Which is fine but

GLU.gluUnproject

Requires the x coordinate (returned as a buffer by readpixels) to be float.

Also in the NeHe tutorial UnProject returns three floats but here it seems to only leave room for one so I have no idea how to get the variables. This is my current glUnproject code which doesn't match the accepted layout yet.

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

these arguments are as such

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

When they should be

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

EDIT:

Right I decided I could do away with the readpixel command because my game is 2D and setting z to 0 should work OK

so the buffer problem is gone

I changed some of my variables to floats and GLU.gluUnProject now returns no errors. I still don't quite know how to return the posx, posy, posz variables however.

Here is my code as it stands.

 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 are global variables and the return Vector3(posx, posy, posz); line currently returns errors.

Your help would be appreciated thankyou.

EDIT: Okay Think I got it

Here is some code (I included the motion event method because that is where the problem currently is and the draw method to give some context)

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

My error log shows

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)

I have a hunch that the problem may be caused by the way i've called Vector3 in randomMethod, but I can't think how else I could achieve the same thing (maybe its the null argument).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎言笑 2024-12-03 16:17:43

这是你的空论点。

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 的地方

It is your null argument.

Vector3 is expecting an instance of GL11 but you are providing it with null, this means that when it comes to

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 and you get a null pointer.

If you are doing this so you can purely get a Z coordinate then you are going to have to place this method somewhere where is has access to GL11

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文