[英]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並將其再次傳遞給着色器。
有關實現細節的更多信息,請閱讀以下內容:
https://www.opengl.org/wiki/Uniform_(GLSL)-在着色器中創建統一變量並更新它們
http://inside.mines.edu/fs_home/gmurray/ArbitraryAxisRotation/-
創建將旋轉頂點的矩陣。
關於矩陣運算的提示:請記住以正確的順序應用它們。 如果要使對象繞其中心旋轉,請確保首先應用旋轉矩陣,並且網格的原點是其中心。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.