2016年3月21日 星期一

week05


-課堂作業1-

了解
->先旋轉後移動
->先移動再旋轉
的差別

程式碼由下往上做執行
所以下圖是縮放scale->轉動rorate->移動translate

右鍵->Swap translate/rotate
使程式執行順序改變
變成下圖是縮放scale->移動translate->轉動rorate

-課堂作業2-

繞Y軸旋轉的茶壺
-程式碼-
#include <GL/glut.h>
float rotX=0;

void display()
{
    glClearColor(1,1,1,1);
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix(); //備份Matrix
        glRotatef(rotX,0,1,0);
        glColor3f(1,0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();   //還原Matrix
    glutSwapBuffers();
}
void motion(int x,int y)
{
    rotX=x;
    display(); //glutPostRedisplay();
}
int main (int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH); //兩倍記憶體
    glutCreateWindow("hello3D");
    glutDisplayFunc(display);
    glutMotionFunc(motion); //呼叫函式
    glutMainLoop();
}

-課堂作業3-

旋轉XY的茶壺
-程式碼-
#include <GL/glut.h>
float rotX=0,rotY=0;  //同時轉X也轉Y

void display()
{
    glClearColor(1,1,1,1);
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
        glRotatef(rotX,0,1,0);
        glRotatef(rotY,1,0,0);  //轉X軸
        glColor3f(1,0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x,int y)
{
    rotX=x;
    rotY=y;  //滑鼠y值移動量
    display();
}
int main (int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutInitWindowSize(400,400);  //視窗大小
    glutInitWindowPosition(700,0);  //視窗跳出之位置

    glutCreateWindow("hello3D");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}

沒有留言:

張貼留言