# Week04
今天的上課內容:
1.老師講解了上週作業2畫圓的方式:利用三角函數去畫圓
程式範例1:
```
#include <GL/glut.h>
#include <math.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,0);
glBegin(GL_POLYGON);
for(float angle=0;angle<3.14159*2; angle+=0.1){
glVertex2f(cos(angle),sin(angle));
}
glEnd();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("week03ªºµøµ¡");
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
```
程式範例2:調整圓的大小:
```C++
#include <GL/glut.h>
#include <math.h>
void myCircle(float r)
{
glBegin(GL_POLYGON);
for(float angle=0;angle<3.14159; angle+=0.1){
glVertex2f(r*cos(angle),r*sin(angle));
}
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,0,0);
myCircle(0.8);
glColor3f(1,1,0);
myCircle(0.6);
glColor3f(0,1,0);
myCircle(0.5);
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("week03ªºµøµ¡");
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
2.開啟第一個範例:
開啟網站https://jsyeh.org/3dcg10/
下載05/04/Examples/data&win32
data解壓縮後的資料夾放進windows解壓縮後的資料夾
打開windows資料夾裡的Transformation.exe即可
3.今天的主題一:Translate移動
(1)打開一個新GLUT檔案,命名為week04_translate,一樣位置在桌面的freeglut裡面
嘗試用程式移動紅色茶壺的位置,要記得要加上glPushMatrix() glPopMatrix這兩行函式,以免移動數值累積,造成茶壺不斷移動。
程式如下:
```
void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix();///push備份矩陣 glTranslatef( 0.3, 0.2, 0);///右上角 ///就不會一直偷偷動,因為移動會累積 glColor3f(1,0,0);///red glutSolidTeapot(0.3); glPopMatrix();///pop還原矩陣 glutSwapBuffers(); } int main( int argc, char**argv ) { glutInit( &argc, argv); glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH ); glutCreateWindow("week04 translate"); glutDisplayFunc(display); glutMainLoop(); }
```
小插曲:老師教我們改blog的發布時間&GitHub的使用時間
4.今天的第二個練習程式:xy座標
分配四個茶壺的位置,程式碼如下:
```
#include <GL/glut.h> void myTeapot(float x, float y)///自訂的函式 { glPushMatrix();///push備份矩陣 glTranslatef( x, y, 0); glutSolidTeapot(0.3); glPopMatrix();///pop還原矩陣 } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1,0,0);///red myTeapot(+0.5, +0.5); myTeapot(+0.5, -0.5); myTeapot(-0.5, -0.5); myTeapot(-0.5, +0.5); glutSwapBuffers(); } int main( int argc, char**argv ) { glutInit( &argc, argv); glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH ); glutCreateWindow("week04 translate"); glutDisplayFunc(display); glutMainLoop(); }
```
5.今天的主題二:滑鼠事件
使用glutMouseFunc(mouse)來註冊
void mouse(int button,int state,int x,int y)這個函式,最後用print()將這四個值印出來
了解: 左鍵0 中鍵1 右鍵2 按下去0 放開1 座標為0...300
程式碼如下:
```
#include <GL/glut.h> #include <stdio.h>///printf() void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glutSwapBuffers(); } void mouse(int button, int state, int x, int y) { printf("%d %d %d %d\n", button, state, x, y); } int main( int argc, char**argv ) { glutInit( &argc, argv); glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH ); glutCreateWindow("week04 mouse"); glutDisplayFunc(display);///Display顯示 glutMouseFunc(mouse);///Mouse滑鼠 glutMainLoop(); }
```6.用滑鼠事件來做座標換算:
#include <GL/glut.h> #include <stdio.h> int mouseX=0, mouseY=0; void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1,1,0);///黃色的 glPushMatrix(); ///口訣:減一半、除一半, y還倒過來 glTranslatef( (mouseX-150)/150.0 , -(mouseY-150)/150.0 , 0); glutSolidTeapot(0.3);///茶壼 glPopMatrix(); glutSwapBuffers(); } void mouse(int button, int state, int x, int y) { //printf("%d %d %d %d\n", button, state, x, y); mouseX=x; mouseY=y; } int main( int argc, char**argv ) { glutInit( &argc, argv); glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH ); glutCreateWindow("week04 mouse"); glutDisplayFunc(display);///Display顯示 glutMouseFunc(mouse);///Mouse滑鼠 glutMainLoop(); }
```7.今天的最後一個程式:老師發明一個程式:透過這個程式我們可以用滑鼠直接畫圖之外,小黑窗也會把座標備份在mx[i]&my[i]裡面,並印出來,方便我們畫出更漂亮的圖。
#include <GL/glut.h> #include <stdio.h> int mouseX=0, mouseY=0, N=0;///N個點!!! int mx[100], my[100];///用來記錄,等一下要畫出來哦!!! void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1,1,0);///黃色的 glBegin(GL_LINE_LOOP); for(int i=0; i<N; i++){ glVertex2f( (mx[i]-150)/150.0, -(my[i]-150)/150.0 ); } glEnd(); glutSwapBuffers(); } void mouse(int button, int state, int x, int y) { mouseX=x; mouseY=y; if(state==GLUT_DOWN){///按下去的 (放開的那個不算數) printf(" glVertex2f( (%d-150)/150.0, -(%d-150)/150.0 );\n", x,y); N++; mx[N-1]=x; my[N-1]=y; } } int main( int argc, char**argv ) { glutInit( &argc, argv); glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH ); glutCreateWindow("week04 mouse"); glutDisplayFunc(display);///Display顯示 glutMouseFunc(mouse);///Mouse滑鼠 glutMainLoop(); }
```
沒有留言:
張貼留言