2020年3月24日 星期二

week04 我的頭快破了

先到facebook裡點入網址



之後下載data,win32,glut32.dill三個檔案另存連結



之後將windows解壓縮,glut32.dill和data的資料放進去,點transformation開啟檔案



正題
開啟之前教過的六個圖案
我們要讓他停止自轉跟著我們的滑鼠轉


旋轉茶壺


程式碼:

#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();
}



上面的程式碼茶壺轉動時會亂跳,所以我們著次要讓茶壺跟著上次的地方繼續轉動


程式碼

#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();
}
int oldX=0;
void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN) oldX = x;///紀錄上次按下的位子
}
void motion(int x, int y)
{
    myAngle += (x-oldX); ///取角度來用
    oldX=x;
    display();   ///每次做動作都重畫畫面
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week 04 rotate");

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

沒有留言:

張貼留言