☝1
今天的第一個程式, 先到老師的網站 https://jsyeh.org/3dcg10/ 下載三個檔案
data win32 glut32.dll
下載完畢後解壓縮
位置要放對哦!
下載 > windows > data ( data 要解壓縮到 windows 裡哦, 裡面有很多的模型 )
下載 > windows >Transformation.exe
點開 Transformation.exe 就能看到今天的第一個程式啦
可以嘗試玩玩看,其中 Translate 就是今天要上的重點啦!
快速複習一下上週的程式碼
讓我們來增加一些東西吧!
glPushMatrix() 備份矩陣!
glTranslatef( x, y, z ) 移動!
glPopMatrix() 還原矩陣!
#include <GL/glut.h>
void display()
{
glPushMatrix();///step 2 備份矩陣
glTranslatef( 0.5, 0, 0);///step 1 只有這行?還不夠
glutSolidTeapot(0.3);///畫出一個實心的茶壺,它的大小是0.3
glPopMatrix();///step 2 還原矩陣
glutSwapBuffers();///請GLUT把畫面swap送到顯示的地方
}
int main(int argc, char *argv[])
{ ///上面是特別的main函式,有很多參數
glutInit(&argc, argv);///把GLUT開起來 (GLUT初始化)
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
///上面這行是把顯示的模式設定好
glutCreateWindow("GLUT Shapes");///要開視窗
glutDisplayFunc(display);///要顯示的對應函式
glutMainLoop();///最後用main迴圈,壓在最後面
}
我們想試著讓茶壺更自由的動起來,首先來了解一下 mouse 函式
使用 glutMouseFunc(mouse) 請mouse函式幫忙
因為想了解輸出了什麼,加上了 printf ,因為使用 printf 所以需要引用 <stdio.h>
#include <GL/glut.h>
void display()
{
glPushMatrix();///備份矩陣
glTranslatef( 0.5, 0, 0);///只有這行?還不夠
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
}
#include <stdio.h>
void mouse(int button, int state, int x,int y)
{ ///請mouse函式幫忙
printf("%d %d %d %d\n",button, state, x, y);
/// button 左鍵 右鍵等, state 按下去是0 抬起來是1
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week03");
glutDisplayFunc(display);
glutMouseFunc(mouse);///請mouse函式幫忙
glutMainLoop();
}
ex: 左鍵 按下 位置 x軸16 y軸8
左鍵 提起 位置 x軸16 y軸8
了解過後,我們修改 mouse 函數,讓茶壺真正動起來吧!
茶壺最後出現的位置用大寫的 X 和 Y 表示
為了避免圖案ㄍㄡˊ在一起,使用glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );來清背景
因為這次不印出值了,<stdio.h>就能註解掉了
小寫 x y 是我們按下回傳給電腦的位置
一樣運用口訣,減一半、除一半、y負號
把換算好的值丟入我們大寫的 X Y 中
接著,老師介紹一個新功能能讓我們的 blogger 變得更好看!
首先!在 https://gist.github.com/ 登入
輸入完畢後, 點選 Embed, 複製貼來blogger 就會有好看的程式碼啦
(使用HTML檢式, 再將複製的網址貼上, 就是要有點網頁的概念, 不然像我一樣排版到頭好大)
✌2
今天的第二個程式, 前面都一樣啦 ~
不過這次!我們想讓小黑自己生成程式給我,就不用像上週那樣拼命找點了!
這次我們自己設置視窗大小,glutInitWindowSize(500,500) 500 X 500
一樣讓 mouse 函式幫助我們
上週提過重要的函式,用 glVertex2f( x, y ) 畫頂點
如果 state 等於 滑鼠按下去的話,印出程式碼
if( state == GLUT_DOWN ){
printf(" glVertex2f(%.3f,%.3f);\n", X, Y);
}
printf(" glVertex2f(%.3f,%.3f);\n", X, Y);
}
此時視窗中出現好多行程式碼,全部複製下來!
貼到上週教的 glBegin(GL_POLYGON) 開始畫圖型的函式 和 glEnd() 結束畫圖中間
沒有留言:
張貼留言