2020年3月24日 星期二

week04


glTranslatef glRotatef glScalef 
glRotatef ( 旋轉角度 , x軸 , y軸 , z軸 )


==============================================================

滑鼠移動控制圖形方向






26行: 
float myAngle=0; ///變數的宣告

31行: 
const double a = myAngle; ///把角度塞進去

125-128行:
void motion(int x,int y) ///mouse motion現在動到哪裡了?
{
    myAngle=x; ///我的角度,就是x的值
}

142行:
 glutMotionFunc(motion); ///加了mouse motion函式,可捉mouse的動態

==============================================================

滑鼠移動控制圖形方向--2





#include <GL/glut.h>
float myAngle=0; ///宣告我的角度變數
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(myAngle,0,0,1); ///對Z軸做旋轉
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x,int y)
{
    myAngle = x; ///我的角度,等下藥拿來用
    display(); ///每次做動作,就重畫畫面
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week  04 rotate");

    glutMotionFunc(motion); ///加motion函式
    glutDisplayFunc(display);
    glutMainLoop();
}
==============================================================

滑鼠移動控制圖形方向--3



#include <GL/glut.h>
float myAngle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(myAngle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}

int oldx=0; ///TODO2: (0) 宣告變數
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN) oldx=x; ///TODO2:(1)按下去時,要記一下在哪裡
}
void motion(int x,int y)
{ ///笑話:把大象放到冰箱裡:(1)把冰箱門打開 (2) 把大象放進去 (3) 把冰箱門關起來
    myAngle += (x-oldx); ///TODO2:(2) 你加了多少度?
    oldx=x; ///TODO2: (3)
    display();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week  04 rotate");

    glutMotionFunc(motion);
    glutMouseFunc(mouse); ///TODO2
    glutDisplayFunc(display);
    glutMainLoop();
}




沒有留言:

張貼留言