为什么我的转换矩阵需要转置? [英] Why does my translation matrix needs to be transposed?

查看:427
本文介绍了为什么我的转换矩阵需要转置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenGL的小型图形引擎,我有一些问题与我的翻译矩阵。我使用OpenGL 3.3,GLSL和C ++。
情况是这样的:我定义了一个小的立方体,我想在屏幕上呈现。立方体使用它自己的坐标系,所以我创建了一个模型矩阵,以便能够转换立方体。为了使自己更容易一点,我开始只使用一个翻译矩阵作为多维数据集的模型矩阵,经过一些编码我已经设法使一切工作,立方体出现在屏幕上。没有什么都特别,但有一件事我的翻译矩阵,我觉得有点奇怪。

现在据我所知,一个翻译矩阵定义如下:

  1,0,0,x 
0,1,0,y
0,0 ,1,z
0,0,0,1

为了我。当我以这种方式定义我的翻译矩阵时,屏幕上没有任何东西出现。它只有在我这样定义我的翻译矩阵时才有效:

  1,0,0,0 
0,1 ,0,0
0,0,1,0
x,y,z,1


$ b b

现在我已经在我的代码几次,以找出为什么是这种情况,但我似乎不知道为什么,或者我只是错误,做一个翻译矩阵需要定义像转置一个在这上面?



我的矩阵被定义为从左到右,从上到下的一维数组。



这是我的一些代码可能有帮助:

  //这是在多维数据集渲染之前调用
void DisplayObject :: updateMatrices()
{
modelMatrix = identityMatrix();
modelMatrix = modelMatrix * translateMatrix(xPos,yPos,zPos);

/ * update modelview-projection matrix * /
mvpMatrix = modelMatrix *(* projMatrix);
}

//这将创建我的转换矩阵,使多维数据集消失
const Matrix4 translateMatrix(float x,float y,float z)
{
Matrix4 tranMatrix = identityMatrix();

tranMatrix.data [3] = x;
tranMatrix.data [7] = y;
tranMatrix.data [11] = z;

return Matrix4(tranMatrix);
}

这是我简单的测试顶点着色器:

  #version 150核心

在vec3 vPos中;

uniform mat4 mvpMatrix;

void main()
{
gl_Position = mvpMatrix * vec4(vPos,1.0);
}



我也做过测试检查我的矩阵乘法是否有效。
I * randomMatrix仍然只是randomMatrix



我希望你们可以帮助。
感谢



编辑:



这是我将矩阵数据发送到OpenGL的方法:

  void DisplayObject :: render()
{
updateMatrices

glBindVertexArray(vaoID);
glUseProgram(progID);
glUniformMatrix4fv(glGetUniformLocation(progID,mvpMatrix),1,GL_FALSE,& mvpMatrix.data [0]);
glDrawElements(GL_TRIANGLES,bufferSize [index],GL_UNSIGNED_INT,0);
}

mvpMatrix.data是std :: vector:

解决方案

对于OpenGL

  1,0,0 ,0 
0,1,0,0
0,0,1,0
x,y,z,1

是正确的转换矩阵。
为什么? Opengl使用列主题矩阵排序。这是您最初呈现的矩阵的移调,它在 row-major 顺序中。



请参阅:http://www.mindcontrol.org/~hplus/graphics/matrix-layout.html


I'm working on a small graphics engine using OpenGL and I'm having some issues with my translation matrix. I'm using OpenGL 3.3, GLSL and C++. The situation is this: I have defined a small cube which I want to render on screen. The cube uses it's own coordinate system, so I created a model matrix to be able to transform the cube. To make it myself a bit easier I started out with just a translation matrix as the cube's model matrix and after a bit of coding I've managed to make everything work and the cube appears on the screen. Nothing all to special, but there is one thing about my translation matrix that I find a bit odd.

Now as far as I know, a translation matrix is defined as follows:

1, 0, 0, x
0, 1, 0, y
0, 0, 1, z
0, 0, 0, 1

However, this does not work for me. When I define my translation matrix this way, nothing appears on the screen. It only works when I define my translation matrix like this:

1, 0, 0, 0
0, 1, 0, 0
0, 0, 1, 0
x, y, z, 1

Now I've been over my code several times to find out why this is the case, but I can't seem to find out why or am I just wrong and does a translation matrix needs to be defined like the transposed one here above?

My matrices are defined as a one-dimensional array going from left to right, top to bottom.

Here is some of my code that might help:

//this is called just before cube is being rendered
void DisplayObject::updateMatrices()
{
    modelMatrix = identityMatrix();
    modelMatrix = modelMatrix * translateMatrix( xPos, yPos, zPos );

    /* update modelview-projection matrix */
    mvpMatrix = modelMatrix * (*projMatrix);
}

//this creates my translation matrix which causes the cube to disappear
const Matrix4 translateMatrix( float x, float y, float z )
{
    Matrix4 tranMatrix = identityMatrix();

    tranMatrix.data[3]  = x;
    tranMatrix.data[7]  = y;
    tranMatrix.data[11] = z;

    return Matrix4(tranMatrix);
}

This is my simple test vertex shader:

#version 150 core

in vec3 vPos;

uniform mat4 mvpMatrix;

void main()
{
    gl_Position = mvpMatrix * vec4(vPos, 1.0);
}

I've also did tests to check if my matrix multiplication works and it does. I * randomMatrix is still just randomMatrix

I hope you guys can help. Thanks

EDIT:

This is how I send the matrix data to OpenGL:

void DisplayObject::render()
{
    updateMatrices();

    glBindVertexArray(vaoID);
    glUseProgram(progID);
    glUniformMatrix4fv( glGetUniformLocation(progID, "mvpMatrix"), 1, GL_FALSE, &mvpMatrix.data[0] );
    glDrawElements(GL_TRIANGLES, bufferSize[index], GL_UNSIGNED_INT, 0);
}

mvpMatrix.data is a std::vector:

解决方案

For OpenGL

1, 0, 0, 0
0, 1, 0, 0
0, 0, 1, 0
x, y, z, 1

Is the correct Translation Matrix. Why? Opengl Uses column-major matrix ordering. Which is the Transpose of the Matrix you initially presented, which is in row-major ordering. Row major is used in most math text-books and also DirectX, so it is a common point of confusion for those new to OpenGL.

See: http://www.mindcontrol.org/~hplus/graphics/matrix-layout.html

这篇关于为什么我的转换矩阵需要转置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