简体   繁体   中英

Texture 2D Rotating to a point

Is there anyway I can rotate my 2D Texture image rotating to a point? I've tried the glRotate function, It rotates and moves the object. I'm looking for a function/way for rotating my textures to a point by not moving them.

Here's my draw code:

static void Draw2DTexture(int textureid, float x1, float y1, float width, float height, Color color)
{
    float x2 = x1 + width;
    float y2 = y1 + height;
    RectangleF rect;
    rect = new RectangleF(0, 0, 1, 1);
    GL.PushAttrib(AttribMask.ColorBufferBit);
    GL.Color3(color);
    GL.BindTexture(TextureTarget.Texture2D, textureid);
    GL.Enable(EnableCap.Texture2D);
    GL.Disable(EnableCap.DepthTest);
    GL.Rotate(35, 0f, 0f, 1f);
    GL.Begin(BeginMode.Quads);
    GL.TexCoord2(rect.Right, rect.Bottom); GL.Vertex2(x2, y2);
    GL.TexCoord2(rect.Right, rect.Top); GL.Vertex2(x2, y1);
    GL.TexCoord2(rect.Left, rect.Top); GL.Vertex2(x1, y1);
    GL.TexCoord2(rect.Left, rect.Bottom); GL.Vertex2(x1, y2);
    GL.End();
    GL.Enable(EnableCap.DepthTest);
    GL.PopAttrib();
}

Draw2DTexture(5, 300f, 300f, 256f, 512f, Color.White);

"It rotates and moves the object"

I'm not sure what rotating to a point is, but the problem you mention here is about having the proper center of rotation.

Whenever you apply a rotation to an object, it rotates it about the origin . This is great, but what happens when your object isn't centered about the origin? You need to translate your object to the origin, rotate it, and translate it back to its original position.

  1. Object is currently centered at (x,y)
  2. Translate object by (-x,-y)
  3. Object is now centered at (0,0)
  4. Perform rotation
  5. Translate object by (x,y)
  6. Object centered again at (x,y)

This will rotate in place.

What about rotation around a point ? Instead of translating to the origin via the rotating object's position, translate by the point around which you want to rotate

  1. Object is currently centered at (x,y)
  2. Center object about which to rotate is at (a,b)
  3. Translate object by (-a,-b)
  4. Perform rotation
  5. Translate object by (a,b)
  6. Object has now rotated around (a,b)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM