繁体   English   中英

如何在openGL中旋转图像?

[英]How to rotate an image in openGL?

我在C ++ OpenGL中有以下代码。 它有六个形成六边形的三角形。 但是,我需要能够垂直旋转它。 有人可以帮忙吗? TNX

详细信息:我有六个具有顶点的独立三角形。 此外,还有用于颜色的二维数组。 在第[line]行有一个循环,其中两个保持Windows渲染直到退出。 [here-two]行的另一行用于显示所有三角形及其颜色。

    //coordinates of triangle
    float triangle[6][9] = {
        {
            0.0, 0.0, 0.0,
            -0.5, 0.87, 0.0,
            0.5, 0.87, 0.0
        },
        {
            0.0, 0.0, 0.0,
            -0.5, -0.87, 0.0,
            0.5, -0.87, 0.0
        },
        {
            0.0, 0.0, 0.0,
            0.5, 0.87, 0.0,
            1.0, 0.0, 0.0
        },
        {
            0.0, 0.0, 0.0,
            0.5, -0.87, 0.0,
            1.0, 0.0, 0.0
        },
        {
            0.0, 0.0, 0.0,
            -0.5, 0.87, 0.0,
            -1.0, 0.0, 0.0
        },
        {
            0.0, 0.0, 0.0,
            -0.5, -0.87, 0.0,
            -1.0, 0.0, 0.0
        }

    };

    float color[][9]{
        {
            255, 0, 0,
            255, 0, 0,
            255, 0, 0
        },
        {
            0, 255, 0,
            0, 255, 0,
            0, 255, 0
        },
        {
            0, 0, 255,
            0, 0, 255,
            0, 0, 255
        }
    };
    int count = 0;

    /* Loop until the user closes the window */ [here]      while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);
 [here-two]             for (int i = 0; i < 6; i++)
        {
            //Render OpenGL here
            glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_COLOR_ARRAY);
            glVertexPointer(3, GL_FLOAT, 0, triangle[i]);
            glColorPointer(3, GL_FLOAT, 0, color[count]);
            glDrawArrays(GL_TRIANGLES, 0, 3);
            glDisableClientState(GL_COLOR_ARRAY);
            glDisableClientState(GL_VERTEX_ARRAY);
            count++;
            if (count > 2) count = 0;
        }
        //Swap front and back buffers
        glfwSwapBuffers(window);

        //Poll for and process events
        glfwPollEvents();

        // Poll for and process events
        glfwPollEvents();
    }

阅读有关矩阵使用的信息。 在这种情况下,大多数游戏所要做的就是在着色器中应用矩阵(作为均匀变量),该矩阵将旋转对象。 在这种情况下,您将创建一个角度为x的旋转矩阵,将其传递给着色器,然后每个新帧递增x并将其再次传递给着色器。

有关实现细节的更多信息,请阅读以下内容:

关于矩阵运算的提示:请记住以正确的顺序应用它们。 如果要使对象绕其中心旋转,请确保首先应用旋转矩阵,并且网格的原点是其中心。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM