OpenCV + OpenGL: How to use SolvePnP Rotation and translation vector in OpenGL
I'm working on an AR program.
please heip me dealing's almost done.
I found marker and rvec,tvec by SolvePnP.
I can not draw object in OpenGL.
I am beginer OpenGL,OpenCV.
I want to render 3D cube over that marker using OpenGL.
use support opengl mode by build opencv.
int main(int argc, char* argv[])
{
....
namedWindow(winname, CV_WINDOW_OPENGL);
setOpenGlDrawCallback(winname.c_str(), on_opengl);
.....
find marker
According to "http://answers.opencv.org/question/23089/opencv-opengl-proper-camera-pose-using-solvepnp/" I'm doing it like this:
Mat rotation(4, 4, CV_64F);
Mat viewMatrix = cv::Mat::zeros(4, 4, CV_64FC1);
cv::Rodrigues(rvec, rotation);
for (unsigned int row = 0; row<3; ++row)
{
for (unsigned int col = 0; col<3; ++col)
{
viewMatrix.at<double>(row, col) = rotation.at<double>(row, col);
}
viewMatrix.at<double>(row, 3) = tvec.at<double>(row, 0);
}
viewMatrix.at<double>(3, 3) = 1.0f;
cv::Mat glViewMatrix = cv::Mat::zeros(4, 4, CV_64F);
cv::transpose(viewMatrix, glViewMatrix);
glViewMatrix2 = glViewMatrix.clone();
opengl callback
void on_opengl(void* param)
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLoadMatrixd(&glViewMatrix2.at<double>(0, 0));
//glTranslated(glViewMatrix2.at<double>(0, 3), -glViewMatrix2.at<double>(1, 3),0);
//glTranslated(glViewMatrix2.at<double>(3, 0), -glViewMatrix2.at<double>(3, 1), -glViewMatrix2.at<double>(3, 2));
glTranslated(0.0, 0.0, 0.0);
static const int coords[6][4][3] = {
{ { +1, -1, -1 },{ -1, -1, -1 },{ -1, +1, -1 },{ +1, +1, -1 } },
{ { +1, +1, -1 },{ -1, +1, -1 },{ -1, +1, +1 },{ +1, +1, +1 } },
{ { +1, -1, +1 },{ +1, -1, -1 },{ +1, +1, -1 },{ +1, +1, +1 } },
{ { -1, -1, -1 },{ -1, -1, +1 },{ -1, +1, +1 },{ -1, +1, -1 } },
{ { +1, -1, +1 },{ -1, -1, +1 },{ -1, -1, -1 },{ +1, -1, -1 } },
{ { -1, -1, +1 },{ +1, -1, +1 },{ +1, +1, +1 },{ -1, +1, +1 } }
};
for (int i = 0; i < 6; ++i) {
glColor3ub(i * 20, 100 + i * 10, i * 42);
glBegin(GL_QUADS);
for (int j = 0; j < 4; ++j) {
glVertex3d(0.2 * coords[i][j][0], 0.2 * coords[i][j][1], 0.2 * coords[i][j][2]);
}
glEnd();
}
}
i cannot find cube.
if I delete "glLoadMatrixd(&glViewMatrix2.at<double>(0, 0));" this line.
I can find cube on center.
Q2-How to use glut.where can I do"glutInit(&argc2, argv2);"
Maybe some helpful links (I have never touched OpenGL):