矩阵乘法 - glTranslate 自己的实现?错误的矩阵

发布于 2024-10-15 19:26:23 字数 2911 浏览 3 评论 0原文

假设我有以下投影矩阵(使用单位矩阵上的 gluPerspective(40, 1.0, 0.2, 200.0); 计算得出):

2.75, 0.00, 0.00, 0.00, 
0.00, 2.75, 0.00, 0.00, 
0.00, 0.00, -1.00, -1.00, 
-0.00, -0.00, -0.40, -0.00

并且想要执行 glTranslatef(0.0, 0.0, - 10.0); 该矩阵上的命令。使用 openGL ES 1.1 函数,我收到了(在我看来这似乎是正确的):

2.75, 0.00, 0.00, 0.00, 
0.00, 2.75, 0.00, 0.00, 
0.00, 0.00, -1.00, -1.00, 
0.00, 0.00, 9.62, 10.00 

但是如果我使用自己的实现(openGL ES 2.0),我会得到一些奇怪的行为:

2.75, 0.00, 0.00, 0.00, 
0.00, 2.75, 0.00, 0.00, 
0.00, 0.00, 9.00, -1.00, 
0.00, 0.00, -0.40, 0.00

我的代码与 Apples openGLES1/2 示例和此站点非常相似< a href="http://iphonedevelopment.blogspot.com/2009/06/opengl-es-from-ground-up-part-7_04.html" rel="nofollow">http://iphonedevelopment.blogspot.com/ 2009/06/opengl-es-from-ground-up-part-7_04.html:

void Matrix::translate(GLfloat xTranslate, GLfloat yTranslate, GLfloat zTranslate){
    GLfloat matrix[16];

    matrix[0] = matrix[5] =  matrix[10] = matrix[15] = 1.0;
    matrix[1] = matrix[2] = matrix[3] = matrix[4] = 0.0;
    matrix[6] = matrix[7] = matrix[8] = matrix[9] = 0.0;    
    matrix[11] = 0.0;
    matrix[12] = xTranslate;
    matrix[13] = yTranslate;
    matrix[14] = zTranslate;

    multiMatrix(matrix);
}
void Matrix::multiMatrix(const GLfloat* a){
    GLfloat* matrix;
    GLfloat b[16];
    matrix = currentMatrix(); //gets the ProjectionMatrix as GLfloat*
    copyMatrix(matrix, b);
    matrix[0]  = a[0] * b[0]  + a[4] * b[1]  + a[8] * b[2]   + a[12] * b[3];
    matrix[1]  = a[1] * b[0]  + a[5] * b[1]  + a[9] * b[2]   + a[13] * b[3];
    matrix[2]  = a[2] * b[0]  + a[6] * b[1]  + a[10] * b[2]  + a[14] * b[3];
    matrix[3]  = a[3] * b[0]  + a[7] * b[1]  + a[11] * b[2]  + a[15] * b[3];

    matrix[4]  = a[0] * b[4]  + a[4] * b[5]  + a[8] * b[6]   + a[12] * b[7];
    matrix[5]  = a[1] * b[4]  + a[5] * b[5]  + a[9] * b[6]   + a[13] * b[7];
    matrix[6]  = a[2] * b[4]  + a[6] * b[5]  + a[10] * b[6]  + a[14] * b[7];
    matrix[7]  = a[3] * b[4]  + a[7] * b[5]  + a[11] * b[6]  + a[15] * b[7];

    matrix[8]  = a[0] * b[8]  + a[4] * b[9]  + a[8] * b[10]  + a[12] * b[11];
    matrix[9]  = a[1] * b[8]  + a[5] * b[9]  + a[9] * b[10]  + a[13] * b[11];
    matrix[10] = a[2] * b[8]  + a[6] * b[9]  + a[10] * b[10] + a[14] * b[11];
    matrix[11] = a[3] * b[8]  + a[7] * b[9]  + a[11] * b[10] + a[15] * b[11];

    matrix[12] = a[0] * b[12] + a[4] * b[13] + a[8] * b[14]  + a[12] * b[15];
    matrix[13] = a[1] * b[12] + a[5] * b[13] + a[9] * b[14]  + a[13] * b[15];
    matrix[14] = a[2] * b[12] + a[6] * b[13] + a[10] * b[14] + a[14] * b[15];
    matrix[15] = a[3] * b[12] + a[7] * b[13] + a[11] * b[14] + a[15] * b[15];

}
void Matrix::copyMatrix(const GLfloat* source, GLfloat* destination){
    for(int i=0; i<16; i++){
        destination[i] = source[i];   
    }
}

