【问题标题】:translateM makes object disappear from view porttranslateM 使对象从视口消失
【发布时间】:2014-02-22 21:11:58
【问题描述】:

我已经开始学习 OpenGL ES 2.0。我目前正在尝试通过在 onDrawFrame() 方法上使用 translateM 方法来使对象动态化。不幸的是,当我这样做时,对象会短暂出现然后消失。我不确定发生了什么。如果我在 onSurfaceChanged() 中使用相同的代码进行翻译,它就可以工作。这是我的代码(没有应该在 x 轴上移动它的 x 整数,所以我知道它不会移动。但即使删除动态 int 也会消失):

  package com.background.gl.glcirclebackgroundanimation;

import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES20.glClear;
import static android.opengl.GLES20.glClearColor;
import static android.opengl.GLES20.glViewport;
import static android.opengl.Matrix.multiplyMM;
import static android.opengl.Matrix.setIdentityM;
import static android.opengl.Matrix.translateM;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;

import com.background.gl.helper.ColorShaderProgram;
import com.background.gl.helper.TextureShaderProgram;
import com.background.gl.objects.GLCircle;
import com.background.gl.objects.Mallet;
import com.background.gl.objects.Table;
import com.background.gl.util.MatrixHelper;
import com.background.gl.util.TextureHelper;

public class CircleDynamicBackgroundRenderer implements Renderer {
private final Context context;

    private final float[] projectionMatrix = new float[16];
    private final float[] modelMatrix = new float[16];

    private Table table;
    private Mallet mallet;
    private GLCircle circle;
    float x = 0.01f;

    private TextureShaderProgram textureProgram;
    private ColorShaderProgram colorProgram;

    private int texture;


    public CircleDynamicBackgroundRenderer(Context context) {
        this.context = context;
    }

    @Override
    public void onSurfaceChanged(GL10 glUnused, int width, int height) {
        glViewport(0, 0, width, height);

        MatrixHelper.perspectiveM(projectionMatrix, 45, (float) width
                / (float) height, 1f, 10f);
        setIdentityM(modelMatrix, 0);

    }

    @Override
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
        glClearColor(0.0f, 0.0f, 1.0f, 0.0f);

        table = new Table();
        mallet = new Mallet();
        circle = new GLCircle();

        textureProgram = new TextureShaderProgram(context);
        colorProgram = new ColorShaderProgram(context);

        texture = TextureHelper.loadTexture(context, R.drawable.air_hockey_surface);
        //texture2 = TextureHelper.loadTexture(context, R.drawable.air_hockey_surface_2);
    }

    @Override
    public void onDrawFrame(GL10 glUnused) {
        //Clear the rendering surface
        glClear(GL_COLOR_BUFFER_BIT);
        //x+=0.01f;
        translateM(modelMatrix, 0, 0f, 0f, -10f);
        final float[] temp = new float[16];
        multiplyMM(temp, 0, projectionMatrix, 0, modelMatrix, 0);
        System.arraycopy(temp, 0, projectionMatrix, 0, temp.length);
        textureProgram.useProgram();
        //Pass data into our shaders(u_matrix) and enable/bind the texture
        //textureProgram.setUniforms2(projectionMatrix, texture, texture2);
        textureProgram.setUniforms(projectionMatrix, texture);
        //Bind our [vertex array] data to our shaders(attribute data)
        circle.bindData(textureProgram);
        //Draw it

        circle.draw();
        /*
        // Draw the mallets.
        colorProgram.useProgram();
        colorProgram.setUniforms(projectionMatrix);
        mallet.bindData(colorProgram);
        mallet.draw();*/
    }

}

我已经搜索了多种解决方案,但我无法让它们中的任何一个起作用。我真的可以使用一些帮助。 :)

编辑:是不是因为我将矩阵相乘,所以每帧自动将其移回 -10?因为这可以解释我的问题。

Edit2:所以我将它从 -10f 更改为 -0.01f,它可以工作。但是,当我对 x 轴尝试相同的操作时,它就保持不变。这是为什么?是因为投影矩阵如何将所有值除以 w,所以这就是 z 变化而不是 x 或 y 的原因?那么如何在 x 轴上左右移动呢?

Edit3:我的宽度和高度分别是 768、1184。那么,如果我在 x 上将其平移 0.1f,为什么会有很大差异?宽高是768和1184,还是1和1?

Edit4:好的,现在即使我从 onDrawFrame 中删除 translateM,它仍然会移动。我迷路了

【问题讨论】:

  • translate as in glTranslate 更改矩阵,并且由于您没有重置它,因此您将每帧的翻译相加。所以翻译在第 1 帧中为 -10,在第 2 帧中为 -20 等等。
  • 那么将setIdentityM(modelMatrix, 0) 添加到onDrawFrame() 中?当我尝试这样做时,甚至什么都没有出现。我会再试一次。
  • 好的。我得到了它。谢谢。 :) 你知道为什么到目前为止的动作要翻译 0.01 吗?如果我将它移动 0.05,它就会移出视口
  • 我只能在这里猜测。也许它离相机太近了。但没有任何形象,我不能舒尔。另一件事。尽量不要在你的绘图函数中创建任何垃圾(使用任何新的)。这些垃圾会加起来并使 gc 忙。这个单一的矩阵不会是一个问题,但这样的事情加起来。
  • 嗯...看来我没有修复它。即使我在执行任何翻译之前将 modelMatrix 设置为它的标识,当我将它翻译 -0.001f 时它仍然在移动。我不知道为什么。

标签: android matrix opengl-es opengl-es-2.0


【解决方案1】:

这一行是错误的

System.arraycopy(temp, 0, projectionMatrix, 0, temp.length);

我不知道你写它时的想法,所以我无法解释你是如何失败的,你可能只会在窗口纵横比发生变化时覆盖投影矩阵。其余时间这个矩阵必须是常数,你可能不会写它。在着色器中,您需要传递投影和平​​移矩阵的乘积。但是当你将它相乘时,将结果存储在 temp 中并将 temp 传递给着色器。

【讨论】:

  • 我从 Android 的 OpenGL ES 2.0 书中得到了这个方法。我不确定你要我做什么。这个投影矩阵在我的着色器代码中相乘
【解决方案2】:

试试看

translateM(modelMatrix, 0, 0.5f, 0f, -10f);

X 和 Y 轴必须在 [-1,1] 范围内

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 2015-03-20
    相关资源
    最近更新 更多