Week14-1
製作計時器
void timer(int t)
{
glutTimerFunc(500,timer,t+1);
angle += 90;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Timer");
glutDisplayFunc(display);
glutTimerFunc(2000,timer,0);
glutMainLoop();
}
初始設定為2秒開始轉,之後每0.5秒再轉一次,1秒1000
Week14-2
用鍵盤啟動計時器
void keyboard(unsigned char key,int x,int y)
{
glutTimerFunc(0,timer,0);
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Timer");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
按下任意鍵即可開始轉動
Week14-3
使用滑鼠決定茶壺旋轉起始位置與結束位置
float angle = 0,oldAngle = 0,newAngle = 0;
void timer(int t)
{
if(t<100) glutTimerFunc(5,timer,t+1);
float alpha = t/ 100.0;
angle = newAngle*alpha + (1-alpha) * oldAngle;
glutPostRedisplay();
}
void motion(int x,int y)
{
angle = x;
glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN) oldAngle = x;
if(state==GLUT_UP) newAngle = x;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Timer");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
用mouse函數決定茶壺如何轉動,用motion函數讓畫面不斷更新
Week14-4
將上週的 Final_Project 使用鍵盤選擇部位再將其上色
int show[4]={1,1,1,1};
int ID=0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') ID = 0;
if(key=='1') ID = 1;
if(key=='2') ID = 2;
if(key=='3') ID = 3;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glScalef(0.2,0.2,0.2);
if(body==NULL)
{
head = glmReadOBJ("model/head.obj");
body = glmReadOBJ("model/body.obj");
uparmL = glmReadOBJ("model/uparmL.obj");
lowarmL = glmReadOBJ("model/lowarmL.obj");
}
if(ID==0)glColor3f(1,0,0);
else glColor3f(1,1,1);
if(show[0]) glmDraw(head, GLM_MATERIAL);
if(ID==1)glColor3f(1,0,0);
else glColor3f(1,1,1);
if(show[1]) glmDraw(body, GLM_MATERIAL);
glPushMatrix();
glTranslatef(teapotX,teapotY,0);
if(ID==2)glColor3f(1,0,0);
else glColor3f(1,1,1);
if(show[2]) glmDraw(uparmL, GLM_MATERIAL);
glPopMatrix();
if(ID==3)glColor3f(1,0,0);
else glColor3f(1,1,1);
if(show[3]) glmDraw(lowarmL, GLM_MATERIAL);
glPopMatrix();
glutSwapBuffers();
}
數字鍵0,1,2,3選擇部位顯示紅色其餘部位都顯示白色
Week14-5
旋轉手臂
void display()
{
glPushMatrix();
glTranslatef(1.339999, 0.373333, 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(-1.339999, -0.373333, 0);
if(ID==2)glColor3f(1,0,0);
else glColor3f(1,1,1);
if(show[2]) glmDraw(uparmL, GLM_MATERIAL);
glPopMatrix();
if(ID==3)glColor3f(1,0,0);
else glColor3f(1,1,1);
if(show[3]) glmDraw(lowarmL, GLM_MATERIAL);
glPopMatrix();
glPushMatrix();
glColor3f(0,1,0);
glutSolidTeapot(0.02);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
teapotX += (x - oldX)/150.0;
teapotY -= (y - oldY)/150.0;
oldX = x;
oldY = y;
angle = x;
printf("glTranslatef(%f, %f, 0);\n", teapotX, teapotY);
glutPostRedisplay();
}
將手臂調整到適當位置即可使用滑鼠旋轉
加上手,跟著一起旋轉
glPushMatrix();
glTranslatef(1.339999, 0.373333, 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(-1.339999, -0.373333, 0);
///glTranslatef(teapotX, teapotY, 0);
if(ID==2)glColor3f(1,0,0);
else glColor3f(1,1,1);
if(show[2]) glmDraw(uparmL, GLM_MATERIAL);
glPushMatrix();
glTranslatef(1.879999, 0.080000, 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(-1.879999, -0.080000, 0);
///glTranslatef(teapotX, teapotY, 0);
if(ID==3)glColor3f(1,0,0);
else glColor3f(1,1,1);
if(show[3]) glmDraw(lowarmL, GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
沒有留言:
張貼留言