2020年3月25日 星期三

The學 week04

一開始在下方網址下載第二週載過的3個檔案
http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/

解壓縮這三個檔案後放入windows資料夾並開啟Transformation.exe

開啟後按滑鼠右鍵點選AI Capone
 
--------------------------------------------------------------------------------------------------------------------------
跟前幾週一樣 下載freeglut 然後開啟codeblock的專案,更改與新增下列程式碼
float myAngle=0;
void motion(int x , int y)
{
    myAngle=x;
    glutPostRedisplay();
}

    glutMotionFunc(motion);
    glClearColor(1,1,1,1);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

執行後可根據自己的滑鼠轉動圓圈

--------------------------------------------------------------------------------------------------------------------------
沿用剛剛的程式,以及之前的程式碼,使茶杯可以旋轉

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

因為轉起來不太順的感覺,因此新增程式碼使其完善

程式碼:

#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);///對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);
    glutMouseFunc(mouse);
    glutMainLoop();
}

沒有留言:

張貼留言