c++ - 在opengl中寻找旋转图像的中心

标签 c++ opengl rotation

所以我有这段代码,它几乎在屏幕上绘制了各种 2D 纹理,尽管有多个 Sprite 必须从纹理 (spritesheet) 中“剖析”。问题是轮换工作不正常;当它旋转时,它不会在纹理的中心旋转,这正是我想要做的。我已将其缩小为翻译不正确:

    glTranslatef(x + sr->x/2 - sr->w/2,
                 y + sr->y/2 - sr->h/2,0);

    glRotatef(ang,0,0,1.f);

    glTranslatef(-x + -sr->x/2 - -sr->w/2,
                 -y + -sr->y/2 - -sr->h/2,0);

X 和 Y 是它被绘制到的位置,sheet rect 结构包含从纹理中绘制的 Sprite 的位置 X 和 Y,以及 w 和 h,它们是 Sprite 的宽度和高度' 从纹理。我尝试了各种其他公式,例如:

 glTranslatef(x, y, 0);

 The below three switching the negative sign to positive (x - y to x + y)

 glTranslatef(sr->x/2 - sr->w/2, sr->y/2 - sr->h/2 0 );

 glTranslatef(sr->x - sr->w/2, sr->y - sr->h/2, 0 );

 glTranslatef(sr->x - sr->w, sr->y - sr->w, 0 );

 glTranslatef(.5,.5,0);

这样说可能也有帮助:

glOrtho(0,screen_width,screen_height,0,-2,10);

正在使用中。

我尝试阅读各种教程,浏览各种论坛,询问各种人,但似乎没有有效的解决方案,也找不到任何有用的资源来向我解释如何找到中心图像以便将其转换为“(0,0)”。我对 OpenGL 还很陌生,所以很多东西我都需要一段时间才能消化。

这是整个函数:

void Apply_Surface( float x, float y, Sheet_Container* source, Sheet_Rect* sr , float ang = 0, bool flipx = 0, bool flipy = 0, int e_x = -1, int e_y = -1 ) {
 float imgwi,imghi;

 glLoadIdentity();
 glEnable(GL_TEXTURE_2D);
 glBindTexture(GL_TEXTURE_2D,source->rt());     
 // rotation
 imghi = source->rh();
 imgwi = source->rw();
 Sheet_Rect t_shtrct(0,0,imgwi,imghi);
 if ( sr == NULL ) // in case a sheet rect is not provided, assume it's width
                   //and height of texture with 0/0 x/y
    sr = &t_shtrct;

 glPushMatrix();

     // 
     int wid, hei;
     glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&wid);
     glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&hei);
     glTranslatef(-sr->x + -sr->w,
                 -sr->y + -sr->h,0);
     glRotatef(ang,0,0,1.f);

     glTranslatef(sr->x + sr->w,
                 sr->y + sr->h,0);

     // Yeah, out-dated way of drawing to the screen but it works for now.
     GLfloat tex[] = {
        (sr->x+sr->w * flipx) /imgwi, 1 - (sr->y+sr->h  *!flipy )/imghi,
        (sr->x+sr->w * flipx) /imgwi, 1 - (sr->y+sr->h  * flipy)/imghi,
        (sr->x+sr->w * !flipx) /imgwi, 1 - (sr->y+sr->h * flipy)/imghi,
        (sr->x+sr->w * !flipx) /imgwi, 1 - (sr->y+sr->h *!flipy)/imghi
     };
     GLfloat vertices[] = { // vertices to put on screen
          x,        (y + sr->h),
          x,         y,
         (x +sr->w), y,
         (x +sr->w),(y +sr->h)
     };
     // index array
     GLubyte index[6] = { 0,1,2, 2,3,0 };
     float fx = (x/(float)screen_width)-(float)sr->w/2/(float)imgwi;
     float fy = (y/(float)screen_height)-(float)sr->h/2/(float)imghi;

     // activate arrays
     glEnableClientState(GL_VERTEX_ARRAY);
     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
     // pass verteices and texture information
     glVertexPointer(2, GL_FLOAT, 0, vertices);
     glTexCoordPointer(2, GL_FLOAT, 0, tex);
     glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, index);

     glDisableClientState(GL_VERTEX_ARRAY);
     glDisableClientState(GL_TEXTURE_COORD_ARRAY);

 glPopMatrix();

 glDisable(GL_TEXTURE_2D);

}

工作表容器类:

class Sheet_Container {
    GLuint texture;
    int width, height;
public:
    Sheet_Container();
    Sheet_Container(GLuint, int = -1,int = -1);
    void Load(GLuint,int = -1,int = -1);
    float rw();
    float rh();
    GLuint rt();
};

工作表矩形类:

struct Sheet_Rect {
    float x, y, w, h;
    Sheet_Rect();
    Sheet_Rect(int xx,int yy,int ww,int hh);
};

图片加载函数:

Sheet_Container Game_Info::Load_Image(const char* fil) {
    ILuint t_id;
    ilGenImages(1, &t_id);
    ilBindImage(t_id);
    ilLoadImage(const_cast<char*>(fil));
    int width = ilGetInteger(IL_IMAGE_WIDTH), height = ilGetInteger(IL_IMAGE_HEIGHT);
    return Sheet_Container(ilutGLLoadImage(const_cast<char*>(fil)),width,height);
}

最佳答案

您的四边形(两个三角形)位于:

( x + sr->w / 2,  y + sr->h / 2 )

你需要将该点移动到原点,旋转,然后将其移回:

 glTranslatef ( (x + sr->w / 2.0f),  (y + sr->h / 2.0f), 0.0f); // 3rd
 glRotatef    (0,0,0,1.f);                                      // 2nd
 glTranslatef (-(x + sr->w / 2.0f), -(y + sr->h / 2.0f), 0.0f); // 1st

这就是我认为您被绊倒的地方。人们自然而然地认为 OpenGL 会按照它们出现的顺序(从上到下)应用变换,但事实并非如此。每次将两个矩阵相乘时,OpenGL 都会有效地交换操作数:

M1 x M2 x M3
~~~~~~~
  (1)
  ~~~~~~~~~~
      (2)

(1) M2 * M1
(2) M3 * (M2 * M1)   -->   M3 * M2 * M1  (row-major / textbook math notation)

这方面的技术术语是后乘法,它与 OpenGL 中矩阵的实现方式(列优先)有关。可以这么说,您通常应该从下到上阅读 glTranslatefglRotatefglScalef 等调用。

除此之外,您当前的轮换没有任何意义。

您是在告诉 GL 围绕轴旋转 0 度:<0,0,1>(换句话说就是 z 轴)。轴是正确的,但是 0 度 旋转不会做任何事情 ;)

关于c++ - 在opengl中寻找旋转图像的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23819986/

相关文章:

c++ - 无法显示 OpenGL 纹理

ios - 如何通知父 View Controller 有关模态视图 Controller 中屏幕方向的更改?

c++ - vector 下标超出范围 - 冒泡排序 - 改进

c++ - 如何在 C++ 中对包含动态数组的结构数组进行排序?

c++ - 从通过 MPI_Comm_spawn 产生的 child 重定向 stdout

c++ - 仅在 1 个轴上旋转对象

java - 在 Java 中旋转多边形

c++ - 推导的模板 arg 和自动 arg 之间有什么区别吗?

c++ - 为什么我们需要维护自己的矩阵来转换 Game 对象?

macos - Carbon 窗口(OS Lion、Mono)上的 OpenGL 3.2 上下文