1.課堂互評上週作業
2.實作glutMouseFunc()以及glutMotionFunc()兩個函式
Step1: 請先初始化GLUT專案(GLUT Project -> 下載freeglut並解壓縮到桌面 -> 刪除多餘程式碼,保留以下程式碼 -> 測試結果並執行)
#include <GL/glut.h>
static void display(void) //display函式
{
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc, char *argv[]) //主函式
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMainLoop();
}
Step2: 加入glutMouseFunc();和glutMotionFunc();在glutDisplayFunc(display);下面兩行,並加上註解(//雙斜線)
Step3: 取消註解glutMouseFunc();,加入標頭檔#include<stdio.h>,並在main函式上方實作mouse()函式(請見下圖紅框框處)
Step4: 執行結果,請在茶壺的視窗任意處點擊,可在cmd視窗發現每點擊一次,就會把觸發、x座標、y座標顯示出來(即mouse函式內printf的功能)
Step5: 接下來要來改變「每點擊任一座標,就能改變茶壺顏色」。在printf()函式下加入這兩行程式碼:
glColor3f(1,x/300.0,y/300.0);
glutPostRedisplay(); //呼叫此函式來重新顯示(整理)螢幕
#注意,在glColor3f中,我們根據顯示結果畫面除以300.0,是老師demo的預設值,可根據自己執行畫面的座標來更改數值。
Step6: 實作glutMotionFunc(motion);。在motion()函式中新增功能。這個功能是偵測滑鼠在視窗中實現拖曳的偵測,故我們依照下圖開始修改程式碼:
#註: 在motion內有3行程式碼,功能分別為:
printf("%d %d\n",x,y); ///用以顯示拖曳時的座標位置
glColor3f(1,x/300.0,y/300.0); ///依作標位置來改變顏色
glutPostRedisplay(); ///重新整理(重整畫面)
3.畫圓形
Step1: 刪除茶壺圖案glutSolidTeapot(0.3);
Step2: 新增標頭檔#include <math.h>,如此一來可以使用sin、cos函式
Step3: 新增以下函式:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
for(float angle = 0 ; angle < 3.1415926 * 2 ; angle += 0.001)
glVertex2f(cos(angle),sin(angle));
glEnd();
Step4: 測試結果並執行,可以在300x300的視窗中顯示一個圓。






沒有留言:
張貼留言