opengl画起重机代码
时间: 2023-07-03 11:15:30 浏览: 137
opengl代码
以下是使用OpenGL画起重机的简单代码示例:
```
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
// 绘制起重机臂
glBegin(GL_QUADS);
glColor3f(0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.0f, 0.0f);
glVertex3f(-0.5f, 1.0f, 0.0f);
glVertex3f(0.5f, 1.0f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
glEnd();
// 绘制起重机钩
glPushMatrix();
glTranslatef(0.0f, 1.0f, 0.0f);
glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-0.1f, 0.0f, 0.0f);
glVertex3f(-0.1f, 0.2f, 0.0f);
glVertex3f(0.1f, 0.2f, 0.0f);
glVertex3f(0.1f, 0.0f, 0.0f);
glEnd();
glPopMatrix();
// 绘制起重机轮子
glPushMatrix();
glTranslatef(-0.5f, -0.5f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glutSolidSphere(0.2f, 20, 20);
glPopMatrix();
glPushMatrix();
glTranslatef(0.5f, -0.5f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glutSolidSphere(0.2f, 20, 20);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Crane");
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, 1.33f, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
```
以上代码可以绘制一个简单的起重机模型,包括一个臂、一个钩和两个轮子。在这里,我们使用了OpenGL的基本绘图函数,如glBegin、glEnd和glVertex,以及变换函数glTranslatef和glRotatef,来控制物体的位置和方向。同时,我们使用了GLUT库来创建窗口和处理事件。
阅读全文