複習上週的東西 Mouse移動
範例 Transformation.exe
練習滑鼠轉動
#include <stdio.h>
#include <GL/glut.h>
float teapotX=0, teapotY=0, angle=0; //Now:rotate旋轉要的angle
int oldX=0, oldY=0, nowRotate=0; //Now3: oldX,oldY 幫忙, 了解你走了多少地方
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); //備份矩陣
glTranslatef(teapotX, teapotY, 0);
glRotatef(angle,0,0,1); //依照茶壺的座標移動
glutSolidTeapot(0.3); //Now: 茶壺要對著(0,0,1)做旋轉angle度
glPopMatrix(); //還原矩陣
glutSwapBuffers(); //搭配GLUT_DOUBLE兩倍顯示
}
void motion(int x, int y) //mouse motion 事件
{
if(nowRotate==1) angle+=(x-oldX);
else{
teapotX = (x-150)/150.0; //依照motion時的x來改teapot的座標
teapotY = (150-y)/150.0;
} //依照motion時的y來改teapot的座標
angle=x;
glutPostRedisplay(); //重畫畫面
}
void mouse (int button, int state, int x, int y) //button 代表左 中 右鍵,state 代表按下去/彈起來的mouse位置
{
if(button==GLUT_LEFT && state==GLUT_DOWN) nowRotate=1;
if(button==GLUT_LEFT && state==GLUT_DOWN) nowRotate=0;
oldX=x; oldY=y;
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week03 Mouse");
glutDisplayFunc(display);
//glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop(); //主要GLUT迴圈
}
其中考題
1.glPushMatrix (); //備份矩陣
2. glTranslatef (x,y,z); //移動
3. glRotatef (angle,x,y,z); //轉動
4. glBegin (GL_POLYGON); //開始畫
5. glTexCoord2f (tx.ty); //貼圖座標 (還沒教)
6. glNormal3f (nx,ny,nz); //法向量 (還沒教)
7. glColor3f (r,g,b); //顏色
8. glVertex3f (x,y,z); //頂點
9. glEnd (); //結束畫
10.glPopMatrix (); //還原矩陣

沒有留言:
張貼留言