opengl动画问题求助
要做的是一个流水线,方块到第一个机床变色,第二个机床变形状,第三个机床加盖子。
然后现在用定时器做动画,第一条第二条是正常运行的,第三条和第四条流水线运动就非常奇怪,不知道该怎么修改,感觉好像是受到了前两条流水线下movex,movez函数的影响?
希望有大佬能帮忙,万分感谢
#include<Windows.h>
#include"head.h"
static float shoulder = 0, elbow1 = 0, elbow2 = 0,movex=0.2,movez=5,movey=2.0,t=0;
void init(void)
{
GLfloat mau[] = { 1.0,1.0,0.5,1.0 };
GLfloat mcu[] = { 50.0 };
GLfloat light_position[] = { 1.0,1.0,1.0,2.0 };//光照位置
GLfloat white_light[] = { 1.0,1.0,1.0,1.0 };
GLfloat msu[] = { 0.1,0.1,0.1,1.0 };
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mau);
glMaterialfv(GL_FRONT, GL_SHININESS, mcu);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, msu);
glEnable(GL_LIGHTING);//启动光照
glEnable(GL_LIGHT0);//启动第一盏灯的光照
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_TEXTURE_2D);
}
void display(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glColor3f(1.0, 1.0, 1.0);
glLoadIdentity();
gluLookAt(3, 10, 10, 10, 0.0, 0.0, 0.0, 1.0, 0.0);
//机床
glPushMatrix();
glTranslatef(0, 1.75, -5);
glScalef(3, 3, 4);
glColor3f(5, 1, 0);
glutSolidCube(1.0);
glPopMatrix();
//地面
glPushMatrix();
glColor3f(0.0, 0.2, 0.5);
glTranslatef(2.0, -2.0, 0.0);
glScalef(50.0, 1.0, 50.0);
glutSolidCube(1.0);
glPopMatrix();
//天花板
glPushMatrix();
glColor3f(0.2, 0.2, 0.3);
glTranslatef(4.0, 17.0, -20.0);
glScalef(50.0, 1.0, 100.0);
glutSolidCube(1.0);
glPopMatrix();
//可乐瓶
glPushMatrix();
if (t == 1.0)//瓶
{
glColor3f(1.0, 1.0, 0.0);
GLUquadricObj *pobj;
pobj = gluNewQuadric();
gluQuadricNormals(pobj, GLU_SMOOTH);
glTranslatef(movex, 2, movez);
glPushMatrix();
glRotatef(90, 1, 0, 0);
gluCylinder(pobj, 0.5, 0.5, 1.2, 26, 13);
//gluDisk(pobj,0.0f, 0.5f, 20, 20);
glPopMatrix();
}
if (t == 1.5)//+盖子
{
glColor3f(1.0, 1.0, 0.0);
GLUquadricObj *pobj;
pobj = gluNewQuadric();
gluQuadricNormals(pobj, GLU_SMOOTH);
glTranslatef(movex, movey, movez);
glPushMatrix();
glRotatef(90, 1, 0, 0);
gluCylinder(pobj, 0.5, 0.5, 1.2, 26, 13);
gluDisk(pobj, 0.0f, 0.5f, 20, 20);
glPopMatrix();
glPushMatrix();
glTranslatef(0, 0.45, 0);
glColor3f(1, 0, 0);
glRotatef(90, 1, 0, 0);
gluCylinder(pobj, 0.3, 0.3, 0.4, 26, 13);
gluDisk(pobj, 0.0f, 0.3f, 20, 20);
glPopMatrix();
}
else if (t == 0)//方块
{
glColor3f(1.0, 0.0, 0.0);
glTranslatef(movex, 1.3, movez);
glScalef(0.8, 1.2, 0.8);
glutSolidCube(1.0);
}
else if (t == 0.5)//变色方块
{
glColor3f(1.0, 1.0, 0.0);
glTranslatef(movex, 1.3, movez);
glScalef(0.8, 1.2, 0.8);
glutSolidCube(1.0);
}
glPopMatrix();
//机械臂1
glColor3f(0.5, 2, 2);
glPushMatrix();
glTranslatef(-2, 0, 0);
glRotatef((GLfloat)shoulder, 0, 0, 1);
glTranslatef(0, 1, 0);
glPushMatrix();
glScalef(0.4, 4, 1.0);
glutSolidCube(1);
glPopMatrix();
glTranslatef(0, 2, 0);
glRotatef((GLfloat)elbow1, 0, 0, 1);
glTranslatef(1, 0, 0);
glPushMatrix();
glScalef(2, 0.4, 1);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
//机械臂2
glPushMatrix();
glTranslatef(10, 0, 0);
glRotatef((GLfloat)shoulder, 0, 0, 1);
glTranslatef(0, 1, 0);
glPushMatrix();
glScalef(0.4, 4, 1.0);
glutSolidCube(1);
glPopMatrix();
glTranslatef(0, 2, 0);
glRotatef((GLfloat)elbow2, 0, 0, 1);
glTranslatef(1, 0, 0);
glPushMatrix();
glScalef(2, 0.4, 1);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
//传输带
glColor3f(1.0, 1.0, 1.0);
glScalef(1.2, 1.5, 12);
glutSolidCube(1.0);
glPushMatrix();
glTranslatef(10.0, 0.0, 0.0);
glScalef(1.0, 1.0, 1.0);
glutSolidCube(1.0);
glPopMatrix();
glPushMatrix();
glTranslatef(5.0, 0.0, -0.5);
glScalef(11.0, 1.0, 0.2);
glutSolidCube(1.0);
glPopMatrix();
glPushMatrix();
glTranslatef(15.0, 0.0, 0.5);
glScalef(11, 1.0, 0.1);
glutSolidCube(1.0);
glPopMatrix();
glutSwapBuffers();
//glFlush();
}
//机械臂控制动画
void down1(void)
{
if (elbow1> -30)
elbow1 = elbow1 - 0.02;
glutPostRedisplay();
}
void up1(void)
{
if (elbow1<0)
elbow1 = elbow1 + 0.02;
glutPostRedisplay();
}
void down2(void)
{
if (elbow2> -30)
elbow2 = elbow2 - 0.02;
glutPostRedisplay();
}
void up2(void)
{
if (elbow2<0)
elbow2 = elbow2 + 0.02;
glutPostRedisplay();
}
//传送带控制动画
void move(int id)
{
if (movez >= 0.05)
movez -=0.03;
glutPostRedisplay();
glutTimerFunc(10, move, 0);
}
void move2(int id)
{
if (movez > -6)
{
t = 0.5;
movez -= 0.03;
}
glutPostRedisplay();
glutTimerFunc(10, move2, 2);
}
void move3(int id)
{
if (movex < 12)
{
t = 1;
movex = movex + 0.03;
}
glutPostRedisplay();
glutTimerFunc(10, move3, 3);
}
void move4(int id)
{
if (movez<0.1)
movez = movez + 0.03;
glutPostRedisplay();
glutTimerFunc(10, move4, 4);
}
void move5(int id)
{
if (movez<6)
movez = movez + 0.03;
glutPostRedisplay();
glutTimerFunc(10, move5, 6);
}
void move6(int id)
{
if (movex < 25)
movex = movex + 0.03;
else if (movey > -1)
movey = movey - 0.1;
glutPostRedisplay();
glutTimerFunc(10, move6, 7);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case'q':
glutIdleFunc(up1);
break;
case'a':
glutIdleFunc(down1);
break;
case'e':
glutIdleFunc(up2);
break;
case'd':
glutIdleFunc(down2);
break;
case's':
{
glutTimerFunc(0, move, 0);
glutTimerFunc(3000, move2, 2);
glutTimerFunc(5000, move3, 3);
glutTimerFunc(80000, move4, 4);
glutTimerFunc(8000, move5, 6);
glutTimerFunc(145000, move6, 7);
}
break;
}
}
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(120, (GLfloat)width / (GLfloat)height, 1.0f, 25.0f);//建立一个透视投影视图体,格式为:gluPerspective(视域的角度,宽高比,视点到近裁剪面的距离(总为正),视点到远裁剪面的距离(总为正))
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(50, 50);
glutInitWindowSize(800, 800);
glutCreateWindow("Cola Factory");
init();
glutDisplayFunc(&display);
glutKeyboardFunc(&keyboard);
glutReshapeFunc(&reshape);
glutMainLoop();
return 0;
}