2018年3月19日 星期一

05160461_廖柏宇 第一次上課筆記在blog Week04(移動、旋轉、縮放)期中考80%

Part1
檔案資源下載:http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
1.下載 datawin32glut32.dll這三個檔案
(PS:滑鼠右鍵→另存連結)
2.將glut32.dll和data解壓縮出來的資料夾放在跟windows.zip(win32)同一個目錄下
沒有同一個目錄下的話可能會無法開啟或閃退新增說明文字
3.開啟Transformation.exe


Part2
旋轉體驗
glRotatef(角度, X軸, Y軸, Z軸)
gl=>OpenGL    、    f=>float

(用右手安培去旋轉來判斷)




















(用右手安培去旋轉來判斷)

Part3
選轉

程式碼
#include <GL/glut.h>

float angle=0;
void display()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle, 0, 0, 1);
        glColor3f(0, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
    angle++;
}


int main(int argc, char**argv)
{

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04");

    glutIdleFunc(display);
    glutDisplayFunc(display);


    glutMainLoop();

}

Part4
旋轉滑鼠

程式碼
#include <GL/glut.h>

float angle=0;
void display()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle, 0, 0, 1);
        glColor3f(0, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
    //angle++;
}
void mouse(int button, int state, int x, int y)
{

}
void motion(int x, int y)
{
    angle=x;
    glutPostRedisplay();
}

int main(int argc, char**argv)
{

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    //glutIdleFunc(display);
    glutDisplayFunc(display);


    glutMainLoop();

}

Part5

(旋轉+移動)滑鼠

程式碼

#include <GL/glut.h>

float angle=0, teapotY=0, teapotX=0;
int oldx=0,  oldy=0, nowRotate=0;
void display()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX, teapotY, 0);
        glRotatef(angle, 0, 0, 1);
        glColor3f(0, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
    //angle++;
}
void mouse(int button, int state, int x, int y)
{
    if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) nowRotate=1;
    if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) nowRotate=0;
    oldx=x;
    oldy=y;
}
void motion(int x, int y)
{
    if(nowRotate==1) angle+=(x-oldx);
    else
    {

        teapotX +=(x-oldx)/150.0;
        teapotY += (oldy-y)/150.0;

    }
    oldx=x;
    oldy=y;
    glutPostRedisplay();
}

int main(int argc, char**argv)
{

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    //glutIdleFunc(display);
    glutDisplayFunc(display);


    glutMainLoop();

}

-------------------------期中考80%重點------------------------------------


(移動和旋轉這地方比較容易寫錯)





沒有留言:

張貼留言