2021電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2021年6月7日 星期一
電腦圖學 week 15
2021年6月2日 星期三
本人發言不代表本人立場 week15
主題:機器人2.0 (關節轉動、存讀檔)
1. fout 、fscanf
- 用fout時,file指標fout配合模式w+
#include <stdio.h>
int main(int argc, char**argv)
{
FILE*fout=NULL;///檔案的指標
fout=fopen("new.txt","w+");///用w+模式開檔
printf("Hello World\n");
fprintf(fout,"Hello World\n");
}
- 用fscanf時,file指標fin配合模式r
fin = fopen("new1.txt","r");
char line[100];
fscanf(fin,"%s",line);
printf("what you read: %s\n",line);
fscanf(fin,"%s",line);
printf("what you read: %s\n ",line);
2. 關節
- 先複製老師給的程式碼,用display畫出三個茶壺(手臂)
- 接著利用timer改變角度
#include <stdio.h>#include <GL/glut.h>float angle=0,diff=2;void timer(int t){glutTimerFunc(30,timer,t+1);angle+=diff;if(angle>90) diff=-2;if(angle<0) diff=+2;glutPostRedisplay();///重畫}void display(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glPushMatrix();///glutSolidTeapot( 0.3 );///身體glPushMatrix();glTranslatef(-0.3,0,0);///(3)掛在正確的地方glRotatef(angle,0,0,1);///(2)旋轉glTranslatef(-0.4,0,0);///(1)把關節旋轉中心放到畫面中心glutSolidTeapot( 0.3 );///左手臂(重疊了)glPushMatrix();glutSolidTeapot( 0.3 );///左手肘(重疊了)glPopMatrix();glPopMatrix();glPopMatrix();glutSwapBuffers();}int main( int argc, char ** argv ){glutInit( &argc, argv );glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);glutCreateWindow("week15");glutTimerFunc(0,timer,0);glutDisplayFunc(display);glutMainLoop();}
執行結果:
- 利用TRT完成左手臂
void display(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glPushMatrix();glutSolidTeapot( 0.3 );///身體glPushMatrix();glTranslatef(-0.3,0,0);///(3)掛在正確的地方glRotatef(angle,0,0,1);///(2)旋轉glTranslatef(-0.4,0,0);///(1)把關節旋轉中心放到畫面中心glutSolidTeapot( 0.3 );///左手臂(重疊了)glPushMatrix();glTranslatef(-0.3,0,0);///(3)掛在正確的地方glRotatef(angle,0,0,1);///(2)旋轉glTranslatef(-0.4,0,0);///(1)把關節旋轉中心放到畫面中心glutSolidTeapot( 0.3 );///左手肘(重疊了)glPopMatrix();glPopMatrix();glPopMatrix();glutSwapBuffers();}
執行結果:
- 用陣列整合angle
- 用鍵盤控制轉動
#include <stdio.h>#include <GL/glut.h>float angle[20]={},diff=2;///本來只有一個角度,現在有很多個,初始為0int angleID=0;///現在要改第幾個角度void timer(int t){glutTimerFunc(30,timer,t+1);angle[angleID]+=diff;if(angle[angleID]>90) diff=-2;if(angle[angleID]<0) diff=+2;glutPostRedisplay();///重畫}void keyboard(unsigned char key,int x,int y){if(key=='1') angleID=1;if(key=='0') angleID=0;if(key=='2') angleID=2;if(key=='3') angleID=3;}void display(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glPushMatrix();glutSolidTeapot( 0.3 );///身體glPushMatrix();glTranslatef(-0.3,0,0);///(3)掛在正確的地方glRotatef(angle[0],0,0,1);///(2)旋轉glTranslatef(-0.4,0,0);///(1)把關節旋轉中心放到畫面中心glutSolidTeapot( 0.3 );///左手臂(重疊了)glPushMatrix();glTranslatef(-0.3,0,0);///(3)掛在正確的地方glRotatef(angle[1],0,0,1);///(2)旋轉glTranslatef(-0.4,0,0);///(1)把關節旋轉中心放到畫面中心glutSolidTeapot( 0.3 );///左手肘(重疊了)glPopMatrix();glPopMatrix();glPopMatrix();glPushMatrix();glTranslatef(0.3,0,0);///(3)掛在正確的地方glRotatef(angle[2],0,0,1);///(2)旋轉glTranslatef(0.3,0,0);///(1)把關節旋轉中心放到畫面中心glutSolidTeapot( 0.3 );///右手臂(重疊了)glPushMatrix();glTranslatef(0.3,0,0);///(3)掛在正確的地方glRotatef(angle[3],0,0,1);///(2)旋轉glTranslatef(0.3,0,0);///(1)把關節旋轉中心放到畫面中心glutSolidTeapot( 0.3 );///右手肘(重疊了)glPopMatrix();glPopMatrix();glutSwapBuffers();}int main( int argc, char ** argv ){glutInit( &argc, argv );glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);glutCreateWindow("week15");glutKeyboardFunc(keyboard);glutTimerFunc(0,timer,0);glutDisplayFunc(display);glutMainLoop();}
築損 Week15
第0部份
前置作業
printf 直接顯示在執行視窗
fprintf 能存在檔案中
修蓋cbp檔
將word dir=".";
檔案就會存到專案中
能讀取檔案中文字
執行結果
-----------------------------------------------------------------------------------------------------------------------------
第1部份
TRT
先讓手臂轉動
程式碼
glPushMatrix();
glTranslatef(-0.3,0,0);///掛在正確的地方
glRotatef(angle,0,0,1);///旋轉
glTranslatef(-0.3,0,0);///旋轉中心放到畫面中心
glutSolidTeapot( 0.3 );///左手臂
glPopMatrix();
再加上身體跟手肘
程式碼
glPushMatrix();
glutSolidTeapot( 0.3 );///身體
glPushMatrix();
glTranslatef(-0.3,0,0);///掛在正確的地方
glRotatef(angle,0,0,1);///旋轉
glTranslatef(-0.3,0,0);///旋轉中心放到畫面中心
glutSolidTeapot( 0.3 );///左手臂
glPushMatrix();
glTranslatef(-0.3,0,0);///掛在正確的地方
glRotatef(angle,0,0,1);///旋轉
glTranslatef(-0.3,0,0);///旋轉中心放到畫面中心
glutSolidTeapot( 0.3 );///左手肘
glPopMatrix();
glPopMatrix();
glPopMatrix();
接著增加鍵盤控制手臂手肘
先把angle改成angle[20]={};
新增angleID=0;
把timer裡 angle改成 angle[angleID]
新增keyboardc函式
程式碼
void timer(int t)
{
glutTimerFunc(30,timer,t+1);
angle[angleID]+=diff;
if(angle[angleID]>90) diff=-2;
if(angle[angleID]<0) diff=+2;
glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='0')angleID=0;
if(key=='1')angleID=1;
}
按1或0能讓手臂或手肘停止
複製
右半邊就出來了
總程式碼
執行結果
-----------------------------------------------------------------------------------------------------------------------------
第0部份
執行結果
-----------------------------------------------------------------------------------------------------------------------------
第0部份
執行結果
-----------------------------------------------------------------------------------------------------------------------------
距地表面160 Week11
#include "glm.h" GLMmodel* pmodel = NULL; void drawmodel(void) { if (!pmodel) { pmodel = glmReadOBJ("data/porsche.obj...
-
先用上一周的開始加新的東西 ///把 Week15_file 的 main.cpp 拿來用 #include <stdio.h> #include <GL/glut.h> float angle[20]={}, diff=2;///Step05 本來只有...
-
加背 景 上程式碼 #include <opencv/highgui.h> ///使用 OpenCV 2.1 比較簡單, 只要用 High GUI 即可 #include <opencv/cv.h> #include <GL/glut.h>...
-
下載 OpenCV-2.1.0-win32-vs2008.exe 下載選第2個 Add OpenCV to PATH(執行的目錄之一) 位置用預設的 C:\OpenCV2.1 安裝完重開codeblocks cv210 cxcore210 highgui210 ---...


















