豪哥的電腦圖學筆記week17
step02-1
1.working dir 路徑改成 "." 然後桌面freeglut資料夾複製freeglut.dll進專案資料夾
茶壺打光
2022電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
豪哥的電腦圖學筆記week17
step02-1
1.working dir 路徑改成 "." 然後桌面freeglut資料夾複製freeglut.dll進專案資料夾
茶壺打光
week16筆記
step01-1
alpha內插公式 alpha '0.0~1.0'
angle=alpha*新+(1-alpha)*舊
alpha:0 > 舊
alpha:0.5 > 半新半舊
alpha:1 > 新
可以用excel來實作
step01-2
用week15_angles_TRT_again 拿來改
1.File new project week16_interpolation 內插
2.按s存一行,按r動作不連續
///week16_interpolation
///改編自week15_angles_TRT_again
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleID=0;
FILE * fout=NULL, * fin = NULL;
void myWrite()
{///每呼叫一次myWrite
if(fout == NULL)fout=fopen("file.txt","w+");
for(int i = 0;i<20;i++)
{
printf("%.1f ",angle[i]);///小黑印出來
fprintf(fout,"%.1f ",angle[i]);///檔案印出來
}///這裡老師沒有fclose
printf("\n");///小黑印出跳行
fprintf(fout,"\n");///檔案跳行
}
float NewAngle[20],OldAngle[20];
void myRead()
{
if(fout!=NULL){fclose(fout); fout=NULL;}
if(fin==NULL) fin=fopen("file.txt","r");
for(int i=0;i<20;i++)
{
OldAngle[i] = NewAngle[i];
fscanf(fin, "%f", &NewAngle[i] );
///fscanf(fin,"%f",&angle[i]);
}
glutPostRedisplay();///重劃畫面
}
void myInterpolate(float alpha){
for(int i=0;i<20;i++){
angle[i]= alpha * NewAngle[i] + (1-alpha) * OldAngle[i];
}
}
int t=0;
void keyboard(unsigned char key,int x,int y)
{
if(key=='p'){
if(t%30==0) myRead();
myInterpolate( (t%30)/30.0 );///介於0.0~1.0
glutPostRedisplay();
t++;
}
if(key=='s') myWrite();///調好動作才save存檔
if(key=='r') myRead();
if(key=='0') angleID=0;
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
}
void mouse(int button,int state,int x,int y)
{
oldX=x;
}
void motion(int x,int y)
{
angle[angleID]+=(x-oldX);
///myWrite();
oldX=x;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf(0.3,0.5,-0.3,-0.5);
glPushMatrix();
glTranslatef(0.3,0.4,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,-0.4,0);
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPushMatrix();
glTranslatef(0.7,0.4,0);
glRotatef(angle[1],0,0,1);
glTranslatef(-0.7,-0.4,0);
glColor3f(0,1,0);
glRectf(0.7,0.5,1.0,0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3,0.4,0);
glRotatef(angle[2],0,0,1);
glTranslatef(0.3,-0.4,0);
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.7,0.3);
glPushMatrix();
glTranslatef(-0.7,0.4,0);
glRotatef(angle[3],0,0,1);
glTranslatef(0.7,-0.4,0);
glColor3f(0,1,0);
glRectf(-0.7,0.5,-1.0,0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15_angles_TRT_again");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
電腦圖學筆記week15
step01-1
PlaySound() 更詳細解說
0.New Empty File,存檔成week15-1_PlaySound.cpp
1.#include<mmsystem.h>上週教的
2.#include<windows.h>這週教的
3.PlaySound( " 檔名.wav" , NULL,SND_ASYNC );//上週
4.PlaySound( "檔名.wav" , NULL,SND_SYNC );//這週
5.Setting-Complier Setting,Linker 加入 winmm
step02-1
WAV (大/原始)vs. MP3(小/有壓縮)
PlaySound只能用WAV檔
0. New Empty File ,week15-3_mp3.cpp
1.在moodle下載 CMP3_MCI.h
2.程式碼 #include "CMP3_MCI.h"
3.宣告 CMP3_MCI mp3;
4.mp3.Load("檔名.mp3");
5.mp3.Play();
#include <stdio.h>
#include "CMP3_MCI.h"
CMP3_MCI mp3;
int main()
{
mp3.Load("badbad.mp3");
mp3.Play();
printf("隨便等你輸入數字,程式就卡住囉: ");
int N;
scanf("%d",&N);
}
Week14筆記
step01-1
寫檔(為了記錄動畫的動作)
0.new empty file,存檔成week14-1_fprintf裡的week14.cpp
1.fopen() 開檔案
2.printf() ==> fprintf()
week13
step01-1
1.新增專案
2.加入新教的程式碼(rect),glutInitWindowSize(600,600);可調整視窗大小
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT);
glRectf(0.3,0.5,-0.3,-0.5);
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("week13 rect TRT");
glutDisplayFunc(display);
glutMainLoop();
}
step1-1
1.主題TRT(移動-旋轉-移動)對特定軸轉動
2.下周考試TRT
step01-2
1.用程式來理解對特定軸轉動120.125.80.50/GL
2.右下角點ToDraw 左邊黑色畫圖
3.ctrl-r reload可以清空
4.可以畫身體 手臂 叫做 myDrawObject(0)和myDrawObject(1)
5.改程式碼的順序
6.可以按angle= 再按空白鍵 會自動改變動畫
step2-1
1.把glRotatef()那一行變紅色,就可以移動左邊的東西
```cpp
glPushMatrix();
myDrawObject(0);//畫身體
glRotatef(angle,0,0,1);//這個旋轉,會轉下面整個東西
glTranslatef(-0.3,-0.19,0);//往左下方移(讓軸心 放世界的中心)
myDrawObject(1);//畫手臂(右上方)
glPopMatrix();
```
step2-2
講解下周考試題目
glPushMatrix();
glTranslatef(-0.5,-0.9,0);
glRotatef(-45,0,0,1);
glTranslatef(-0.8,0.9,0);
drawHand();
glPopMatrix();
會換圖
step2-3
1.開新GLUT專案 week12_TRT
2.把之前的10行筆記貼上來
3.茶壺旋轉
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.2,0,0);
glRotatef(angle,0,0,1);
glTranslatef(0.2,0,0);
glColor3f(1,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glutSwapBuffers();
angle+=0.1; ///每次執行 display() 加1度
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week12 TRT");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();
}
TRT2
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glutSolidTeapot(0.3);///茶壺當身體
glPushMatrix();///右邊的手臂手肘
glTranslatef(0.2,0,0);///(3)掛到右邊(整個往動)
glRotatef(angle, 0,0,1);///(2)旋轉
glTranslatef(0.2,0,0);
glColor3f(1,0,0);
glutSolidTeapot(0.2);///小茶壺,當作小手臂
glPushMatrix();
glTranslatef(0.2,0,0);///(3)掛到右邊(整個往動)
glRotatef(angle, 0,0,1);///(2)旋轉
glTranslatef(0.2,0,0);
glColor3f(1,0,0);
glutSolidTeapot(0.2);///小茶壺,當作小手肘
glPopMatrix();
glPopMatrix();
glPushMatrix();///左邊的手臂手肘
glTranslatef(-0.2,0,0);///(3)掛到左邊(整個往動)
glRotatef(-angle, 0,0,1);///(2)旋轉
glTranslatef(-0.2,0,0);
glColor3f(1,0,0);
glutSolidTeapot(0.2);///小茶壺,當作小手臂
glPushMatrix();
glTranslatef(-0.2,0,0);///(3)掛到左邊(整個往動)
glRotatef(-angle, 0,0,1);///(2)旋轉
glTranslatef(-0.2,0,0);
glColor3f(1,0,0);
glutSolidTeapot(0.2);///小茶壺,當作小手肘
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
angle+=0.01; ///每次執行 display() 加1度
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week12_TRT");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();
}
一. 一樣先安裝且設定好freeglut,OpecCV, 開啟CodeBlocks建立新專案 week11_gundam, 把 MyGundam.zip下載解壓縮後的data資料夾放到freeglut/bin裡面 把week09_openc...