學期內容回顧
1.week01: WebGL,OpenGL,OpenGL專案,GLUT專案
2.week02: 點線面,色彩,茶壺
3.week03:移動、轉動、縮放、矩陣
4.week04: mouse加入、motion加入
5.week05: 階層轉動
6.week06: TRT轉動
7.week07: 貼圖、地球(地圖) 原定這週,不過改在第十週
8.week08:打光(法向量)
9.week10: 期中考
10. week11: 聲音PlaySound()、音樂CMP3_MCZ,h
11. week12: 3D模型
12. week13: 擺Pose
13. week14: 讀、寫檔案
Week01: WebGL , OpenGL, OpenGL專案 ,GLUT專案
嘗試跑WebGL water
OpenGL專案
GLUT專案
用簡短程式碼寫出OpenGL
程式碼如下
#include<GL/glut.h>
float angle=0;
void display(){
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.87f, -0.5f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(-0.87f, -0.5f);
glEnd();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week01");///之後才會有GL
glutDisplayFunc(display);
glutMainLoop();
}
Week02:點、線、面,色彩、茶壺
拖曳三角形
程式碼如下
#include<GL/glut.h>
#include<stdio.h>
float angle=0 ,X=150 ,Y=150 /*oldX=0, oldY=0*/;
void display(){
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef( (X-150)/150.0, -(Y-150)/150.0, 0);
printf("%.2f %.2f\n" , (X-150)/150.0 , -(Y-150)/150.0);
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.87f, -0.5f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(-0.87f, -0.5f);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
///mouse和motion的部分
/*void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
}*/
void motion(int x, int y)
{
X=x; Y=y;
glutPostRedisplay();
}
void idle()
{
angle+=0.01;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week03");///之後才會有GL
glutDisplayFunc(display);
glutMotionFunc(motion);
/// glutMouseFunc(mouse);
glutIdleFunc(idle);
glutMainLoop();
}
開始畫出茶壺和關節
程式碼如下
#include<GL/glut.h>
#include<stdio.h>
float angle=0 ,X=150 ,Y=150 , oldX=0, oldY=0;
void display(){
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot(0.2);
///TRT轉動
glTranslatef(0.2,0,0);
glRotatef(angle, 0.0f, 0.0f,1.0f);
glTranslatef(0.2,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glutSwapBuffers();
}
///加入mouse和motion的部分
///加入mouse和motion的部分
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
}
void motion(int x, int y)
{
angle += x-oldX;
X=x; Y=y;
X=x; Y=y;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week03");///之後才會有GL
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutMainLoop();
}
#include<stdio.h>
///float angle=0 ,angle2=0, angle3=0, angle4=0,X=150 ,Y=150 , oldX=0, oldY=0;
float angle[20], X=150,Y=150, oldX=0, oldY=0;
int ID=0;
void display(){
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(0.2,0,0);
glRotatef(angle[0], 0.0f, 0.0f,1.0f);
glTranslatef(0.2,0,0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(0.2,0,0);
glRotatef(angle[2], 0.0f, 0.0f,1.0f);
glTranslatef(0.2,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,0,0);
glRotatef(angle[3], 0.0f, 0.0f,1.0f);
glTranslatef(-0.2,0,0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(-0.2,0,0);
glRotatef(angle[4], 0.0f, 0.0f,1.0f);
glTranslatef(-0.2,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
///鍵盤移動的部分
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') ID=1;
if(key=='2') ID=2;
if(key=='3') ID=3;
if(key=='4') ID=4;
if(key=='5') ID=5;
}
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
}
void motion(int x, int y)
{
angle[ID] += x-oldX;
oldX=x;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week03");///之後才會有GL
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
///glutIdleFunc(idle);
glutMainLoop();
}
用鍵盤的按鍵讓關節移動
程式碼如下
#include<GL/glut.h>#include<stdio.h>
///float angle=0 ,angle2=0, angle3=0, angle4=0,X=150 ,Y=150 , oldX=0, oldY=0;
float angle[20], X=150,Y=150, oldX=0, oldY=0;
int ID=0;
void display(){
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(0.2,0,0);
glRotatef(angle[0], 0.0f, 0.0f,1.0f);
glTranslatef(0.2,0,0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(0.2,0,0);
glRotatef(angle[2], 0.0f, 0.0f,1.0f);
glTranslatef(0.2,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,0,0);
glRotatef(angle[3], 0.0f, 0.0f,1.0f);
glTranslatef(-0.2,0,0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(-0.2,0,0);
glRotatef(angle[4], 0.0f, 0.0f,1.0f);
glTranslatef(-0.2,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
///鍵盤移動的部分
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') ID=1;
if(key=='2') ID=2;
if(key=='3') ID=3;
if(key=='4') ID=4;
if(key=='5') ID=5;
}
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
}
void motion(int x, int y)
{
angle[ID] += x-oldX;
oldX=x;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week03");///之後才會有GL
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
///glutIdleFunc(idle);
glutMainLoop();
}
貼圖
1.要在地球上秀圖檔,請先做以下設定
2.先到上課用軟體下載MyEarth並解壓縮
3.以Notepad++開啟MyEarth中的cbp檔,並更改其目錄
(注意:上圖為更改前,下圖為更改後)
更改完後要儲存檔案,並以Codeblocks開啟檔案
4.先執行看看MyEarth的檔案
會發現找不到檔案
5.請把這些加入(請依照圖示去做)
6.這時在執行一次會發現有這錯誤,原因是未放入freeglut.dll的檔案
7.這時只要有圖檔就能把圖貼到球上
聲音,音效,音樂
5.可以在程式碼再做些改變
程式碼如下
#include<windows.h>///(0)要使用windows.h裡的定義
#include<mmsystem.h>///(1)多媒體系統
#include<stdio.h>
int main()
{
PlaySound("forest,wav",NULL,SND_ASYNC);///等它
printf("hello world\n");
int n;
scanf("%d",&n);
}
#include<windows.h>///(0)要使用windows.h裡的定義
#include<mmsystem.h>///(1)多媒體系統
#include<stdio.h>
int main()
{
PlaySound("forest,wav",NULL,SND_ASYNC);///等它
printf("hello world\n");
int n;
scanf("%d",&n);
}
計時器Timer
1.輸入下列程式碼產生計時器
1. #include<windows.h> 因為mmsystem.h需要window.h
2. #include<mmsystem.h> 多媒體系統才會有
3. void timer(int t)
{
glutTimerFunc(1000, timer, t+1); ///等多久,呼叫誰,參數 1000=1秒
PlaySound("Do.wav", NULL , SND_ASYNC);
}
4.glutTimerFunc(5000, timer,0); 在createwindow的這行程式碼下輸入
5000是指5秒後聲音即會想起
線性內插、動畫
注意:為了要了解內插,請先用excel去了解
1.先了解內差的公式
alpha* 新(角度值)+(1-alpha)*舊(角度值)
這時開啟excel去熟悉內插
alpha* 新(角度值)+(1-alpha)*舊(角度值)
這時開啟excel去熟悉內插
2.用程式寫內插
先把程式碼刪除(注意:請留下圖中有的程式碼)
加入下列的程式碼 (下方紅字請注意)
#include<GL/glut.h>
#include <stdio.h>
float angle=0,oldAngle=0,newAngle=90;///Now:角度、舊角度、新角度
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,0,1);///Now:會照角度去旋轉
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void timer(int t)
{
glutTimerFunc(1000, timer, t+1);///等多久,呼叫誰,參數
float alpha=(t)/30.0;///Now:算出一個內插的alpha值
angle= newAngle*alpha+oldAngle*(1-alpha);///Now:角度去內插
glutPostRedisplay();///Now:讀照著上面的角度,重畫
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutTimerFunc(0, timer,0);///Now:有個計時器函式 timer
glutDisplayFunc(display);
glutMainLoop();
}


沒有留言:
張貼留言