glm inverse示例

示例

  • 片段
glm::vec3 vec3var = glm::vec3(1.00000f, 2.000000f, 3.00000f);
glm::vec4 vec4var = glm::vec4(vec3var, 1.0f);

glm::mat4 view = glm::lookAt(
       glm::vec3(0.f, 0.f, 10.f),        // Camera position
       glm::vec3(0, 0, 0),               // looks at the origin
       glm::vec3(0, 1, 0)                // Head up
   );

glm::mat4 project = glm::perspective(45.0f, 1920.f / 1080, 1.f, 100.f);

glm::mat4 model = glm::mat4(1.0f);

printf("view:\n");
for (int i=0 ; i < 4; i++)
{
    for (int j = 0; j < 4; j++)
    {
        printf("%f ", view[i][j]);
    }
    printf("\n");
}

printf("project:\n");
for (int i=0 ; i < 4; i++)
{
    for (int j = 0; j < 4; j++)
    {
        printf("%f ", project[i][j]);
    }
    printf("\n");
}

glm::vec4 resultRight = project * view * model * vec4var;
printf("resultRight=vec4(%f,%f,%f,%f)\n", resultRight[0], resultRight[1], resultRight[2], resultRight[3]);

glm::vec4 resultRightInv = glm::inverse(project * view * model) * resultRight;
printf("resultRightInv=vec4(%f,%f,%f,%f)\n", resultRightInv[0], resultRightInv[1], resultRightInv[2], resultRightInv[3]);

glm::vec4 resultLeft = vec4var * model *  view * project;
printf("resultLeft=vec4(%f,%f,%f,%f)\n", resultLeft[0], resultLeft[1], resultLeft[2], resultLeft[3]);

glm::vec4 resultLeftInv = resultLeft * glm::inverse(model *  view * project);
printf("resultLeftInv=vec4(%f,%f,%f,%f)\n", resultLeftInv[0], resultLeftInv[1], resultLeftInv[2], resultLeftInv[3]);
  • 结果
view:
1.000000 0.000000 -0.000000 0.000000
-0.000000 1.000000 -0.000000 0.000000
0.000000 0.000000 1.000000 0.000000
-0.000000 -0.000000 -10.000000 1.000000
project:
1.008332 0.000000 0.000000 0.000000
0.000000 1.792591 0.000000 0.000000
0.000000 0.000000 -1.020202 -1.000000
0.000000 0.000000 -2.020202 0.000000
resultRight=vec4(1.008332,3.585182,5.121213,7.000000)
resultRightInv=vec4(1.000000,2.000000,2.999996,1.000000)
resultLeft=vec4(1.008332,3.585182,25.939394,-6.060606)
resultLeftInv=vec4(1.000000,2.000000,3.000000,1.000000)

参考

  • glm

你可能感兴趣的:(c++,图形学,图形学)