Aus*_*ore 2 c++ opengl graphics rotation
I made a fancy shape with OpenGL and I draw that shape this function:
drawShape(const Point & center, char radius, int points, int rotation)
Run Code Online (Sandbox Code Playgroud)
Inside the function I have code that tells OpenGL where the vertexes are:
glBegin(GL_LINE_LOOP);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(center.getX() + 0.0, center.getY() + 1.0);
// more vertices
glEnd();
Run Code Online (Sandbox Code Playgroud)
现在,当我添加 时glRotatef(rotation, 0.0, 0.0, 1.0)
,我只希望我绘制的这个形状在屏幕上旋转。但是,如果我将它添加到它上面,glBegin()
它会旋转窗口中的所有内容。如果我在对象之间包含所有代码glPushMatrix()
并glPopMatrix()
旋转对象,但围绕窗口中心旋转。如何仅旋转我绘制的对象?
您正在通过将center.getX
此类添加到值中来完成 OpenGL 的工作。
你想要的是这个:
glPushMatrix();
glTranslatef(center.getX(), center.getY(), 0.0f);
glRotatef(rotation, 0.0, 0.0, 1.0);
glBegin(GL_LINE_LOOP);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(0.0, 1.0);
// more vertices
glEnd();
glPopMatrix();
Run Code Online (Sandbox Code Playgroud)
您可以通过使用glScale
矩阵并假设您的glVertex
调用中的半径为 1.0来应用半径。