用dev c++的opengl库制作可用鼠标及键盘可控制其旋转,移动的精美机器人
时间: 2024-02-20 20:56:58 浏览: 94
dev c++ 连接 mysql 方法、库
好的,下面是一个简单的Dev C++ OpenGL机器人示例代码,你可以使用鼠标和键盘来控制它的旋转和移动:
```c++
#include <windows.h>
#include <GL/glut.h>
int screenWidth = 800;
int screenHeight = 600;
float robotX = 0.0f;
float robotY = 0.0f;
float robotZ = 0.0f;
float robotRotateX = 0.0f;
float robotRotateY = 0.0f;
float robotRotateZ = 0.0f;
void init()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_SMOOTH);
}
void drawRobot()
{
// 画头
glColor3f(1.0f, 1.0f, 0.0f);
glPushMatrix();
glTranslatef(0.0f, 1.0f, 0.0f);
glutSolidSphere(0.5f, 20, 20);
glPopMatrix();
// 画身体
glColor3f(0.0f, 1.0f, 1.0f);
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.0f);
glScalef(1.0f, 2.0f, 0.5f);
glutSolidCube(1.0f);
glPopMatrix();
// 画左臂
glColor3f(0.0f, 0.0f, 1.0f);
glPushMatrix();
glTranslatef(-0.75f, 0.0f, 0.0f);
glScalef(0.5f, 2.0f, 0.5f);
glutSolidCube(1.0f);
glPopMatrix();
// 画右臂
glColor3f(1.0f, 0.0f, 0.0f);
glPushMatrix();
glTranslatef(0.75f, 0.0f, 0.0f);
glScalef(0.5f, 2.0f, 0.5f);
glutSolidCube(1.0f);
glPopMatrix();
// 画左腿
glColor3f(0.0f, 1.0f, 0.0f);
glPushMatrix();
glTranslatef(-0.25f, -2.0f, 0.0f);
glScalef(0.5f, 2.0f, 0.5f);
glutSolidCube(1.0f);
glPopMatrix();
// 画右腿
glColor3f(1.0f, 1.0f, 1.0f);
glPushMatrix();
glTranslatef(0.25f, -2.0f, 0.0f);
glScalef(0.5f, 2.0f, 0.5f);
glutSolidCube(1.0f);
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, (float)screenWidth / (float)screenHeight, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0f, 4.0f, 10.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glTranslatef(robotX, robotY, robotZ);
glRotatef(robotRotateX, 1.0f, 0.0f, 0.0f);
glRotatef(robotRotateY, 0.0f, 1.0f, 0.0f);
glRotatef(robotRotateZ, 0.0f, 0.0f, 1.0f);
drawRobot();
glutSwapBuffers();
}
void specialKeys(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT:
robotX -= 0.1f;
break;
case GLUT_KEY_RIGHT:
robotX += 0.1f;
break;
case GLUT_KEY_UP:
robotZ -= 0.1f;
break;
case GLUT_KEY_DOWN:
robotZ += 0.1f;
break;
}
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
robotRotateY += 5.0f;
if (robotRotateY >= 360.0f)
robotRotateY -= 360.0f;
glutPostRedisplay();
}
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
robotRotateY -= 5.0f;
if (robotRotateY < 0.0f)
robotRotateY += 360.0f;
glutPostRedisplay();
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(screenWidth, screenHeight);
glutCreateWindow("Robot");
init();
glutDisplayFunc(display);
glutSpecialFunc(specialKeys);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
```
在这个代码中,我们使用glutSolidCube()和glutSolidSphere()函数来绘制机器人的各个部分,使用gluPerspective()函数来设置投影矩阵,使用gluLookAt()函数来设置视点位置和方向,使用glTranslatef()和glRotatef()函数来控制机器人的位置和旋转。我们还使用GLUT的键盘和鼠标回调函数来控制机器人的移动和旋转。
阅读全文