2013-01-08 116 views
0

增強現實的圖像說話總是比一噸文本的多,這裏就是我想要做的事: Augmented reality scence與Android傳感器和OpenGL

什麼是圓的中心是用戶的手機位置(起源)。該應用程序顯示一個自定義相機視圖,它還顯示一個OpenGL場景(取決於你在哪裏尋找)。 OpenGL場景僅由一個簡單的多維數據集組成,並且當用戶朝着正確的方向查看時,該多維數據集將被渲染。

我是OpenGL的新手,我實現了在相機前顯示立方體,但它是靜態的:我無法繞過360°視圖。

我從傳感器獲取設備的方位:

int type = event.sensor.getType(); 
    float[] data; 
    if (type == Sensor.TYPE_ACCELEROMETER) { 
     mGData = data; 
    } else if (type == Sensor.TYPE_MAGNETIC_FIELD) { 
     mMData = data; 
    } else { 
     // we should not be here. 
     return; 
    } 
    for (int i=0 ; i<3 ; i++) 
     data[i] = event.values[i]; 

    SensorManager.getRotationMatrix(mR, mI, mGData, mMData); 
    SensorManager.getOrientation(mR, mOrientation); 

據我明白了,3個同時正交旋轉角被存儲在mOrientation。但是呢?我想在方法中製作類似GLU.lookAt(0, 0, 0, X, Y, Z, ?, ?, ?)的方法,但它不起作用。我想讓這樣的人說他做不到(見最後一段:https://stackoverflow.com/a/9114246/1304830)。

這是我在onDrawFram使用的代碼:

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
    gl.glMatrixMode(GL10.GL_MODELVIEW); 

    // Look in a direction (with the sensors) 
    gl.glLoadIdentity(); 
    GLU.gluLookAt(gl, 0.0f, 0.0f, 0.0f, ?, ?, ?, ?, ?, ?); // Where I need help 

    gl.glPushMatrix(); 

    // Place the cube in the scene 
    gl.glTranslatef(0, 0, -5); 
    mCube.draw(gl); 

感謝您的幫助

+0

甚至沒有線索? – Fr4nz

+0

嗯,我從來沒有嘗試過這個,但是一旦我將旋轉和定向矩陣作爲模型或視圖矩陣原樣使用,會發生什麼情況。 – harism

回答

0

這將做什麼人要怎樣做,但他的想法,問題是上向量總是指向y軸。所以如果你打電話,相機不會隨它一起滾動。

float pi = (float) Math.PI; 
float rad2deg = 180/pi; 

// Get the pitch, yaw and roll from the sensor. 

float yaw = orientation[0] * rad2deg; 
float pitch = orientation[1] * rad2deg; 
float roll = orientation[2] * rad2deg; 

// Convert pitch, yaw and roll to a vector 

float x = (float)(Math.cos(yaw) * Math.cos(pitch)); 
float y = (float)(Math.sin(yaw) * Math.cos(pitch)); 
float z = (float)(Math.sin(pitch)); 

GLU.gluLookAt(gl, 0.0f, 0.0f, 0.0f, x, y, z, 0.0f, 1.0f, 0.0f); 

使用三個glRotates是一個更好的選擇國際海事組織,除非你想鎖定滾動出於某種原因。

注意:我不確定Android調用哪個方向與手機的屏幕有關,所以我可能會有偏航,俯仰和滾動配置錯誤。