電腦圖學 學習日誌
week3
1. 主題: Mouse函式 ( 座標系統 & 小畫家結合 & 利用Mouse來寫程式 )
2. 主題: 移動 glTranslatef(x,y,z)
3. 作業: 用Mouse來寫程式(描點畫圖)
(一) Mouse函式
▶ Mouse函式
void mouse(int button, int state, int x,int y)
{
printf("%d %d %d %d\n", button, state,x ,y);
}
→ 顯示座標位置
→ 在 main 函式需呼叫 glutMouseFunc(mouse);
(二) 移動 glTranslatef(x,y,z)
→ 修改 display() 和 mouse() 函式
→ mouse() 函式 為換算 X ,Y 座標
X: 0....150...300
減去150 -150......0.....150
除去150 -1..........0.......+1
→ mouse() 函式 為換算 X ,Y 座標
X: 0....150...300
減去150 -150......0.....150
除去150 -1..........0.......+1
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); /// TODO2: 備份矩陣
glTranslatef( teapotX, teapotY, 0); ///移動
glutSolidTeapot(0.3);
glPopMatrix(); /// TODO2: 備份矩陣
glutSwapBuffers();
}
void mouse(int button, int state, int x,int y) ///mouse的函式
{
teapotX = (x-150)/150.0;
teapotY = -(y-150)/150.0;
}
(三) 用Mouse來寫程式(描點畫圖)
#include <GL/glut.h>
float teapotX = -1, teapotY = +1;/// TODO2: 用變數 -1 ....+1 表示座標
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f( (227-150)/150.0 , -(132-150)/150.0); ///127,132
glVertex2f( (151-150)/150.0 , -(115-150)/150.0); ///151,115
glVertex2f( (85-150)/150.0 , -(141-150)/150.0); ///85,141
glVertex2f( (118-150)/150.0, -(188-150)/150.0); ///118,188
glVertex2f( (180-150)/150.0, -(186-150)/150.0); ///180,186
glEnd();
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week03-1");
glutDisplayFunc(display);
glutMainLoop();
}


沒有留言:
張貼留言