像真正的风力涡轮机一样旋转物体
Posted
技术标签:
【中文标题】像真正的风力涡轮机一样旋转物体【英文标题】:Rotate object like a real wind turbine 【发布时间】:2018-12-23 13:23:01 【问题描述】:#include <stdio.h>
#include <math.h>
#include "glut.h"
void init()
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 400, 0.0, 300.0);
void drawTurbine()
// draw the tower
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(160, 40);
glVertex2f(163, 180);
glVertex2f(160, 40);
glVertex2f(175, 40);
glVertex2f(175, 40);
glVertex2f(172, 180);
glEnd();
// Rotate object
glPushMatrix();
glRotatef(100, 0, 1, 0);
// draw first rotor blade
glBegin(GL_TRIANGLES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(173, 180);
glVertex2f(163, 180);
glVertex2f(168, 270);
glEnd();
// draw second rotor blade
glBegin(GL_TRIANGLES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(170, 174);
glVertex2f(175, 180);
glVertex2f(247, 140);
glEnd();
// draw third rotor blade
glBegin(GL_TRIANGLES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(162, 180);
glVertex2f(167, 174);
glVertex2f(88, 140);
glEnd();
// circle in the middle
float theta;
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
for (int i = 0; i <= 360; i++)
theta = i * 3.142 / 180;
glVertex2f(168 + 7 * cos(theta), 180 + 6.5 * sin(theta));
glEnd();
glPopMatrix();
void display()
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawTurbine();
glFlush();
glutSwapBuffers();
glutPostRedisplay();
void idle()
display();
int main(int argc, char** argv)
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(700, 600);
glutInitWindowPosition(10, 10);
glutCreateWindow("Wind Turbine");
init();
glutIdleFunc(idle);
glutDisplayFunc(display);
glutMainLoop();
return 0;
我已经创建了一个风力涡轮机,我正在尝试旋转转子叶片,但代码只是让它消失了。我希望它顺时针旋转。我只需要一个关于如何让它像真正的风力涡轮机一样旋转的想法。我是使用 glRotatef 的新手,这将是我第一次使用 OpenGL 旋转对象。
【问题讨论】:
【参考方案1】:您需要一个用于风力涡轮机当前角度 (current_angle
) 和每帧旋转角度 (step_angle
) 的变量。进一步你必须知道风力涡轮机的中心(center_x
,center_y
):
float current_angle = 0.0f;
float step_angle = 0.2f;
float center_x = 168.0f;
float center_y = 180.0f;
要围绕枢轴旋转,您必须定义一个模型矩阵,该矩阵由反转的枢轴置换,然后旋转并最终变换回枢轴。旋转轴是 z 轴 (0, 0, 1),因为您的几何图形是在 xy 平面上绘制的:
glTranslatef( center_x, center_y, 0.0f );
glRotatef(current_angle, 0, 0, 1);
current_angle += step_angle;
glTranslatef(-center_x, -center_y, 0.0f );
预览:
函数drawTurbine
的完整代码:
void drawTurbine()
// draw the tower
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(160, 40);
glVertex2f(163, 180);
glVertex2f(160, 40);
glVertex2f(175, 40);
glVertex2f(175, 40);
glVertex2f(172, 180);
glEnd();
// Rotate object
glPushMatrix();
glTranslatef( center_x, center_y, 0.0f );
glRotatef(current_angle, 0, 0, 1);
current_angle += step_angle;
glTranslatef(-center_x, -center_y, 0.0f );
// draw first rotor blade
glBegin(GL_TRIANGLES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(173, 180);
glVertex2f(163, 180);
glVertex2f(168, 270);
glEnd();
// draw second rotor blade
glBegin(GL_TRIANGLES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(170, 174);
glVertex2f(175, 180);
glVertex2f(247, 140);
glEnd();
// draw third rotor blade
glBegin(GL_TRIANGLES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(162, 180);
glVertex2f(167, 174);
glVertex2f(88, 140);
glEnd();
// circle in the middle
float theta;
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
for (int i = 0; i <= 360; i++)
theta = i * 3.142 / 180;
glVertex2f(168 + 7 * cos(theta), 180 + 6.5 * sin(theta));
glEnd();
glPopMatrix();
【讨论】:
以上是关于像真正的风力涡轮机一样旋转物体的主要内容,如果未能解决你的问题,请参考以下文章