OpenGL - 如何将仿射变换函数与程序中的事件相连接

时间:2011-11-10 06:18:37

标签: c++ opengl ubuntu glut

我是使用openGL的初学者。

我使用了一个程序,我通过互联网在屏幕上绘制一个立方体,并根据某些键盘笔划进行平移,缩放和旋转。

贝娄,我附上了执行此操作的代码:

#define  RADDEG  57.29577951f

float XUP[3] = {1,0,0}, XUN[3] = {-1, 0, 0},
  YUP[3] = {0,1,0}, YUN[3] = { 0,-1, 0},
  ZUP[3] = {0,0,1}, ZUN[3] = { 0, 0,-1},
  ORG[3] = {0,0,0};

GLfloat viewangle = 0, tippangle = 0, traj[120][3];

GLfloat d[3] = {0.1, 0.1, 0.1};

GLfloat  xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;

GLfloat scaleF = 0.2;


//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4

//  Use arrow keys to rotate entire scene !!!

void Special_Keys (int key, int x, int y)
{
switch (key) {

   case GLUT_KEY_LEFT :  viewangle -= 5;  break;
   case GLUT_KEY_RIGHT:  viewangle += 5;  break;
   case GLUT_KEY_UP   :  tippangle -= 5;  break;
   case GLUT_KEY_DOWN :  tippangle += 5;  break;

   default: printf ("   Special key %c == %d\n", key, key);
}

glutPostRedisplay();
}

//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4

void Keyboard (unsigned char key, int x, int y)
{
switch (key) {

   case 'j' : d[0] += 0.1;  break;
   case 'k' : d[0] -= 0.1;  break;
   case 'n' : d[1] += 0.1; break;
   case 'm' : d[1] -= 0.1; break;
   //case 'l' : d[2] += 0.1;  break;

   case 'z' : xAngle += 5;  break;
   case 'x' : yAngle += 5;  break;
   case 'c' : zAngle += 5;  break;

   case 'q' : scaleF += 0.1; break;
   case 'w' : scaleF -= 0.1; break;

   default: cout<< "Redo a valid keystroke;"<<endl;
}

glutPostRedisplay();
}

//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4

void Triad (void)
{
glColor3f (1.0, 1.0, 1.0);

glBegin (GL_LINES);
   glVertex3fv (ORG); glVertex3fv (XUP);
   glVertex3fv (ORG); glVertex3fv (YUP);
   glVertex3fv (ORG); glVertex3fv (ZUP);
glEnd ();

glRasterPos3f (1.1, 0.0, 0.0);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, 'X');

glRasterPos3f (0.0, 1.1, 0.0);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, 'Y');

glRasterPos3f (0.0, 0.0, 1.1);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, 'Z');
}

//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4

void Draw_Box (void)
{
glBegin (GL_QUADS);

glColor3f(1,0,0);
    glVertex3f(1,1,1);
    glVertex3f(-1,1,1);
    glVertex3f(-1,-1,1);
    glVertex3f(1,-1,1);

    glColor3f(0,1,1);
    glVertex3f(1,1,-1);
    glVertex3f(-1,1,-1);
    glVertex3f(-1,-1,-1);
    glVertex3f(1,-1,-1);

    glColor3f(0,1,0);
    glVertex3f(1,1,1);
    glVertex3f(1,-1,1);
    glVertex3f(1,-1,-1);
    glVertex3f(1,1,-1);

    glColor3f(1,0,1);
    glVertex3f(-1,1,1);
    glVertex3f(-1,-1,1);
    glVertex3f(-1,-1,-1);
    glVertex3f(-1,1,-1);

    glColor3f(0,0,1);
    glVertex3f(1,1,1);
    glVertex3f(-1,1,1);
    glVertex3f(-1,1,-1);
    glVertex3f(1,1,-1);

    glColor3f(1,1,0);
    glVertex3f(1,-1,1);
    glVertex3f(-1,-1,1);
    glVertex3f(-1,-1,-1);
    glVertex3f(1,-1,-1);

glEnd();
}

//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4

void redraw (void)
{
int v;

glClear  (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable (GL_DEPTH_TEST);

glLoadIdentity ();

glTranslatef (0, 0, -3);
glRotatef (tippangle, 1,0,0);  // Up and down arrow keys 'tip' view.
glRotatef (viewangle, 0,1,0);  // Right/left arrow keys 'turn' view.

glDisable (GL_LIGHTING);

Triad ();

glPushMatrix ();
   glTranslatef (d[0], d[1], d[2]);    // Move box down X axis.
   glScalef (scaleF, scaleF, scaleF);
   glRotatef (zAngle, 0,0,1);
   glRotatef (yAngle, 0,1,0);
   glRotatef (xAngle, 1,0,0);
   Draw_Box ();
glPopMatrix ();

glutSwapBuffers();
}

//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

int main (int argc, char **argv)
{
glutInit               (&argc, argv);
glutInitWindowSize     (900, 600);
glutInitWindowPosition (300, 300);
glutInitDisplayMode    (GLUT_DEPTH | GLUT_DOUBLE);

glutCreateWindow ("Orbital Font Demo");
glutDisplayFunc  (   redraw   );
glutKeyboardFunc (  Keyboard  );
//glutSpecialFunc  (Special_Keys);



glClearColor (0.1, 0.0, 0.1, 1.0);

glMatrixMode   (GL_PROJECTION);
gluPerspective (60, 1.5, 1, 10);
glMatrixMode   (GL_MODELVIEW);
glutPostRedisplay();

glutMainLoop   ();



return 1;
}

//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4

问题是我想将此代码复制到另一个C ++程序中,我使用openCV库来连接我的VGA摄像头。基于在相机前面执行的移动,我使用SVM模型对执行的移动进行分类。 我想使用SVM模型的输出,它基本上是一个整数值,并将其传递给openGL代码,以便在窗口中移动多维数据集。

在上面提到的代码中,此过程通过使用击键和隐式glKeyboardFunc函数来执行。为了将SVM模型的输出连接到上述代码的redraw函数,我应该使用哪些函数?

1 个答案:

答案 0 :(得分:1)

您应该使用glutIdle来检查是否有新框架。如果有,您应该使用glTexSubImage2D *。

更新新图像的纹理

* 您应该使用纹理来显示自定义图像。