2017年2月1日 星期三

simple opencv drawing opengl example

draw 2 sphere and a rotating cube in opencv by opengl using opencv setopengldrawcallback
the opencv should be cmaked and check the "WITH_OPENGL"





float gggg=55;
float gginin=3.14/6;
void on_opengl(void* param)
{
 //draw a cube
 glLoadIdentity();
 gluLookAt(20*cos(gginin), 0, 20*sin(gginin), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
 glTranslated(0.0, 0.0, -1.0);
 glDrawArrays(GL_POINTS, 0, 1);
 glRotatef( gggg, 1, 0, 0 );
 //glRotatef( gggg, 0, 1, 0 );
 //glRotatef( gggg, 0, 0, 1 );
 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();
 }
 //draw a sphere
 glLoadIdentity();
 gluLookAt(20*cos(gginin), 0, 20*sin(gginin), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
 glTranslatef(1.0f,2.0f,1.0f);
 glColor3ub( 255,0,0);
 glBegin(GL_TRIANGLES);
 GLUquadricObj *quadric;
 quadric = gluNewQuadric();
 gluQuadricDrawStyle(quadric, GLU_FILL );
 gluSphere( quadric , .1 , 36 , 18 );
 glEnd();
 //draw another sphere
 glLoadIdentity();
 gluLookAt(20*cos(gginin), 0, 20*sin(gginin), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
 glTranslatef(4.1f, 2.45f, 10.0f);
 glBegin(GL_TRIANGLES);
 GLUquadricObj *quadric2;
 quadric2 = gluNewQuadric();
 gluQuadricDrawStyle(quadric2, GLU_FILL );
 gluSphere( quadric , .6 , 36 , 18 );
 glEnd();
 glLoadIdentity();
}
int _tmain(int argc, _TCHAR* argv[])
{
 string openGLWindowName = "OpenGL Test";
 namedWindow(openGLWindowName, WINDOW_OPENGL);
 resizeWindow(openGLWindowName, 640, 480);
 glMatrixMode( GL_PROJECTION );
 glLoadIdentity();
 gluPerspective(90.0, 640/480, 1, 1000000.0);
 glMatrixMode(GL_MODELVIEW);
 setOpenGlContext(openGLWindowName);
 setOpenGlDrawCallback(openGLWindowName, on_opengl, NULL);
 while(true)
 {
  updateWindow(openGLWindowName);// when needed
  gggg++;
  gginin+=3.14/36;
  cvWaitKey(30);
  cout<<"ggininder"<<endl;
 }
return 0;
}