1. 下載課本的範例 https://jsyeh.org/3dcg10/ 👉 [data][win32]
3. mouse_glScalef
stap1:打開 Transformation 移動 (考試 : 一個佔十分)
上週的mouse滑鼠
step2: 桌面有"葉正聖老師上課用軟體" freeglut 安裝,再把 lib 的 libfreeglut.a 複製 成 libglut32.a
step3: 在 CodeBlocks 開新專案,專案名稱為 "week04_mouse_glScalef"
step4: 把 blog / github 的十行程式碼貼上
#include <GL/glut.h>
void display()
{
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);///143行
glutCreateWindow("檔名");
glutDisplayFunc(display);
glutMainLoop();
}
step5: 設定清背景的顏色
display 👉 glClearColor(1, 1, 0.9, 1);///R,G,B,A
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0, 1, 0);///3f=R,G,B ///設定其他顏色
step6: 利用滑鼠左右移動,放大縮小
display 👉 glPushMatrix();
glScalef(s, s, s); ///縮放s倍
glutSolidTeapot( 0.3 );
glPopMatrix();
👉 void motion(int x, int y)
{
s = 1 + (x-150)/150.0; ///會變大變小的變數0~1~2
display();///重畫畫面
}
include 下方 👉 float s = 1;
👉 glutMotionFunc(motion);///拖動motion的時候
4. 試著交換 translate/rotate 兩行程式
step1: 試課本範例 Transformation
step2: 先把 translate 移到右方,在rotate看看
step3: 下方程式,按右鍵,可 swap trnslate/rotate 交換
step4:在 rotate 看看,差在哪裡 👉 一個是公轉,一個是自轉
公轉:
自轉:
👀💬 理解技巧:
自轉
(1-1) 把左耳"靠著"左肩,再看
(1-2) 把"旋轉中、長胖的藍色車子"移到右邊
旋轉中、長胖的藍色車子
長胖的藍色車子
藍色車子
車子
公轉
(2-1) 把"全部的東西"做旋轉
(2-2) 放在右邊的高瘦的藍色車子
高瘦的藍色車子
藍色車子
車子
6. 將公轉和自轉運用到茶壺
step1: 開新專案,專案名稱為 "week04_mouse_translate_rotate_scale"
step2: 複製 "week04_mouse_glScalef" 的程式碼
step3: 修改、新增程式碼
display 的 glPushMatrix(); 👉 glTranslatef(teapotX, teapotY, 0);
glRotatef(angle++, 0, 0, 1);
include 下 👉 float teapotX = 0.5, teapotY = 0; ///放右邊
float angle = 0; ///旋轉角度
float s = 0.3; ///一開始是原本1倍的大小 ///修
main 👉 glutIdleFunc(display); /// 閒閒沒事時,就 display()
茶壺自轉
整個"旋轉中的縮很小的綠色的茶壺"移到右邊去
旋轉中的縮很小的綠色的茶壺
縮很小的綠色的茶壺
綠色的茶壺
茶壺
step4: 開新專案,專案名稱為 "week04_mouse_rotate_translate_scale"
step5: 複製 "week04_mouse_translate_rotate_scale" 的程式碼
step6: 交換2行程式碼
display 的 glPushMatrix(); 👉 glRotatef(angle++, 0, 0, 1); ///旋轉
glTranslatef(teapotX, teapotY, 0); ///移動
茶壺公轉
茶壺公轉+放大縮小
7. 集大成
step1: 開新專案,專案名稱為 "week04_keyboard_mouse_motion"
step2: 貼上10行程式碼
step3: 清背景、畫茶杯的程式碼
step4: 加入鍵盤事件函式
main 👉 glutKeyboardFunc(keyboard); ///鍵盤事件的函式
👉 void keyboard(unsigned char key, int x, int y)
{
printf("key:%c x:%d y:%d\n", key, x, y);
}
step5: 加入滑鼠事件函式
main 👉 void keyboard(unsigned char key, int x, int y)
{
printf("key:%c x:%d y:%d\n", key, x, y);
}
👉 void mouse(int button, int state, int x, int y)
{
teapotX[N] = (x-150)/150.0;
teapotY[N] = -(y-150)/150.0;
N++;
}
👉 int N=0;
float teapotX[1000], teapotY[1000];
display
👉 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);///清背景
for(int i=0; i<N; i++){
glPushMatrix();
glTranslatef(teapotX[i], teapotY[i], 0);
glutSolidTeapot( 0.1 ); ///茶壺大小,原0.3
glPopMatrix();
}
glutSwapBuffers();
沒有留言:
張貼留言