Results 1 to 6 of 6

Thread: How to move Rectangle from one location to other location?

  1. #1
    Junior Member
    Join Date
    Mar 2008
    Posts
    5

    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?

  2. #2
    Junior Member
    Join Date
    Mar 2008
    Posts
    15
    hello,
    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 :


    Code :
            glMatrixMode (GL_MODELVIEW);
    	glLoadIdentity();
     
    	//first object
    	glPushMatrix();
    	glTranslatef();
    	glRotatef();
    	//rendering
            glPopMatrix();
     
    	//second object
    	glPushMatrix();
    	glTranslatef();
    	glRotatef();
    	//rendering
            glPopMatrix();

    Try it ...

  3. #3
    Junior Member
    Join Date
    Mar 2008
    Posts
    5
    Hello,

    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.

    4. Render


    But I observed that both rectangles get moved.

  4. #4
    Senior Member
    Join Date
    May 2006
    Posts
    353
    Quote Originally Posted by mnarkhede
    6. Push the Matrix to stack again.
    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
    7. ...
    Georg Kolling, Imagination Technologies
    Please ask questions specific to PowerVR hardware or SDKs on the PowerVR Insider Forum
    DevTech@imgtec.com | http://www.powervrinsider.com

  5. #5
    Junior Member
    Join Date
    Mar 2008
    Posts
    5
    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?

  6. #6
    Senior Member
    Join Date
    May 2006
    Posts
    353
    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.
    Georg Kolling, Imagination Technologies
    Please ask questions specific to PowerVR hardware or SDKs on the PowerVR Insider Forum
    DevTech@imgtec.com | http://www.powervrinsider.com

Similar Threads

  1. Access violation reading location
    By guy.brush in forum OpenCL
    Replies: 0
    Last Post: 04-26-2010, 01:03 AM
  2. Undefined buffer object location ?
    By pmdinu in forum OpenCL
    Replies: 3
    Last Post: 07-21-2009, 01:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •