opengl动画问题求助

zhangjiali12011 2017-11-06 05:39:17
要做的是一个流水线,方块到第一个机床变色,第二个机床变形状,第三个机床加盖子。
然后现在用定时器做动画,第一条第二条是正常运行的,第三条和第四条流水线运动就非常奇怪,不知道该怎么修改,感觉好像是受到了前两条流水线下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;
}
...全文
153 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiht594 2017-11-07
  • 打赏
  • 举报
回复
void move(int id)
{
if (movez >= 0.05)
{
movez -=0.03;
glutPostRedisplay();
glutTimerFunc(10, move, 0);
}
}

glutTimerFunc这个函数的执行是有条件的。
可以先把所有的glutTimerFunc都放入大括号中、至少图形能走到最后。 具体条件你自己再考虑
Welcome to my OpenGL tutorials. I am an average guy with a passion for OpenGL! The first time I heard about OpenGL was back when 3Dfx released their Hardware accelerated OpenGL driver for the Voodoo 1 card. Immediately I knew OpenGL was something I had to learn. Unfortunately, it was very hard to find any information about OpenGL in books or on the net. I spent hours trying to make code work and even more time begging people for help in email and on IRC. I found that those people that understood OpenGL considered themselves elite, and had no interest in sharing their knowledge. VERY frustrating! I created this web site so that people interested in learning OpenGL would have a place to come if they needed help. In each of my tutorials I try to explain, in as much detail as humanly possible, what each line of code is doing. I try to keep my code simple (no MFC code to learn)! An absolute newbie to both Visual C++ and OpenGL should be able to go through the code, and have a pretty good idea of what’s going on. My site is just one of many sites offering OpenGL tutorials. If you’re a hardcore OpenGL programmer, my site may be too simplistic, but if you’re just starting out, I feel my site has a lot to offer! This tutorial was completely rewritten January 2000. This tutorial will teach you how to set up an OpenGL window. The window can be windowed or fullscreen, any size you want, any resolution you want, and any color depth you want. The code is very flexible and can be used for all your OpenGL projects. All my tutorials will be based on this code! I wrote the code to be flexible, and powerful at the same time. All errors are reported. There should be no memory leaks, and the code is easy to read and easy to modify. Thanks to Fredric Echols for his modifications to the code! I’ll start this tutorial by jumping right into the code. The first thing you will have to do is build a project in Visual C++. If you don’t know how to do that, you should not be learning OpenGL, you should be learning Visual C++. The downloadable code is Visual C++ 6.0 code. Some versions of VC++ require that bool is changed to BOOL, true is changed to TRUE, and false is changed to FALSE. By making the changes mentioned, I have been able to compile the code on Visual C++ 4.0 and 5.0 with no other problems. After you have created a new Win32 Application (NOT a console application) in Visual C++, you will need to link the OpenGL libraries. In Visual C++ go to Project, Settings, and then click on the LINK tab. Under „Object/Library Modules” at the beginning of the line (before kernel32.lib) add OpenGL32.lib Glu32.lib and Glaux.lib. Once you’ve done this click on OK. You’re now ready to write an OpenGL Windows program.

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