Opengl 在每次点击时添加一个新对象?

Opengl add a new object in each click?

void onClick(int button, int state, int x, int y) {
  if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    drawHouse(x,y);
}

我对 opengl onclick 函数有疑问。我画了一个物体(原始房屋),我想在单击鼠标时显示它。我该怎么做?

我的老师给出了这个命令:“在用户按下鼠标左键后添加一个新对象,在第一部分中定义。每次单击都会在单击的位置添加一个新对象。最多 10可以在屏幕上创建对象。然后,每次单击后,一个新对象应替换第一个对象。"

一个可能的解决方案是将鼠标位置存储在一个长度为 10 的数组中。每次单击都会向该数组添加一个新条目。如果数组已满,条目将被覆盖:

#define MAX_OBJ 10

int pos_x[MAX_OBJ], pos_y[MAX_OBJ];
int count  = 0;
int next_i = 0;

void onClick(int button, int state, int x, int y)
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
       pos_x[next_i] = x;
       pos_y[next_i] = y;
       next_i ++;
       if ( next_i == MAX_OBJ ) next_i = 0;
       if ( count < MAX_OBJ )   count ++;
    }
}

在主循环中,您可以将对象绘制到已知位置:

for( int i = 0; i < MAX_OBJ; ++ i )
    drawHouse( pos_x[i], pos_y[i] );