2017電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2017年6月28日 星期三
2017年6月6日 星期二
Week 16_04160373 梁家豪_洞陸洞陸_上課筆記
Week16
HW1_利用Excel嘗試內差公式
- 公式 A2(時間)*newvalue+(1-A2)*oldValue
HW2_用王婉懿的範例Build成功
- 用Notepad++開啟cbp專案檔,修改working、include、lib的路徑位置
HW3_讀檔案
- 使用File記事本紀錄動作座標,用fscanf();
2017年5月23日 星期二
Week 14_04160373 梁家豪_洞伍倆叁_上課筆記
Week14
(2)Build&Run
(3)Week14_01timer.cpp加入上週回家作業
1.在Main函式裡
2.
#include <stdio.h>
void timer(int t)
{
glutTimerFunc(100,timer,t+1);
angle++;
glutPostRedisplay();
}
void mouse(int button,int state, int x,int y)
{
oldX=x;oldY=y;
glutPostRedisplay();
}
void keyboard(unsigned char key,int x, int y)
{
if(key=='0') now=0;
if(key=='1') now=1;
if(key=='2') now=2;
if(key=='3') now=3;
if(key=='4') now=4;
}
HW1_開啟上週作業
(1).cpb專案檔,用Noteped++開啟
include目錄、lib目錄設定對Working目錄設"。"(句點)
(2)Build&Run
(3)Week14_01timer.cpp加入上週回家作業
HW2_Timer計時器
1.在Main函式裡
glutDisplayFunc(display);
glutTimerFunc(2000等多久,2000毫秒,timer呼叫函式,0參數)
#include <stdio.h>
void timer(int t)
{
glutTimerFunc(100,timer,t+1);
angle++;
glutPostRedisplay();
}
- Mouse函式來旋轉角度
void mouse(int button,int state, int x,int y)
{
oldX=x;oldY=y;
glutPostRedisplay();
}
- KeyBoard函式選擇要移動部位
void keyboard(unsigned char key,int x, int y)
{
if(key=='0') now=0;
if(key=='1') now=1;
if(key=='2') now=2;
if(key=='3') now=3;
if(key=='4') now=4;
}
- 角度陣列
float angle = 0 ; → float angle[20] = { } ;///歸零
2017年5月16日 星期二
Week 13_04160373 梁家豪_洞伍么陸_上課筆記
Week 13
glPushMatrix();//備份
glTranslatef();//手肘中心_(4)
glRotatef();//旋轉手臂_(3)
glTranslatef();//移動手肘到中心點_(2)
drawHand();//畫手函式(步驟1先畫)
glPopMatrix();//還原
HW1_練習小考1_TRT旋轉
glPushMatrix();//備份
glTranslatef();//手肘中心_(4)
glRotatef();//旋轉手臂_(3)
glTranslatef();//移動手肘到中心點_(2)
drawHand();//畫手函式(步驟1先畫)
glPopMatrix();//還原
HW2_openCV讀圖檔
Codeblock專案>Console App貝殼專案
#include <opencv/highgui.h>///(opencv目錄)High高級GUI使用者介面
using namespace std;
int main()
{
IplImage * img = cvLoadImage("c:/image.png");
cvShowImage("my2DImage",img);///秀圖
cvWaitKey(0);///讓程式不要直接結束
}
Step 1 設定專案Build Option設定compiler include:C:\OpenCV2.0\include
Step 2 Search directories > Linker lib:C:\opencv2.1\lib
Step 3 Link Setting cv210,cxcore210,highgui210
成功跑出圖片
HW3_轉動地球myEarth.zip專案
安裝opencv2.1、freeglut後打開myEarth.zip.cpp並執行
2017年5月9日 星期二
Week 12_04160373 梁家豪_洞伍洞勾_上課筆記
Week12
(1) #include "CMP3_MCI.h"///(1) 放在同一個程式碼專案的目錄中
(2) CMP3_MCI myMP3;///(2) 宣告一個變數,叫 myMP3
在 main()函式裡面
(3) myMP3.Load("C:\\dropbox\\yukai.mp3"); /// "C:/dropbox/yukai.mp3"
(4) myMP3.Play();/// (4) Play the file
gluLookAt(eyeX,eyeY,eyeZ,(調整)
centerX,centerY,
centerZ,upX,upY,upZ)
參數是 gluPerspective(fov張角, 長寬比例, zNear, zFar)
fov是field of view 視野張角, 以y方向來量測
zNear 及 zFar 裡面夾出來的模型, 才會顯示
HW1_MP3檔播放
Step1 下載CMP3_MCI.h檔
Step2 開啟CodeBlock專案
使用 CMP3_MCI.h 便可以輕鬆地將 MP3檔播放出來。(1) #include "CMP3_MCI.h"///(1) 放在同一個程式碼專案的目錄中
(2) CMP3_MCI myMP3;///(2) 宣告一個變數,叫 myMP3
在 main()函式裡面
(3) myMP3.Load("C:\\dropbox\\yukai.mp3"); /// "C:/dropbox/yukai.mp3"
(4) myMP3.Play();/// (4) Play the file
HW2_Michael Bay
Youtube搜尋Michael Bay觀看變型金剛導演拍攝時運鏡。
HW3_
jsyeh.org.3dcg10下載 data.zip。window.zip。glut32.dll。開啟Protation.exegluLookAt(eyeX,eyeY,eyeZ,(調整)
centerX,centerY,
centerZ,upX,upY,upZ)
參數是 gluPerspective(fov張角, 長寬比例, zNear, zFar)
fov是field of view 視野張角, 以y方向來量測
zNear 及 zFar 裡面夾出來的模型, 才會顯示
aspect值大小變化(長寬比例 aspect ration 就是 XY 方向的比例)
HW4_gluLookAt專案
11
2017年5月2日 星期二
Week 11_04160373 梁家豪_洞伍洞倆_上課筆記
Week 11
Link:(http://www.freespacemods.net/e107_plugins/download/download.php?view.129)
Step2 Link:jsyeh/3dcg10有data.zip解壓縮在桌面
Step3 3D Exploration,去看step2模型
Step4 匯出cpp(OpenGL )程式(使用Notepad++觀察soccerball)
Export Dialog裡Sample.cpp(才會有main()函式)
Step5 Codeblock.File-New-Project 開啟OpenGL專案
Remove移除main.c
Add新增soccerball.cpp
可調Visible區域才存
Dophins.obj→(data目錄裡需要有)
{Dophins1.obj Dophins1.mtl,Dophins2.obj Dophins2.mtl,Dophins3.obj Dophins3.mtl}
my
HW1_google 3D exploration(3D的檔案總管)
Step1 1.81免費試用版本(4.5MB)Link:(http://www.freespacemods.net/e107_plugins/download/download.php?view.129)
Step2 Link:jsyeh/3dcg10有data.zip解壓縮在桌面
Step3 3D Exploration,去看step2模型
Step4 匯出cpp(OpenGL )程式(使用Notepad++觀察soccerball)
Export Dialog裡Sample.cpp(才會有main()函式)
Step5 Codeblock.File-New-Project 開啟OpenGL專案
Remove移除main.c
Add新增soccerball.cpp
F9 Build&Run 有2行錯誤.刪掉3280跟3281行
HW2_用3D Exploration來折模型
Save As:Wavefront Obj檔可調Visible區域才存
Dophins.obj→(data目錄裡需要有)
{Dophins1.obj Dophins1.mtl,Dophins2.obj Dophins2.mtl,Dophins3.obj Dophins3.mtl}
my
Dophins2和3如Dophins1一樣匯出。
2017年4月25日 星期二
Week 10_04160373 梁家豪_洞肆倆伍_上課筆記
Week 10
Step2 開啟Transformation>Soccerball
HW1_Maya匯出OBJ模型
Step1 Download Link:http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
下載: [Source][data][win32] glut32.dll
下載: [Source][data][win32] glut32.dll
Step2 開啟Transformation>Soccerball
Step3 用右鍵Notepad++開啟data資料夾內soccerball.mtl、soccerball.obj檔案
HW2
將老師提供的myGLMsample解壓縮試著執行
HW3
Google:NeHe Lesson32 Download:visual studio.Net版本
射擊遊戲 lesson32解壓縮,並執行Lesson32
HW4_加入WAV音效
在Lesson32.cpp找sound。
PlaySound("路徑/檔名.wav",NULL.SND_ASYNC)
#include <mmsystem.h>
void mouse(int button,int state,int x,int y)
{
PlaySound("聲音檔所在的位置",NULL,SND_ASYNC);
}
glutMouseFunc(mouse);
#include <mmsystem.h>
void mouse(int button,int state,int x,int y)
{
PlaySound("聲音檔所在的位置",NULL,SND_ASYNC);
}
glutMouseFunc(mouse);
HW5
2017年4月11日 星期二
Week 8_04160373 梁家豪_洞肆么么_上課筆記
Week08
Step (1)找light留8行
Step (2)找light留8行
Step (3)找light留8行
Step (4)留你的display()
Step (5)留你的main()
#include <GL/glut.h>
static void display(void)
{
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
const double a = t*90.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);
glPushMatrix();//備份矩陣
glutSolidTeapot(0.3);//茶壺
glPopMatrix();//還原矩陣
glutSwapBuffers();
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
///glutReshapeFunc(resize);
glutDisplayFunc(display);
///glutKeyboardFunc(key);
///glutIdleFunc(idle);
glClearColor(1,1,1,1);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutMainLoop();
return EXIT_SUCCESS;
}
HW1_打光 Project GLUT
Step (2)找light留8行
Step (3)找light留8行
Step (4)留你的display()
Step (5)留你的main()
#include <GL/glut.h>
static void display(void)
{
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
const double a = t*90.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);
glPushMatrix();//備份矩陣
glutSolidTeapot(0.3);//茶壺
glPopMatrix();//還原矩陣
glutSwapBuffers();
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
///glutReshapeFunc(resize);
glutDisplayFunc(display);
///glutKeyboardFunc(key);
///glutIdleFunc(idle);
glClearColor(1,1,1,1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutMainLoop();
return EXIT_SUCCESS;
}
HW2_延伸Hw1打光 Project GLUT
茶壺視窗閒置時,做旋轉
茶壺視窗閒置時,做旋轉
加入旋轉角度angle;
#include <GL/glut.h>
float angle=0;
static void display(void)
{
angle++;
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
const double a = t*90.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);
glPushMatrix();//備份矩陣
glRotated(anfle,0,1,0);//旋轉
glutSolidTeapot(0.3);//茶壺
glPopMatrix();//還原矩陣
glutSwapBuffers();
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
///glutReshapeFunc(resize);
glutDisplayFunc(display);
///glutKeyboardFunc(key);
glutIdleFunc(display);///閒置時候,就一直呼叫diplay()
glClearColor(1,1,1,1);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutMainLoop();
return EXIT_SUCCESS;
}
float angle=0;
static void display(void)
{
angle++;
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
const double a = t*90.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);
glPushMatrix();//備份矩陣
glRotated(anfle,0,1,0);//旋轉
glutSolidTeapot(0.3);//茶壺
glPopMatrix();//還原矩陣
glutSwapBuffers();
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
///glutReshapeFunc(resize);
glutDisplayFunc(display);
///glutKeyboardFunc(key);
glutIdleFunc(display);///閒置時候,就一直呼叫diplay()
glClearColor(1,1,1,1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutMainLoop();
return EXIT_SUCCESS;
}
HW3_glut各種形狀
glutSolidTeapot(大小)
glutWireTeapot(大小)
glutSolidSphere(r,slice,stack)//半徑,柳丁切,堆疊
2017年3月28日 星期二
Week 6_04160373 梁家豪_洞叁倆八_上課筆記
Week 06
HW1_練習期中考題函式
glPushMatrix();//備份矩陣glRotatef(angle,x,y,z);//(旋轉角度,x,y,z)
glTranslatef(x,y,z);//移動
glScalef(x,y,z);//縮放
glBegin(GL_POLYGON);//開始畫
glNormal3f(nx,ny,nz);//打光的法向量
glTexCoord2f(tx,ty);//貼圖座標
glColor3f(r,g,b);//顏色
glVertex3f(x,y,z);//頂點
glEnd();//結束
glPopMatrix();//還原矩陣
HW2_理解函式運作邏輯
glPushMatrix();
glRotatef(angle,x,y,z);
glTranslatef(x,y,z);
glScalef(x,y,z);
glBegin(GL_POLYGON);
glNormal3f(nx,ny,nz);
glTexCoord2f(tx,ty);
glColor3f(r,g,b);
glVertex3f(x,y,z);
glEnd();
glPopMatrix();
glut函式具有階層性的轉動,先執行Begin(開始)~End(結束)函式及其內部函式。接續到外層的縮放(Scalef)、移動(Translatef)、在旋轉(Rotatef),由內而外的運行glut函式。
理解在在Transformation.exe檔中,可以利用右手來判別物體旋轉的方向。
Rotatef(x,y,y軸的方向,z)
HW3_T-R-T
#include <GL/glut.h>float mouseX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(mouseX, 0,0,1);
glColor3f(1,0,0);glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.5, 0.14,0);//把白色茶壺手把移動到紅色茶壺嘴
glRotatef(mouseX, 0,0,1);//step2滑鼠轉動
glTranslatef(0.41, -0.05,0);//step1 把手把移到轉盤位置
glColor3f(1,1,1); glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
mouseX = x;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("TRT robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
2017年3月21日 星期二
Week 5_04160373 梁家豪_洞叁倆么_上課筆記
Week 5
HW1_登入Zuvio.com.tw回答Week4問題
HW2_下載glut32
Link:http://jsyeh.ong/3dcg10
步驟1Download (1)data.zip(2)win32.zip(3)glut32.dll
步驟2開啟Transformation.exe試著縮放,旋轉
HW3_PushMatrix,PopMatrix
#include<gl/glut.h>
void display()
{
glPushMatrix();//備份
glTranslatef(0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();//還原
glutSwapBuffers();
}
int main(int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("04160373_translate rotate scale");
glutDisplayFunc(display);
glutMainLoop();
}
HW4_茶壺移動Translate旋轉Rotate縮放Scale
移動Translate,滑鼠左鍵按住茶壺可以移動它
#include<gl/glut.h>
float mouseX=0,mouseY=0;//滑鼠座標
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//做清除顏色及深度動作
glPushMatrix();//備份
glTranslatef(mouseX,mouseY,0);
glutSolidTeapot(0.3);
glPopMatrix();//還原
glutSwapBuffers();
}
void motion(int x,int y)
{
mouseX=(x-150)/150.0;
mouseY= -(y-150)/150.0;
glutPostRedisplay();//貼一張便利貼Post,更新電腦畫面
}
int main(int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("04160373_translate rotate scale");
glutDisplayFunc(display);
glutMotionFunc(motion);//移動函式
glutMainLoop();
}
縮放Scale,滑鼠左鍵按住茶壺可以縮放它
#include<gl/glut.h>
float mouseX=0,mouseY=0;//滑鼠座標
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//做清除顏色及深度動作
glPushMatrix();//備份
glScalef(mouseX,mouseY,0);//縮放函式
glutSolidTeapot(0.3);
glPopMatrix();//還原
glutSwapBuffers();
}
void motion(int x,int y)
{
mouseX=(x-150)/150.0;
mouseY= -(y-150)/150.0;
glutPostRedisplay();//貼一張便利貼Post,更新電腦畫面
}
int main(int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("04160373_translate rotate scale");
glutDisplayFunc(display);
glutMotionFunc(motion);//移動函式
glutMainLoop();
}
旋轉Rotate,滑鼠左鍵按住茶壺可以旋轉它
#include<gl/glut.h>
float mouseX=0,mouseY=0,rotX=0;//滑鼠座標
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//做清除顏色及深度動作
glPushMatrix();//備份
glRotatef(rotX,1,0,0);//旋轉函式(x,y,z)
glutSolidTeapot(0.3);
glPopMatrix();//還原
glutSwapBuffers();
}
void motion(int x,int y)
{
mouseX=(x-150)/150.0;
mouseY= -(y-150)/150.0;
rotX=x;
glutPostRedisplay();//貼一張便利貼Post,更新電腦畫面
}
int main(int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("04160373_translate rotate scale");
glutDisplayFunc(display);
glutMotionFunc(motion);//移動函式
glutMainLoop();
}
2017年3月14日 星期二
Week 4_04160373 梁家豪_洞叁么肆_上課筆記
Week 4
HW1互評同學GLUT作業圖,挑選出3個自認為最好作品上傳至Moodle。
HW2利用三角函數畫出正圓形
glVertex2f(cos(angle),sin(angle));
#include <GL/glut.h>
#include <math.h>//宣告數學函式頭檔
void display()
{
glBegin(GL_POLYGON);
for(float angle=0; angle <= 3.1415926 *2; angle += 0.01){
glVertex2f( cos(angle), sin(angle) );
}
glEnd();
glutSwapBuffers();
}
int main(int argc, char*argv[] )
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB |GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("GLUT shape");//視窗名稱
glutDisplayFunc(display);
glutMainLoop();
}
Hw3 加上Mouse函式
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
static void display(void)
{
glBegin(GL_POLYGON);
for(float angle=0;angle<=3.1415926*2;angle+=0.01){
glVertex2f(cos(angle),sin(angle));
}
glEnd();
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
printf("%d %d %d %d\n",button,state,x,y);
glColor3f(1,x/300.0,y/300.0);//x,y表示滑鼠作標,除以圓大小300*300
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160373 Taiwan Number One");
glutDisplayFunc(display);
glutMouseFunc(mouse);//呼叫mouse函式
glutMainLoop();
return EXIT_SUCCESS;
}
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
static void display(void)
{
glBegin(GL_POLYGON);
for(float angle=0;angle<=3.1415926*2;angle+=0.01){
glVertex2f(cos(angle),sin(angle));
}
glEnd();
glutSwapBuffers();
}
void motion(int x,int y)
{
glColor3f(1,x/300.0,y/300.0);
glutPostRedisplay();//Post個便利貼,告知電腦Re-display
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160373 Taiwan Number One");
glutDisplayFunc(display);
glutMotionFunc(motion);//滑鼠拖曳變色函式
glutMainLoop();
return EXIT_SUCCESS;
}
Hw4嘴巴會動的小精靈
加入Motion函式 拖曳(Drag)使圖形動起來
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
float mouth=0;
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//做清除
glBegin(GL_TRIANGLE_FAN);//三角扇形
glVertex2f(0,0);
for(float angle=0 +mouth;angle<=3.1415926*2 - mouth;angle+=3.1415926/100){//
glVertex2f(cos(angle),sin(angle));
}
glEnd();
glutSwapBuffers();
}
void motion(int x,int y)//MOUSE Dragging!!滑鼠拖曳
{
mouth = x/300.0;
glColor3f(1,x/300.0,y/300.0);
glutPostRedisplay();//Post個便利貼,告知電腦Re-display
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160373 Taiwan Number One");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
return EXIT_SUCCESS;
}
訂閱:
文章 (Atom)














































