• opengl中的旋转与平移


    近来在看openglsuperbible,看到了旋转与平移这一张,在书中提到了平移与旋转的先后顺序问题,改变平移与旋转的先后顺序将会带来图形坐标位置的不同。这句话一开始初看上来很好理解,一开始我的理解就是例如在X轴上有个点P(1,0),如果先对它进行平移(1,0)再绕原点逆时针旋转90度,那么它的值就是(0,2),相反则是(1,1),在这个基础上我学习了opengl的源代码但是有个非常疑惑的地方。

    // Move.cpp
    // Move a Block based on arrow key movements
    
    #include <GLTools.h>    // OpenGL toolkit
    #include <GLShaderManager.h>
    #include <math3d.h>
    
    #ifdef __APPLE__
    #include <glut/glut.h>
    #else
    #define FREEGLUT_STATIC
    #include <GL/glut.h>
    #endif
    
    GLBatch    squareBatch;
    GLShaderManager    shaderManager;
    
    
    GLfloat blockSize = 0.1f;
    GLfloat vVerts[] = { -blockSize, -blockSize, 0.0f, 
    blockSize, -blockSize, 0.0f,
    blockSize,  blockSize, 0.0f,
    -blockSize,  blockSize, 0.0f};
    
    GLfloat xPos = 0.0f;
    GLfloat yPos = 0.0f;
    
    
    ///////////////////////////////////////////////////////////////////////////////
    // This function does any needed initialization on the rendering context. 
    // This is the first opportunity to do any OpenGL related tasks.
    void SetupRC()
    {
        // Black background
        glClearColor(0.0f, 0.0f, 1.0f, 1.0f );
    
        shaderManager.InitializeStockShaders();
    
        // Load up a triangle
        squareBatch.Begin(GL_TRIANGLE_FAN, 4);
        squareBatch.CopyVertexData3f(vVerts);
        squareBatch.End();
    }
    
    // Respond to arrow keys by moving the camera frame of reference
    void SpecialKeys(int key, int x, int y)
    {
        GLfloat stepSize = 0.025f; 
    
    
        if(key == GLUT_KEY_UP)
            yPos += stepSize;
    
        if(key == GLUT_KEY_DOWN)
            yPos -= stepSize;
    
        if(key == GLUT_KEY_LEFT)
            xPos -= stepSize;
    
        if(key == GLUT_KEY_RIGHT)
            xPos += stepSize;
    
        // Collision detection
        if(xPos < (-1.0f + blockSize)) xPos = -1.0f + blockSize;
    
        if(xPos > (1.0f - blockSize)) xPos = 1.0f - blockSize;
    
        if(yPos < (-1.0f + blockSize))  yPos = -1.0f + blockSize;
    
        if(yPos > (1.0f - blockSize)) yPos = 1.0f - blockSize;
    
        glutPostRedisplay();
    }
    
    
    
    
    
    ///////////////////////////////////////////////////////////////////////////////
    // Called to draw scene
    void RenderScene(void)
    {
        // Clear the window with current clearing color
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    
        GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    
        M3DMatrix44f mFinalTransform, mTranslationMatrix, mRotationMatrix;
    
        // Just Translate
        m3dTranslationMatrix44(mTranslationMatrix, xPos, yPos, 0.0f);
    
        // Rotate 5 degrees evertyime we redraw
        static float yRot = 0.0f;
        yRot += 5.0f;
        m3dRotationMatrix44(mRotationMatrix, m3dDegToRad(yRot), 0.0f, 0.0f, 1.0f);
    
        m3dMatrixMultiply44(mFinalTransform,mRotationMatrix,mTranslationMatrix);
    
    
        shaderManager.UseStockShader(GLT_SHADER_FLAT, mFinalTransform, vRed);
        squareBatch.Draw();
    
        // Perform the buffer swap
        glutSwapBuffers();
    }
    
    
    
    ///////////////////////////////////////////////////////////////////////////////
    // Window has changed size, or has just been created. In either case, we need
    // to use the window dimensions to set the viewport and the projection matrix.
    void ChangeSize(int w, int h)
    {
        glViewport(0, 0, w, h);
    }
    
    ///////////////////////////////////////////////////////////////////////////////
    // Main entry point for GLUT based programs
    int main(int argc, char* argv[])
    {
        gltSetWorkingDirectory(argv[0]);
    
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
        glutInitWindowSize(600, 600);
        glutCreateWindow("Move Block with Arrow Keys");
    
        GLenum err = glewInit();
        if (GLEW_OK != err)
        {
            // Problem: glewInit failed, something is seriously wrong.
            fprintf(stderr, "Error: %s
    ", glewGetErrorString(err));
            return 1;
        }
    
        glutReshapeFunc(ChangeSize);
        glutDisplayFunc(RenderScene);
        glutSpecialFunc(SpecialKeys);
    
        SetupRC();
    
        glutMainLoop();
        return 0;
    }


    这段代码的作用就是显示出一个正方形,然后通过上下左右键来改变这个正方形的位置并且一直在旋转。在矩阵变换这一段代码中我们看到了先是进行了平移运算

    m3dTranslationMatrix44(mTranslationMatrix, xPos, yPos, 0.0f);

    而后进行了旋转运算:

    m3dRotationMatrix44(mRotationMatrix, m3dDegToRad(yRot), 0.0f, 0.0f, 1.0f);

    看到这里我就感到了不解的地方,如果先平移到我们想要移动到的位置,再对其进行绕原点的旋转,那么结果就和预期的不一致了。

    为了解决额这个问题,我首先尝试了分别对旋转和平移做单独的处理,试试证明它们单独作用时的确都没问题,平移是的确把图形移到了相应的位置,而旋转也的确是绕着原点在做旋转。

    但是显然像源代码那样的写法没有问题,那么问题出在哪呢?

    我们再仔细看了看前面的内容,并且加以现象的推敲,终于得出了结果,结果就是opengl在处理综合变换的时候,是基于相对坐标系的而不是绝对坐标系,所以在旋转m3dRotationMatrix44(mRotationMatrix, m3dDegToRad(yRot), 0.0f, 0.0f, 1.0f)这个函数中,所看到的(0.0f, 0.0f, 1.0f)这个所谓的过原点的z方向,并不是指的绝对坐标系中的点,而是进行过平移之后的相对坐标系的原点。那么现在就很显然了,为什么先平移后旋转可以而先旋转后平移是错误的,并不是像一开始所说的那样,而是,前者的平移所做的是把点移到相应的位置,并且所形成的相对坐标系的方向没有变化,相对坐标系的原点就是原来实体的原点移动之后的地方,在此例中就是正方形的中点,继而进行的旋转是基于这个相对坐标系的,那么显然结果是对的。而在我们认为对的第二种方式中,先对正方形进行了旋转操作,这个操作就是未改变相对坐标系的原点,但是相对坐标系的方向发生了变化,之后的平移坐标系是这个相对坐标系,显然所移动的方向与预期的不一样。

  • 相关阅读:
    ios动态创建类Class
    iOS 视图控制器转场详解
    HTML5新增标签属性
    HTML5入门篇
    ExtJs 添加员工 实例 ---- 锚点布局 anchor 可自动伸缩
    Ext 初级UI设计
    Ext 面向对象程序设计 入门篇
    改善EF代码的方法(下)
    改善EF代码的方法(上)
    EF 存储过程(下)
  • 原文地址:https://www.cnblogs.com/xds1224/p/3499001.html
Copyright © 2020-2023  润新知