2020年3月25日 星期三

誰會在凌晨三點吃美味蟹堡?

一開始下載曾經在第二週上課的檔案

http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/



解壓縮三個檔案


將三個檔案放進windows資料夾並開啟 Transformation.exe


進去點選AICapone

glRotatef(角度,X軸,Y軸,Z軸)因為比較特別 所以才打出來

再來開始第四週的rotate
老樣子 複製改檔名



開啟CodeBlocks
開啟GLUT projects
並更改程式

加入一個glutMotionFunc(motion);



再回頭加入函式



就可以自行用滑鼠轉動範例程式了



繼續沿用剛才的程式 結合上週與上上週的程式碼



可以旋轉茶壺了



程式碼:
#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();
        glRotated(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();
}

因為看起來轉得不太順 所以又新增了Mouse來讓它轉動順起來


程式碼:

#include <GL/glut.h>
float myAngle=0,oldX=0;

void motion(int x,int y){

    myAngle += (x-oldX);
    oldX = x;
    glutPostRedisplay();
}
void mouse(int button, int state, int x,int y){
    oldX = x;
}
void display(){

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotated(myAngle,0,0,1);
        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);
    glutMouseFunc(mouse);
    glutMainLoop();
}

沒有留言:

張貼留言