1

我有一个基于 OpenGL GLUT(是的,我知道,GLUT 很古老)的 C++ 程序,它交替打印出一个蓝色矩形和一个红色正方形,位于 GLUT 窗口中用户单击鼠标的位置。这工作正常。

然而,当我试图修改上述程序以使上述形状在其初始动画后仍保留在画布上时,我遇到了令人难以置信的麻烦。

我创建了一个 Shape 类,它包含顶点数、颜色、坐标等信息。当程序一次只绘制一个形状时,该类似乎功能齐全。

为了解决多个形状一次的问题,我创建了一个std::list<Shape>链表。但是,当我通过该机制遍历链表时std::list<Shape>::iterator,对象似乎在内存中捆绑在一起。也就是说,对于链表中的每个索引,迭代都会产生完全相同的形状对象、坐标和所有对象。

我尝试了以下解决方案:

使链表 astd::list<Shape*>而不是 a std::list<Shape>

  • 通过使用堆进行对象分配Shape* my_shape = new Shape(params)
  • 并结合上述两种方法。

这是我的void display()GLUT 函数,以及相关的全局变量和类定义/声明:

class Shape
{

public:
    Shape();
    Shape(int, double[], int[]);
    int type; //0 = rectangle, 1 = circle, 2 = triangle
    int numVertices; //stores total number of vertices in the 2D object.
    double* vertexArray; //a dynamic array that stores each vertex's (x, y)-coordinates in alternating successive indices
    int* rgb; //an array that contains the 3 rgb values s.t. rgb = {r, g, b}
    double* center; //an array that contains the (x, y)-coordinates of the shape's center on the 2d plane.
    int velocity[2]; //a array of size 2 that holds the object's x-velocity in index 0 and the object's y-velocity in index 1

};

    //default Shape constructor
Shape::Shape()
{

}

//Shape constructor 
Shape::Shape(int shapeType, double vertices[], int color[]) //constructor for creating a stationary 2D shape
{
    type = shapeType;

    if (shapeType!=1) //as long as shape is NOT a circle, interpret the second constructor parameter as a list of vertices 
    {
        vertexArray = vertices;
    }

    rgb = color;

    if (shapeType==0) //shape is a rectangle
    {
        numVertices = 4;
    }
    else if(shapeType==1) //shape is a circle
    {
        //shape is a circle, therefore the second array param is in fact an array of size 2 containing the (x, y)-coordinates of the circle origin...
        center = vertices;
    }
    else if (shapeType==2) //shape is a triangle
    {
        numVertices = 3; 
    }
}


std::list<Shape> shapeList;
void my_display(void) 
{
  /* clear the buffer */
  glClear(GL_COLOR_BUFFER_BIT);


  //altFlag is just a value to allow alternating between rectangles/circles being printed

  if (altFlag==1)
  {
      printf("Drawing rectangle at: (%g, %g)\n", my_x, my_y);

      /*instantiate a Shape() object representing a blue rectangle*/
      int rgbColor[3] = {0, 0, 1};

      double vertices[4] = {my_x/window_w, my_y/window_h, my_x/window_w + my_rect_w, my_y/window_h + my_rect_h};

      Shape my_rectangle(0, vertices, rgbColor); //uses constructor (shape type, list of vertex coordinates, length of coordinate list, color)

      glColor3f((GLfloat)my_rectangle.rgb[0], (GLfloat)my_rectangle.rgb[1], (GLfloat)my_rectangle.rgb[2]) ; /* (Red, Green, Blue); so here we ask for Blue */
      glRectf(my_rectangle.vertexArray[0], my_rectangle.vertexArray[1], my_rectangle.vertexArray[2], my_rectangle.vertexArray[3]); //call to function to draw a rectangle
      altFlag=0;

      shapeList.push_front(my_rectangle);
  }
  else
  {
      /*instantiate a Shape() object representing a red circle*/
      int circleColor[3] = {1, 0, 0};
      double circleCenter[2] = {(my_x), (my_y)}; //{center x coord, center y coord}
      //Shape* my_circle = new Shape(1, circleCenter, circleColor); 
      Shape my_circle(1, circleCenter, circleColor);

      glColor3f(my_circle.rgb[0], my_circle.rgb[1], my_circle.rgb[2]);
      glCirclef(my_circle.center[0], my_circle.center[1]); //call to function to draw pseudocircle

      altFlag=1;

      shapeList.push_front(my_circle);
  }

  //iterate over shapeList, print out values of the rgb array.
  for (std::list<Shape>::iterator iter = shapeList.begin(); iter != shapeList.end(); iter++)
  {
      printf("%d, %d, %d\n", iter->rgb[0], iter->rgb[1], iter->rgb[2]);
  }

  glutSwapBuffers();

  return;

虽然这是作业的一部分,但问题与所使用的语言有关,而不是作为课程重点的图形库。

4

1 回答 1

2

您的参数化构造函数Shape执行此操作:

vertexArray = vertices;

vertices声明为的函数参数在哪里double vertices[]。这是语法糖double *vertices,即,您正在复制一个指针(因为数组在传递给函数时会衰减为指针)。

vertexArray被定义为类内部的指针。您甚至提到它是一个“动态数组”,但随后您使用分配在堆栈上的数组(内部的局部变量display())对其进行初始化。这使得指针悬空。

您将必须创建vertexArray一个数组并复制数据,而不仅仅是指针。像这样的东西:

class Shape
{
  // ...
  double vertexArray[4];
  // ...
};

Shape::Shape(int shapeType, double vertices[], int color[])
{
    type = shapeType;

    if (shapeType==0) //shape is a rectangle
    {
        numVertices = 4;
    }
    else if(shapeType==1) //shape is a circle
    {
        //shape is a circle, therefore the second array param is in fact an array of size 2 containing the (x, y)-coordinates of the circle origin...
        center = vertices;
    }
    else if (shapeType==2) //shape is a triangle
    {
        numVertices = 3; 
    }

    if (shapeType!=1) //as long as shape is NOT a circle, interpret the second constructor parameter as a list of vertices 
    {
        std::copy(vertices, vertices + numVertices, vertexArray);
    }
}

当然,类比也是color如此。

额外建议

当然,这段代码使用std::vector或类似的东西代替普通数组会好得多,使用 andenum代替intforshapeType等。我建议你拿起一本好的 C++ 书

于 2013-08-29T11:55:40.873 回答