Week03_電腦圖學筆記
1. 複習點線面
2.評分介紹(HW1,HW2)
3.主題:滑鼠(Mouse) 鍵盤(Keyboard)
4.主題:移動(Move)
Step 1, 開啟新的專案,使用codeblocks glut專案把程式打上去

程式碼:
#include <GL/glut.h> //呼叫GLUT_外掛
void display()
{
(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3); /// 呼叫茶壺圖示
glutSwapBuffers();
}
int main(int argc ,char **argv) //呼叫main函式
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GL_DEPTH);
glutCreateWindow("05160612_Week03_MOUSE"); /// 視窗標題
glutDisplayFunc(display);
///glutMouseFunc(Mouse); ///預留滑鼠偵測
///glutMoveFunc(Move); ///預留滑鼠偵測
glutMainLoop(); // 函式迴圈
}
結果詳見figure 1.1
結果圖 figure 1.1
程式Round2-滑鼠事件偵聽
程式碼:
void display()
{
glClearColor(1,1,0,1); //設定背景顏色
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,1); //設定茶壺得顏色
glutSolidTeapot(0.3);
glutSwapBuffers();
}
#include <stdio.h> //匯入函式庫
void mouse(int button,int state,int x,int y) //呼叫滑鼠函式
{
if(state==GLUT_DOWN) printf("glVertex2f(%f, %f);\n",(x-150)/150.0,-(y-150)/150.0);
}
int main(int argc ,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GL_DEPTH);
glutCreateWindow("05160612_Week03_MOUSE"); //標題名稱
glutDisplayFunc(display);
glutMouseFunc(mouse); //新增滑鼠事件方程式
///glutMoveFunc(motion);
glutMainLoop();
}
結果詳見figure 1.2

結果圖 figure 1.2
程式Round3-移動事件偵聽
(含滑鼠)
程式碼:
float teapotX=0, teapotY=0; ///teapot的位置, 一開始在(0,0)
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///2種模式
glPushMatrix();///備份矩陣
glTranslatef(teapotX, teapotY, 0); ///glTranslatef(x,y,z);///移動得參數要準備好
glutSolidTeapot(0.3);
glPopMatrix(); ///還原矩陣
glutSwapBuffers(); ///交換畫面buffer便會顯示
}
#include <stdio.h>
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN) printf("glVertex2f(%f, %f);\n",(x-150)/150.0,-(y-150)/150.0);
}
void motion(int x,int y)
{
teapotX= (x-150)/150.0; teapotY=-(y-150)/150.0; ///把你的mouse的位置,設成teapot的位置
printf("%d %d \n",x,y); ///印出移動的數字
glutPostRedisplay(); ///重新整理畫面
}
int main(int argc ,char**argv) ///進階的main()三數(個數,字串)
{
glutInit(&argc, argv); ///進階參數,初始glut
glutInitDisplayMode(GLUT_DOUBLE|GL_DEPTH); ///開啟兩種模式
glutCreateWindow("05160612_Week03_MOUSE"); ///盡量不要用中文
glutDisplayFunc(display); ///等一下樓上要準備void display()
glutMouseFunc(mouse); ///等一下也會準備好mouse()功能,按鈕
glutMotionFunc(motion); ///等一下也會準備好motion(),移動
glutMainLoop(); ///主要迴圈
}
結果詳見figure 1.2
沒有留言:
張貼留言