我调试了两天,现在知道我错过了什么......有什么想法吗?

Assuming I have the following Projection Matrix (its computed using gluPerspective(40, 1.0, 0.2, 200.0); on the identity matrix):

2.75, 0.00, 0.00, 0.00, 
0.00, 2.75, 0.00, 0.00, 
0.00, 0.00, -1.00, -1.00, 
-0.00, -0.00, -0.40, -0.00

and want to do an glTranslatef(0.0, 0.0, -10.0); command on this matrix. With openGL ES 1.1 function I recieve (that seems correct in my opinion):

2.75, 0.00, 0.00, 0.00, 
0.00, 2.75, 0.00, 0.00, 
0.00, 0.00, -1.00, -1.00, 
0.00, 0.00, 9.62, 10.00 

But if I use my own implementation (openGL ES 2.0), I got some strange behavior:

2.75, 0.00, 0.00, 0.00, 
0.00, 2.75, 0.00, 0.00, 
0.00, 0.00, 9.00, -1.00, 
0.00, 0.00, -0.40, 0.00

My code is quite similiar to Apples openGLES1/2 Example and this site http://iphonedevelopment.blogspot.com/2009/06/opengl-es-from-ground-up-part-7_04.html:

void Matrix::translate(GLfloat xTranslate, GLfloat yTranslate, GLfloat zTranslate){
    GLfloat matrix[16];

    matrix[0] = matrix[5] =  matrix[10] = matrix[15] = 1.0;
    matrix[1] = matrix[2] = matrix[3] = matrix[4] = 0.0;
    matrix[6] = matrix[7] = matrix[8] = matrix[9] = 0.0;    
    matrix[11] = 0.0;
    matrix[12] = xTranslate;
    matrix[13] = yTranslate;
    matrix[14] = zTranslate;

    multiMatrix(matrix);
}
void Matrix::multiMatrix(const GLfloat* a){
    GLfloat* matrix;
    GLfloat b[16];
    matrix = currentMatrix(); //gets the ProjectionMatrix as GLfloat*
    copyMatrix(matrix, b);
    matrix[0]  = a[0] * b[0]  + a[4] * b[1]  + a[8] * b[2]   + a[12] * b[3];
    matrix[1]  = a[1] * b[0]  + a[5] * b[1]  + a[9] * b[2]   + a[13] * b[3];
    matrix[2]  = a[2] * b[0]  + a[6] * b[1]  + a[10] * b[2]  + a[14] * b[3];
    matrix[3]  = a[3] * b[0]  + a[7] * b[1]  + a[11] * b[2]  + a[15] * b[3];

    matrix[4]  = a[0] * b[4]  + a[4] * b[5]  + a[8] * b[6]   + a[12] * b[7];
    matrix[5]  = a[1] * b[4]  + a[5] * b[5]  + a[9] * b[6]   + a[13] * b[7];
    matrix[6]  = a[2] * b[4]  + a[6] * b[5]  + a[10] * b[6]  + a[14] * b[7];
    matrix[7]  = a[3] * b[4]  + a[7] * b[5]  + a[11] * b[6]  + a[15] * b[7];

    matrix[8]  = a[0] * b[8]  + a[4] * b[9]  + a[8] * b[10]  + a[12] * b[11];
    matrix[9]  = a[1] * b[8]  + a[5] * b[9]  + a[9] * b[10]  + a[13] * b[11];
    matrix[10] = a[2] * b[8]  + a[6] * b[9]  + a[10] * b[10] + a[14] * b[11];
    matrix[11] = a[3] * b[8]  + a[7] * b[9]  + a[11] * b[10] + a[15] * b[11];

    matrix[12] = a[0] * b[12] + a[4] * b[13] + a[8] * b[14]  + a[12] * b[15];
    matrix[13] = a[1] * b[12] + a[5] * b[13] + a[9] * b[14]  + a[13] * b[15];
    matrix[14] = a[2] * b[12] + a[6] * b[13] + a[10] * b[14] + a[14] * b[15];
    matrix[15] = a[3] * b[12] + a[7] * b[13] + a[11] * b[14] + a[15] * b[15];

}
void Matrix::copyMatrix(const GLfloat* source, GLfloat* destination){
    for(int i=0; i<16; i++){
        destination[i] = source[i];   
    }
}

