opengl glulookat翘曲对象

时间:2015-10-21 04:28:32

标签: android opengl-es-2.0 glulookat

首先道歉,我想我有多个条目有类似的问题,但无法解决我的问题,因为我不相信我能够正确解释我的问题,希望粗略绘制的图像将有助于解释我的问题。 enter image description here

我有一个正方形在x和y轴上绕着我的世界移动,物体离开屏幕并在它移动时有时会回来。我想在设备中使用陀螺仪来获取值,这些值将用于通过在x和y方向上移动来改变相机的位置,以尝试在物体在世界各地移动时搜索物体。这是当前代码,当我移动设备时,它似乎会扭曲方形的形状。

class SquareRenderer implements GLSurfaceView.Renderer {

private int counter = 0;
private boolean mTranslucentBackground;
private Square mSquare;
private float mTransY;
private float mTransX;
private float mAngle;
private Context context;

private float xPoint = 0.0f;
private float yPoint = 0.0f;


public SquareRenderer(boolean useTranslucentBackground,Context context) {
    mTranslucentBackground = useTranslucentBackground;
    this.context=context;
    mSquare = new Square();
}

public void onDrawFrame(GL10 gl) {
    gl.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    gl.glMatrixMode(GL11.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glTranslatef(mTransX, mTransY, -10.0f);
    GLU.gluLookAt(gl, 0.0f, 0.0f, 0.0f,(float) MainActivity.azimuth , (float) MainActivity.roll, 0.0f, 0.0f, 1, 0.0f);

    mSquare.draw(gl);
    //GLU.gluLookAt(gl, 0.0f, 0.0f, 0.0f, (float) MainActivity.azimuth, (float) MainActivity.roll, 0.0f, 0.0f, 1, 0.0f);
    Random rnd = new Random();

    float maxx = 180.0f;
    float minx = -180.0f;
    float maxy = 90.0f;
    float miny = -90.0f;

    if( xPoint == 0.0f && yPoint == 0.0f){
        xPoint = rnd.nextFloat()*(maxx-minx)+minx;
        xPoint = round(xPoint,1);
        yPoint = rnd.nextFloat()*(maxy-miny)+miny;
        yPoint = round(yPoint,1);

        Log.d("XPOINT    YPOINT",xPoint +"      " +yPoint);
    }
    if(mTransX != xPoint && xPoint>mTransX) {
        mTransX += 0.1f;
        mTransX = round(mTransX,1);
    }else if(mTransX != xPoint && xPoint<mTransX){
        mTransX -= 0.1f;
        mTransX = round(mTransX,1);
    }else if(mTransX == xPoint){
        xPoint = rnd.nextFloat()*(maxx-minx)+minx;
        xPoint = round(xPoint,1);
    }
    if(mTransY != yPoint && yPoint >mTransY) {
        mTransY += 0.1f;
        mTransY = round(mTransY,1);
    }else if(mTransY != yPoint && yPoint < mTransY){
        mTransY -= 0.1f;
        mTransY = round(mTransY,1);
    }else if(mTransY == yPoint){
        yPoint = rnd.nextFloat()*(maxy-miny)+miny;
        yPoint = round(yPoint,1);
    }
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);
    float ratio = (float) width / height;
    gl.glMatrixMode(GL11.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glDisable(GL11.GL_DITHER);
    gl.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_FASTEST);
    gl.glClearColor(0,0,0,0);
    gl.glEnable(GL11.GL_CULL_FACE);
    gl.glShadeModel(GL11.GL_SMOOTH);
    gl.glEnable(GL11.GL_DEPTH_TEST);
}

public static float round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.floatValue();
}

方位角和滚动值是通过陀螺仪获得的值。这是我第一次制作增强现实风格的应用程序,所以任何帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

为什么你仍然使用方位角和滚动作为笛卡尔矢量分量?这些是极坐标,需要使用三角函数进行变换,或者在矩阵上使用旋转方法。

虽然这似乎是你的主要问题,但我可能错了。您需要一个如何实际识别问题的系统。你怎么样首先添加一些触摸事件,使相机处于拖动状态,看看你是否得到了预期的结果。那些应该模拟你的方位角和掷骰值。

至于陀螺仪与相机的集成,已有多种答案。即使是像this one

这样的Android