简体   繁体   English

如何在openGL中旋转图像?

[英]How to rotate an image in openGL?

I have a code below in C++ OpenGL. 我在C ++ OpenGL中有以下代码。 It has six triangles that form hexagonal. 它有六个形成六边形的三角形。 However, I need to be able to rotate it in vertically. 但是,我需要能够垂直旋转它。 Can someone help? 有人可以帮忙吗? TNX TNX

Details: I have six independent triangles with vertices. 详细信息:我有六个具有顶点的独立三角形。 In addition, there is two-dimensional array that is used for colors. 此外,还有用于颜色的二维数组。 There is a loop starts at line [here] two keep windows rendering until it is exited. 在第[line]行有一个循环,其中两个保持Windows渲染直到退出。 Another line at line [here-two] that is used to show all the triangles with their color. [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();
    }

Read up on the use of matrices. 阅读有关矩阵使用的信息。 What most games do in this case is they apply a matrix in the shader (as a uniform variable) that will rotate the object. 在这种情况下,大多数游戏所要做的就是在着色器中应用矩阵(作为均匀变量),该矩阵将旋转对象。 In this case, you would create a rotation matrix of angle x, pass it to the shader, and then every new frame increment x and pass it to the shader again. 在这种情况下,您将创建一个角度为x的旋转矩阵,将其传递给着色器,然后每个新帧递增x并将其再次传递给着色器。

For more information on the specifics of the implementation read these: 有关实现细节的更多信息,请阅读以下内容:

And a tip with matrix operations: remember to apply them in the right order. 关于矩阵运算的提示:请记住以正确的顺序应用它们。 If you want to get the object to rotate around it's centre, make sure the rotation matrix is applied first and that the origin of your mesh is it's centre. 如果要使对象绕其中心旋转,请确保首先应用旋转矩阵,并且网格的原点是其中心。

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

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