Im debugging this for 2 days now and have now idea what Im missing... Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖阳 2024-10-22 19:26:23

我总是参考 这个出色的实现。该代码很容易理解并且可以正常工作。

他们的 translate 方法粘贴在这里:

translate: function (tx, ty, tz) {
    this.elements[3*4+0] += this.elements[0*4+0] * tx + this.elements[1*4+0] * ty + this.elements[2*4+0] * tz;
    this.elements[3*4+1] += this.elements[0*4+1] * tx + this.elements[1*4+1] * ty + this.elements[2*4+1] * tz;
    this.elements[3*4+2] += this.elements[0*4+2] * tx + this.elements[1*4+2] * ty + this.elements[2*4+2] * tz;
    this.elements[3*4+3] += this.elements[0*4+3] * tx + this.elements[1*4+3] * ty + this.elements[2*4+3] * tz;

    return this;
},

I always refer to this excellent implementation. The code is easy to follow and it just works.

Their translate method is pasted here:

translate: function (tx, ty, tz) {
    this.elements[3*4+0] += this.elements[0*4+0] * tx + this.elements[1*4+0] * ty + this.elements[2*4+0] * tz;
    this.elements[3*4+1] += this.elements[0*4+1] * tx + this.elements[1*4+1] * ty + this.elements[2*4+1] * tz;
    this.elements[3*4+2] += this.elements[0*4+2] * tx + this.elements[1*4+2] * ty + this.elements[2*4+2] * tz;
    this.elements[3*4+3] += this.elements[0*4+3] * tx + this.elements[1*4+3] * ty + this.elements[2*4+3] * tz;

    return this;
},
谁的新欢旧爱 2024-10-22 19:26:23

从您的代码中很难看出,但是您打算对矩阵使用行顺序还是列顺序?行顺序会更常见,但您正在创建一个列顺序翻译矩阵。平移矩阵的平移 3 向量从右上角开始沿着最右列运行,而不是沿着底行运行。

在行排序矩阵中,行是连续的块,因此前四个数字是顶行。在按列排序的矩阵中,列是连续的块,因此前四个数字是第一列。

如果我正确地阅读了您的代码,那么看起来您的 multiMatrix 函数正在使用行顺序将参数矩阵与常设类矩阵右乘。如果矩阵是按行排序的,那么这对于翻译来说是正确的,因此您需要转置正在创建的翻译矩阵。

It's a bit hard to tell from your code, but are you intending to use row-order or column-order for your matrices? Row-order would be more common, but you are creating a column-order translation matrix. A translation matrix has the translation 3-vector running down the right-most column from the top-right corner, not along the bottom row.

In a row-ordered matrix, the rows are contiguous blocks so the first four numbers are the top row. In a column-ordered matrix, the columns are contiguous blocks so the first four numbers are the first column.

If I'm reading your code correctly, then it looks like your multiMatrix function is right-multiplying the parameter matrix against the standing class matrix, using row-order. That would be correct for translate if the matrix was row-ordered, so you need to transpose the translation matrix you are creating.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文