2015-10-21 49 views
0

首先道歉,我認爲我有多個項目有類似的問題,但一直不能解決我的問題,因爲我不相信我能夠正確解釋我的問題希望粗略繪製的圖像將有助於解釋我的問題。 enter image description hereopengl glulookat整經對象

我有一個正方形在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(); 
} 

方位角和橫滾值是通過陀螺儀獲得的值。這是我第一次製作增強現實應用程序,所以任何幫助將不勝感激。

回答

0

爲什麼你仍然使用方位角和滾動作爲笛卡爾矢量分量?這些是極座標,需要使用三角函數進行變換,或者在矩陣上使用旋轉方法。

雖然這似乎是你的主要問題,但我可能是錯的。你需要一個關於如何真正識別問題的系統。如何首先添加一些觸發事件來拖動相機以查看是否獲得了預期結果。這些應該模擬你的方位角和滾動值。

至於整合陀螺儀與相機已有多個答案。即使對於Android,如this one