HGE-OpenGL移植Gfx_SetTransform函数

时间:2011-04-15 07:45:22

标签: opengl graphics game-engine

首先 - 我是图形编程微笑。

我支持基于移植版HGE的游戏,该版本不包含 Gfx_SetTransform 的OpenGL版本。 我找到了几个HGE-Opengl移植的例子,但是任何一个例子都有这个功能。

代码:

void CALL HGE_Impl::Gfx_SetTransform(float x, float y, float dx, float dy, float rot, float hscale, float vscale)
{
   D3DXMATRIX tmp;

   if(vscale==0.0f) D3DXMatrixIdentity(&matView);
   else
   {
      D3DXMatrixTranslation(&matView, -x, -y, 0.0f);
      D3DXMatrixScaling(&tmp, hscale, vscale, 1.0f);
      D3DXMatrixMultiply(&matView, &matView, &tmp);
      D3DXMatrixRotationZ(&tmp, -rot);
      D3DXMatrixMultiply(&matView, &matView, &tmp);
      D3DXMatrixTranslation(&tmp, x+dx, y+dy, 0.0f);
      D3DXMatrixMultiply(&matView, &matView, &tmp);
   }

   _render_batch();
   pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
}

那么,任何人都可以帮助移植 Gfx_SetTransform 或提出建议(可能是一些我应该寻找的简单示例或OpenGL函数)? 感谢。

1 个答案:

答案 0 :(得分:1)

使用OpenGL版本不包括3版本,您可以使用以下内容替换它:

void GL2_SetTransform(float x, float y, float dx, float dy, float rot, float hscale, float vscale)
{
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity(); // we start off a identity matrix

   // Instead of testing for 0, you should test for some
   // threshold, to increase numerical stability
   if( fabs(vscale) >= 1e-7 ) {
      glTranslatef(-x, -y, 0.0f);
      glScalef(hscale, vscale, 1.0f);
      glRotatef(-rot, 0., 0., 1.);
      glTranslatef(x+dx, y+dy, 0.0f);
   }
}

OpenGL-3弃用了矩阵操作函数,因此代码看起来与DirectX版本几乎相同(我改变了一些东西以提高稳定性):

typedef float mat4x4[4][4];
// OpenGL uses column major mode, so the first
// index selects column, the second row - this is
// opposite to the usual C notation.

// The following functions must be implemented as well.
// But they're easy enough.

// function to set the matrix to identity
void mat4x4_identity(mat4x4 *M);

// those functions transform the matrix in-place,
// i.e. no temporaries needed.
void mat4x4_rotX(mat4x4 *M, float angle);
void mat4x4_rotY(mat4x4 *M, float angle);
void mat4x4_rotZ(mat4x4 *M, float angle);

void mat4x4_scale(mat4x4 *M, float x, float y, float z);

void mat4x4_translate(mat4x4 *M, float x, float y, float z);


void GL3_SetTransform(float x, float y, float dx, float dy, float rot, float hscale, float vscale)
{
   mat4x4 view;
   mat4x4_identity(view);

   if( fabs(vscale) >= 1e-8 ) {
      mat4x4_translate(view, -x, -y, 0.0f);
      mat4x4_scale(view, hscale, vscale, 1.0f);
      mat4x4_rotZ(view, -rot);
      mat4x4_translate(view, x+dx, y+dy, 0.0f);
   }

   _render_batch();
   // get_view_uniform_location returns the handle for the currently loaded
   // shader's modelview matrix' location. Application specific, so you've to
   // come up with that yourself.
   glUniformMatrix4fv(get_view_uniform_locaton(), 1, false, view);
}

我在这里提出了矩阵操作函数的源代码: http://pastebin.com/R0PHTW0M 但是,他们没有使用完全相同的命名方案。