#include <GL/glut.h> #include <mmsystem.h>///NOW (0)發聲音 void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glutSwapBuffers(); } void timer(int t)///NOW (2) { glutTimerFunc(1000, timer, 0);///計時器函式(延遲秒數,呼叫函式,t); PlaySoundA("Boing0.wav", NULL, SND_ASYNC);///NOW (2) } int main(int argc, char**argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(500,500);///大一點 glutCreateWindow("week14 timer"); glutDisplayFunc(display); glutTimerFunc(1000, timer, 0);///NOW (1) glutMainLoop(); }
2.計時器+茶壺旋轉
#include <GL/glut.h> #include <mmsystem.h>///NOW (0)發聲音 #include <stdio.h>///NOW3 for printf() float angle=0;///NOW4 void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix();///NOW4 glRotatef(angle, 0, 0, 1);///NOW4 glutSolidTeapot( 0.3 );///NOW4 glPopMatrix();///NOW4 glutSwapBuffers(); } void timer(int t)///NOW (2) { glutTimerFunc(30, timer, t+1);///每隔0.03秒呼叫自己一次並將t+1 printf("現在的 timer(t)的t是: %d\n", t);///NOW3印出t值 angle = t;///旋轉角度增加 glutPostRedisplay();///NOW4 要更新畫面 } int main(int argc, char**argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(500,500);///大一點 glutCreateWindow("week14 timer"); glutDisplayFunc(display); glutTimerFunc(1000, timer, 0);///NOW (1)第一次呼叫timer並將t設為0 glutMainLoop(); }
3.內插法
內插:α*新+(1-α)*舊
α=0.0~1.0
#include <GL/glut.h>
#include <mmsystem.h>///NOW (0)發聲音
#include <stdio.h>///NOW3 for printf()
float angle=0;///NOW4
float nowX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///NOW4
/// glRotatef(angle,0,0,1);
glTranslatef(nowX,0,0);
glutSolidTeapot( 0.3 );///NOW4
glPopMatrix();///NOW4
glutSwapBuffers();
}
void timer(int t)///NOW (2)
{ ///1000 ms = 1秒
///33ms=0.033秒 = > 33*30=1000,30fps
glutTimerFunc(33, timer, t+1);///NOW4 ///NOW3 一起床,馬上撥下一個鬧鐘, 時間才會準
printf("現在的 timer(t)的t是: %d\n", t);///NOW3
float alpha = (t%30)/30.0;
//angle = t;///NOW4
nowX=alpha*1.0+(1-alpha)* -1.0;
glutPostRedisplay();///NOW4 要更新畫面
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);///大一點
glutCreateWindow("week14 timer");
glutDisplayFunc(display);
glutTimerFunc(1000, timer, 0);///NOW (1)
glutMainLoop();
}
沒有留言:
張貼留言