1.主題:計時器Timer
2.主題:內插公式
3.主題:動畫
4.上週HW問題回覆
4.期末作品
下載檔案
將檔案移至桌面並開啟
將檔案移至桌面並開啟
將lib打開
將此檔案複製並貼上
將貼上的檔案重新命名為libglut32.a
File→New→Project
選取 GLUT project
輸入名稱,將檔案儲存在桌面
先打上茶壺的程式碼
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);///變大了
glutCreateWindow("week 14 timer");
glutDisplayFunc(display);
glutMainLoop();
}
到網路上下載聲音檔,這個程式碼執行後,會發出一聲自己下載的聲音檔
#include <GL/glut.h>
#include <mmsystem.h>///Now (1)
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
int main(int argc, char**argv)
{
PlaySoundA("Wate.wav", NULL, SND_ASYNC);///Now (2)
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);///變大了
glutCreateWindow("week 14 timer");
glutDisplayFunc(display);
glutMainLoop();
}
加上粗體的程式碼,可以在執行後3秒鐘,再發出聲響
#include <GL/glut.h>
#include <mmsystem.h>///Now (1)
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
void timer(int t)///NOW timer!!!!
{
PlaySoundA("Wate.wav", NULL, SND_ASYNC);///Now (2) ///NOW timer!!!!
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);///變大了
glutCreateWindow("week 14 timer");
glutDisplayFunc(display);
glutTimerFunc(3000, timer, 0);///NOW timer!!!!
glutMainLoop();
}
加上粗體的程式碼,可以在執行後3秒鐘,再發出聲響,而且會一直出聲音不會停,也同時因出「t」的值
#include <GL/glut.h>
#include <mmsystem.h>///Now (1)
#include <stdio.h>///printf()
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
void timer(int t)///NOW timer!!!!
{
glutTimerFunc(1000, timer, t+1 );///NOW timer!!!! 起床要播下一個鬧鐘
printf("timer(t) 的 t是 %d\n", t);///順便印一下現在的t是多少
PlaySoundA("Wate.wav", NULL, SND_ASYNC);///Now (2) ///NOW timer!!!!
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);///變大了
glutCreateWindow("week 14 timer");
glutDisplayFunc(display);
glutTimerFunc(3000, timer, 0);///NOW timer!!!!
glutMainLoop();
}
加上粗體的程式碼,可以讓茶壺1秒轉33度
#include <GL/glut.h>
#include <mmsystem.h>///Now (1)
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0,0,1);
glutSolidTeapot( 0.3 );
glPopMatrix();
glutSwapBuffers();
}
void timer(int t)///NOW timer!!!!
{
/// 1000 ms = 1 sec, 30 ms = 1/33 sec , 33 fps
glutTimerFunc(30, timer, t+1 );///NOW timer!!!! 起床要播下一個鬧鐘
angle += 1;
glutPostRedisplay();///系統重繪畫面
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);///變大了
glutCreateWindow("week 14 timer");
glutDisplayFunc(display);
glutTimerFunc(3000, timer, 0);///NOW timer!!!!
glutMainLoop();
}
觀察內插法
使用內插法,使茶壺水平移動
#include <GL/glut.h>
#include <mmsystem.h>///Now (1)
float angle=0;
float nowX=0;///NOW2
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
///glRotatef(angle, 0,0,1);
glTranslatef(nowX, 0, 0);///NOW2
glutSolidTeapot( 0.3 );
glPopMatrix();
glutSwapBuffers();
}
void timer(int t)///NOW timer!!!!
{
/// 1000 ms = 1 sec, 30 ms = 1/33 sec , 33 fps
glutTimerFunc(30, timer, t+1 );///NOW timer!!!! 起床要播下一個鬧鐘
float alpha = (t%33) / 33.0;///NOW2 alpha 內插
nowX = alpha* 1 + (1-alpha)* -1 ;///NOW2
angle += 1;
glutPostRedisplay();///系統重繪畫面
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);///變大了
glutCreateWindow("week 14 timer");
glutDisplayFunc(display);
glutTimerFunc(1000, timer, 0);///NOW timer!!!!
glutMainLoop();
}
使用內插法,使茶壺水平移動
#include <GL/glut.h>
#include <mmsystem.h>///Now (1)
float angle=0;
float nowX=0;///NOW2
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
///glRotatef(angle, 0,0,1);
glTranslatef(nowX, 0, 0);///NOW2
glutSolidTeapot( 0.3 );
glPopMatrix();
glutSwapBuffers();
}
void timer(int t)///NOW timer!!!!
{
/// 1000 ms = 1 sec, 30 ms = 1/33 sec , 33 fps
glutTimerFunc(30, timer, t+1 );///NOW timer!!!! 起床要播下一個鬧鐘
float alpha = (t%33) / 33.0;///NOW2 alpha 內插
nowX = alpha* 1 + (1-alpha)* -1 ;///NOW2
angle += 1;
glutPostRedisplay();///系統重繪畫面
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);///變大了
glutCreateWindow("week 14 timer");
glutDisplayFunc(display);
glutTimerFunc(1000, timer, 0);///NOW timer!!!!
glutMainLoop();
}
沒有留言:
張貼留言