Move first rectangle which is at (100,100) to (200,200)

Render

It can be possible using glPushMatrix and glPopMatrix but I don’t understand how it can be done.

I tired to push matrix of first rectangle to stack and then to pop matrix and
move the rectangle using glTranslate and again push matrix of the rectangle to stack as below.

Draw a rectangle of 50X50 at 10,10

Push matrix to stack

Draw second rectangle of 50X50 at 100,100

Pop the matrix.( Matrix of first rectangle will available) .

Move first rectangle which is at (100,100) to (200,200)

Sorry, I did not explain the exact situation. There are 3-4 rectangles, plus some bitmaps and others like triangles on the screen.

There is a requirement like after receiving any event or if key is pressed then, only particular one rectangle should move by given offset.

I was thinking that while drawing the rectangle the matrix of the rectangle can be pushed to stack. When event is generated the matrix matrix will be poped and moved by required offset and new matrix is pushed to the stack again. So that for next event the rectangle will be moved from last location.

But still there is problem if application needs more matrix to be pushed then it will be difficult to handle.

I am confused. Is there any way so that we can specify the matrix of the rectangle and translate that matrix ultimately rectangle?

I’m not sure why you’d want to use the matrix stack at all. It’s useful if you have object hierarchies where objects are placed relative to a parent node, but not for independently positioned objects.

Surely you’re storing the position of each rectangle. So whenever you want to draw a rectangle, you call glLoadIdentity and then apply the translation according to the rectangle’s position.

Though for individual rectangles it may be even cheaper to directly modify the vertex data.