C++ 旋转使用OpenGL绘制的二维对象
我用OpenGL制作了一个奇特的形状,并使用此函数绘制该形状:C++ 旋转使用OpenGL绘制的二维对象,c++,opengl,graphics,rotation,C++,Opengl,Graphics,Rotation,我用OpenGL制作了一个奇特的形状,并使用此函数绘制该形状: drawShape(const Point & center, char radius, int points, int rotation) 在函数中,我有代码告诉OpenGL顶点的位置: glBegin(GL_LINE_LOOP); glColor3f(1.0, 1.0, 1.0); glVertex2f(center.getX() + 0.0, center.getY() + 1.0); // more v
drawShape(const Point & center, char radius, int points, int rotation)
在函数中,我有代码告诉OpenGL顶点的位置:
glBegin(GL_LINE_LOOP);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(center.getX() + 0.0, center.getY() + 1.0);
// more vertices
glEnd();
现在,当我添加
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();
您可能可以通过使用
glScale
矩阵并在glVertex
调用中假设半径为1.0来应用半径。您通过将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();
您可能可以通过使用
glScale
矩阵并在glVertex
调用中假设半径为1.0来应用半径。此外,我建议您学习使用顶点数组和顶点缓冲区对象,并取消立即模式(即不再使用glBegin…glEnd)。此外,我建议您学习使用顶点数组和顶点缓冲区对象,并取消立即模式(即不再使用glBegin…glEnd)。