Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to succesfully create a std::list of unique objects
    primarykey
    data
    text
    <p>I have an OpenGL GLUT-based (yes, I know, GLUT is ancient) C++ program that alternatingly prints out a blue rectangle and a red square, located at the point in the GLUT window where the user clicks the mouse. This is working fine.</p> <p>I encountered a mind-boggling amount of trouble, however, when I attempted to modify the above program so that the aforementioned shapes REMAIN on the canvas after their initial animation. </p> <p>I have created a Shape class which holds such information as the number of vertices, color, coordinates, etc. The class seems fully functional when the program is only drawing one shape at a time. </p> <p>In order to solve the multiple-shapes-at-once problem, I created a <code>std::list&lt;Shape&gt;</code> linked list. However, when I iterate through the linked list via the <code>std::list&lt;Shape&gt;::iterator</code> mechanism, the objects appear to be tied together in memory. That is, iteration yields the exact same shape object, coordinates and all, for each index in the Linked List.</p> <p>I have tried the following solutions:</p> <p>making the linked list a <code>std::list&lt;Shape*&gt;</code> instead of a <code>std::list&lt;Shape&gt;</code>,</p> <ul> <li>utilizing the heap for object allocation via <code>Shape* my_shape = new Shape(params)</code>,</li> <li>and combining the the two above methods.</li> </ul> <p>Here is my <code>void display()</code> GLUT function, along with the relevant global variables AND the class definition/declaration:</p> <pre><code>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&lt;Shape&gt; 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&lt;Shape&gt;::iterator iter = shapeList.begin(); iter != shapeList.end(); iter++) { printf("%d, %d, %d\n", iter-&gt;rgb[0], iter-&gt;rgb[1], iter-&gt;rgb[2]); } glutSwapBuffers(); return; </code></pre> <p>While this is part of an assignment, the question pertains to the language being used, as opposed to the graphics library which is the focus of the course. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload