去網頁找freeglut windows download
進入https://www.transmissionzero.co.uk/software/freeglut-devel/
找到freeglut 3.0.0 MinGW Package然後下載
把freeglut資料夾拉到桌面上
打開資料夾→lib→複製libfreeglut.a→改檔名變成libglut32.a→複製資料夾的網址貼到codeblocks→刪除程式碼
(2)準備音樂(去網路上找wav下載)
PlaySoundA("thwap.wav", NULL, SND_ASYNC);→" "放下載的檔名
把下載的聲音拉進資料夾裡
程式碼:
#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)
{
PlaySoundA("thwap.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();
}
然後就會有聲音
glutTimerFunc(1000, timer, 0);→1000表示1秒後發出聲音,數字可以改
再多加一行程式碼,讓他一直發出聲音
程式碼:
#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);///NOW2
PlaySoundA("thwap.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();
}
他就會一直發出聲音
(3)想讓他準確告訴你發出聲音的時間
程式碼:
#include <GL/glut.h>
#include <mmsystem.h>///NOW (0)發聲音
#include <stdio.h>///NOW3 for printf()
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
void timer(int t)///NOW (2)
{
glutTimerFunc(1000, timer, t+1);///NOW3 一起床,馬上撥下一個鬧鐘, 時間才會準
printf("現在的 timer(t)的t是: %d\n", t);///NOW3
PlaySoundA("thwap.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();
}
接下來做個簡單動畫
程式碼:
#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);///NOW4 ///NOW3 一起床,馬上撥下一個鬧鐘, 時間才會準
printf("現在的 timer(t)的t是: %d\n", t);///NOW3
angle = t;///NOW4
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();
}
就會有一個會轉的茶壺,一秒鐘後開始轉
程式碼:
#include <GL/glut.h>
#include <mmsystem.h>///NOW (0)發聲音
#include <stdio.h>///NOW3 for printf()
float angle=0;///NOW4
float nowX=0;///NOW5
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///NOW4
///glRotatef(angle, 0, 0, 1);///NOW4
glTranslatef(nowX, 0, 0);///NOW5
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;///變成 alpha: 0.0 ~ 1.0 ///NOW5
nowX = alpha*1.0 + (1-alpha)* -1.0;///NOW5
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();
}
動畫就能出現
沒有留言:
張貼留言