Thursday, 15 May 2014

c++ - OpenGL: How to scale and keep its location(X, Y) where it was before -


I do not know the correct word to describe it, so I can not find it in Google, I want the object to be Scale, in particular, put it on your XY coordinates on 2D rectangle.

My current solution is:

  glTranslatef (x, y, 0); GlScalef (scaleX, scaleY, 0); GlTranslatef (-x, -y, 0); Draw (x, y, width, height, texture);   

I pretend to glrcate as a rotation on my center as glrcate, it works to some extent, but when I scale the other values, it is a bit adjusted on its coordination Can see

And also, which is better to do, Apply variables like X, Y on glTranslate or just variables, I do not see any difference while switching from them, but I "Pushing and Matriss Popping "Concerning connecting to the scene and the performance of other programs will affect the behavior of other objects, because I will use a lot of rectangles.

Update:

So you can imagine how I just want to receive

With these beliefs, you can achieve your goal by resetting the modelview stack, and then go to the object space before scaling. So you are really close, but depending on what you did with modelwall matrix first, you can experience variations in the result.

The following code should do you:

  glPushMatrix (); GlLoadIdentity (); GlTranslatef (X, Y, 0); // now in "object space" glScalef (scaleX, scaleY, 0); Draw (0, 0, width, height, texture); GlPopMatrix ();   

I do not know what the DRA function does, but I think it's only a set of glVertex and GlTexCoord call, that is not connected to this modelview matrix, else my code can also break it.

Note that if you call wrap all the object rendering with glPushMatrix and glPopMatrix , you do not need to call glLoadIdentity And it can also be a counter productive, for example, if you organize your object data in the form of a hierarchy, each sub object is placed in a relatively small position in your container, you will need to call that call completely The object must be removed from the The hierarchy must be recursively propounded.

One of the more worrying skills is to save and restore the matrix is ​​quite expensive. Given that your question code is using instant mode, I think that this was not your main concern. Object changes would definitely be faster to implement by hand (which, by computing it with its matrix library) , And submit them to the OpenLeal model without any calls without the Hertipulan routine. However, it should be covered with second (more math-oriented) questions and answers.

No comments:

Post a Comment