///使用"Computer Graphics"示範。

(1)可調整"位置"(視角) 、"弧度"、"角度"。
(2)角色可更換
1.做一個滑鼠motion動作的函式
152行 glutMotionFunc(motion);
**到40行那邊把void moton()函式寫出來
**目標:mouse motion時,讓他照著轉動。

2.滑鼠旋轉

46行
//static void display(void)-----這是原來display()函式
40-45行
float myAngle=0; 宣告角度變數
void motion(int x, int y) 滑鼠移動時,有XY座標
{
myAngle = x; 讓mouse的x座標,當我們要轉動的角度
glutPostRedisplay(); 讓GLUT去貼一個便條"post",記得去Re-display
}
3.用滑鼠旋轉茶壺
#include <GL/glut.h>
float myAngle=0;
void motion(int x, int y)
{
myAngle =x;
glutPostRedisplay();
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(myAngle, 0,0,1); 對Z軸做旋轉
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week04");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
#include <GL/glut.h>
float myAngle=0, oldX=0;
void motion(int x, int y)
{
myAngle += (x-oldX);
oldX = x; 移掉x(退休了)
glutPostRedisplay();
}
void mouse (int button, int state, int x, int y) 上周教的mouse ()函式
{
oldX=x; 按下去時,把位子記起來,他是oldX
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(myAngle, 0,0,1); 對z軸做旋轉
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("0325work");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse); 註冊好mouse()函式,如果(1)按下去,會叫mouse()
glutMainLoop();
}

沒有留言:
張貼留言