How to move Rectangle from one location to other location?

I am trying to move existing rectangle to other location.
It can be done using glLoadIdentity and glTranslatef.

But if there are 2-3 rectangles on the screen and only one of them is to be moved then what should be done?

If glLoadIdentity and glTranslatef are used then all rectangles get moved.
Please tell me how it can be done?

I am not sure of what I am telling to you but the two functions glPushMatrix() and glPopMatrix() do what you want no ?

For example, you use it like this :

        glMatrixMode (GL_MODELVIEW);

	//first object

	//second object

Try it …


Thanks for replying

But suppose I have situation like given below:

  1. Draw a rectangle of 50X50 at 10,10

  2. Draw second rectangle of 50X50 at 100,100

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

  4. 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.

  1. Draw a rectangle of 50X50 at 10,10

  2. Push matrix to stack

  3. Draw second rectangle of 50X50 at 100,100

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

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

  6. Push the Matrix to stack again.

  7. Render

But I observed that both rectangles get moved.

Why push here? For every PushMatrix there needs to be a PopMatrix in your code otherwise the matrix stack will overflow.

And if your rectangles are not positioned relative to each other, it’s best to translate them independently:

  1. glLoadIdentity
  2. glTranslatef
  3. Draw first rectangle
  4. glLoadIdentity
  5. glTranslatef
  6. Draw second rectangle

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.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.