从solvepnp在Android中使用Opengl增强现实

从solvepnp在Android中使用Opengl增强现实,android,opencv,opengl-es,augmented-reality,Android,Opencv,Opengl Es,Augmented Reality,我正在尝试在Android的摄像头视图上投影opengl中的3D曲面。我在本机代码中使用opencv函数solvepnp来获取设备相机的旋转和平移向量。我使用这些向量计算modelView矩阵和modelViewProjection矩阵,如下链接所示: 最后,我将这些矩阵以浮点数组的形式传递给java代码,但是当我在渲染器中使用这些矩阵时,我无法获得屏幕上的曲面投影。我确信我的3D曲面生成正确,因为当我使用modelView和modelView投影矩阵的任意值时,我可以看到投影。但是当我使用从

我正在尝试在Android的摄像头视图上投影opengl中的3D曲面。我在本机代码中使用opencv函数solvepnp来获取设备相机的旋转和平移向量。我使用这些向量计算modelView矩阵和modelViewProjection矩阵,如下链接所示:

最后,我将这些矩阵以浮点数组的形式传递给java代码,但是当我在渲染器中使用这些矩阵时,我无法获得屏幕上的曲面投影。我确信我的3D曲面生成正确,因为当我使用modelView和modelView投影矩阵的任意值时,我可以看到投影。但是当我使用从solvepnp函数计算出的矩阵时,我什么也得不到。有人能告诉我哪里错了吗?以下是我的代码的相关部分:

"Native code in cpp file"

Rodrigues(r, expandedR);  // r is the rotation vector received form solvepnp function

Mat Rt = Mat::zeros(4, 4, CV_64FC1);
for (int y = 0; y < 3; y++) {
   for (int x = 0; x < 3; x++) {
      Rt.at<double>(y, x) = expandedR.at<double>(y, x);
   }
   Rt.at<double>(y, 3) = t.at<double>(y, 0);    //t is the translation vector from solvepnp
}
Rt.at<double>(3, 3) = 1.0;

//OpenGL has reversed Y & Z coords
Mat reverseYZ = Mat::eye(4, 4, CV_64FC1);
reverseYZ.at<double>(1, 1) = reverseYZ.at<double>(2, 2) = -1;

//since we are in landscape mode
Mat rot2D = Mat::eye(4, 4, CV_64FC1);
rot2D.at<double>(0, 0) = rot2D.at<double>(1, 1) = 0;
rot2D.at<double>(0, 1) = 1;
rot2D.at<double>(1, 0) = -1;

Mat projMat = Mat::zeros(4, 4, CV_64FC1);
float fard = 10000, neard = 5;
float imageWidth=1024.0f;
float imageHeight=576.0f;
projMat.at<double>(0, 0) = 2*scaledCameraMatrix.at<double>(0, 0)/imageWidth;
projMat.at<double>(0, 2) = -1 + (2*scaledCameraMatrix.at<double>(0, 2)/imageWidth);
projMat.at<double>(1, 1) = 2*scaledCameraMatrix.at<double>(1, 1)/imageHeight;
projMat.at<double>(1, 2) = -1 + (2*scaledCameraMatrix.at<double>(1, 2)/imageHeight);
projMat.at<double>(2, 2) = -(fard+neard)/(fard-neard);
projMat.at<double>(2, 3) = -2*fard*neard/(fard-neard);
projMat.at<double>(3, 2) = -1;

Mat mvMat = reverseYZ * Rt;
projMat = rot2D * projMat;

Mat mvp = projMat * mvMat;
float arr[16];
float arr2[16];

int count=0;
for(int i=0;i<4;i++)
{
    for(int j=0;j<4;j++)
    {
        arr[count]=(float)mvp.at<double>(i,j);
        arr2[count++]=(float)mvMat.at<double>(i,j);
    }
}

env->SetFloatArrayRegion(sendfloatArray, 0,16, &arr[0]);
env->SetFloatArrayRegion(sendfloatArray, 16,16, &arr2[0]);
以下是我的片段着色器中的代码:

precision mediump float;        // Set the default precision to medium. We don't need as high of a 
                            // precision in the fragment shader.



 varying vec4 v_Color;              // This is the color from the vertex shader interpolated across the 
                            // triangle per fragment.


  // The entry point for our fragment shader.
  void main() {                              


  gl_FragColor = v_Color;                                       
}                                                                       
下面是我的顶点着色器中的代码

  uniform mat4 u_MVPMatrix;     // A constant representing the combined model/view/projection matrix.                  
 uniform mat4 u_MVMatrix;       // A constant representing the combined model/view matrix.              

  attribute vec4 a_Position;        // Per-vertex position information we will pass in.                 
  attribute vec4 a_Color;           // Per-vertex color information we will pass in.                


   varying vec4 v_Color;            // This will be passed into the fragment shader.                


  // The entry point for our vertex shader.  
 void main() {                                                    



// Pass through the color.
v_Color = a_Color;



// gl_Position is a special variable used to store the final position.
// Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
gl_Position = u_MVPMatrix * a_Position;                               
}                                  

你有没有想出一些答案???我也有同样的问题…你有没有想出一些答案???我也有同样的问题…你有没有想出一些答案???我也有同样的问题。。
  uniform mat4 u_MVPMatrix;     // A constant representing the combined model/view/projection matrix.                  
 uniform mat4 u_MVMatrix;       // A constant representing the combined model/view matrix.              

  attribute vec4 a_Position;        // Per-vertex position information we will pass in.                 
  attribute vec4 a_Color;           // Per-vertex color information we will pass in.                


   varying vec4 v_Color;            // This will be passed into the fragment shader.                


  // The entry point for our vertex shader.  
 void main() {                                                    



// Pass through the color.
v_Color = a_Color;



// gl_Position is a special variable used to store the final position.
// Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
gl_Position = u_MVPMatrix * a_Position;                               
}