我想旋转一个对象,而它的移动,但我不能使它沿着我希望它移动的点移动和旋转。无法旋转c中的移动对象使用opengl
struct point {
int x;
int y;
};
// A line between two points.
struct line {
struct point start;
struct point end;
};
float j = 100.0;
float rotationAngle = 0.0f; // The angle of rotation for our object
void rotateObjectWithMovement(void functionToDrawObject(), struct point pivot, float rotationAngle) {
glPushMatrix();
glTranslatef(pivot.x, pivot.y, 0.0f);
glRotatef(rotationAngle, 0.0f, 0.0f, 1.0f);
functionToDrawObject();
glPopMatrix();
}
void rotateObjectWithoutMovement(void functionToDrawObject(), struct point pivot, float rotationAngle) {
glPushMatrix();
glTranslatef(pivot.x, pivot.y, 0.0f);
glRotatef(rotationAngle, 0.0f, 0.0f, 1.0f);
glTranslatef(-pivot.x, -pivot.y, 0.0f);
functionToDrawObject();
glPopMatrix();
}
void rocket() {
struct point allPoints[2] = {
{ 100, 100},
{ 150, 150}
};
struct line line1 = {allPoints[0], allPoints[1]};
glColor3f(1.0, 1.0, 0.0);
drawLines(&line1, 1);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
struct point pivot={j,100};
rotateObjectWithoutMovement(rocket,pivot,rotationAngle);
//rotateObjectWithMovement(rocket,pivot,rotationAngle);
//reference line
glBegin(GL_LINES);
glVertex2f(0, 100);
glVertex2d(1000, 100);
glEnd();
j += 1;
rotationAngle += 0.5f; // Increment our rotation value
if (rotationAngle > 360.0f) // If we have rotated beyond 360 degrees (a full rotation)
rotationAngle -= 360.0f; // Subtract 360 degrees off of our rotation
glutSwapBuffers(); // Swap our buffers
}
void myinit() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 988.0, 0.0, 999.0);
glClearColor(0.5f, 0.5f, 1, 1);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(700, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL Hierarchical Modeling 2D Example");
myinit();
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
return 0;
}
rotateWithMoment()函数接受一个对象,静止坐标和角度,所述对象具有被旋转到并与统计的共ordinats作为枢轴转动它。
rotateWithoutMovement()函数需要一个对象,改变对象必须旋转到的坐标和角度,并且以变化的坐标作为枢轴旋转它(例如:旋转线条看起来像轮子旋转)发送变化的co这个函数使这条线以某种方式旋转。
我一直在努力让它工作2天,不知道我哪里出错了。
我想让我发送的对象移动到(x,y)点并以指定的角度旋转。要做到这一点,我试图建立这样的功能,通过接受函数来绘制要旋转和移动的对象。
rotateWithoutMovement很好,如果我发送一个不断的坐标。
定义“不工作”。 –
你的'main(...)'函数在哪里? – ryyker
我不明白C++标记。你的标题说“C”语言。他们是不同的语言。例如,C++具有'std :: vector'和'std :: string',C不具有。在C中,我可以无错地命名变量“class”。 –